xo-interpreter2 stack: lambda expr -> closure runs in VSM utest

This commit is contained in:
Roland Conybeare 2026-02-08 23:32:20 -05:00
commit 0170b8dacf
22 changed files with 358 additions and 73 deletions

View file

@ -20,6 +20,7 @@ namespace xo {
**/
class DLambdaExpr {
public:
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
using TypeDescr = xo::reflect::TypeDescr;
using ppindentinfo = xo::print::ppindentinfo;
@ -78,6 +79,14 @@ namespace xo {
TypeDescr valuetype() const noexcept;
void assign_valuetype(TypeDescr td) noexcept;
///@}
/** @defgroup scm-lambdaexpr-gcobject-facet **/
///@{
std::size_t shallow_size() const noexcept;
DLambdaExpr * shallow_copy(obj<AAllocator> mm) const noexcept;
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
/** @defgroup scm-lambdaexpr-printable-facet **/
///@{
@ -95,7 +104,7 @@ namespace xo {
/** name for this lambda (generated if necessary) **/
const DUniqueString * name_ = nullptr;
#ifdef NOT_YET
#ifdef NOT_YET // when enabled, need to visit forward_children()
/** e.g.
* i64(f64,string)
* for function of two arguments with types (f64, string) respectively,

View file

@ -0,0 +1,13 @@
/** @file IfElseExpr.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "DIfElseExpr.hpp"
#include "detail/IExpression_DIfElseExpr.hpp"
#include "detail/IGCObject_DIfElseExpr.hpp"
#include "detail/IPrintable_DIfElseExpr.hpp"
/* end IfElseExpr.hpp */

View file

@ -0,0 +1,12 @@
/** @file UniqueString.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "DUniqueString.hpp"
#include "detail/IGCObject_DUniqueString.hpp"
#include "detail/IPrintable_DUniqueString.hpp"
/* end UniqueString.hpp */

View file

@ -10,15 +10,13 @@ set(SELF_SRCS
DVariable.cpp
DVarRef.cpp
DDefineExpr.cpp
DLambdaExpr.cpp
DApplyExpr.cpp
DIfElseExpr.cpp
DSequenceExpr.cpp
TypeRef.cpp
Binding.cpp
IExpression_Any.cpp
ISymbolTable_Any.cpp
IExpression_DConstant.cpp
IGCObject_DConstant.cpp
@ -39,25 +37,28 @@ set(SELF_SRCS
IGCObject_DApplyExpr.cpp
IPrintable_DApplyExpr.cpp
DLambdaExpr.cpp
IExpression_DLambdaExpr.cpp
IGCObject_DLambdaExpr.cpp
IPrintable_DLambdaExpr.cpp
DIfElseExpr.cpp
IExpression_DIfElseExpr.cpp
IGCObject_DIfElseExpr.cpp
IPrintable_DIfElseExpr.cpp
DSequenceExpr.cpp
IExpression_DSequenceExpr.cpp
IGCObject_DSequenceExpr.cpp
IPrintable_DSequenceExpr.cpp
DLocalSymtab.cpp
DGlobalSymtab.cpp
ISymbolTable_Any.cpp
ISymbolTable_DLocalSymtab.cpp
IGCObject_DLocalSymtab.cpp
IPrintable_DLocalSymtab.cpp
DGlobalSymtab.cpp
StringTable.cpp
DUniqueString.cpp

View file

@ -80,32 +80,6 @@ namespace xo {
typeref_.resolve(td);
}
bool
DIfElseExpr::pretty(const ppindentinfo & ppii) const
{
auto test
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(test_);
auto when_true
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_true_);
auto when_false
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_false_);
bool test_present = test;
bool when_true_present = when_true;
bool when_false_present = when_false;
return ppii.pps()->pretty_struct
(ppii,
"DIfElseExpr",
refrtag("typeref", typeref_),
refrtag("test", test, test_present),
refrtag("when_true", when_true, when_true_present),
refrtag("when_false", when_false, when_false_present));
}
// GCObject facet
std::size_t
@ -145,6 +119,34 @@ namespace xo {
return shallow_size();
}
// ----- printable facet -----
bool
DIfElseExpr::pretty(const ppindentinfo & ppii) const
{
auto test
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(test_);
auto when_true
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_true_);
auto when_false
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(when_false_);
bool test_present = test;
bool when_true_present = when_true;
bool when_false_present = when_false;
return ppii.pps()->pretty_struct
(ppii,
"DIfElseExpr",
refrtag("typeref", typeref_),
refrtag("test", test, test_present),
refrtag("when_true", when_true, when_true_present),
refrtag("when_false", when_false, when_false_present));
}
// ----------------------------------------------------------------
#ifdef NOPE

View file

@ -3,16 +3,16 @@
* @author Roland Conybeare, Jan 2026
**/
#include "DLambdaExpr.hpp"
#include "detail/IExpression_DLambdaExpr.hpp"
#include "DLocalSymtab.hpp"
#include "symtab/IPrintable_DLocalSymtab.hpp"
#include "LambdaExpr.hpp"
#include "LocalSymtab.hpp"
#include "UniqueString.hpp"
#include <xo/alloc2/Allocator.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/reflectutil/typeseq.hpp>
namespace xo {
using xo::mm::AGCObject;
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::reflect::TypeDescr;
@ -134,6 +134,49 @@ namespace xo {
typeref_.resolve(td);
}
std::size_t
DLambdaExpr::shallow_size() const noexcept {
return sizeof(DLambdaExpr);
}
DLambdaExpr *
DLambdaExpr::shallow_copy(obj<AAllocator> mm) const noexcept {
DLambdaExpr * copy = (DLambdaExpr *)mm.alloc_copy((std::byte *)this);
if (copy) {
*copy = *this;
}
return copy;
}
std::size_t
DLambdaExpr::forward_children(obj<ACollector> gc) noexcept {
{
auto iface = xo::facet::impl_for<AGCObject,DUniqueString>();
gc.forward_inplace(&iface, (void **)(&name_));
}
// type_name_str_
{
auto iface = xo::facet::impl_for<AGCObject,DLocalSymtab>();
gc.forward_inplace(&iface, (void **)(&local_symtab_));
}
{
auto iface = body_expr_.to_facet<AGCObject>().iface();
gc.forward_inplace(iface, (void **)(&body_expr_));
}
// xxx free_var_set
// xxx captured_var_set
// xxx layer_var_map
// xxx nested_lambda_map
return shallow_size();
}
bool
DLambdaExpr::pretty(const ppindentinfo & ppii) const
{

View file

@ -3,13 +3,14 @@
* @author Roland Conybeare, Jan 2026
**/
#include "DLocalSymtab.hpp"
#include "LocalSymtab.hpp"
#include "Variable.hpp"
#include "DUniqueString.hpp"
#include <xo/expression2/detail/IPrintable_DVariable.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/indentlog/scope.hpp>
namespace xo {
using xo::mm::AGCObject;
using xo::print::APrintable;
using xo::facet::typeseq;
using xo::print::ppstate;
@ -77,6 +78,50 @@ namespace xo {
return Binding();
}
// ----- gcobject facet -----
std::size_t
DLocalSymtab::shallow_size() const noexcept
{
return (sizeof(DLocalSymtab) + (capacity_ * sizeof(Slot)));
}
DLocalSymtab *
DLocalSymtab::shallow_copy(obj<AAllocator> mm) const noexcept
{
DLocalSymtab * copy = (DLocalSymtab *)mm.alloc_copy((std::byte *)this);
if (copy) {
*copy = *this;
::memcpy((void*)&(copy->slots_[0]),
(void*)&(slots_[0]),
capacity_ * sizeof(Slot));
}
return copy;
}
std::size_t
DLocalSymtab::forward_children(obj<ACollector> gc) noexcept
{
{
auto iface
= xo::facet::impl_for<AGCObject,DLocalSymtab>();
gc.forward_inplace(&iface, (void **)(&parent_));
}
auto iface
= xo::facet::impl_for<AGCObject,DVariable>();
for (size_type i = 0; i < size_; ++i) {
gc.forward_inplace(&iface, (void **)(&(slots_[i].var_)));
}
return shallow_size();
}
// ----- printable facet -----
bool
DLocalSymtab::pretty(const ppindentinfo & ppii) const
{

View file

@ -17,28 +17,16 @@
#include <xo/expression2/detail/IPrintable_DVariable.hpp>
#include <xo/expression2/VarRef.hpp>
#include <xo/expression2/detail/IExpression_DConstant.hpp>
#include <xo/expression2/detail/IGCObject_DConstant.hpp>
#include <xo/expression2/detail/IPrintable_DConstant.hpp>
#include <xo/expression2/Constant.hpp>
#include <xo/expression2/ApplyExpr.hpp>
#include <xo/expression2/detail/IExpression_DLambdaExpr.hpp>
//#include <xo/expression2/detail/IGCObject_DLambdaExpr.hpp>
#include <xo/expression2/detail/IPrintable_DLambdaExpr.hpp>
#include <xo/expression2/detail/IExpression_DIfElseExpr.hpp>
#include <xo/expression2/detail/IGCObject_DIfElseExpr.hpp>
#include <xo/expression2/detail/IPrintable_DIfElseExpr.hpp>
#include <xo/expression2/LambdaExpr.hpp>
#include <xo/expression2/IfElseExpr.hpp>
#include <xo/expression2/detail/IExpression_DSequenceExpr.hpp>
#include <xo/expression2/detail/IGCObject_DSequenceExpr.hpp>
#include <xo/expression2/detail/IPrintable_DSequenceExpr.hpp>
#include <xo/expression2/symtab/ISymbolTable_DLocalSymtab.hpp>
//#include <xo/expression2/detail/IGCObject_DLocalSymtab.hpp>
#include <xo/expression2/symtab/IPrintable_DLocalSymtab.hpp>
#include <xo/expression2/LocalSymtab.hpp>
#include <xo/gc/detail/AGCObject.hpp>
#include <xo/printable2/detail/APrintable.hpp>
@ -70,6 +58,10 @@ namespace xo {
// +- IfElseExpr
// \- SequenceExpr
// SymbolTable
// +- LocalSymtab
// \- GlobalSymtab
FacetRegistry::register_impl<AExpression, DConstant>();
FacetRegistry::register_impl<AGCObject, DConstant>();
FacetRegistry::register_impl<APrintable, DConstant>();
@ -91,7 +83,7 @@ namespace xo {
FacetRegistry::register_impl<APrintable, DApplyExpr>();
FacetRegistry::register_impl<AExpression, DLambdaExpr>();
//FacetRegistry::register_impl<AGCObject, DLambdaExpr>();
FacetRegistry::register_impl<AGCObject, DLambdaExpr>();
FacetRegistry::register_impl<APrintable, DLambdaExpr>();
FacetRegistry::register_impl<AExpression, DIfElseExpr>();
@ -103,7 +95,7 @@ namespace xo {
FacetRegistry::register_impl<APrintable, DSequenceExpr>();
FacetRegistry::register_impl<ASymbolTable, DLocalSymtab>();
//FacetRegistry::register_impl<AGCObject, DLocalSymtab>();
FacetRegistry::register_impl<AGCObject, DLocalSymtab>();
FacetRegistry::register_impl<APrintable, DLocalSymtab>();
log && log(xtag("DUniqueString.tseq", typeseq::id<DUniqueString>()));

View file

@ -127,6 +127,18 @@ xo_add_genfacetimpl(
OUTPUT_CPP_DIR src/interpreter2
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-interpreter2-facetimpl-printable-localenv
FACET_PKG xo_printable2
FACET Printable
REPR LocalEnv
INPUT idl/IPrintable_DLocalEnv.json5
OUTPUT_HPP_DIR include/xo/interpreter2
OUTPUT_IMPL_SUBDIR detail
OUTPUT_CPP_DIR src/interpreter2
)
# ----------------------------------------------------------------
xo_add_genfacet_all(xo-interpreter2-genfacet-all)

View file

@ -0,0 +1,13 @@
{
mode: "implementation",
includes: [ "<xo/printable2/Printable.hpp>",
"<xo/printable2/detail/IPrintable_Xfer.hpp>" ],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Printable.json5",
brief: "provide APrintable interface for DLocalEnv",
using_doxygen: true,
repr: "DLocalEnv",
doc: [ "implement APrintable for DLocalEnv" ],
}

View file

@ -58,6 +58,12 @@ namespace xo {
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
/** @defgroup scm-localenv-printable-facet **/
///@{
bool pretty(const ppindentinfo & ppii) const noexcept;
///@}
private:
/** parent environment (from closure) **/

View file

@ -7,6 +7,6 @@
#include "DLocalEnv.hpp"
#include "detail/IGCObject_DLocalEnv.hpp"
//#include "detail/IPrintable_DLocalEnv.hpp"
#include "detail/IPrintable_DLocalEnv.hpp"
/* end LocalEnv.hpp */

View file

@ -216,6 +216,7 @@ namespace xo {
* in execution
**/
DLocalEnv * local_env_ = nullptr;
protected: // temporarily, to appease compiler
/** environment pointer. Maintains bindings
* for global variables.

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DLocalEnv.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DLocalEnv.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DLocalEnv.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DLocalEnv.hpp"
namespace xo { namespace scm { class IPrintable_DLocalEnv; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DLocalEnv>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DLocalEnv,
xo::scm::IPrintable_DLocalEnv>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DLocalEnv
**/
class IPrintable_DLocalEnv {
public:
/** @defgroup scm-printable-dlocalenv-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-dlocalenv-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DLocalEnv & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -17,10 +17,12 @@ set(SELF_SRCS
IPrintable_DVsmApplyFrame.cpp
DClosure.cpp
IGCObject_DClosure.cpp
IProcedure_DClosure.cpp
IGCObject_DClosure.cpp
IPrintable_DClosure.cpp
IGCObject_DLocalEnv.cpp
IPrintable_DLocalEnv.cpp
DLocalEnv.cpp
VsmInstr.cpp

View file

@ -10,6 +10,7 @@
namespace xo {
using xo::mm::AGCObject;
using xo::print::APrintable;
namespace scm {
@ -69,6 +70,24 @@ namespace xo {
return shallow_size();
}
// ----- printable facet -----
bool
DClosure::pretty(const ppindentinfo & ppii) const
{
obj<APrintable,DLambdaExpr> lambda_pr(const_cast<DLambdaExpr *>(lambda_));
obj<APrintable,DLocalEnv> env_pr(const_cast<DLocalEnv *>(env_));
bool lambda_present = lambda_pr;
bool env_present = env_pr;
return ppii.pps()->pretty_struct
(ppii,
"DClosure",
refrtag("lambda", lambda_pr, lambda_present),
refrtag("env", env_pr, env_present));
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -121,6 +121,22 @@ namespace xo {
return shallow_size();
}
// ----- printable facet -----
bool
DLocalEnv::pretty(const ppindentinfo & ppii) const noexcept
{
// print local bindings, perhaps
// symtab_
// args_
return ppii.pps()->pretty_struct
(ppii,
"DLocalEnv",
refrtag("n_args", args_->size())
);
}
} /*namespace scm*/
} /*namespace xo*/

View file

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

View file

@ -241,10 +241,9 @@ namespace xo {
lambda.data(),
local_env_);
this->value_ = obj<AGCObject>(obj<AGCObject,DClosure>(closure));
// not implemented
assert(false);
this->value_
= obj<AGCObject>(obj<AGCObject,DClosure>(closure));
this->pc_ = this->cont_;
}
void

View file

@ -8,6 +8,7 @@
#include "VsmApplyFrame.hpp"
#include "VsmEvalArgsFrame.hpp"
#include "Closure.hpp"
#include "LocalEnv.hpp"
#include <xo/printable2/detail/APrintable.hpp>
#include <xo/facet/FacetRegistry.hpp>
@ -37,9 +38,13 @@ namespace xo {
FacetRegistry::register_impl<AGCObject, DVsmEvalArgsFrame>();
FacetRegistry::register_impl<APrintable, DVsmEvalArgsFrame>();
FacetRegistry::register_impl<AProcedure, DClosure>();
FacetRegistry::register_impl<AGCObject, DClosure>();
FacetRegistry::register_impl<APrintable, DClosure>();
FacetRegistry::register_impl<AGCObject, DLocalEnv>();
FacetRegistry::register_impl<APrintable, DLocalEnv>();
// Procedure
// +- Primitive_gco_2_gco_gco
// \- Closure
@ -49,6 +54,7 @@ namespace xo {
log && log(xtag("DVsmApplyFrame.tseq", typeseq::id<DVsmApplyFrame>()));
log && log(xtag("DVsmEvalArgsFrame.tseq", typeseq::id<DVsmEvalArgsFrame>()));
log && log(xtag("DClosure.tseq", typeseq::id<DClosure>()));
log && log(xtag("DLocalEnv.tseq", typeseq::id<DLocalEnv>()));
return true;
}

View file

@ -4,10 +4,11 @@
**/
#include <xo/interpreter2/VirtualSchematikaMachine.hpp>
#include <xo/object2/DFloat.hpp>
#include <xo/object2/number/IGCObject_DFloat.hpp>
#include <xo/object2/DInteger.hpp>
#include <xo/object2/number/IGCObject_DInteger.hpp>
#include <xo/interpreter2/Closure.hpp>
#include <xo/object2/Float.hpp>
//#include <xo/object2/number/IGCObject_DFloat.hpp>
#include <xo/object2/Integer.hpp>
//#include <xo/object2/number/IGCObject_DInteger.hpp>
#ifdef NOT_YET
#include <xo/reader2/SchematikaParser.hpp>
@ -28,6 +29,7 @@ namespace xo {
using xo::scm::VirtualSchematikaMachine;
using xo::scm::VsmConfig;
using xo::scm::VsmResultExt;
using xo::scm::DClosure;
using xo::scm::DFloat;
using xo::scm::DInteger;
using xo::mm::AGCObject;
@ -196,17 +198,17 @@ namespace xo {
bool eof_flag = false;
vsm.begin_interactive_session();
VsmResultExt res = vsm.read_eval_print(span_type::from_cstr("lambda (x : i64) -> i64 { x * x; };"), eof_flag);
VsmResultExt res = vsm.read_eval_print(span_type::from_cstr("lambda (x : i64) -> i64 { x * x; }"), eof_flag);
REQUIRE(res.is_value());
REQUIRE(res.value());
log && log(xtag("res.tseq", res.value()->_typeseq()));
auto x = obj<AGCObject,DFloat>::from(*res.value());
auto x = obj<AGCObject,DClosure>::from(*res.value());
REQUIRE(x);
REQUIRE(x.data()->value() == 1.570796325);
//REQUIRE(x.data()->value() == 1.570796325);
REQUIRE(res.remaining_.size() == 1);
REQUIRE(*res.remaining_.lo() == '\n');

View file

@ -1,5 +1,5 @@
/** @file DArray.cpp
*
*
* @author Roland Conybeare, Jan 2026
**/
@ -121,7 +121,9 @@ namespace xo {
constexpr auto c_obj_z = sizeof(obj<AGCObject>);
/* memcpy sufficient for obj<A,D> */
::memcpy((void*)&(copy->elts_[0]), (void*)&(elts_[0]), capacity_ * c_obj_z);
::memcpy((void*)&(copy->elts_[0]),
(void*)&(elts_[0]),
capacity_ * c_obj_z);
}
return copy;