xo-interpreter2 stack: work on variable references [WIP]

This commit is contained in:
Roland Conybeare 2026-02-05 10:44:11 -05:00
commit 4cd4328f07
6 changed files with 274 additions and 21 deletions

View file

@ -3,23 +3,18 @@
* @author Roland Conybeare, Jan 2026
**/
#include "DExpectExprSsm.hpp"
#include "ExpectExprSsm.hpp"
#include "ParserStateMachine.hpp"
#include "SyntaxStateMachine.hpp"
#include "ssm/ISyntaxStateMachine_DExpectExprSsm.hpp"
#include "ssm/ISyntaxStateMachine_DProgressSsm.hpp"
#include "DSequenceSsm.hpp"
#include "syntaxstatetype.hpp"
#include <xo/expression2/DConstant.hpp>
#include <xo/expression2/detail/IExpression_DConstant.hpp>
#include <xo/object2/DBoolean.hpp>
#include <xo/object2/boolean/IGCObject_DBoolean.hpp>
#include <xo/object2/DInteger.hpp>
#include <xo/object2/number/IGCObject_DInteger.hpp>
#include <xo/object2/DFloat.hpp>
#include <xo/object2/number/IGCObject_DFloat.hpp>
#include <xo/object2/DString.hpp>
#include <xo/object2/string/IGCObject_DString.hpp>
#include <xo/expression2/Variable.hpp>
#include <xo/expression2/Constant.hpp>
#include <xo/object2/Boolean.hpp>
#include <xo/object2/Integer.hpp>
#include <xo/object2/Float.hpp>
#include <xo/object2/String.hpp>
#include <xo/gc/GCObject.hpp>
#include <xo/facet/facet_implementation.hpp>
@ -196,7 +191,27 @@ namespace xo {
DExpectExprSsm::on_symbol_token(const Token & tk,
ParserStateMachine * p_psm)
{
Super::on_token(tk, p_psm);
scope log(XO_DEBUG(p_psm->debug_flag()));
log && log(xtag("tk", tk));
const DVariable * var = p_psm->lookup_variable(tk.text());
if (!var) {
p_psm->error_unbound_variable(ssm_classname(),
tk.text());
}
// examples of possible continuations from symbol foo
// foo ; // (1) foo is entire rvalue expression
// foo + ... // (2) foo begin operator expression
// foo(..); // (3) foo begin apply function
//
//
DProgressSsm::start(p_psm->parser_alloc(),
obj<AExpression,DVariable>(const_cast<DVariable *>(var)),
p_psm);
}
#ifdef NOT_YET

View file

@ -68,8 +68,7 @@ namespace xo {
obj<AAllocator> expr_mm,
ParserStateMachine * p_psm)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
scope log(XO_DEBUG(p_psm->debug_flag()));
DIfElseExpr * if_expr = DIfElseExpr::_make_empty(expr_mm);
DIfElseSsm * if_ssm = DIfElseSsm::_make(parser_mm, if_expr);

View file

@ -107,6 +107,48 @@ namespace xo {
return stringtable_.gensym(str);
}
Binding
ParserStateMachine::lookup_binding(std::string_view symbolname)
{
scope log(XO_DEBUG(debug_flag_));
if (!local_symtab_)
return Binding::null();
const DUniqueString * ustr = stringtable_.lookup(symbolname);
if (!ustr) {
// if not in string table, then can't be a variable either
return Binding::null();
}
DLocalSymtab * symtab = local_symtab_;
// count #of nested scopes to cross, to reach symbol
//
int32_t link_count = 0;
while (symtab) {
Binding b = symtab->lookup_binding(ustr);
if (b.is_local()) {
assert(b.i_link() == 0);
return Binding(link_count, b.j_slot());
}
++link_count;
symtab = symtab->parent();
}
// TODO: check global symtab also
log.retroactively_enable();
log("STUB: check global symtab");
return Binding::null();
}
void
ParserStateMachine::push_local_symtab(DLocalSymtab * symtab)
{
@ -400,6 +442,21 @@ namespace xo {
this->capture_error(ssm_name, errmsg);
}
void
ParserStateMachine::error_unbound_variable(std::string_view ssm_name,
std::string_view sym)
{
auto errmsg_string = tostr("No binding for symbol",
xtag("symbol", sym),
xtag("ssm", ssm_name));
auto errmsg = DString::from_view(expr_alloc_,
std::string_view(errmsg_string));
this->capture_error(ssm_name, errmsg);
}
} /*namespace scm*/
} /*namespace xo*/