xo-expression: generalize envframestack + handle explicit lm retturn

This commit is contained in:
Roland Conybeare 2025-07-28 13:16:09 -04:00
commit 5c7b756bd1
15 changed files with 112 additions and 74 deletions

View file

@ -12,7 +12,7 @@ namespace xo {
using xo::scm::Variable;
namespace scm {
bp<LocalEnv>
bp<Environment>
envframestack::top_envframe() const {
std::size_t z = stack_.size();
@ -25,7 +25,7 @@ namespace xo {
}
void
envframestack::push_envframe(const rp<LocalEnv> & frame)
envframestack::push_envframe(const rp<Environment> & frame)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag),
@ -38,7 +38,7 @@ namespace xo {
stack_[z] = frame;
}
rp<LocalEnv>
rp<Environment>
envframestack::pop_envframe() {
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
@ -48,7 +48,7 @@ namespace xo {
if (z > 0) {
//std::unique_ptr<exprstate> top = std::move(stack_[z-1]);
rp<LocalEnv> retval = stack_.at(z-1);
rp<Environment> retval = stack_.at(z-1);
stack_.resize(z-1);
@ -58,7 +58,7 @@ namespace xo {
}
}
bp<Variable>
bp<Expression>
envframestack::lookup(const std::string & x) const {
for (std::size_t i = 0, z = this->size(); i < z; ++i) {
const auto & frame = (*this)[i];

View file

@ -156,6 +156,7 @@ namespace xo {
parserstatemachine * p_psm)
{
constexpr const char * c_self_name = "lambda_xs::on_typedescr";
scope log(XO_DEBUG(p_psm->debug_flag()));
assert(td);
@ -189,13 +190,17 @@ namespace xo {
&& (p_psm->env_stack_size() >= 2)
)
{
bp<LocalEnv> def_env = p_psm->lookup_envframe(1);
const define_xs * def_xs = dynamic_cast<const define_xs*>(&(p_psm->lookup_exprstate(2)));
assert(def_env->n_arg() == 1);
assert(def_xs);
bp<Variable> def_var = def_env->lookup_arg(0).get();
bp<Variable> def_var = def_xs->lhs_variable();
if (def_var->valuetype() == nullptr) {
log && log("assign discovered lambda type T to enclosing define",
xtag("lhs", def_var),
xtag("T", print::unq(this->lambda_td_->canonical_name())));
def_var->assign_valuetype(lambda_td_);
} else {
/* don't need to unify here. if def already hasa a type,

View file

@ -11,8 +11,8 @@
#include "xo/expression/DefineExpr.hpp"
#include "xo/expression/Constant.hpp"
#include "xo/expression/ConvertExpr.hpp"
//#include "xo/expression/GlobalEnv.hpp"
#include "xo/expression/LocalEnv.hpp"
#include "xo/expression/GlobalEnv.hpp"
//#include "xo/expression/LocalEnv.hpp"
//#include <regex>
#include <stdexcept>
@ -28,7 +28,7 @@ namespace xo {
: psm_{debug_flag}
{
/* top-level environment. initially empty */
rp<LocalEnv> toplevel_env = LocalEnv::make_empty();
rp<Environment> toplevel_env = GlobalEnv::make_empty();
this->psm_.env_stack_.push_envframe(toplevel_env);
}
@ -70,6 +70,12 @@ namespace xo {
return psm_.result_;
} /*include_token*/
void
parser::reset_result()
{
psm_.result_ = parser_result::none();
}
void
parser::reset_to_idle_toplevel()
{

View file

@ -17,7 +17,7 @@ namespace xo {
namespace scm {
bp<Variable>
parserstatemachine::lookup_var(const std::string & x) const {
return env_stack_.lookup(x);
return Variable::from(env_stack_.lookup(x));
}
void
@ -45,30 +45,28 @@ namespace xo {
xs_stack_.push_exprstate(std::move(x));
}
bp<LocalEnv>
bp<Environment>
parserstatemachine::top_envframe() const {
return env_stack_.top_envframe();
}
bp<LocalEnv>
bp<Environment>
parserstatemachine::lookup_envframe(std::size_t i) const {
return env_stack_[i];
}
void
parserstatemachine::push_envframe(const rp<LocalEnv> & x) {
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("frame", x));
env_stack_.push_envframe(x);
}
rp<LocalEnv>
rp<Environment>
parserstatemachine::pop_envframe() {
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
return env_stack_.pop_envframe();
}
@ -76,8 +74,7 @@ namespace xo {
void
parserstatemachine::on_expr(bp<Expression> x)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("x", x),
xtag("psm", this));
@ -88,8 +85,7 @@ namespace xo {
void
parserstatemachine::on_expr_with_semicolon(bp<Expression> x)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("x", x),
xtag("psm", this));
@ -102,8 +98,7 @@ namespace xo {
void
parserstatemachine::on_symbol(const std::string & x)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("x", x),
xtag("psm", this));
@ -114,8 +109,7 @@ namespace xo {
void
parserstatemachine::on_semicolon_token(const token_type & tk)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("tk", tk),
xtag("psm", this));
@ -126,8 +120,7 @@ namespace xo {
void
parserstatemachine::on_operator_token(const token_type & tk)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("tk", tk),
xtag("psm", this));
@ -138,8 +131,7 @@ namespace xo {
void
parserstatemachine::on_leftbrace_token(const token_type & tk)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(debug_flag_));
log && log(xtag("tk", tk),
xtag("psm", this));

View file

@ -63,6 +63,10 @@ namespace xo {
log && log(xtag("outcome", "victory!"),
xtag("expr", parser_result.result_expr()));
rp<Expression> result_expr = parser_result.result_expr();
this->parser_.reset_result();
/* token completes an expression -> victory */
return reader_result(parser_result.result_expr(),
expr_span, parser_.stack_size(), reader_error());