xo-expression: refactor: xtract -> method regularize_layer_vars()

This commit is contained in:
Roland Conybeare 2024-07-03 14:39:32 -04:00
commit a76c835477
2 changed files with 33 additions and 19 deletions

View file

@ -91,6 +91,14 @@ namespace xo {
/** compute free-variable set for this lambda **/
std::set<std::string> calc_free_variables() const;
/** ensure at most one Variable instance with a particular name
* in this lambda, but ignore nested lambdas.
*
* Goal is to unify variables that can use the same binding
* path to determine memory location at runtime.
**/
void regularize_layer_vars();
private:
/** lambda name. Initially supporting only form like
* (define (foo x y z)

View file

@ -69,25 +69,9 @@ namespace xo {
return retval;
} /*calc_free_variables*/
Lambda::Lambda(const std::string & name,
TypeDescr lambda_type,
const rp<LocalEnv> & local_env,
const ref::rp<Expression> & body)
: FunctionInterface(exprtype::lambda, lambda_type),
name_{name},
body_{body},
local_env_{local_env}
void
Lambda::regularize_layer_vars()
{
stringstream ss;
ss << "double";
ss << "(";
for (std::size_t i = 0, n = this->n_arg(); i < n; ++i) {
if (i > 0)
ss << ",";
ss << "double";
}
ss << ")";
/* regularize local_env+body: make sure exactly one instance
* (i.e. with object identity) of a Variable appears
* within one layer of a lambda body.
@ -133,11 +117,33 @@ namespace xo {
}
});
}
} /*regularize_layer_vars*/
Lambda::Lambda(const std::string & name,
TypeDescr lambda_type,
const rp<LocalEnv> & local_env,
const ref::rp<Expression> & body)
: FunctionInterface(exprtype::lambda, lambda_type),
name_{name},
body_{body},
local_env_{local_env}
{
stringstream ss;
ss << "double";
ss << "(";
for (std::size_t i = 0, n = this->n_arg(); i < n; ++i) {
if (i > 0)
ss << ",";
ss << "double";
}
ss << ")";
this->type_str_ = ss.str();
this->free_var_set_ = this->calc_free_variables();
body_->attach_envs(local_env_);
this->regularize_layer_vars();
this->body_->attach_envs(local_env_);
} /*ctor*/
void