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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue