xo-expression: pref xo::bp to xo::ref::brw
This commit is contained in:
parent
155b2dbec0
commit
a4b2299537
20 changed files with 66 additions and 60 deletions
|
|
@ -42,8 +42,8 @@ namespace xo {
|
|||
const rp<Expression> & rhs);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<Apply> from(ref::brw<Expression> x) {
|
||||
return ref::brw<Apply>::from(x);
|
||||
static bp<Apply> from(bp<Expression> x) {
|
||||
return bp<Apply>::from(x);
|
||||
}
|
||||
|
||||
const rp<Expression> & fn() const { return fn_; }
|
||||
|
|
@ -98,7 +98,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> p) override {
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
fn_->attach_envs(p);
|
||||
|
||||
for (const auto & arg : argv_)
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ namespace xo {
|
|||
static rp<AssignExpr> make(const rp<Variable> & lhs,
|
||||
const rp<Expression> & rhs);
|
||||
|
||||
static ref::brw<AssignExpr> from(ref::brw<Expression> x) {
|
||||
return ref::brw<AssignExpr>::from(x);
|
||||
static bp<AssignExpr> from(bp<Expression> x) {
|
||||
return bp<AssignExpr>::from(x);
|
||||
}
|
||||
|
||||
const rp<Variable> & lhs() const { return lhs_; }
|
||||
|
|
@ -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(ref::brw<Environment> p) override;
|
||||
virtual void attach_envs(bp<Environment> p) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ namespace xo {
|
|||
ConstantInterface(exprtype extype, TypeDescr valuetype) : Expression{extype, valuetype} {}
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<ConstantInterface> from(ref::brw<Expression> x) {
|
||||
return ref::brw<ConstantInterface>::from(x);
|
||||
static bp<ConstantInterface> from(bp<Expression> x) {
|
||||
return bp<ConstantInterface>::from(x);
|
||||
}
|
||||
|
||||
/** type description for representation of literal value **/
|
||||
|
|
@ -40,8 +40,7 @@ namespace xo {
|
|||
return std::set<std::string>();
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> /*p*/) override {}
|
||||
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override {}
|
||||
|
||||
}; /*ConstantInterface*/
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ namespace xo {
|
|||
static rp<ConvertExpr> make(TypeDescr dest_type,
|
||||
rp<Expression> arg);
|
||||
|
||||
static ref::brw<ConvertExpr> from(ref::brw<Expression> x) {
|
||||
return ref::brw<ConvertExpr>::from(x);
|
||||
static bp<ConvertExpr> from(bp<Expression> x) {
|
||||
return bp<ConvertExpr>::from(x);
|
||||
}
|
||||
|
||||
const rp<Expression> & arg() const { return arg_; }
|
||||
|
|
@ -61,7 +61,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> p) override {
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
arg_->attach_envs(p);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,13 +28,15 @@ namespace xo {
|
|||
rp<Expression> value);
|
||||
|
||||
|
||||
static ref::brw<DefineExpr> from(ref::brw<Expression> x) {
|
||||
return ref::brw<DefineExpr>::from(x);
|
||||
static bp<DefineExpr> from(bp<Expression> x) {
|
||||
return bp<DefineExpr>::from(x);
|
||||
}
|
||||
|
||||
const std::string & lhs_name() const { return lhs_name_; }
|
||||
const rp<Expression> & rhs() const { return rhs_; }
|
||||
|
||||
rp<Variable> lhs_variable() const;
|
||||
|
||||
std::set<std::string> calc_free_variables() const;
|
||||
|
||||
// ----- Expression -----
|
||||
|
|
@ -69,7 +71,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> p) override {
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
rhs_->attach_envs(p);
|
||||
}
|
||||
|
||||
|
|
@ -124,5 +126,4 @@ namespace xo {
|
|||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end DefineExpr.hpp */
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace xo {
|
|||
/** lookup variable-expression @p vname in this environment.
|
||||
* returns llvm::Value representing code that produces a value for vname
|
||||
**/
|
||||
virtual ref::brw<Expression> lookup_var(const std::string & vname) const = 0;
|
||||
virtual bp<Expression> lookup_var(const std::string & vname) const = 0;
|
||||
};
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ namespace xo {
|
|||
class Expression : public GeneralizedExpression {
|
||||
public:
|
||||
using VisitFn = std::function
|
||||
<void (ref::brw<Expression>)>;
|
||||
<void (bp<Expression>)>;
|
||||
using TransformFn = std::function
|
||||
<rp<Expression> (ref::brw<Expression>)>;
|
||||
<rp<Expression> (bp<Expression>)>;
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
|
||||
public:
|
||||
|
|
@ -72,7 +72,7 @@ namespace xo {
|
|||
* from @p X.argv
|
||||
* - resolve free variables from @p parent
|
||||
**/
|
||||
virtual void attach_envs(ref::brw<Environment> parent) = 0;
|
||||
virtual void attach_envs(bp<Environment> parent) = 0;
|
||||
|
||||
/** append to *p_set the set of free variables in this expression.
|
||||
* returns the number of free variables introduced
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ namespace xo {
|
|||
: Expression(extype, fn_type) {}
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<FunctionInterface> from(ref::brw<Expression> x) {
|
||||
return ref::brw<FunctionInterface>::from(x);
|
||||
static bp<FunctionInterface> from(bp<Expression> x) {
|
||||
return bp<FunctionInterface>::from(x);
|
||||
}
|
||||
|
||||
virtual const std::string & name() const = 0;
|
||||
|
|
@ -30,5 +30,4 @@ namespace xo {
|
|||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/** end FunctionInterface.hpp **/
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ namespace xo {
|
|||
/** create instance. Probably only need one of these **/
|
||||
static rp<GlobalEnv> make() { return new GlobalEnv(); }
|
||||
|
||||
ref::brw<Expression> require_global(const std::string & vname,
|
||||
ref::brw<Expression> expr) {
|
||||
bp<Expression> require_global(const std::string & vname,
|
||||
bp<Expression> expr) {
|
||||
global_map_[vname] = expr.get();
|
||||
return expr;
|
||||
} /*require_global*/
|
||||
|
|
@ -33,12 +33,12 @@ namespace xo {
|
|||
return { -1, 0 };
|
||||
}
|
||||
|
||||
virtual ref::brw<Expression> lookup_var(const std::string & vname) const override {
|
||||
virtual bp<Expression> lookup_var(const std::string & vname) const override {
|
||||
auto ix = global_map_.find(vname);
|
||||
|
||||
if (ix == global_map_.end()) {
|
||||
/* not found */
|
||||
return ref::brw<Expression>::from_native(nullptr);
|
||||
return bp<Expression>::from_native(nullptr);
|
||||
}
|
||||
|
||||
return ix->second;
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ namespace xo {
|
|||
const rp<Expression> & when_false);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<IfExpr> from(ref::brw<Expression> x) {
|
||||
return ref::brw<IfExpr>::from(x);
|
||||
static bp<IfExpr> from(bp<Expression> x) {
|
||||
return bp<IfExpr>::from(x);
|
||||
}
|
||||
|
||||
const rp<Expression> & test() const { return test_; }
|
||||
|
|
@ -87,14 +87,14 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> p) override {
|
||||
virtual void attach_envs(bp<Environment> p) override {
|
||||
test_->attach_envs(p);
|
||||
when_true_->attach_envs(p);
|
||||
when_false_->attach_envs(p);
|
||||
}
|
||||
|
||||
#ifdef NOT_USING
|
||||
virtual std::int32_t find_free_vars(std::set<ref::brw<Variable>> * p_set) override {
|
||||
virtual std::int32_t find_free_vars(std::set<bp<Variable>> * p_set) override {
|
||||
return (test_->find_free_vars(p_set)
|
||||
+ when_true_->find_free_vars(p_set)
|
||||
+ when_false_->find_free_vars(p_set));
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ namespace xo {
|
|||
const rp<Expression> & body);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<Lambda> from(ref::brw<Expression> x) {
|
||||
return ref::brw<Lambda>::from(x);
|
||||
static bp<Lambda> from(bp<Expression> x) {
|
||||
return bp<Lambda>::from(x);
|
||||
}
|
||||
|
||||
const std::string & type_str() const { return type_str_; }
|
||||
|
|
@ -84,7 +84,7 @@ namespace xo {
|
|||
return this;
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> p) override;
|
||||
virtual void attach_envs(bp<Environment> p) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
|
||||
|
|
@ -160,7 +160,7 @@ namespace xo {
|
|||
std::map<std::string, rp<Variable>> layer_var_map_;
|
||||
|
||||
/** all lambdas nested once inside this lambda's body **/
|
||||
std::map<std::string, ref::brw<Lambda>> nested_lambda_map_;
|
||||
std::map<std::string, bp<Lambda>> nested_lambda_map_;
|
||||
|
||||
/** established (once) by @ref attach_envs.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
/** single-assign this environment's parent **/
|
||||
void assign_parent(ref::brw<Environment> p) {
|
||||
void assign_parent(bp<Environment> p) {
|
||||
assert(parent_env_.get() == nullptr);
|
||||
parent_env_ = p.get();
|
||||
}
|
||||
|
|
@ -59,7 +59,7 @@ namespace xo {
|
|||
|
||||
virtual binding_path lookup_binding(const std::string & vname) const override;
|
||||
|
||||
virtual ref::brw<Expression> lookup_var(const std::string & target) const override {
|
||||
virtual bp<Expression> lookup_var(const std::string & target) const override {
|
||||
for (const auto & arg : argv_) {
|
||||
if (arg->name() == target)
|
||||
return arg;
|
||||
|
|
|
|||
|
|
@ -18,12 +18,12 @@ namespace xo {
|
|||
using void_function_type = void (*)();
|
||||
|
||||
public:
|
||||
PrimitiveInterface(TypeDescr fn_type)
|
||||
explicit PrimitiveInterface(TypeDescr fn_type)
|
||||
: FunctionInterface(exprtype::primitive, fn_type) {}
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<PrimitiveInterface> from(ref::brw<Expression> x) {
|
||||
return ref::brw<PrimitiveInterface>::from(x);
|
||||
static bp<PrimitiveInterface> from(bp<Expression> x) {
|
||||
return bp<PrimitiveInterface>::from(x);
|
||||
}
|
||||
|
||||
/** if true, Jit will try to explicitly symbol for this primitive
|
||||
|
|
@ -66,7 +66,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> /*p*/) override {}
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override {}
|
||||
|
||||
private:
|
||||
}; /*PrimitiveInterface*/
|
||||
|
|
|
|||
|
|
@ -33,7 +33,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(ref::brw<Environment> parent) override;
|
||||
virtual void attach_envs(bp<Environment> parent) override;
|
||||
|
||||
// ----- from GeneralizedExpression ----
|
||||
|
||||
|
|
|
|||
|
|
@ -25,14 +25,14 @@ namespace xo {
|
|||
return new Variable(name, var_type);
|
||||
}
|
||||
|
||||
/** return copy of x: same var, different object identity **/
|
||||
static rp<Variable> copy(ref::brw<Variable> x) {
|
||||
/** return copy of @p x: same var, different object identity **/
|
||||
static rp<Variable> copy(bp<Variable> x) {
|
||||
return new Variable(x->name(), x->valuetype());
|
||||
}
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<Variable> from(ref::brw<Expression> x) {
|
||||
return ref::brw<Variable>::from(x);
|
||||
static bp<Variable> from(bp<Expression> x) {
|
||||
return bp<Variable>::from(x);
|
||||
}
|
||||
|
||||
const std::string & name() const { return name_; }
|
||||
|
|
@ -57,7 +57,7 @@ namespace xo {
|
|||
return xform_fn(this);
|
||||
}
|
||||
|
||||
virtual void attach_envs(ref::brw<Environment> /*p*/) override;
|
||||
virtual void attach_envs(bp<Environment> /*p*/) override;
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
AssignExpr::attach_envs(ref::brw<Environment> p) {
|
||||
AssignExpr::attach_envs(bp<Environment> p) {
|
||||
lhs_->attach_envs(p);
|
||||
rhs_->attach_envs(p);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
|
||||
#include "DefineExpr.hpp"
|
||||
#include "Variable.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
|
|
@ -31,6 +32,12 @@ namespace xo {
|
|||
this->free_var_set_ = this->calc_free_variables();
|
||||
}
|
||||
|
||||
rp<Variable>
|
||||
DefineExpr::lhs_variable() const
|
||||
{
|
||||
return Variable::make(lhs_name(), valuetype());
|
||||
}
|
||||
|
||||
std::set<std::string>
|
||||
DefineExpr::calc_free_variables() const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -128,10 +128,10 @@ namespace xo {
|
|||
|
||||
this->body_
|
||||
= (body_->xform_layer
|
||||
([&var_map](ref::brw<Expression> x) -> rp<Expression>
|
||||
([&var_map](bp<Expression> x) -> rp<Expression>
|
||||
{
|
||||
if (x->extype() == exprtype::variable) {
|
||||
ref::brw<Variable> var = Variable::from(x);
|
||||
bp<Variable> var = Variable::from(x);
|
||||
|
||||
auto ix = var_map.find(var->name());
|
||||
if (ix == var_map.end()) {
|
||||
|
|
@ -167,14 +167,14 @@ namespace xo {
|
|||
|
||||
this->free_var_set_ = this->calc_free_variables();
|
||||
|
||||
std::map<std::string, ref::brw<Lambda>> nested_lambda_map;
|
||||
std::map<std::string, bp<Lambda>> nested_lambda_map;
|
||||
{
|
||||
this->body_->visit_layer
|
||||
([&nested_lambda_map]
|
||||
(ref::brw<Expression> expr)
|
||||
(bp<Expression> expr)
|
||||
{
|
||||
if (expr->extype() == exprtype::lambda) {
|
||||
ref::brw<Lambda> lm = Lambda::from(expr);
|
||||
bp<Lambda> lm = Lambda::from(expr);
|
||||
|
||||
nested_lambda_map[lm->name()] = lm.get();
|
||||
}
|
||||
|
|
@ -245,14 +245,14 @@ namespace xo {
|
|||
|
||||
this->free_var_set_ = this->calc_free_variables();
|
||||
|
||||
std::map<std::string, ref::brw<Lambda>> nested_lambda_map;
|
||||
std::map<std::string, bp<Lambda>> nested_lambda_map;
|
||||
{
|
||||
this->body_->visit_layer
|
||||
([&nested_lambda_map]
|
||||
(ref::brw<Expression> expr)
|
||||
(bp<Expression> expr)
|
||||
{
|
||||
if (expr->extype() == exprtype::lambda) {
|
||||
ref::brw<Lambda> lm = Lambda::from(expr);
|
||||
bp<Lambda> lm = Lambda::from(expr);
|
||||
|
||||
nested_lambda_map[lm->name()] = lm.get();
|
||||
}
|
||||
|
|
@ -295,7 +295,7 @@ namespace xo {
|
|||
} /*ctor*/
|
||||
|
||||
void
|
||||
Lambda::attach_envs(ref::brw<Environment> p) {
|
||||
Lambda::attach_envs(bp<Environment> p) {
|
||||
local_env_->assign_parent(p);
|
||||
|
||||
/** establish a binding path for each variable **/
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
Sequence::attach_envs(ref::brw<Environment> p) {
|
||||
Sequence::attach_envs(bp<Environment> p) {
|
||||
for (const auto & x : expr_v_)
|
||||
x->attach_envs(p);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
namespace xo {
|
||||
namespace ast {
|
||||
void
|
||||
Variable::attach_envs(ref::brw<Environment> e) {
|
||||
Variable::attach_envs(bp<Environment> 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