xo-reader2: utest with variable reference. Works !

This commit is contained in:
Roland Conybeare 2026-02-16 23:25:34 -05:00
commit 10db8493f7
3 changed files with 117 additions and 44 deletions

View file

@ -11,6 +11,7 @@
#include "DIfElseSsm.hpp"
#include "ParenSsm.hpp"
#include "ExpectExprSsm.hpp"
#include "VarRef.hpp"
#include <xo/expression2/DConstant.hpp>
#include <xo/expression2/detail/IExpression_DConstant.hpp>
@ -204,23 +205,23 @@ namespace xo {
void
DToplevelSeqSsm::on_symbol_token(const Token & tk,
ParserStateMachine * p_psm)
ParserStateMachine * p_psm)
{
switch (seqtype_) {
case exprseqtype::toplevel_interactive:
{
#ifdef NOT_YET
obj<AExpression,DVariable> var = p_psm->lookup_var(tk.text());
auto varref = obj<AExpression,DVarRef>(p_psm->lookup_varref(tk.text()));
if (var) {
DProgressSsm::start(var, p_psm);
if (varref) {
DProgressSsm::start(p_psm->parser_alloc(),
varref,
p_psm);
return;
} else {
p_psm->unknown_variable_error("DToplevelSeqSsm::on_symbol_token",
tk,
this->get_expect_str(),
p_psm);
p_psm->error_unbound_variable("DToplevelSeqSsm",
tk.text());
return;
}
#endif
}
break;
case exprseqtype::toplevel_batch:

View file

@ -156,6 +156,16 @@ namespace xo {
{
scope log(XO_DEBUG(debug_flag_));
const DUniqueString * ustr = stringtable_.lookup(symbolname);
if (!ustr) {
// if we don't already know the symbol,
// -> can't be a valid variable reference
// (whether global or local)
return nullptr;
}
// TODO:
// 1. check global symtab
// 2. combine local+global symtab into indept struct
@ -163,52 +173,46 @@ namespace xo {
//
if (local_symtab_) {
const DUniqueString * ustr = stringtable_.lookup(symbolname);
DLocalSymtab * symtab = local_symtab_;
if (ustr) {
DLocalSymtab * symtab = local_symtab_;
// count #of nested scopes to cross, to reach symbol
//
int32_t link_count = 0;
// count #of nested scopes to cross, to reach symbol
//
int32_t link_count = 0;
while (symtab) {
Binding b = symtab->lookup_binding(ustr);
while (symtab) {
Binding b = symtab->lookup_binding(ustr);
if (b.is_local()) {
assert(b.i_link() == 0);
if (b.is_local()) {
assert(b.i_link() == 0);
DVariable * vardef = symtab->lookup_var(b);
assert(vardef);
DVariable * vardef = symtab->lookup_var(b);
assert(vardef);
/** ascii diagram here
**/
/** ascii diagram here
**/
return DVarRef::make(expr_alloc_,
vardef,
link_count);
} else {
assert(b.is_null());
}
++link_count;
symtab = symtab->parent();
return DVarRef::make(expr_alloc_,
vardef,
link_count);
} else {
assert(b.is_null());
}
} else {
// if we don't already know the symbol,
// -> can't be a valid variable reference
// (whether global or local)
return nullptr;
++link_count;
symtab = symtab->parent();
}
}
// TODO: check global symtab also
DVariable * vardef = global_symtab_->lookup_variable(ustr);
log.retroactively_enable();
log("STUB: check global symtab");
if (vardef) {
return DVarRef::make(expr_alloc_,
vardef,
0 /*link_count -- n/a for globals*/);
}
// symbol not found
return nullptr;
}