xo-expression: + Expression::get_free_variables()
This commit is contained in:
parent
28884e1f4f
commit
b7db7c5454
7 changed files with 59 additions and 0 deletions
|
|
@ -36,6 +36,20 @@ namespace xo {
|
||||||
const ref::rp<Expression> & fn() const { return fn_; }
|
const ref::rp<Expression> & fn() const { return fn_; }
|
||||||
const std::vector<ref::rp<Expression>> & argv() const { return argv_; }
|
const std::vector<ref::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();
|
||||||
|
|
||||||
|
for (const auto & arg : argv_) {
|
||||||
|
std::set<std::string> arg_free_set
|
||||||
|
= arg->get_free_variables();
|
||||||
|
|
||||||
|
for (const auto & name : arg_free_set)
|
||||||
|
retval.insert(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
||||||
std::size_t n = 1;
|
std::size_t n = 1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,10 @@ namespace xo {
|
||||||
|
|
||||||
// ----- Expression -----
|
// ----- Expression -----
|
||||||
|
|
||||||
|
virtual std::set<std::string> get_free_variables() const override {
|
||||||
|
return std::set<std::string>();
|
||||||
|
}
|
||||||
|
|
||||||
virtual void attach_envs(ref::brw<Environment> /*p*/) override {}
|
virtual void attach_envs(ref::brw<Environment> /*p*/) override {}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,12 @@ namespace xo {
|
||||||
exprtype extype() const { return extype_; }
|
exprtype extype() const { return extype_; }
|
||||||
TypeDescr valuetype() const { return valuetype_; }
|
TypeDescr valuetype() const { return valuetype_; }
|
||||||
|
|
||||||
|
/** find free named variables in this expression.
|
||||||
|
* comprises the set of names that don't match formal parameters in
|
||||||
|
* enclosing lambdas.
|
||||||
|
**/
|
||||||
|
virtual std::set<std::string> get_free_variables() const = 0;
|
||||||
|
|
||||||
/** visit each Expression node in this AST,
|
/** visit each Expression node in this AST,
|
||||||
* and invoke @p fn for each.
|
* and invoke @p fn for each.
|
||||||
* Returns the number of nodes visited.
|
* Returns the number of nodes visited.
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,21 @@ namespace xo {
|
||||||
|
|
||||||
// ----- Expression -----
|
// ----- Expression -----
|
||||||
|
|
||||||
|
virtual std::set<std::string> get_free_variables() const override {
|
||||||
|
std::set<std::string> retval = test_->get_free_variables();
|
||||||
|
|
||||||
|
std::set<std::string> free_vars;
|
||||||
|
free_vars = when_true_->get_free_variables();
|
||||||
|
for (const auto & s : free_vars)
|
||||||
|
retval.insert(s);
|
||||||
|
|
||||||
|
free_vars = when_false_->get_free_variables();
|
||||||
|
for (const auto & s : free_vars)
|
||||||
|
retval.insert(s);
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
||||||
std::size_t n = 1;
|
std::size_t n = 1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,16 @@ namespace xo {
|
||||||
|
|
||||||
// ----- Expression -----
|
// ----- Expression -----
|
||||||
|
|
||||||
|
virtual std::set<std::string> get_free_variables() const override {
|
||||||
|
std::set<std::string> retval = body_->get_free_variables();
|
||||||
|
|
||||||
|
/* but remove formals. */
|
||||||
|
for (const auto & var : local_env_->argv())
|
||||||
|
retval.erase(var->name());
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
||||||
std::size_t n = 1;
|
std::size_t n = 1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,10 @@ namespace xo {
|
||||||
|
|
||||||
// ----- Expression -----
|
// ----- Expression -----
|
||||||
|
|
||||||
|
virtual std::set<std::string> get_free_variables() const override {
|
||||||
|
return std::set<std::string>();
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
||||||
visitor_fn(this);
|
visitor_fn(this);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,12 @@ namespace xo {
|
||||||
|
|
||||||
const std::string & name() const { return name_; }
|
const std::string & name() const { return name_; }
|
||||||
|
|
||||||
|
virtual std::set<std::string> get_free_variables() const override {
|
||||||
|
std::set<std::string> retval;
|
||||||
|
retval.insert(this->name_);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
|
||||||
visitor_fn(this);
|
visitor_fn(this);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue