From 7028fa49f8ef463409127682c9931bf6ef9185d8 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 2 Feb 2026 10:53:28 -0500 Subject: [PATCH] xo-interpreter2: refactor to setup vsm utest + repl --- .../include/xo/alloc2/alloc/RAllocator.hpp | 3 ++ xo-expression2/utest/DApplyExpr.test.cpp | 14 ++--- xo-expression2/utest/DConstant.test.cpp | 10 ++-- xo-expression2/utest/DDefineExpr.test.cpp | 16 +++--- xo-expression2/utest/DIfElseExpr.test.cpp | 20 +++---- xo-expression2/utest/DVariable.test.cpp | 12 ++--- xo-facet/include/xo/facet/box.hpp | 12 +++-- .../interpreter2/VirtualSchematikaMachine.hpp | 24 +++++++++ .../include/xo/interpreter2/VsmConfig.hpp | 4 +- .../interpreter2/VirtualSchematikaMachine.cpp | 54 +++++++++++++++++++ .../utest/VirtualSchematikaMachine.test.cpp | 5 ++ .../include/xo/reader2/ReaderConfig.hpp | 2 +- 12 files changed, 133 insertions(+), 43 deletions(-) diff --git a/xo-alloc2/include/xo/alloc2/alloc/RAllocator.hpp b/xo-alloc2/include/xo/alloc2/alloc/RAllocator.hpp index c07b5e81..bbd35d5c 100644 --- a/xo-alloc2/include/xo/alloc2/alloc/RAllocator.hpp +++ b/xo-alloc2/include/xo/alloc2/alloc/RAllocator.hpp @@ -27,6 +27,9 @@ namespace xo { RAllocator() {} RAllocator(Object::DataPtr data) : Object{std::move(data)} {} + RAllocator(const AAllocator * iface, void * data) + requires std::is_same_v + : Object(iface, data) {} typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); } void _drop() const noexcept { O::iface()->_drop(O::data()); } diff --git a/xo-expression2/utest/DApplyExpr.test.cpp b/xo-expression2/utest/DApplyExpr.test.cpp index d151b776..f217d47e 100644 --- a/xo-expression2/utest/DApplyExpr.test.cpp +++ b/xo-expression2/utest/DApplyExpr.test.cpp @@ -47,7 +47,7 @@ namespace ut { using xo::mm::ACollector; using xo::mm::AGCObject; using xo::mm::DX1Collector; - using xo::mm::CollectorConfig; + using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; @@ -71,7 +71,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_make2_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -111,7 +111,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_extype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -146,7 +146,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_n_args_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -181,7 +181,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_fn_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -219,7 +219,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_arg_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -263,7 +263,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dapplyexpr_pretty_test", .arena_config_ = ArenaConfig{ .size_ = 8192, diff --git a/xo-expression2/utest/DConstant.test.cpp b/xo-expression2/utest/DConstant.test.cpp index ddff5b20..fd8e9b1b 100644 --- a/xo-expression2/utest/DConstant.test.cpp +++ b/xo-expression2/utest/DConstant.test.cpp @@ -39,7 +39,7 @@ namespace ut { using xo::mm::ACollector; using xo::mm::AGCObject; using xo::mm::DX1Collector; - using xo::mm::CollectorConfig; + using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; @@ -66,7 +66,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dconstant_float_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -105,7 +105,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dconstant_int_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -146,7 +146,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dconstant_pp_float_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -190,7 +190,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dconstant_pp_int_test", .arena_config_ = ArenaConfig{ .size_ = 8192, diff --git a/xo-expression2/utest/DDefineExpr.test.cpp b/xo-expression2/utest/DDefineExpr.test.cpp index 5bb91582..5f78891d 100644 --- a/xo-expression2/utest/DDefineExpr.test.cpp +++ b/xo-expression2/utest/DDefineExpr.test.cpp @@ -44,7 +44,7 @@ namespace ut { using xo::mm::ACollector; using xo::mm::AGCObject; using xo::mm::DX1Collector; - using xo::mm::CollectorConfig; + using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; @@ -68,7 +68,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_make_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -103,7 +103,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_lhs_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -138,7 +138,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_rhs_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -174,7 +174,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_name_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -207,7 +207,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_extype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -239,7 +239,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_valuetype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -273,7 +273,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "ddefineexpr_pretty_test", .arena_config_ = ArenaConfig{ .size_ = 8192, diff --git a/xo-expression2/utest/DIfElseExpr.test.cpp b/xo-expression2/utest/DIfElseExpr.test.cpp index a82521a0..2dee2f26 100644 --- a/xo-expression2/utest/DIfElseExpr.test.cpp +++ b/xo-expression2/utest/DIfElseExpr.test.cpp @@ -42,15 +42,15 @@ namespace ut { using xo::mm::ACollector; using xo::mm::AGCObject; using xo::mm::DX1Collector; - using xo::mm::CollectorConfig; + using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; using xo::print::ppconfig; - using xo::facet::FacetRegistry; + //using xo::facet::FacetRegistry; using xo::facet::with_facet; using xo::facet::obj; - using xo::reflect::Reflect; + //using xo::reflect::Reflect; using xo::InitEvidence; using xo::InitSubsys; using xo::scope; @@ -66,7 +66,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_make_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -107,7 +107,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_test_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -145,7 +145,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_when_true_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -183,7 +183,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_when_false_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -221,7 +221,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_extype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -256,7 +256,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_valuetype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -296,7 +296,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "difelseexpr_pretty_test", .arena_config_ = ArenaConfig{ .size_ = 8192, diff --git a/xo-expression2/utest/DVariable.test.cpp b/xo-expression2/utest/DVariable.test.cpp index 8afee360..9c26a7c4 100644 --- a/xo-expression2/utest/DVariable.test.cpp +++ b/xo-expression2/utest/DVariable.test.cpp @@ -37,7 +37,7 @@ namespace ut { using xo::mm::AAllocator; using xo::mm::ACollector; using xo::mm::DX1Collector; - using xo::mm::CollectorConfig; + using xo::mm::X1CollectorConfig; using xo::mm::ArenaConfig; using xo::print::APrintable; using xo::print::ppstate_standalone; @@ -61,7 +61,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dvariable_make_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -92,7 +92,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dvariable_extype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -122,7 +122,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dvariable_valuetype_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -152,7 +152,7 @@ namespace ut { { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dvariable_name_test", .arena_config_ = ArenaConfig{ .size_ = 8192, @@ -185,7 +185,7 @@ namespace ut { REQUIRE(s_init.evidence()); - CollectorConfig cfg{ + X1CollectorConfig cfg{ .name_ = "dvariable_pretty_test", .arena_config_ = ArenaConfig{ .size_ = 8192, diff --git a/xo-facet/include/xo/facet/box.hpp b/xo-facet/include/xo/facet/box.hpp index 25a6561e..54e724ed 100644 --- a/xo-facet/include/xo/facet/box.hpp +++ b/xo-facet/include/xo/facet/box.hpp @@ -46,7 +46,7 @@ namespace xo { box(box && other) requires (std::is_same_v || std::is_same_v) - : obj() + : RoutingType>() { /* replacing .iface_ along w/ .data_ */ this->from_obj(other); @@ -55,8 +55,10 @@ namespace xo { } /** explicit conversion to obj **/ - obj to_op() const noexcept { return obj(this->iface(), this->data()); } - + obj to_op() const noexcept { + return obj(this->iface(), this->data()); + } + /** Take ownership from unowned object **/ template box & adopt(const obj & other) @@ -65,11 +67,11 @@ namespace xo { { /* replace .iface_ along w/ .data_ */ this->from_obj(other); - + return *this; } - ~box() { + ~box() { auto p = this->data(); this->_drop(); ::operator delete(p); diff --git a/xo-interpreter2/include/xo/interpreter2/VirtualSchematikaMachine.hpp b/xo-interpreter2/include/xo/interpreter2/VirtualSchematikaMachine.hpp index 6e1595da..c9dc0749 100644 --- a/xo-interpreter2/include/xo/interpreter2/VirtualSchematikaMachine.hpp +++ b/xo-interpreter2/include/xo/interpreter2/VirtualSchematikaMachine.hpp @@ -14,6 +14,23 @@ namespace xo { namespace scm { + /** similar to @ref xo::scm::ReaderResult **/ + struct VsmResult { + using AGCObject = xo::mm::AGCObject; + using span_type = xo::mm::span; + + bool is_tk_error() const { return tk_error_.is_error(); } + + /** result of evaluating first expression encountered in input **/ + obj value_; + + /** unconsumed portion of input span **/ + span_type remaining_input_; + + /** {src_function, error_description, input_state, error_pos} **/ + TokenizerError tk_error_; + }; + /** @class VirtualSchematikaMachine * @brief virtual machine for schematika **/ @@ -23,10 +40,17 @@ namespace xo { using Stack = void *; using AAllocator = xo::mm::AAllocator; using AGCObject = xo::mm::AGCObject; + using span_type = xo::mm::span; public: VirtualSchematikaMachine(const VsmConfig & config); + /** consume input @p input_cstr **/ + VsmResult read_eval_print(span_type input_span, bool eof); + + /** evaluate expression @p expr **/ + std::pair, TokenizerError> eval(obj expr); + /** borrow calling thread to run indefinitely, * until halt instruction **/ diff --git a/xo-interpreter2/include/xo/interpreter2/VsmConfig.hpp b/xo-interpreter2/include/xo/interpreter2/VsmConfig.hpp index 5071c020..2957b297 100644 --- a/xo-interpreter2/include/xo/interpreter2/VsmConfig.hpp +++ b/xo-interpreter2/include/xo/interpreter2/VsmConfig.hpp @@ -10,11 +10,13 @@ namespace xo { namespace scm { - /** Configuration for virtual schematika machine + /** Configuration for virtual schematika machine **/ struct VsmConfig { using X1CollectorConfig = xo::mm::X1CollectorConfig; + VsmConfig() = default; + /** reader configuration **/ ReaderConfig rdr_config_; /** Configuration for allocator/collector. diff --git a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp index f7e8259e..fabc5441 100644 --- a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp +++ b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp @@ -8,10 +8,18 @@ #include #include #include +#include +#include #include namespace xo { + using xo::print::APrintable; + using xo::print::ppconfig; + using xo::print::ppstate_standalone; + using xo::mm::AGCObject; using xo::mm::DX1Collector; + using xo::facet::FacetRegistry; + using std::cout; namespace scm { @@ -21,6 +29,52 @@ namespace xo { reader_{config.rdr_config_, mm_.to_op()} {} + VsmResult + VirtualSchematikaMachine::read_eval_print(span_type input, bool eof) + { + if (input.empty()) { + return VsmResult(); + } + + auto [expr, remaining, error1] + = reader_.read_expr(input, eof); + + if (!expr) { + return { + .remaining_input_ = remaining, + .tk_error_ = error1 + }; + } + + auto [value, error2] = this->eval(expr); + + if (!value) { + return { + .remaining_input_ = remaining, + .tk_error_ = error2 + }; + } + + obj value_pr + = FacetRegistry::instance().variant(value); + + // pretty_toplevel(value_pr, &cout, ppconfig()); + ppconfig ppc; + ppstate_standalone pps(&cout, 0, &ppc); + pps.prettyn(value_pr); + + return { .remaining_input_ = remaining }; + } + + std::pair, TokenizerError> + VirtualSchematikaMachine::eval(obj expr) + { + (void)expr; + + assert(false); + return std::make_pair(obj(), TokenizerError()); + } + void VirtualSchematikaMachine::run() { diff --git a/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp b/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp index 29301b1f..8dacb437 100644 --- a/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp +++ b/xo-interpreter2/utest/VirtualSchematikaMachine.test.cpp @@ -20,6 +20,9 @@ #include namespace xo { + using xo::scm::VirtualSchematikaMachine; + using xo::scm::VsmConfig; + #ifdef NOT_YET using xo::scm::SchematikaParser; using xo::scm::ASyntaxStateMachine; @@ -42,6 +45,8 @@ namespace xo { namespace ut { TEST_CASE("VirtualSchematikaMachine-ctor", "[interpreter2][VSM]") { + VirtualSchematikaMachine vsm(VsmConfig); + #ifdef NOT_YET ArenaConfig config; config.name_ = "test-arena"; diff --git a/xo-reader2/include/xo/reader2/ReaderConfig.hpp b/xo-reader2/include/xo/reader2/ReaderConfig.hpp index 826bb215..5b02e62c 100644 --- a/xo-reader2/include/xo/reader2/ReaderConfig.hpp +++ b/xo-reader2/include/xo/reader2/ReaderConfig.hpp @@ -41,7 +41,7 @@ namespace xo { size_t max_stringtable_cap_ = 64*1024; /** debug flag for schematika_reader **/ - bool reader_debug_flag_ = false;; + bool reader_debug_flag_ = false; }; } /*namespace scm*/