xo-expression: + Lambda::captured_var_set; assigned in ctor

This commit is contained in:
Roland Conybeare 2024-07-03 16:37:29 -04:00
commit cdb4dd8427
2 changed files with 39 additions and 10 deletions

View file

@ -125,6 +125,9 @@ namespace xo {
/** free variables for this lambda **/
std::set<std::string> free_var_set_;
/** variables that appear free in some nested lambda **/
std::set<std::string> captured_var_set_;
/** map giving unique identity to each variable appearing in this layer.
* includes:
* - formal parameters

View file

@ -149,24 +149,50 @@ namespace xo {
this->free_var_set_ = this->calc_free_variables();
std::map<std::string, ref::brw<Lambda>> nested_lambda_map;
{
this->body_->visit_layer
([&nested_lambda_map]
(ref::brw<Expression> expr)
{
if (expr->extype() == exprtype::lambda) {
ref::brw<Lambda> lm = Lambda::from(expr);
this->body_->visit_layer
([&nested_lambda_map]
(ref::brw<Expression> expr)
{
if (expr->extype() == exprtype::lambda) {
ref::brw<Lambda> lm = Lambda::from(expr);
nested_lambda_map[lm->name()] = lm.get();
}
});
nested_lambda_map[lm->name()] = lm.get();
}
});
}
this->nested_lambda_map_ = std::move(nested_lambda_map);
/* establish the set of captured local vars.
* These are any formal parameters that appear free in
* any layer of a nested lambda.
*/
std::set<std::string> captured_var_set;
{
for (const auto & ix : nested_lambda_map_) {
std::set<std::string> nested_free_var_set
= ix.second->get_free_variables();
for (const auto & jx : nested_free_var_set) {
/* check whether variable *jx is one of this lambda's formals */
auto bind = this->local_env_->lookup_local_binding(jx);
if (bind.i_link_ == 0) {
/* yup, it's a formal parameter of this lambda */
captured_var_set.insert(jx);
}
}
}
}
this->captured_var_set_ = std::move(captured_var_set);
/* in particular:
* - establish binding path for each variable
* - establish binding path (intrusively) for each variable
* assigns Variable::path_
*/
this->body_->attach_envs(local_env_);
} /*ctor*/
void