xo-expression: + Lambda::nested_lambda_map

This commit is contained in:
Roland Conybeare 2024-07-03 16:20:18 -04:00
commit 4b0a2cff2a
5 changed files with 31 additions and 5 deletions

View file

@ -26,7 +26,7 @@ namespace xo {
virtual bool is_global_env() const override { return true; }
virtual binding_path lookup_binding(const std::string & vname) const override {
virtual binding_path lookup_binding(const std::string & /*vname*/) const override {
/* i_link: -1 for global environment
* j_slot: not used
*/

View file

@ -135,6 +135,9 @@ namespace xo {
**/
std::map<std::string, ref::rp<Variable>> layer_var_map_;
/** all lambdas nested once inside this lambda's body **/
std::map<std::string, ref::brw<Lambda>> nested_lambda_map_;
/** established (once) by @ref attach_envs.
*
* @note data dependency on ancestor expressions that don't exist yet

View file

@ -13,8 +13,8 @@ namespace xo {
* providing its location.
**/
struct binding_path {
/** @of parent links to traverse. -1 if global **/
int i_link_ = -1;
/** @of parent links to traverse. -1 if global. -2 if sentinel **/
int i_link_ = -2;
/** for variables bound in some local environment:
* slot# within that environment.
*

View file

@ -83,7 +83,7 @@ namespace xo {
* Motivation is to unify Variables that would use the same
* binding_path to resolve their runtime location.
*/
std::map<std::string, rp<Variable>> var_map;
std::map<std::string, ref::rp<Variable>> var_map;
for (const auto & arg : local_env_->argv()) {
/* each arg name can appear at most once
@ -148,6 +148,24 @@ 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);
nested_lambda_map[lm->name()] = lm.get();
}
});
this->nested_lambda_map_ = std::move(nested_lambda_map);
/* in particular:
* - establish binding path for each variable
*/
this->body_->attach_envs(local_env_);
} /*ctor*/
@ -155,6 +173,7 @@ namespace xo {
Lambda::attach_envs(ref::brw<Environment> p) {
local_env_->assign_parent(p);
/** establish a binding path for each variable **/
}
void

View file

@ -8,7 +8,11 @@ namespace xo {
void
Variable::attach_envs(ref::brw<Environment> e) {
/** e makes accessible all enclosing lexical scopes **/
this->path_ = e->lookup_binding(this->name_);
if (this->path_.i_link_ == -2 /*sentinel*/) {
this->path_ = e->lookup_binding(this->name_);
} else {
/* have already established binding for this Variable */
}
} /*attach_envs*/
void