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
|
|
@ -129,7 +129,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> p) override;
|
||||
virtual void attach_envs(bp<SymbolTable> p) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ namespace xo {
|
|||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override;
|
||||
virtual std::size_t visit_layer(VisitFn visitor_fn) override;
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override;
|
||||
virtual void attach_envs(bp<Environment> p) override;
|
||||
virtual void attach_envs(bp<SymbolTable> p) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ namespace xo {
|
|||
return std::set<std::string>();
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override {}
|
||||
virtual void attach_envs(bp<SymbolTable> /*p*/) override {}
|
||||
|
||||
}; /*ConstantInterface*/
|
||||
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
virtual void attach_envs(bp<SymbolTable> p) override {
|
||||
arg_->attach_envs(p);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
virtual void attach_envs(bp<SymbolTable> p) override {
|
||||
rhs_->attach_envs(p);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ namespace xo {
|
|||
namespace scm {
|
||||
class Variable; /* see Variable.hpp */
|
||||
class Lambda; /* see Lamnbda.hpp */
|
||||
class Environment; /* see Environment.hpp */
|
||||
class SymbolTable; /* see SymbolTable.hpp */
|
||||
|
||||
/** @class Expression
|
||||
* @brief abstract syntax tree for an EGAD program
|
||||
|
|
@ -72,7 +72,7 @@ namespace xo {
|
|||
* from @p X.argv
|
||||
* - resolve free variables from @p parent
|
||||
**/
|
||||
virtual void attach_envs(bp<Environment> parent) = 0;
|
||||
virtual void attach_envs(bp<SymbolTable> parent) = 0;
|
||||
|
||||
/** append to *p_set the set of free variables in this expression.
|
||||
* returns the number of free variables introduced
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
/* file GlobalEnv.hpp
|
||||
/* file GlobalSymtab.hpp
|
||||
*
|
||||
* author: Roland Conybeare, Jun 2024
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Environment.hpp"
|
||||
#include "SymbolTable.hpp"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
class GlobalEnv : public Environment {
|
||||
class GlobalSymtab : public SymbolTable {
|
||||
public:
|
||||
/** create instance. Probably only need one of these **/
|
||||
static rp<GlobalEnv> make_empty() { return new GlobalEnv(); }
|
||||
static rp<GlobalSymtab> make_empty() { return new GlobalSymtab(); }
|
||||
|
||||
bp<Expression> require_global(const std::string & vname,
|
||||
bp<Expression> expr);
|
||||
|
|
@ -51,7 +51,7 @@ namespace xo {
|
|||
virtual std::uint32_t pretty_print(const xo::print::ppindentinfo & ppii) const override;
|
||||
|
||||
private:
|
||||
GlobalEnv();
|
||||
GlobalSymtab();
|
||||
|
||||
private:
|
||||
/* for assignable globals, need to allocate memory
|
||||
|
|
@ -63,4 +63,4 @@ namespace xo {
|
|||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end GlobalEnv.hpp */
|
||||
/* end GlobalSymtab.hpp */
|
||||
|
|
@ -87,7 +87,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
virtual void attach_envs(bp<SymbolTable> p) override {
|
||||
test_->attach_envs(p);
|
||||
when_true_->attach_envs(p);
|
||||
when_false_->attach_envs(p);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
#include "Expression.hpp"
|
||||
#include "FunctionInterface.hpp"
|
||||
#include "Variable.hpp"
|
||||
#include "LocalEnv.hpp"
|
||||
#include "LocalSymtab.hpp"
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
|
@ -30,7 +30,7 @@ namespace xo {
|
|||
**/
|
||||
static rp<Lambda> make(const std::string & name,
|
||||
TypeDescr lambda_type,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<LocalSymtab> & local_env,
|
||||
const rp<Expression> & body);
|
||||
|
||||
/**
|
||||
|
|
@ -42,7 +42,7 @@ namespace xo {
|
|||
static rp<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);
|
||||
|
||||
/**
|
||||
* @p name Name for this lambda -- must be unique
|
||||
|
|
@ -50,7 +50,7 @@ namespace xo {
|
|||
* @p body Expression for body of function
|
||||
**/
|
||||
static rp<Lambda> make_from_env(const std::string & name,
|
||||
const rp<LocalEnv> & env,
|
||||
const rp<LocalSymtab> & env,
|
||||
TypeDescr explicit_return_td,
|
||||
const rp<Expression> & body);
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ namespace xo {
|
|||
return this;
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> p) override;
|
||||
virtual void attach_envs(bp<SymbolTable> p) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
|
||||
|
|
@ -138,7 +138,7 @@ namespace xo {
|
|||
**/
|
||||
Lambda(const std::string & name,
|
||||
TypeDescr lambda_type,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<LocalSymtab> & local_env,
|
||||
const rp<Expression> & body);
|
||||
|
||||
/** compute free-variable set for this lambda **/
|
||||
|
|
@ -201,14 +201,14 @@ namespace xo {
|
|||
* when Lambda constructor runs, so we need to assign @ref local_env_
|
||||
* later.
|
||||
**/
|
||||
rp<LocalEnv> local_env_;
|
||||
rp<LocalSymtab> local_env_;
|
||||
}; /*Lambda*/
|
||||
|
||||
inline rp<Lambda>
|
||||
make_lambda(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body,
|
||||
const rp<Environment> & parent_env)
|
||||
const rp<SymbolTable> & parent_env)
|
||||
{
|
||||
return Lambda::make(name, argv, body, parent_env);
|
||||
}
|
||||
|
|
@ -218,7 +218,7 @@ namespace xo {
|
|||
static rp<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);
|
||||
static rp<LambdaAccess> make_empty();
|
||||
|
||||
/** assign body + compute derived members
|
||||
|
|
@ -232,7 +232,7 @@ namespace xo {
|
|||
**/
|
||||
LambdaAccess(const std::string & name,
|
||||
TypeDescr lambda_type,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<LocalSymtab> & local_env,
|
||||
const rp<Expression> & body);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
/* file LocalEnv.hpp
|
||||
/* file LocalSymtab.hpp
|
||||
*
|
||||
* author: Roland Conybeare, Jun 2024
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Environment.hpp"
|
||||
#include "SymbolTable.hpp"
|
||||
#include "Variable.hpp"
|
||||
#include "xo/reflect/TypeDescr.hpp"
|
||||
|
||||
|
|
@ -20,20 +20,20 @@ namespace xo {
|
|||
* parameters, but also links to @ref Environment for
|
||||
* innermost enclosing @ref Lambda.
|
||||
**/
|
||||
class LocalEnv : public Environment {
|
||||
class LocalSymtab : public SymbolTable {
|
||||
public:
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
|
||||
public:
|
||||
static rp<LocalEnv> make_empty();
|
||||
static rp<LocalSymtab> make_empty();
|
||||
/** named ctor idiom. Create instance with local variables per @p argv **/
|
||||
static rp<LocalEnv> make(const std::vector<rp<Variable>> & argv,
|
||||
const rp<Environment> & parent_env);
|
||||
static rp<LocalSymtab> make(const std::vector<rp<Variable>> & argv,
|
||||
const rp<SymbolTable> & parent_env);
|
||||
/** Create instance with single local variable @ap argv1 **/
|
||||
static rp<LocalEnv> make1(const rp<Variable> & arg1,
|
||||
const rp<Environment> & parent_env);
|
||||
static rp<LocalSymtab> make1(const rp<Variable> & arg1,
|
||||
const rp<SymbolTable> & parent_env);
|
||||
/** runtime downcast. nullptr if @p x is not a LocalEnv instance **/
|
||||
static bp<LocalEnv> from(const bp<Environment> & x) { return bp<LocalEnv>::from(x); }
|
||||
static bp<LocalSymtab> from(const bp<SymbolTable> & x) { return bp<LocalSymtab>::from(x); }
|
||||
|
||||
Lambda * origin() const { return origin_; }
|
||||
const std::vector<rp<Variable>> & argv() const { return argv_; }
|
||||
|
|
@ -53,7 +53,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
/** single-assign this environment's parent **/
|
||||
void assign_parent(bp<Environment> p);
|
||||
void assign_parent(bp<SymbolTable> p);
|
||||
|
||||
// ----- Environment -----
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ namespace xo {
|
|||
virtual std::uint32_t pretty_print(const print::ppindentinfo & ppii) const override;
|
||||
|
||||
private:
|
||||
LocalEnv(const std::vector<rp<Variable>> & argv, const rp<Environment> & parent_env);
|
||||
LocalSymtab(const std::vector<rp<Variable>> & argv, const rp<SymbolTable> & parent_env);
|
||||
|
||||
private:
|
||||
/** Lambda for which this environment created.
|
||||
|
|
@ -103,17 +103,21 @@ namespace xo {
|
|||
**/
|
||||
Lambda * origin_ = nullptr;
|
||||
|
||||
/** formal argument names **/
|
||||
/** formal argument names.
|
||||
* all variables in @ref argv_ have distinct names.
|
||||
* if @c .lookup_binding(vname) returns a binding path with @c .i_link=0 and @c .j_slot=j
|
||||
* then @c argv_[j]->name_ is @c vname.
|
||||
**/
|
||||
std::vector<rp<Variable>> argv_;
|
||||
|
||||
/** parent environment. A free variable in this lambda's
|
||||
* body will be resolved by referring them to @ref parent_env_.
|
||||
**/
|
||||
rp<Environment> parent_env_;
|
||||
rp<SymbolTable> parent_env_;
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end LocalEnv.hpp */
|
||||
/* end LocalSymtab.hpp */
|
||||
|
|
@ -66,7 +66,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override {}
|
||||
virtual void attach_envs(bp<SymbolTable> /*p*/) override {}
|
||||
|
||||
private:
|
||||
}; /*PrimitiveInterface*/
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace xo {
|
|||
/** note: borken if .expr_v_ contains any def-exprs **/
|
||||
virtual std::size_t visit_layer(VisitFn visitor_fn) override;
|
||||
virtual rp<Expression> xform_layer(TransformFn visitor_fn) override;
|
||||
virtual void attach_envs(bp<Environment> parent) override;
|
||||
virtual void attach_envs(bp<SymbolTable> parent) override;
|
||||
|
||||
// ----- from GeneralizedExpression ----
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
/* file Environment.hpp
|
||||
/* file SymbolTable.hpp
|
||||
*
|
||||
* author: Roland Conybeare, Jun 2024
|
||||
*/
|
||||
|
|
@ -22,7 +22,7 @@ namespace xo {
|
|||
* When generating code (see xo-jit): rhs can be any expression,
|
||||
* for example a Lambda.
|
||||
**/
|
||||
class Environment : public ref::Refcount {
|
||||
class SymbolTable : public ref::Refcount {
|
||||
public:
|
||||
/** true if this is toplevel (global) environment.
|
||||
* Toplevel environment doesn't have slot numbers.
|
||||
|
|
@ -57,7 +57,7 @@ namespace xo {
|
|||
};
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, const Environment & x) {
|
||||
operator<< (std::ostream & os, const SymbolTable & x) {
|
||||
x.print(os);
|
||||
return os;
|
||||
}
|
||||
|
|
@ -65,4 +65,4 @@ namespace xo {
|
|||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end Environment.hpp */
|
||||
/* end SymbolTable.hpp */
|
||||
|
|
@ -64,7 +64,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override;
|
||||
virtual void attach_envs(bp<SymbolTable> /*p*/) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
virtual std::uint32_t pretty_print(const ppindentinfo & ppii) const override;
|
||||
|
|
|
|||
|
|
@ -4,32 +4,32 @@
|
|||
|
||||
#include "xo/indentlog/print/pretty.hpp"
|
||||
#include "xo/refcnt/pretty_refcnt.hpp"
|
||||
#include "LocalEnv.hpp"
|
||||
#include "LocalSymtab.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace print {
|
||||
template <>
|
||||
struct ppdetail<xo::scm::Environment> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::Environment & x) {
|
||||
struct ppdetail<xo::scm::SymbolTable> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::SymbolTable & x) {
|
||||
return x.pretty_print(ppii);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ppdetail<xo::scm::LocalEnv> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::LocalEnv & x) {
|
||||
struct ppdetail<xo::scm::LocalSymtab> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::LocalSymtab & x) {
|
||||
return x.pretty_print(ppii);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ppdetail<xo::scm::LocalEnv*> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::LocalEnv* x) {
|
||||
struct ppdetail<xo::scm::LocalSymtab*> {
|
||||
static bool print_pretty(const ppindentinfo & ppii, const xo::scm::LocalSymtab* x) {
|
||||
if (x) {
|
||||
return x->pretty_print(ppii);
|
||||
} else {
|
||||
ppii.pps()->write("<nullptr ");
|
||||
ppii.pps()->write(reflect::type_name<xo::scm::LocalEnv>());
|
||||
ppii.pps()->write(reflect::type_name<xo::scm::LocalSymtab>());
|
||||
ppii.pps()->write(">");
|
||||
return ppii.pps()->has_margin();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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