xo-expression/xo-reader: refactor Environment -> SymbolTable
This commit is contained in:
parent
8746188971
commit
63399df3ce
23 changed files with 104 additions and 101 deletions
|
|
@ -149,7 +149,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
Apply::attach_envs(bp<Environment> p) {
|
||||
Apply::attach_envs(bp<SymbolTable> p) {
|
||||
fn_->attach_envs(p);
|
||||
|
||||
for (const auto & arg : argv_)
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
AssignExpr::attach_envs(bp<Environment> p) {
|
||||
AssignExpr::attach_envs(bp<SymbolTable> p) {
|
||||
lhs_->attach_envs(p);
|
||||
rhs_->attach_envs(p);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ set(SELF_SRCS
|
|||
Variable.cpp
|
||||
IfExpr.cpp
|
||||
Sequence.cpp
|
||||
GlobalEnv.cpp
|
||||
LocalEnv.cpp
|
||||
GlobalSymtab.cpp
|
||||
LocalSymtab.cpp
|
||||
ConvertExpr.cpp
|
||||
Primitive.cpp
|
||||
typeinf/type_ref.cpp
|
||||
|
|
|
|||
|
|
@ -4,15 +4,15 @@
|
|||
*/
|
||||
|
||||
#include "xo/indentlog/print/ppdetail_atomic.hpp"
|
||||
#include "GlobalEnv.hpp"
|
||||
#include "GlobalSymtab.hpp"
|
||||
#include "Expression.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
GlobalEnv::GlobalEnv() = default;
|
||||
GlobalSymtab::GlobalSymtab() = default;
|
||||
|
||||
bp<Expression>
|
||||
GlobalEnv::require_global(const std::string & vname,
|
||||
GlobalSymtab::require_global(const std::string & vname,
|
||||
bp<Expression> expr)
|
||||
{
|
||||
this->global_map_[vname] = expr.get();
|
||||
|
|
@ -21,21 +21,21 @@ namespace xo {
|
|||
} /*require_global*/
|
||||
|
||||
void
|
||||
GlobalEnv::upsert_local(bp<Variable> target) {
|
||||
GlobalSymtab::upsert_local(bp<Variable> target) {
|
||||
// in practice: paraphrase of .require_global()
|
||||
|
||||
this->global_map_[target->name()] = target.promote();
|
||||
}
|
||||
|
||||
void
|
||||
GlobalEnv::print(std::ostream & os) const {
|
||||
GlobalSymtab::print(std::ostream & os) const {
|
||||
os << "<GlobalEnv"
|
||||
<< xtag("size", global_map_.size())
|
||||
<< ">";
|
||||
}
|
||||
|
||||
std::uint32_t
|
||||
GlobalEnv::pretty_print(const xo::print::ppindentinfo & ppii) const
|
||||
GlobalSymtab::pretty_print(const xo::print::ppindentinfo & ppii) const
|
||||
{
|
||||
using xo::print::ppstate;
|
||||
|
||||
|
|
@ -87,7 +87,7 @@ namespace xo {
|
|||
rp<Lambda>
|
||||
Lambda::make(const std::string & name,
|
||||
TypeDescr lambda_td,
|
||||
const rp<LocalEnv> & env,
|
||||
const rp<LocalSymtab> & env,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
return new Lambda(name, lambda_td, env, body);
|
||||
|
|
@ -95,7 +95,7 @@ namespace xo {
|
|||
|
||||
rp<Lambda>
|
||||
Lambda::make_from_env(const std::string & name,
|
||||
const rp<LocalEnv> & env,
|
||||
const rp<LocalSymtab> & env,
|
||||
TypeDescr explicit_return_td,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
|
|
@ -117,9 +117,9 @@ namespace xo {
|
|||
Lambda::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body,
|
||||
const rp<Environment> & parent_env)
|
||||
const rp<SymbolTable> & parent_env)
|
||||
{
|
||||
rp<LocalEnv> env = LocalEnv::make(argv, parent_env);
|
||||
rp<LocalSymtab> env = LocalSymtab::make(argv, parent_env);
|
||||
|
||||
TypeDescr explicit_return_td = nullptr;
|
||||
|
||||
|
|
@ -257,7 +257,7 @@ namespace xo {
|
|||
|
||||
Lambda::Lambda(const std::string & name,
|
||||
TypeDescr lambda_td,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<LocalSymtab> & local_env,
|
||||
const rp<Expression> & body)
|
||||
: FunctionInterface(exprtype::lambda, lambda_td),
|
||||
name_{name},
|
||||
|
|
@ -334,7 +334,7 @@ namespace xo {
|
|||
} /*ctor*/
|
||||
|
||||
void
|
||||
Lambda::attach_envs(bp<Environment> p) {
|
||||
Lambda::attach_envs(bp<SymbolTable> p) {
|
||||
local_env_->assign_parent(p);
|
||||
|
||||
/** establish a binding path for each variable **/
|
||||
|
|
@ -364,11 +364,11 @@ namespace xo {
|
|||
LambdaAccess::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body,
|
||||
const rp<Environment> & parent_env)
|
||||
const rp<SymbolTable> & parent_env)
|
||||
{
|
||||
TypeDescr explicit_return_td = nullptr;
|
||||
TypeDescr lambda_td = assemble_lambda_td(argv, explicit_return_td, body);
|
||||
rp<LocalEnv> env = LocalEnv::make(argv, parent_env);
|
||||
rp<LocalSymtab> env = LocalSymtab::make(argv, parent_env);
|
||||
|
||||
rp<LambdaAccess> retval
|
||||
= new LambdaAccess(name,
|
||||
|
|
@ -393,7 +393,7 @@ namespace xo {
|
|||
|
||||
LambdaAccess::LambdaAccess(const std::string & name,
|
||||
TypeDescr lambda_td,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<LocalSymtab> & local_env,
|
||||
const rp<Expression> & body)
|
||||
: Lambda(name, lambda_td, local_env, body)
|
||||
{}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
/* file LocalEnv.cpp
|
||||
/* file LocalSymtab.cpp
|
||||
*
|
||||
* author: Roland Conybeare
|
||||
*/
|
||||
|
||||
#include "LocalEnv.hpp"
|
||||
#include "LocalSymtab.hpp"
|
||||
#include "pretty_variable.hpp"
|
||||
#include "xo/indentlog/print/pretty_vector.hpp"
|
||||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
|
@ -11,29 +11,29 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
rp<LocalEnv>
|
||||
LocalEnv::make_empty() {
|
||||
return new LocalEnv(std::vector<rp<Variable>>(), nullptr);
|
||||
rp<LocalSymtab>
|
||||
LocalSymtab::make_empty() {
|
||||
return new LocalSymtab(std::vector<rp<Variable>>(), nullptr);
|
||||
}
|
||||
|
||||
rp<LocalEnv>
|
||||
LocalEnv::make(const std::vector<rp<Variable>> & argv,
|
||||
const rp<Environment> & parent_env)
|
||||
rp<LocalSymtab>
|
||||
LocalSymtab::make(const std::vector<rp<Variable>> & argv,
|
||||
const rp<SymbolTable> & parent_env)
|
||||
{
|
||||
return new LocalEnv(argv, parent_env);
|
||||
return new LocalSymtab(argv, parent_env);
|
||||
}
|
||||
|
||||
rp<LocalEnv>
|
||||
LocalEnv::make1(const rp<Variable> & arg1,
|
||||
const rp<Environment> & parent_env)
|
||||
rp<LocalSymtab>
|
||||
LocalSymtab::make1(const rp<Variable> & arg1,
|
||||
const rp<SymbolTable> & parent_env)
|
||||
{
|
||||
std::vector<rp<Variable>> argv = { arg1 };
|
||||
|
||||
return make(argv, parent_env);
|
||||
}
|
||||
|
||||
LocalEnv::LocalEnv(const std::vector<rp<Variable>> & argv,
|
||||
const rp<Environment> & parent_env)
|
||||
LocalSymtab::LocalSymtab(const std::vector<rp<Variable>> & argv,
|
||||
const rp<SymbolTable> & parent_env)
|
||||
: origin_{nullptr},
|
||||
argv_(argv),
|
||||
parent_env_{parent_env}
|
||||
|
|
@ -43,7 +43,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
binding_path
|
||||
LocalEnv::lookup_local_binding(const std::string & vname) const {
|
||||
LocalSymtab::lookup_local_binding(const std::string & vname) const {
|
||||
int j_slot = 0;
|
||||
for (const auto & arg : argv_) {
|
||||
if (arg->name() == vname)
|
||||
|
|
@ -55,7 +55,7 @@ namespace xo {
|
|||
} /*lookup_local_binding*/
|
||||
|
||||
binding_path
|
||||
LocalEnv::lookup_binding(const std::string & vname) const {
|
||||
LocalSymtab::lookup_binding(const std::string & vname) const {
|
||||
{
|
||||
auto local = this->lookup_local_binding(vname);
|
||||
if (local.i_link_ == 0)
|
||||
|
|
@ -71,9 +71,9 @@ namespace xo {
|
|||
} /*lookup_binding*/
|
||||
|
||||
void
|
||||
LocalEnv::assign_parent(bp<Environment> p) {
|
||||
LocalSymtab::assign_parent(bp<SymbolTable> p) {
|
||||
if ((parent_env_.get() != nullptr) && (parent_env_.get() != p.get())) {
|
||||
throw std::runtime_error(tostr("LocalEnv::assign_parent(P2): already have established parent P1",
|
||||
throw std::runtime_error(tostr("LocalSymtab::assign_parent(P2): already have established parent P1",
|
||||
xtag("P1", parent_env_),
|
||||
xtag("P2", p)));
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
LocalEnv::upsert_local(bp<Variable> target) {
|
||||
LocalSymtab::upsert_local(bp<Variable> target) {
|
||||
for (auto & var : this->argv_) {
|
||||
if (var->name() == target->name()) {
|
||||
/* replace existing variable. This may change its type */
|
||||
|
|
@ -99,20 +99,20 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
LocalEnv::print(std::ostream& os) const {
|
||||
os << "<LocalEnv"
|
||||
LocalSymtab::print(std::ostream& os) const {
|
||||
os << "<LocalSymtab"
|
||||
<< xtag("argv", argv_)
|
||||
<< ">";
|
||||
}
|
||||
|
||||
std::uint32_t
|
||||
LocalEnv::pretty_print(const xo::print::ppindentinfo & ppii) const {
|
||||
LocalSymtab::pretty_print(const xo::print::ppindentinfo & ppii) const {
|
||||
using xo::print::ppstate;
|
||||
|
||||
ppstate * pps = ppii.pps();
|
||||
|
||||
if (ppii.upto()) {
|
||||
if (!pps->print_upto("<LocalEnv"))
|
||||
if (!pps->print_upto("<LocalSymtab"))
|
||||
return false;
|
||||
if (!pps->print_upto_tag("argv", argv_))
|
||||
return false;
|
||||
|
|
@ -120,7 +120,7 @@ namespace xo {
|
|||
|
||||
return true;
|
||||
} else {
|
||||
pps->write("<LocalEnv");
|
||||
pps->write("<LocalSymtab");
|
||||
pps->newline_pretty_tag(ppii.ci1(), "this", (void*)this);
|
||||
pps->newline_pretty_tag(ppii.ci1(), "argv", argv_);
|
||||
pps->write(">");
|
||||
|
|
@ -132,5 +132,4 @@ namespace xo {
|
|||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end LocalEnv.cpp */
|
||||
/* end LocalSymtab.cpp */
|
||||
|
|
@ -55,7 +55,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
Sequence::attach_envs(bp<Environment> p) {
|
||||
Sequence::attach_envs(bp<SymbolTable> p) {
|
||||
for (const auto & x : expr_v_)
|
||||
x->attach_envs(p);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/* @file Variable.cpp */
|
||||
|
||||
#include "Variable.hpp"
|
||||
#include "Environment.hpp"
|
||||
#include "SymbolTable.hpp"
|
||||
#include "pretty_expression.hpp"
|
||||
|
||||
namespace xo {
|
||||
|
|
@ -19,7 +19,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
Variable::attach_envs(bp<Environment> e) {
|
||||
Variable::attach_envs(bp<SymbolTable> e) {
|
||||
/** e makes accessible all enclosing lexical scopes **/
|
||||
if (this->path_.i_link_ == -2 /*sentinel*/) {
|
||||
this->path_ = e->lookup_binding(this->name_);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue