xo-interpreter2 stack: refactor + bugfix operator expr

This commit is contained in:
Roland Conybeare 2026-03-12 20:26:08 -05:00
commit 93b613e8fe
14 changed files with 15 additions and 515 deletions

View file

@ -36,10 +36,6 @@ set(SELF_SRCS
IGCObject_DClosure.cpp
IPrintable_DClosure.cpp
DGlobalEnv.cpp
IGCObject_DGlobalEnv.cpp
IPrintable_DGlobalEnv.cpp
DLocalEnv.cpp
IGCObject_DLocalEnv.cpp
IPrintable_DLocalEnv.cpp

View file

@ -1,145 +0,0 @@
/** @file DGlobalEnv.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "GlobalEnv.hpp"
#include <xo/expression2/GlobalSymtab.hpp>
#include <xo/object2/Array.hpp>
namespace xo {
using xo::mm::AAllocator;
using xo::mm::AGCObject;
namespace scm {
DGlobalEnv::DGlobalEnv(DGlobalSymtab * symtab, DArray * values)
: symtab_{symtab}, values_{values}
{}
DGlobalEnv *
DGlobalEnv::_make(obj<AAllocator> mm,
DGlobalSymtab * symtab)
{
DArray * values = DArray::empty(mm, symtab->var_capacity());
void * mem = mm.alloc_for<DGlobalSymtab>();
return new (mem) DGlobalEnv(symtab, values);
}
obj<AGCObject>
DGlobalEnv::lookup_value(Binding ix) const noexcept
{
if (!ix.is_global()) {
assert(false);
return obj<AGCObject>();
}
if (ix.j_slot() >= static_cast<int32_t>(values_->size())) {
assert(false);
return obj<AGCObject>();
}
return (*values_)[ix.j_slot()];
}
void
DGlobalEnv::assign_value(obj<AAllocator> mm, Binding ix, obj<AGCObject> x)
{
scope log(XO_DEBUG(true),
xtag("ix.j_slot", ix.j_slot()),
xtag("values.cap", values_->capacity()));
assert(ix.is_global());
if (ix.j_slot() >= static_cast<int32_t>(values_->size())) {
// Control will come here in interpreter as new definitions are introduced.
// After seeing
// def foo = 1.2345;
// introducing new symbol foo:
// GlobalSymtab extends to include foo without this GlobalEnv
// knowing about it.
if (ix.j_slot() + 1 > static_cast<int32_t>(values_->capacity())) {
// realloc global array for more size
size_t cap_2x = 2 * values_->capacity();
while (cap_2x < static_cast<size_t>(ix.j_slot() + 1))
cap_2x = 2 * cap_2x;
DArray * values_2x = DArray::copy(mm, values_, cap_2x);
assert(values_2x);
if (values_2x) {
log && log("STUB: need write barrier for GC (also in GlobalSymtab!)");
this->values_ = values_2x;
} else {
return;
}
}
/** expand size sot that j_slot is valid **/
values_->resize(ix.j_slot() + 1);
}
log && log("STUB: need write barrier for GC here");
(*values_)[ix.j_slot()] = x;
}
DVariable *
DGlobalEnv::_upsert_value(obj<AAllocator> mm,
const DUniqueString * sym,
TypeDescr td,
obj<AGCObject> value)
{
DVariable * var
= DVariable::make(mm, sym, TypeRef::resolved(td));
assert(var);
symtab_->upsert_variable(mm, var);
this->assign_value(mm, var->path(), value);
return var;
}
// ----- AGCObject facet -----
std::size_t
DGlobalEnv::shallow_size() const noexcept
{
return sizeof(*this);
}
DGlobalEnv *
DGlobalEnv::shallow_copy(obj<AAllocator> mm) const noexcept
{
return mm.std_copy_for<DGlobalEnv>(this);
}
std::size_t
DGlobalEnv::forward_children(obj<ACollector> gc) noexcept
{
gc.forward_inplace(&symtab_);
gc.forward_inplace(&values_);
return this->shallow_size();
}
// ----- APrintable facet -----
bool
DGlobalEnv::pretty(const ppindentinfo & ppii) const
{
return ppii.pps()->pretty_struct
(ppii,
"DGlobalEnv",
refrtag("n_vars", symtab_->n_vars()));
}
} /*namespace scm*/
} /*namespace xo*/
/* end DGlobalEnv.cpp */

View file

@ -1,39 +0,0 @@
/** @file IGCObject_DGlobalEnv.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DGlobalEnv.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DGlobalEnv.json5]
**/
#include "env/IGCObject_DGlobalEnv.hpp"
namespace xo {
namespace scm {
auto
IGCObject_DGlobalEnv::shallow_size(const DGlobalEnv & self) noexcept -> size_type
{
return self.shallow_size();
}
auto
IGCObject_DGlobalEnv::shallow_copy(const DGlobalEnv & self, obj<AAllocator> mm) noexcept -> Opaque
{
return self.shallow_copy(mm);
}
auto
IGCObject_DGlobalEnv::forward_children(DGlobalEnv & self, obj<ACollector> gc) noexcept -> size_type
{
return self.forward_children(gc);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IGCObject_DGlobalEnv.cpp */

View file

@ -1,28 +0,0 @@
/** @file IPrintable_DGlobalEnv.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DGlobalEnv.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DGlobalEnv.json5]
**/
#include "env/IPrintable_DGlobalEnv.hpp"
namespace xo {
namespace scm {
auto
IPrintable_DGlobalEnv::pretty(const DGlobalEnv & self, const ppindentinfo & ppii) -> bool
{
return self.pretty(ppii);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IPrintable_DGlobalEnv.cpp */

View file

@ -27,6 +27,8 @@ namespace xo {
InitEvidence
InitSubsys<S_interpreter2_tag>::require()
{
scope log(XO_DEBUG(true));
InitEvidence retval;
/* direct subsystem deps for xo-interpreter2/ */

View file

@ -63,12 +63,7 @@ namespace xo {
FacetRegistry::register_impl<AGCObject, DVsmSeqContFrame>();
FacetRegistry::register_impl<APrintable, DVsmSeqContFrame>();
// GlobalEnv
FacetRegistry::register_impl<AGCObject, DGlobalEnv>();
FacetRegistry::register_impl<APrintable, DGlobalEnv>();
// LocalEnv
// LocalEnv (see xo-reader2/ for GlobalEnv)
FacetRegistry::register_impl<AGCObject, DLocalEnv>();
FacetRegistry::register_impl<APrintable, DLocalEnv>();

View file

@ -110,13 +110,15 @@ namespace xo {
//using X1CollectorConfig = xo::mm::X1CollectorConfig;
//using DArena = xo::mm::DArena;
//using ArenaConfig = xo::mm::ArenaConfig;
using VsmConfig = xo::scm::VsmConfig;
using Replxx = replxx::Replxx;
using span_type = VirtualSchematikaMachine::span_type;
App(const AppConfig & cfg = AppConfig())
: repl_config_{cfg.repl_config_},
app_arena_{cfg.app_arena_config_},
vsm_{cfg.vsm_config_, obj<AAllocator,DArena>(&app_arena_)}
vsm_config_{cfg.vsm_config_}
//vsm_{cfg.vsm_config_, obj<AAllocator,DArena>(&app_arena_)}
{
this->interactive_ = isatty(STDIN_FILENO);
@ -148,7 +150,8 @@ namespace xo {
/** arena with same lifetime as this application **/
DArena app_arena_;
/** schematika virtual machine **/
VirtualSchematikaMachine vsm_;
VsmConfig vsm_config_;
std::unique_ptr<VirtualSchematikaMachine> vsm_;
};
void
@ -162,13 +165,16 @@ namespace xo {
void
App::_init()
{
// window to contorl size of registries ends as soon as we init other subsystems
// window to control size of registries ends as soon as we init other subsystems
TypeRegistry::instance(1024);
FacetRegistry::instance(1024);
InitEvidence init_evidence_ = (InitSubsys<S_interpreter2_tag>::require());
Subsystem::initialize_all();
vsm_.reset(new VirtualSchematikaMachine(vsm_config_,
obj<AAllocator,DArena>(&app_arena_)));
}
void
@ -176,7 +182,7 @@ namespace xo {
{
welcome(cerr);
vsm_.begin_interactive_session();
vsm_->begin_interactive_session();
}
void
@ -186,7 +192,7 @@ namespace xo {
span_type input;
// outer loop: fetch one line of interactive input
while (replxx_getline(interactive_, vsm_.is_at_toplevel(), rx_, &input)) {
while (replxx_getline(interactive_, vsm_->is_at_toplevel(), rx_, &input)) {
// inner loop: consume up to one expression at a time.
while (!input.empty() && this->_read_eval_print(&input, false /*eof*/))
@ -218,7 +224,7 @@ namespace xo {
if (!p_input || p_input->empty())
return true;
VsmResultExt res = vsm_.read_eval_print(*p_input, eof);
VsmResultExt res = vsm_->read_eval_print(*p_input, eof);
*p_input = res.remaining_;