xo-expression: fix: ns xo::ref::rp -> xo::rp
This commit is contained in:
parent
cdb4dd8427
commit
ca19c65b02
12 changed files with 70 additions and 75 deletions
|
|
@ -25,16 +25,16 @@ namespace xo {
|
|||
public:
|
||||
/** create new apply-expression instance
|
||||
**/
|
||||
static ref::rp<Apply> make(const ref::rp<Expression> & fn,
|
||||
const std::vector<ref::rp<Expression>> & argv);
|
||||
static rp<Apply> make(const rp<Expression> & fn,
|
||||
const std::vector<rp<Expression>> & argv);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<Apply> from(ref::brw<Expression> x) {
|
||||
return ref::brw<Apply>::from(x);
|
||||
}
|
||||
|
||||
const ref::rp<Expression> & fn() const { return fn_; }
|
||||
const std::vector<ref::rp<Expression>> & argv() const { return argv_; }
|
||||
const rp<Expression> & fn() const { return fn_; }
|
||||
const std::vector<rp<Expression>> & argv() const { return argv_; }
|
||||
|
||||
virtual std::set<std::string> get_free_variables() const override {
|
||||
std::set<std::string> retval = fn_->get_free_variables();
|
||||
|
|
@ -76,7 +76,7 @@ namespace xo {
|
|||
return n;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
this->fn_ = fn_->xform_layer(xform_fn);
|
||||
|
||||
for (auto & arg : argv_)
|
||||
|
|
@ -96,17 +96,17 @@ namespace xo {
|
|||
|
||||
private:
|
||||
Apply(TypeDescr apply_valuetype,
|
||||
const ref::rp<Expression> & fn,
|
||||
const std::vector<ref::rp<Expression>> & argv)
|
||||
const rp<Expression> & fn,
|
||||
const std::vector<rp<Expression>> & argv)
|
||||
: Expression(exprtype::apply, apply_valuetype),
|
||||
fn_{fn}, argv_(argv)
|
||||
{}
|
||||
|
||||
private:
|
||||
/** function to invoke **/
|
||||
ref::rp<Expression> fn_;
|
||||
rp<Expression> fn_;
|
||||
/** argument expressions, in l-to-r order **/
|
||||
std::vector<ref::rp<Expression>> argv_;
|
||||
std::vector<rp<Expression>> argv_;
|
||||
}; /*Apply*/
|
||||
|
||||
#ifdef NOT_USING
|
||||
|
|
@ -136,10 +136,10 @@ namespace xo {
|
|||
#endif
|
||||
|
||||
/* reminder: initializer-lists are compile-time only */
|
||||
inline ref::rp<Apply>
|
||||
make_apply(const ref::rp<Expression> & fn,
|
||||
const std::initializer_list<ref::rp<Expression>> args) {
|
||||
std::vector<ref::rp<Expression>> argv(args);
|
||||
inline rp<Apply>
|
||||
make_apply(const rp<Expression> & fn,
|
||||
const std::initializer_list<rp<Expression>> args) {
|
||||
std::vector<rp<Expression>> argv(args);
|
||||
return Apply::make(fn, argv);
|
||||
} /*make_apply*/
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ namespace xo {
|
|||
|
||||
public:
|
||||
/** create constant expression representing literal value x **/
|
||||
static ref::rp<Constant> make(const T & x) {
|
||||
static rp<Constant> make(const T & x) {
|
||||
TypeDescr x_valuetype = Reflect::require<T>();
|
||||
|
||||
return new Constant(x_valuetype, x);
|
||||
|
|
@ -61,7 +61,7 @@ namespace xo {
|
|||
return 1;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
return xform_fn(this);
|
||||
}
|
||||
|
||||
|
|
@ -89,7 +89,7 @@ namespace xo {
|
|||
}; /*Constant*/
|
||||
|
||||
template <typename T>
|
||||
ref::rp<Constant<std::remove_reference_t<T>>>
|
||||
rp<Constant<std::remove_reference_t<T>>>
|
||||
make_constant(const T & x) {
|
||||
return Constant<T>::make(x);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace xo {
|
|||
using VisitFn = std::function
|
||||
<void (ref::brw<Expression>)>;
|
||||
using TransformFn = std::function
|
||||
<ref::rp<Expression> (ref::brw<Expression>)>;
|
||||
<rp<Expression> (ref::brw<Expression>)>;
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
|
||||
public:
|
||||
|
|
@ -69,7 +69,7 @@ namespace xo {
|
|||
virtual std::size_t visit_layer(VisitFn visitor_fn) = 0;
|
||||
|
||||
/** traverse ast @ref visit_preorder but do not visit Lambdas **/
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn visitor_fn) = 0;
|
||||
virtual rp<Expression> xform_layer(TransformFn visitor_fn) = 0;
|
||||
|
||||
/** attach an environment to each lambda expression X in this subtree,
|
||||
* that will:
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace xo {
|
|||
class GlobalEnv : public Environment {
|
||||
public:
|
||||
/** create instance. Probably only need one of these **/
|
||||
static ref::rp<GlobalEnv> make() { return new GlobalEnv(); }
|
||||
static rp<GlobalEnv> make() { return new GlobalEnv(); }
|
||||
|
||||
ref::brw<Expression> require_global(const std::string & vname,
|
||||
ref::brw<Expression> expr) {
|
||||
|
|
@ -51,7 +51,7 @@ namespace xo {
|
|||
/* for assignable globals, need to allocate memory
|
||||
* addresses for these.
|
||||
*/
|
||||
std::map<std::string, ref::rp<Expression>> global_map_;
|
||||
std::map<std::string, rp<Expression>> global_map_;
|
||||
};
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -25,18 +25,18 @@ namespace xo {
|
|||
* @p when_true or @p when_false, depending on result
|
||||
* of evaluating expression @p test
|
||||
**/
|
||||
static ref::rp<IfExpr> make(const ref::rp<Expression> & test,
|
||||
const ref::rp<Expression> & when_true,
|
||||
const ref::rp<Expression> & when_false);
|
||||
static rp<IfExpr> make(const rp<Expression> & test,
|
||||
const rp<Expression> & when_true,
|
||||
const rp<Expression> & when_false);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<IfExpr> from(ref::brw<Expression> x) {
|
||||
return ref::brw<IfExpr>::from(x);
|
||||
}
|
||||
|
||||
const ref::rp<Expression> & test() const { return test_; }
|
||||
const ref::rp<Expression> & when_true() const { return when_true_; }
|
||||
const ref::rp<Expression> & when_false() const { return when_false_; }
|
||||
const rp<Expression> & test() const { return test_; }
|
||||
const rp<Expression> & when_true() const { return when_true_; }
|
||||
const rp<Expression> & when_false() const { return when_false_; }
|
||||
|
||||
// ----- Expression -----
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ namespace xo {
|
|||
return n;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
this->test_ = this->test_->xform_layer(xform_fn);
|
||||
this->when_true_ = this->when_true_->xform_layer(xform_fn);
|
||||
this->when_false_= this->when_false_->xform_layer(xform_fn);
|
||||
|
|
@ -113,13 +113,13 @@ namespace xo {
|
|||
* @p when_false else-branch; executes only when test fails
|
||||
**/
|
||||
IfExpr(TypeDescr ifexpr_type,
|
||||
const ref::rp<Expression> & test,
|
||||
const ref::rp<Expression> & when_true,
|
||||
const ref::rp<Expression> & when_false)
|
||||
rp<Expression> test,
|
||||
rp<Expression> when_true,
|
||||
rp<Expression> when_false)
|
||||
: Expression(exprtype::ifexpr, ifexpr_type),
|
||||
test_{test},
|
||||
when_true_{when_true},
|
||||
when_false_{when_false} {}
|
||||
test_{std::move(test)},
|
||||
when_true_{std::move(when_true)},
|
||||
when_false_{std::move(when_false)} {}
|
||||
|
||||
private:
|
||||
/** if:
|
||||
|
|
@ -127,15 +127,15 @@ namespace xo {
|
|||
*
|
||||
* executes x; if true execute y; otherwise execute z
|
||||
**/
|
||||
ref::rp<Expression> test_;
|
||||
ref::rp<Expression> when_true_;
|
||||
ref::rp<Expression> when_false_;
|
||||
rp<Expression> test_;
|
||||
rp<Expression> when_true_;
|
||||
rp<Expression> when_false_;
|
||||
}; /*IfExpr*/
|
||||
|
||||
inline ref::rp<IfExpr>
|
||||
make_ifexpr(const ref::rp<Expression> & test,
|
||||
const ref::rp<Expression> & when_true,
|
||||
const ref::rp<Expression> & when_false)
|
||||
inline rp<IfExpr>
|
||||
make_ifexpr(const rp<Expression> & test,
|
||||
const rp<Expression> & when_true,
|
||||
const rp<Expression> & when_false)
|
||||
{
|
||||
return IfExpr::make(test, when_true, when_false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ namespace xo {
|
|||
* @p argv Formal parameters, in left-to-right order
|
||||
* @p body Expression for body of this function
|
||||
**/
|
||||
static ref::rp<Lambda> make(const std::string & name,
|
||||
const std::vector<ref::rp<Variable>> & argv,
|
||||
const ref::rp<Expression> & body);
|
||||
static rp<Lambda> make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body);
|
||||
|
||||
/** downcast from Expression **/
|
||||
static ref::brw<Lambda> from(ref::brw<Expression> x) {
|
||||
|
|
@ -37,8 +37,8 @@ namespace xo {
|
|||
}
|
||||
|
||||
const std::string & type_str() const { return type_str_; }
|
||||
const std::vector<ref::rp<Variable>> & argv() const { return local_env_->argv(); }
|
||||
const ref::rp<Expression> & body() const { return body_; }
|
||||
const std::vector<rp<Variable>> & argv() const { return local_env_->argv(); }
|
||||
const rp<Expression> & body() const { return body_; }
|
||||
|
||||
bool needs_closure_flag() const { return !free_var_set_.empty(); }
|
||||
|
||||
|
|
@ -77,7 +77,7 @@ namespace xo {
|
|||
return n;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn /*xform_fn*/) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn /*xform_fn*/) override {
|
||||
/* a layer is bounded by lambdas, don't enter them */
|
||||
return this;
|
||||
}
|
||||
|
|
@ -92,8 +92,8 @@ namespace xo {
|
|||
**/
|
||||
Lambda(const std::string & name,
|
||||
TypeDescr lambda_type,
|
||||
const ref::rp<LocalEnv> & local_env,
|
||||
const ref::rp<Expression> & body);
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<Expression> & body);
|
||||
|
||||
/** compute free-variable set for this lambda **/
|
||||
std::set<std::string> calc_free_variables() const;
|
||||
|
|
@ -104,7 +104,7 @@ namespace xo {
|
|||
* Goal is to unify variables that can use the same binding
|
||||
* path to determine memory location at runtime.
|
||||
**/
|
||||
std::map<std::string, ref::rp<Variable>> regularize_layer_vars();
|
||||
std::map<std::string, rp<Variable>> regularize_layer_vars();
|
||||
|
||||
private:
|
||||
/** lambda name. Initially supporting only form like
|
||||
|
|
@ -120,7 +120,7 @@ namespace xo {
|
|||
**/
|
||||
std::string type_str_;
|
||||
/** function body **/
|
||||
ref::rp<Expression> body_;
|
||||
rp<Expression> body_;
|
||||
|
||||
/** free variables for this lambda **/
|
||||
std::set<std::string> free_var_set_;
|
||||
|
|
@ -136,7 +136,7 @@ namespace xo {
|
|||
* - any variables appearing in nested lambdas
|
||||
* (whether formals or free variables)
|
||||
**/
|
||||
std::map<std::string, ref::rp<Variable>> layer_var_map_;
|
||||
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_;
|
||||
|
|
@ -147,13 +147,13 @@ namespace xo {
|
|||
* when Lambda constructor runs, so we need to assign @ref local_env_
|
||||
* later.
|
||||
**/
|
||||
ref::rp<LocalEnv> local_env_;
|
||||
rp<LocalEnv> local_env_;
|
||||
}; /*Lambda*/
|
||||
|
||||
inline ref::rp<Lambda>
|
||||
inline rp<Lambda>
|
||||
make_lambda(const std::string & name,
|
||||
const std::vector<ref::rp<Variable>> & argv,
|
||||
const ref::rp<Expression> & body)
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
return Lambda::make(name, argv, body);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,12 +26,12 @@ namespace xo {
|
|||
|
||||
public:
|
||||
/** named ctor idiom. Create instance with local variables per @p argv **/
|
||||
static ref::rp<LocalEnv> make(const std::vector<ref::rp<Variable>> & argv) {
|
||||
static rp<LocalEnv> make(const std::vector<rp<Variable>> & argv) {
|
||||
return new LocalEnv(argv);
|
||||
}
|
||||
|
||||
Lambda * origin() const { return origin_; }
|
||||
const std::vector<ref::rp<Variable>> & argv() const { return argv_; }
|
||||
const std::vector<rp<Variable>> & argv() const { return argv_; }
|
||||
int n_arg() const { return argv_.size(); }
|
||||
TypeDescr fn_arg(uint32_t i) const { return argv_[i]->valuetype(); }
|
||||
|
||||
|
|
@ -71,7 +71,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
private:
|
||||
LocalEnv(const std::vector<ref::rp<Variable>> & argv)
|
||||
LocalEnv(const std::vector<rp<Variable>> & argv)
|
||||
: origin_{nullptr}, argv_(argv) {}
|
||||
|
||||
private:
|
||||
|
|
@ -85,12 +85,12 @@ namespace xo {
|
|||
Lambda * origin_ = nullptr;
|
||||
|
||||
/** formal argument names **/
|
||||
std::vector<ref::rp<Variable>> argv_;
|
||||
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_.
|
||||
**/
|
||||
ref::rp<Environment> parent_env_;
|
||||
rp<Environment> parent_env_;
|
||||
};
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ namespace xo {
|
|||
return 1;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
return xform_fn(this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ namespace xo {
|
|||
* identified by @p name, that can take on values
|
||||
* described by @p var_type.
|
||||
**/
|
||||
static ref::rp<Variable> make(const std::string & name,
|
||||
TypeDescr var_type) {
|
||||
static rp<Variable> make(const std::string & name,
|
||||
TypeDescr var_type) {
|
||||
return new Variable(name, var_type);
|
||||
}
|
||||
|
||||
/** return copy of x: same var, different object identity **/
|
||||
static ref::rp<Variable> copy(ref::brw<Variable> x) {
|
||||
static rp<Variable> copy(ref::brw<Variable> x) {
|
||||
return new Variable(x->name(), x->valuetype());
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +53,7 @@ namespace xo {
|
|||
return 1;
|
||||
}
|
||||
|
||||
virtual ref::rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
virtual rp<Expression> xform_layer(TransformFn xform_fn) override {
|
||||
return xform_fn(this);
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +76,7 @@ namespace xo {
|
|||
binding_path path_;
|
||||
}; /*Variable*/
|
||||
|
||||
inline ref::rp<Variable>
|
||||
inline rp<Variable>
|
||||
make_var(const std::string & name,
|
||||
reflect::TypeDescr var_type) {
|
||||
return Variable::make(name, var_type);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
||||
namespace xo {
|
||||
using xo::ref::rp;
|
||||
|
||||
namespace ast {
|
||||
rp<Apply>
|
||||
Apply::make(const rp<Expression> & fn,
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
||||
namespace xo {
|
||||
using xo::ref::rp;
|
||||
|
||||
namespace ast {
|
||||
rp<IfExpr>
|
||||
IfExpr::make(const rp<Expression> & test,
|
||||
|
|
|
|||
|
|
@ -9,14 +9,13 @@
|
|||
namespace xo {
|
||||
using xo::reflect::TypeDescrBase;
|
||||
using xo::reflect::FunctionTdxInfo;
|
||||
using xo::ref::rp;
|
||||
using std::stringstream;
|
||||
|
||||
namespace ast {
|
||||
rp<Lambda>
|
||||
Lambda::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const ref::rp<Expression> & body)
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
using xo::reflect::FunctionTdx;
|
||||
|
||||
|
|
@ -69,7 +68,7 @@ namespace xo {
|
|||
return retval;
|
||||
} /*calc_free_variables*/
|
||||
|
||||
std::map<std::string, ref::rp<Variable>>
|
||||
std::map<std::string, rp<Variable>>
|
||||
Lambda::regularize_layer_vars()
|
||||
{
|
||||
/* regularize local_env+body: make sure exactly one instance
|
||||
|
|
@ -83,7 +82,7 @@ namespace xo {
|
|||
* Motivation is to unify Variables that would use the same
|
||||
* binding_path to resolve their runtime location.
|
||||
*/
|
||||
std::map<std::string, ref::rp<Variable>> var_map;
|
||||
std::map<std::string, rp<Variable>> var_map;
|
||||
|
||||
for (const auto & arg : local_env_->argv()) {
|
||||
/* each arg name can appear at most once
|
||||
|
|
@ -96,7 +95,7 @@ namespace xo {
|
|||
|
||||
this->body_
|
||||
= (body_->xform_layer
|
||||
([&var_map](ref::brw<Expression> x) -> ref::rp<Expression>
|
||||
([&var_map](ref::brw<Expression> x) -> rp<Expression>
|
||||
{
|
||||
if (x->extype() == exprtype::variable) {
|
||||
ref::brw<Variable> var = Variable::from(x);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue