From a4b2299537d8b7d3c4155beb09b7e79c91309c27 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 5 Jul 2025 13:53:05 -0500 Subject: [PATCH] xo-expression: pref xo::bp to xo::ref::brw --- xo-expression/include/xo/expression/Apply.hpp | 6 +++--- .../include/xo/expression/AssignExpr.hpp | 6 +++--- .../xo/expression/ConstantInterface.hpp | 7 +++---- .../include/xo/expression/ConvertExpr.hpp | 6 +++--- .../include/xo/expression/DefineExpr.hpp | 9 +++++---- .../include/xo/expression/Environment.hpp | 2 +- .../include/xo/expression/Expression.hpp | 6 +++--- .../xo/expression/FunctionInterface.hpp | 5 ++--- .../include/xo/expression/GlobalEnv.hpp | 8 ++++---- xo-expression/include/xo/expression/IfExpr.hpp | 8 ++++---- xo-expression/include/xo/expression/Lambda.hpp | 8 ++++---- .../include/xo/expression/LocalEnv.hpp | 4 ++-- .../xo/expression/PrimitiveInterface.hpp | 8 ++++---- .../include/xo/expression/Sequence.hpp | 2 +- .../include/xo/expression/Variable.hpp | 10 +++++----- xo-expression/src/expression/AssignExpr.cpp | 2 +- xo-expression/src/expression/DefineExpr.cpp | 7 +++++++ xo-expression/src/expression/Lambda.cpp | 18 +++++++++--------- xo-expression/src/expression/Sequence.cpp | 2 +- xo-expression/src/expression/Variable.cpp | 2 +- 20 files changed, 66 insertions(+), 60 deletions(-) diff --git a/xo-expression/include/xo/expression/Apply.hpp b/xo-expression/include/xo/expression/Apply.hpp index e95dd76a..465b0ca0 100644 --- a/xo-expression/include/xo/expression/Apply.hpp +++ b/xo-expression/include/xo/expression/Apply.hpp @@ -42,8 +42,8 @@ namespace xo { const rp & rhs); /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } const rp & fn() const { return fn_; } @@ -98,7 +98,7 @@ namespace xo { return xform_fn(this); } - virtual void attach_envs(ref::brw p) override { + virtual void attach_envs(bp p) override { fn_->attach_envs(p); for (const auto & arg : argv_) diff --git a/xo-expression/include/xo/expression/AssignExpr.hpp b/xo-expression/include/xo/expression/AssignExpr.hpp index 0e7d5e5a..248cc7c3 100644 --- a/xo-expression/include/xo/expression/AssignExpr.hpp +++ b/xo-expression/include/xo/expression/AssignExpr.hpp @@ -22,8 +22,8 @@ namespace xo { static rp make(const rp & lhs, const rp & rhs); - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } const rp & 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 xform_layer(TransformFn xform_fn) override; - virtual void attach_envs(ref::brw p) override; + virtual void attach_envs(bp p) override; virtual void display(std::ostream & os) const override; diff --git a/xo-expression/include/xo/expression/ConstantInterface.hpp b/xo-expression/include/xo/expression/ConstantInterface.hpp index 0b10ec41..0e4c9580 100644 --- a/xo-expression/include/xo/expression/ConstantInterface.hpp +++ b/xo-expression/include/xo/expression/ConstantInterface.hpp @@ -25,8 +25,8 @@ namespace xo { ConstantInterface(exprtype extype, TypeDescr valuetype) : Expression{extype, valuetype} {} /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } /** type description for representation of literal value **/ @@ -40,8 +40,7 @@ namespace xo { return std::set(); } - virtual void attach_envs(ref::brw /*p*/) override {} - + virtual void attach_envs(bp /*p*/) override {} }; /*ConstantInterface*/ diff --git a/xo-expression/include/xo/expression/ConvertExpr.hpp b/xo-expression/include/xo/expression/ConvertExpr.hpp index 8c5f9b70..88a821d4 100644 --- a/xo-expression/include/xo/expression/ConvertExpr.hpp +++ b/xo-expression/include/xo/expression/ConvertExpr.hpp @@ -25,8 +25,8 @@ namespace xo { static rp make(TypeDescr dest_type, rp arg); - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } const rp & arg() const { return arg_; } @@ -61,7 +61,7 @@ namespace xo { return xform_fn(this); } - virtual void attach_envs(ref::brw p) override { + virtual void attach_envs(bp p) override { arg_->attach_envs(p); } diff --git a/xo-expression/include/xo/expression/DefineExpr.hpp b/xo-expression/include/xo/expression/DefineExpr.hpp index e7ffce65..2f7ecfa7 100644 --- a/xo-expression/include/xo/expression/DefineExpr.hpp +++ b/xo-expression/include/xo/expression/DefineExpr.hpp @@ -28,13 +28,15 @@ namespace xo { rp value); - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } const std::string & lhs_name() const { return lhs_name_; } const rp & rhs() const { return rhs_; } + rp lhs_variable() const; + std::set calc_free_variables() const; // ----- Expression ----- @@ -69,7 +71,7 @@ namespace xo { return xform_fn(this); } - virtual void attach_envs(ref::brw p) override { + virtual void attach_envs(bp p) override { rhs_->attach_envs(p); } @@ -124,5 +126,4 @@ namespace xo { } /*namespace ast*/ } /*namespace xo*/ - /* end DefineExpr.hpp */ diff --git a/xo-expression/include/xo/expression/Environment.hpp b/xo-expression/include/xo/expression/Environment.hpp index b827a575..f846196f 100644 --- a/xo-expression/include/xo/expression/Environment.hpp +++ b/xo-expression/include/xo/expression/Environment.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 lookup_var(const std::string & vname) const = 0; + virtual bp lookup_var(const std::string & vname) const = 0; }; } /*namespace ast*/ } /*namespace xo*/ diff --git a/xo-expression/include/xo/expression/Expression.hpp b/xo-expression/include/xo/expression/Expression.hpp index 1b71ffcd..3e7b9c67 100644 --- a/xo-expression/include/xo/expression/Expression.hpp +++ b/xo-expression/include/xo/expression/Expression.hpp @@ -34,9 +34,9 @@ namespace xo { class Expression : public GeneralizedExpression { public: using VisitFn = std::function - )>; + )>; using TransformFn = std::function - (ref::brw)>; + (bp)>; 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 parent) = 0; + virtual void attach_envs(bp parent) = 0; /** append to *p_set the set of free variables in this expression. * returns the number of free variables introduced diff --git a/xo-expression/include/xo/expression/FunctionInterface.hpp b/xo-expression/include/xo/expression/FunctionInterface.hpp index a51dc69b..4b369e00 100644 --- a/xo-expression/include/xo/expression/FunctionInterface.hpp +++ b/xo-expression/include/xo/expression/FunctionInterface.hpp @@ -16,8 +16,8 @@ namespace xo { : Expression(extype, fn_type) {} /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } virtual const std::string & name() const = 0; @@ -30,5 +30,4 @@ namespace xo { } /*namespace ast*/ } /*namespace xo*/ - /** end FunctionInterface.hpp **/ diff --git a/xo-expression/include/xo/expression/GlobalEnv.hpp b/xo-expression/include/xo/expression/GlobalEnv.hpp index 9cbf328f..4e10d53e 100644 --- a/xo-expression/include/xo/expression/GlobalEnv.hpp +++ b/xo-expression/include/xo/expression/GlobalEnv.hpp @@ -16,8 +16,8 @@ namespace xo { /** create instance. Probably only need one of these **/ static rp make() { return new GlobalEnv(); } - ref::brw require_global(const std::string & vname, - ref::brw expr) { + bp require_global(const std::string & vname, + bp expr) { global_map_[vname] = expr.get(); return expr; } /*require_global*/ @@ -33,12 +33,12 @@ namespace xo { return { -1, 0 }; } - virtual ref::brw lookup_var(const std::string & vname) const override { + virtual bp lookup_var(const std::string & vname) const override { auto ix = global_map_.find(vname); if (ix == global_map_.end()) { /* not found */ - return ref::brw::from_native(nullptr); + return bp::from_native(nullptr); } return ix->second; diff --git a/xo-expression/include/xo/expression/IfExpr.hpp b/xo-expression/include/xo/expression/IfExpr.hpp index 0f62f86d..912fce90 100644 --- a/xo-expression/include/xo/expression/IfExpr.hpp +++ b/xo-expression/include/xo/expression/IfExpr.hpp @@ -30,8 +30,8 @@ namespace xo { const rp & when_false); /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::from(x); } const rp & test() const { return test_; } @@ -87,14 +87,14 @@ namespace xo { return xform_fn(this); } - virtual void attach_envs(ref::brw p) override { + virtual void attach_envs(bp 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> * p_set) override { + virtual std::int32_t find_free_vars(std::set> * p_set) override { return (test_->find_free_vars(p_set) + when_true_->find_free_vars(p_set) + when_false_->find_free_vars(p_set)); diff --git a/xo-expression/include/xo/expression/Lambda.hpp b/xo-expression/include/xo/expression/Lambda.hpp index 452cafc9..4dd120bb 100644 --- a/xo-expression/include/xo/expression/Lambda.hpp +++ b/xo-expression/include/xo/expression/Lambda.hpp @@ -32,8 +32,8 @@ namespace xo { const rp & body); /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::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 p) override; + virtual void attach_envs(bp p) override; virtual void display(std::ostream & os) const override; @@ -160,7 +160,7 @@ namespace xo { std::map> layer_var_map_; /** all lambdas nested once inside this lambda's body **/ - std::map> nested_lambda_map_; + std::map> nested_lambda_map_; /** established (once) by @ref attach_envs. * diff --git a/xo-expression/include/xo/expression/LocalEnv.hpp b/xo-expression/include/xo/expression/LocalEnv.hpp index 603bbfda..8c02f623 100644 --- a/xo-expression/include/xo/expression/LocalEnv.hpp +++ b/xo-expression/include/xo/expression/LocalEnv.hpp @@ -48,7 +48,7 @@ namespace xo { } /** single-assign this environment's parent **/ - void assign_parent(ref::brw p) { + void assign_parent(bp 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 lookup_var(const std::string & target) const override { + virtual bp lookup_var(const std::string & target) const override { for (const auto & arg : argv_) { if (arg->name() == target) return arg; diff --git a/xo-expression/include/xo/expression/PrimitiveInterface.hpp b/xo-expression/include/xo/expression/PrimitiveInterface.hpp index c8b0758b..b3fb7bd3 100644 --- a/xo-expression/include/xo/expression/PrimitiveInterface.hpp +++ b/xo-expression/include/xo/expression/PrimitiveInterface.hpp @@ -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 from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::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 /*p*/) override {} + virtual void attach_envs(bp /*p*/) override {} private: }; /*PrimitiveInterface*/ diff --git a/xo-expression/include/xo/expression/Sequence.hpp b/xo-expression/include/xo/expression/Sequence.hpp index af2fb0e3..ce17a72c 100644 --- a/xo-expression/include/xo/expression/Sequence.hpp +++ b/xo-expression/include/xo/expression/Sequence.hpp @@ -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 xform_layer(TransformFn visitor_fn) override; - virtual void attach_envs(ref::brw parent) override; + virtual void attach_envs(bp parent) override; // ----- from GeneralizedExpression ---- diff --git a/xo-expression/include/xo/expression/Variable.hpp b/xo-expression/include/xo/expression/Variable.hpp index 43936f70..2d602b21 100644 --- a/xo-expression/include/xo/expression/Variable.hpp +++ b/xo-expression/include/xo/expression/Variable.hpp @@ -25,14 +25,14 @@ namespace xo { return new Variable(name, var_type); } - /** return copy of x: same var, different object identity **/ - static rp copy(ref::brw x) { + /** return copy of @p x: same var, different object identity **/ + static rp copy(bp x) { return new Variable(x->name(), x->valuetype()); } /** downcast from Expression **/ - static ref::brw from(ref::brw x) { - return ref::brw::from(x); + static bp from(bp x) { + return bp::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 /*p*/) override; + virtual void attach_envs(bp /*p*/) override; virtual void display(std::ostream & os) const override; diff --git a/xo-expression/src/expression/AssignExpr.cpp b/xo-expression/src/expression/AssignExpr.cpp index 8d56a058..c8b52711 100644 --- a/xo-expression/src/expression/AssignExpr.cpp +++ b/xo-expression/src/expression/AssignExpr.cpp @@ -74,7 +74,7 @@ namespace xo { } void - AssignExpr::attach_envs(ref::brw p) { + AssignExpr::attach_envs(bp p) { lhs_->attach_envs(p); rhs_->attach_envs(p); } diff --git a/xo-expression/src/expression/DefineExpr.cpp b/xo-expression/src/expression/DefineExpr.cpp index 1c67188c..e9b15b2d 100644 --- a/xo-expression/src/expression/DefineExpr.cpp +++ b/xo-expression/src/expression/DefineExpr.cpp @@ -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 + DefineExpr::lhs_variable() const + { + return Variable::make(lhs_name(), valuetype()); + } + std::set DefineExpr::calc_free_variables() const { diff --git a/xo-expression/src/expression/Lambda.cpp b/xo-expression/src/expression/Lambda.cpp index 5eae7973..7c5875e2 100644 --- a/xo-expression/src/expression/Lambda.cpp +++ b/xo-expression/src/expression/Lambda.cpp @@ -128,10 +128,10 @@ namespace xo { this->body_ = (body_->xform_layer - ([&var_map](ref::brw x) -> rp + ([&var_map](bp x) -> rp { if (x->extype() == exprtype::variable) { - ref::brw var = Variable::from(x); + bp 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> nested_lambda_map; + std::map> nested_lambda_map; { this->body_->visit_layer ([&nested_lambda_map] - (ref::brw expr) + (bp expr) { if (expr->extype() == exprtype::lambda) { - ref::brw lm = Lambda::from(expr); + bp 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> nested_lambda_map; + std::map> nested_lambda_map; { this->body_->visit_layer ([&nested_lambda_map] - (ref::brw expr) + (bp expr) { if (expr->extype() == exprtype::lambda) { - ref::brw lm = Lambda::from(expr); + bp lm = Lambda::from(expr); nested_lambda_map[lm->name()] = lm.get(); } @@ -295,7 +295,7 @@ namespace xo { } /*ctor*/ void - Lambda::attach_envs(ref::brw p) { + Lambda::attach_envs(bp p) { local_env_->assign_parent(p); /** establish a binding path for each variable **/ diff --git a/xo-expression/src/expression/Sequence.cpp b/xo-expression/src/expression/Sequence.cpp index 98269e2c..37e1dafd 100644 --- a/xo-expression/src/expression/Sequence.cpp +++ b/xo-expression/src/expression/Sequence.cpp @@ -54,7 +54,7 @@ namespace xo { } void - Sequence::attach_envs(ref::brw p) { + Sequence::attach_envs(bp p) { for (const auto & x : expr_v_) x->attach_envs(p); } diff --git a/xo-expression/src/expression/Variable.cpp b/xo-expression/src/expression/Variable.cpp index 18434afb..e2fbed35 100644 --- a/xo-expression/src/expression/Variable.cpp +++ b/xo-expression/src/expression/Variable.cpp @@ -6,7 +6,7 @@ namespace xo { namespace ast { void - Variable::attach_envs(ref::brw e) { + Variable::attach_envs(bp e) { /** e makes accessible all enclosing lexical scopes **/ if (this->path_.i_link_ == -2 /*sentinel*/) { this->path_ = e->lookup_binding(this->name_);