From 6bf4378132793740560a404f07b302622491bd7e Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 17 Feb 2026 15:01:46 -0500 Subject: [PATCH] xo-interpreter2: global variable refs working in utest --- .../interpreter2/VirtualSchematikaMachine.cpp | 21 ++++++++++++++----- .../utest/VirtualSchematikaMachine.test.cpp | 8 +++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp index aa0861f5..e7964ab4 100644 --- a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp +++ b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp @@ -113,6 +113,8 @@ namespace xo { return VsmResultExt(); } + reader_.reset_result(); + auto [expr, remaining, error1] = reader_.read_expr(input, eof); @@ -392,12 +394,21 @@ namespace xo { Binding b = var->path(); - if (!local_env_) { - // need lookup on global_env_ - assert(false); + if (local_env_) { + auto value = local_env_->lookup_value(b); + + if (value) { + this->value_ = VsmResult(value); + + this->pc_ = this->cont_; + this->cont_ = VsmInstr::c_sentinel; + return; + } } - auto value = local_env_->lookup_value(b); + // no local binding. perhaps there's a global binding + + auto value = global_env_->lookup_value(b); if (value) { this->value_ = VsmResult(value); @@ -407,7 +418,7 @@ namespace xo { return; } - // no binding + // no local or global binding auto error = DRuntimeError::make(mm_.to_op(), "_do_eval_varref_op", diff --git a/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp b/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp index 1af15c86..3d1a76a8 100644 --- a/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp +++ b/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp @@ -391,9 +391,11 @@ namespace xo { vsm.begin_interactive_session(); - span_type input = span_type::from_cstr("def foo = 3.14159; foo"); + span_type input = span_type::from_cstr("def foo = 3.14159; foo;"); for (int i_expr = 0; i_expr < 2; ++i_expr) { + log && log(xtag("input", input)); + VsmResultExt res = vsm.read_eval_print(input, eof_flag); @@ -414,14 +416,12 @@ namespace xo { } else if (i_expr == 1) { auto x = obj::from(*res.value()); REQUIRE(x); - REQUIRE(x->value() == 3.14159); + REQUIRE_THAT(x->value(), WithinAbs(3.14159, 1e-6)); REQUIRE(res.remaining_.size() == 1); REQUIRE(*res.remaining_.lo() == '\n'); input = res.remaining_; } - - ++i_expr; } log && vsm_fixture.log_memory_layout(&log);