xo-expression: generalize envframestack + handle explicit lm retturn

This commit is contained in:
Roland Conybeare 2025-07-28 13:16:09 -04:00
commit 4ab3a8499b
5 changed files with 56 additions and 30 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include "xo/refcnt/Refcounted.hpp"
#include "Variable.hpp"
#include "binding_path.hpp"
#include "xo/indentlog/print/pretty.hpp"
@ -14,6 +15,13 @@ namespace xo {
namespace scm {
class Expression;
/** @class Environment
* @brief Abstract interface for tracking variable bindings
*
* When parsing (see xo-reader): rhs will always be a variable.
* When generating code (see xo-jit): rhs can be any expression,
* for example a Lambda.
**/
class Environment : public ref::Refcount {
public:
/** true if this is toplevel (global) environment.
@ -36,6 +44,14 @@ namespace xo {
**/
virtual bp<Expression> lookup_var(const std::string & vname) const = 0;
/** like @ref lookup_var but do not delegate to parent environment **/
virtual bp<Expression> lookup_local(const std::string & vname) const = 0;
/** create/replace local variable @p target.
* Narrow use case: intended for when Environment represents a top-level session environment
**/
virtual void upsert_local(bp<Variable> target) = 0;
virtual void print(std::ostream & os) const = 0;
virtual std::uint32_t pretty_print(const xo::print::ppindentinfo & ppii) const = 0;
};

View file

@ -14,7 +14,7 @@ namespace xo {
class GlobalEnv : public Environment {
public:
/** create instance. Probably only need one of these **/
static rp<GlobalEnv> make() { return new GlobalEnv(); }
static rp<GlobalEnv> make_empty() { return new GlobalEnv(); }
bp<Expression> require_global(const std::string & vname,
bp<Expression> expr);
@ -31,16 +31,22 @@ namespace xo {
}
virtual bp<Expression> lookup_var(const std::string & vname) const override {
return this->lookup_local(vname);
}
virtual bp<Expression> lookup_local(const std::string & vname) const override {
auto ix = global_map_.find(vname);
if (ix == global_map_.end()) {
/* not found */
return bp<Expression>::from_native(nullptr);
return bp<Variable>::from_native(nullptr);
}
return ix->second;
}
virtual void upsert_local(bp<Variable> target) override;
virtual void print(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const xo::print::ppindentinfo & ppii) const override;

View file

@ -32,6 +32,8 @@ namespace xo {
/** Create instance with single local variable @ap argv1 **/
static rp<LocalEnv> make1(const rp<Variable> & arg1,
const rp<Environment> & parent_env);
/** runtime downcast. nullptr if @p x is not a LocalEnv instance **/
static bp<LocalEnv> from(const bp<Environment> & x) { return bp<LocalEnv>::from(x); }
Lambda * origin() const { return origin_; }
const std::vector<rp<Variable>> & argv() const { return argv_; }
@ -53,31 +55,38 @@ namespace xo {
/** single-assign this environment's parent **/
void assign_parent(bp<Environment> p);
/** create/replace local variable @p target.
* Narrow use case: intended for when LocalEnv represents a top-level session environment
**/
void upsert_local(bp<Variable> target);
bp<Variable> lookup_local(const std::string & vname) const;
// ----- Environment -----
virtual bool is_global_env() const override { return false; }
virtual binding_path lookup_binding(const std::string & vname) const override;
virtual bp<Expression> lookup_var(const std::string & target) const override {
for (const auto & arg : argv_) {
if (arg->name() == target)
return arg;
}
virtual bp<Expression> lookup_var(const std::string & vname) const override {
bp<Expression> retval = this->lookup_local(vname);
if (retval)
return retval;
/* here: target not found in local vars,
* delegate to innermost ancestor
*/
return parent_env_->lookup_var(target);
return parent_env_->lookup_var(vname);
}
virtual bp<Expression> lookup_local(const std::string & vname) const override {
for (const auto & arg : argv_) {
if (arg->name() == vname)
return arg;
}
return bp<Expression>();
}
/** create/replace local variable @p target.
* Narrow use case: intended for when LocalEnv represents a top-level session environment.
**/
virtual void upsert_local(bp<Variable> target) override;
virtual void print(std::ostream & os) const override;
virtual std::uint32_t pretty_print(const print::ppindentinfo & ppii) const override;
@ -85,7 +94,7 @@ namespace xo {
LocalEnv(const std::vector<rp<Variable>> & argv, const rp<Environment> & parent_env);
private:
/** Lambnda for which this environment created.
/** Lambda for which this environment created.
*
* Invariant:
* @code

View file

@ -16,9 +16,17 @@ namespace xo {
bp<Expression> expr)
{
this->global_map_[vname] = expr.get();
return expr;
} /*require_global*/
void
GlobalEnv::upsert_local(bp<Variable> target) {
// in practice: paraphrase of .require_global()
this->global_map_[target->name()] = target.promote();
}
void
GlobalEnv::print(std::ostream & os) const {
os << "<GlobalEnv"

View file

@ -70,16 +70,6 @@ namespace xo {
return { free.i_link_ + 1, free.j_slot_ };
} /*lookup_binding*/
bp<Variable>
LocalEnv::lookup_local(const std::string & vname) const {
for (const auto & var : this->argv_) {
if (var->name() == vname)
return var;
}
return bp<Variable>::from_native(nullptr);
}
void
LocalEnv::assign_parent(bp<Environment> p) {
if ((parent_env_.get() != nullptr) && (parent_env_.get() != p.get())) {
@ -110,8 +100,7 @@ namespace xo {
void
LocalEnv::print(std::ostream& os) const {
os << "<localenv"
<< xtag("this", (void*)this)
os << "<LocalEnv"
<< xtag("argv", argv_)
<< ">";
}
@ -125,8 +114,6 @@ namespace xo {
if (ppii.upto()) {
if (!pps->print_upto("<LocalEnv"))
return false;
if (!pps->print_upto_tag("this", (void*)this))
return false;
if (!pps->print_upto_tag("argv", argv_))
return false;
pps->write(">");