From 60b8fda134c3c15c5cf026bdf130e4a7aa8dd431 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 5 Feb 2026 15:45:40 -0500 Subject: [PATCH] xo-reader2 stack: top-level lambda w/ apply parses --- CMakeLists.txt | 50 +++++++++ idl/Expression.json5 | 1 + idl/IExpression_DVarRef.json5 | 12 ++ idl/IGCObject_DApplyExpr.json5 | 15 +++ idl/IGCObject_DVarRef.json5 | 15 +++ idl/IPrintable_DVarRef.json5 | 13 +++ include/xo/expression2/ApplyExpr.hpp | 2 +- include/xo/expression2/Binding.hpp | 22 +++- include/xo/expression2/DApplyExpr.hpp | 5 +- include/xo/expression2/DLocalSymtab.hpp | 6 +- include/xo/expression2/DVarRef.hpp | 84 ++++++++++++++ include/xo/expression2/VarRef.hpp | 13 +++ .../detail/IExpression_DVarRef.hpp | 66 +++++++++++ .../detail/IGCObject_DApplyExpr.hpp | 67 +++++++++++ .../expression2/detail/IGCObject_DVarRef.hpp | 67 +++++++++++ .../expression2/detail/IPrintable_DVarRef.hpp | 62 +++++++++++ include/xo/expression2/exprtype.hpp | 10 +- src/expression2/Binding.cpp | 41 +++++++ src/expression2/CMakeLists.txt | 11 +- src/expression2/DApplyExpr.cpp | 42 +++++++ src/expression2/DVarRef.cpp | 105 ++++++++++++++++++ src/expression2/IExpression_DVarRef.cpp | 45 ++++++++ src/expression2/IGCObject_DApplyExpr.cpp | 39 +++++++ src/expression2/IGCObject_DVarRef.cpp | 39 +++++++ src/expression2/IPrintable_DVarRef.cpp | 28 +++++ .../expression2_register_facets.cpp | 18 ++- 26 files changed, 858 insertions(+), 20 deletions(-) create mode 100644 idl/IExpression_DVarRef.json5 create mode 100644 idl/IGCObject_DApplyExpr.json5 create mode 100644 idl/IGCObject_DVarRef.json5 create mode 100644 idl/IPrintable_DVarRef.json5 create mode 100644 include/xo/expression2/DVarRef.hpp create mode 100644 include/xo/expression2/VarRef.hpp create mode 100644 include/xo/expression2/detail/IExpression_DVarRef.hpp create mode 100644 include/xo/expression2/detail/IGCObject_DApplyExpr.hpp create mode 100644 include/xo/expression2/detail/IGCObject_DVarRef.hpp create mode 100644 include/xo/expression2/detail/IPrintable_DVarRef.hpp create mode 100644 src/expression2/Binding.cpp create mode 100644 src/expression2/DVarRef.cpp create mode 100644 src/expression2/IExpression_DVarRef.cpp create mode 100644 src/expression2/IGCObject_DApplyExpr.cpp create mode 100644 src/expression2/IGCObject_DVarRef.cpp create mode 100644 src/expression2/IPrintable_DVarRef.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d826a155..d070190e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,6 +146,44 @@ xo_add_genfacetimpl( # ---------------------------------------------------------------- +# note: manual target; generated code committed to git +xo_add_genfacetimpl( + TARGET xo-expression2-facetimpl-expression-varref + FACET_PKG xo_expression2 + FACET Expression + REPR VarRef + INPUT idl/IExpression_DVarRef.json5 + OUTPUT_HPP_DIR include/xo/expression2 + OUTPUT_IMPL_SUBDIR detail + OUTPUT_CPP_DIR src/expression2 +) + +# note: manual target; generated code committed to git +xo_add_genfacetimpl( + TARGET xo-expression2-facetimpl-gcobject-varref + FACET_PKG xo_gc + FACET GCObject + REPR VarRef + INPUT idl/IGCObject_DVarRef.json5 + OUTPUT_HPP_DIR include/xo/expression2 + OUTPUT_IMPL_SUBDIR detail + OUTPUT_CPP_DIR src/expression2 +) + +# note: manual target; generated code committed to git +xo_add_genfacetimpl( + TARGET xo-expression2-facetimpl-printable-varref + FACET_PKG xo_printable2 + FACET Printable + REPR VarRef + INPUT idl/IPrintable_DVarRef.json5 + OUTPUT_HPP_DIR include/xo/expression2 + OUTPUT_IMPL_SUBDIR detail + OUTPUT_CPP_DIR src/expression2 +) + +# ---------------------------------------------------------------- + # note: manual target; generated code committed to git xo_add_genfacetimpl( TARGET xo-expression2-facetimpl-expression-defineexpr @@ -184,6 +222,18 @@ xo_add_genfacetimpl( OUTPUT_CPP_DIR src/expression2 ) +# note: manual target; generated code committed to git +xo_add_genfacetimpl( + TARGET xo-expression2-facetimpl-gcobject-applyexpr + FACET_PKG xo_gc + FACET GCObject + REPR ApplyExpr + INPUT idl/IGCObject_DApplyExpr.json5 + OUTPUT_HPP_DIR include/xo/expression2 + OUTPUT_IMPL_SUBDIR detail + OUTPUT_CPP_DIR src/expression2 +) + # note: manual target; generated code committed to git xo_add_genfacetimpl( TARGET xo-expression2-facetimpl-printable-applyexpr diff --git a/idl/Expression.json5 b/idl/Expression.json5 index f2f7d44a..83ae1402 100644 --- a/idl/Expression.json5 +++ b/idl/Expression.json5 @@ -67,4 +67,5 @@ attributes: [], } ], + router_facet_explicit_content: [ ], } diff --git a/idl/IExpression_DVarRef.json5 b/idl/IExpression_DVarRef.json5 new file mode 100644 index 00000000..8dea30e5 --- /dev/null +++ b/idl/IExpression_DVarRef.json5 @@ -0,0 +1,12 @@ +{ + mode: "implementation", + includes: [ "\"Expression.hpp\"" ], + local_types: [ ], + namespace1: "xo", + namespace2: "scm", + facet_idl: "idl/Expression.json5", + brief: "provide AExpression interface for DVarRef state", + using_doxygen: true, + repr: "DVarRef", + doc: ["doc for IExpression+DVarRef" ], +} diff --git a/idl/IGCObject_DApplyExpr.json5 b/idl/IGCObject_DApplyExpr.json5 new file mode 100644 index 00000000..4bf4304b --- /dev/null +++ b/idl/IGCObject_DApplyExpr.json5 @@ -0,0 +1,15 @@ +{ + mode: "implementation", + includes: [ + "", + "" + ], + local_types: [ ], + namespace1: "xo", + namespace2: "scm", + facet_idl: "idl/GCObject.json5", + brief: "provide AGCObject interface for DApplyExpr", + using_doxygen: true, + repr: "DApplyExpr", + doc: [ "implement AGCObject for DApplyExpr" ], +} diff --git a/idl/IGCObject_DVarRef.json5 b/idl/IGCObject_DVarRef.json5 new file mode 100644 index 00000000..3101a035 --- /dev/null +++ b/idl/IGCObject_DVarRef.json5 @@ -0,0 +1,15 @@ +{ + mode: "implementation", + includes: [ + "", + "" + ], + local_types: [ ], + namespace1: "xo", + namespace2: "scm", + facet_idl: "idl/GCObject.json5", + brief: "provide AGCObject interface for DVarRef", + using_doxygen: true, + repr: "DVarRef", + doc: [ "implement AGCObject for DVarRef" ], +} diff --git a/idl/IPrintable_DVarRef.json5 b/idl/IPrintable_DVarRef.json5 new file mode 100644 index 00000000..d525886c --- /dev/null +++ b/idl/IPrintable_DVarRef.json5 @@ -0,0 +1,13 @@ +{ + mode: "implementation", + includes: [ "", + "" ], + local_types: [ ], + namespace1: "xo", + namespace2: "scm", + facet_idl: "idl/Printable.json5", + brief: "provide APrintable interface for DVarRef", + using_doxygen: true, + repr: "DVarRef", + doc: [ "implement APrintable for DVarRef" ], +} diff --git a/include/xo/expression2/ApplyExpr.hpp b/include/xo/expression2/ApplyExpr.hpp index fd9c0764..38625855 100644 --- a/include/xo/expression2/ApplyExpr.hpp +++ b/include/xo/expression2/ApplyExpr.hpp @@ -7,7 +7,7 @@ #include "DApplyExpr.hpp" #include "detail/IExpression_DApplyExpr.hpp" -//#include "detail/IGCObject_DApplyExpr.hpp" +#include "detail/IGCObject_DApplyExpr.hpp" #include "detail/IPrintable_DApplyExpr.hpp" /* end ApplyExpr.hpp */ diff --git a/include/xo/expression2/Binding.hpp b/include/xo/expression2/Binding.hpp index a27de6b3..97f74c27 100644 --- a/include/xo/expression2/Binding.hpp +++ b/include/xo/expression2/Binding.hpp @@ -5,14 +5,15 @@ #pragma once +#include #include namespace xo { namespace scm { class Binding { public: - static constexpr int32_t s_link_sentinel = -2; - static constexpr int32_t s_link_global = -1; + static constexpr int32_t c_link_sentinel = -2; + static constexpr int32_t c_link_global = -1; public: Binding() : i_link_{-2}, j_slot_{-1} {} @@ -21,18 +22,22 @@ namespace xo { static Binding null() { return Binding(); } /** global bindings are located by symbol name **/ - static Binding global() { return Binding(s_link_global, 0); } + static Binding global() { return Binding(c_link_global, 0); } static Binding local(int32_t j_slot) { return Binding(0, j_slot); } + static Binding relative(int32_t i_link, Binding def); bool is_null() const { - return (i_link_ == s_link_sentinel) && (j_slot_ == -1); + return (i_link_ == c_link_sentinel) && (j_slot_ == -1); } - bool is_global() const { return i_link_ == s_link_global; } + bool is_global() const { return i_link_ == c_link_global; } bool is_local() const { return (i_link_ == 0) && (j_slot_ >= 0); } int32_t i_link() const noexcept { return i_link_; } int32_t j_slot() const noexcept { return j_slot_; } + /** print human-readable repr to stream @p os **/ + void print(std::ostream & os) const; + private: /** * >= 0: number of parent links to traverse @@ -40,13 +45,18 @@ namespace xo { * -1: resolve globally * -2: sentinel (binding info not computed) **/ - int32_t i_link_ = s_link_sentinel; + int32_t i_link_ = c_link_sentinel; /** if @ref i_link_ >= 0, frame offset * (in 'variables' not bytes). * ignored if @ref i_link_ is global **/ int32_t j_slot_ = -1; }; + + inline std::ostream & operator<< (std::ostream & os, Binding x) { + x.print(os); + return os; + } } /*namespace scm*/ } /*namespace xo*/ diff --git a/include/xo/expression2/DApplyExpr.hpp b/include/xo/expression2/DApplyExpr.hpp index 8502c62a..94bcea73 100644 --- a/include/xo/expression2/DApplyExpr.hpp +++ b/include/xo/expression2/DApplyExpr.hpp @@ -20,6 +20,7 @@ namespace xo { **/ class DApplyExpr { public: + using ACollector = xo::mm::ACollector; using AAllocator = xo::mm::AAllocator; using TypeDescr = xo::reflect::TypeDescr; using ppindentinfo = xo::print::ppindentinfo; @@ -81,7 +82,9 @@ namespace xo { /** @defgroup scm-applyexpr-gcobject-facet **/ ///@{ - // shallow_copy() etc. + std::size_t shallow_size() const noexcept; + DApplyExpr * shallow_copy(obj mm) const noexcept; + std::size_t forward_children(obj gc) noexcept; ///@} /** @defgroup scm-applyexpr-printable-facet **/ diff --git a/include/xo/expression2/DLocalSymtab.hpp b/include/xo/expression2/DLocalSymtab.hpp index 163d031e..6d1a6480 100644 --- a/include/xo/expression2/DLocalSymtab.hpp +++ b/include/xo/expression2/DLocalSymtab.hpp @@ -31,14 +31,14 @@ namespace xo { struct Slot { Slot() = default; - explicit Slot(const DVariable * var) : var_{var} {} + explicit Slot(DVariable * var) : var_{var} {} /** variable representing a formal argument. * binding will be correct only within the same layer * as top-level lambda body * (i.e. up to the doorstep of each and every nested lambda) **/ - const DVariable * var_ = nullptr; + DVariable * var_ = nullptr; }; public: @@ -65,7 +65,7 @@ namespace xo { size_type capacity() const noexcept { return capacity_; } size_type size() const noexcept { return size_; } - const DVariable * lookup_var(Binding ix) const noexcept { + DVariable * lookup_var(Binding ix) noexcept { assert(ix.i_link() == 0); assert(ix.j_slot() < static_cast(size_)); diff --git a/include/xo/expression2/DVarRef.hpp b/include/xo/expression2/DVarRef.hpp new file mode 100644 index 00000000..d4a230b3 --- /dev/null +++ b/include/xo/expression2/DVarRef.hpp @@ -0,0 +1,84 @@ +/** @file DVarRef.hpp + * + * @author Roland Conybeare, Feb 2026 + **/ + +#pragma once + +#include "Variable.hpp" + +namespace xo { + namespace scm { + + /** @class DVarRef + * @brief syntax for a variable reference + * + * Reference to a known variable possibly + * defined in another scope. + * For non-local non-global variables, + **/ + class DVarRef { + public: + using ppindentinfo = xo::print::ppindentinfo; + using ACollector = xo::mm::ACollector; + using AAllocator = xo::mm::AAllocator; + using TypeDescr = xo::reflect::TypeDescr; + + public: + DVarRef(DVariable * vardef, + Binding path); + + /** create instance + * @p mm memory allocator + * @p vardef variable definition (name, typeref, binding) + * @p link number of lexical scope boundaries we must cross + * to reach scope containing @p vardef. + * Only relevant for non-global vardef + **/ + static DVarRef * make(obj mm, + DVariable * vardef, + int32_t link); + + const DUniqueString * name() const; + Binding path() const { return path_; } + + /** @defgroup scm-variable-expression-facet **/ + ///@{ + + exprtype extype() const noexcept { return exprtype::varref; } + TypeRef typeref() const noexcept; + TypeDescr valuetype() const noexcept; + void assign_valuetype(TypeDescr td) noexcept; + + ///@} + /** @defgroup scm-variable-gcobject-facet **/ + ///@{ + + size_t shallow_size() const noexcept; + DVarRef * shallow_copy(obj mm) const noexcept; + size_t forward_children(obj gc) noexcept; + + ///@} + /** @defgroup scm-variable-printable-facet **/ + ///@{ + + bool pretty(const ppindentinfo & ppii) const; + + ///@} + + private: + /** variable definition. Created in the sccope where variable introduced. + * Has an associated @ref Binding, but that binding is only correct + * around any nested scopes. + **/ + DVariable * vardef_ = nullptr; + + /** at runtime: navigate environemnt via this path to get + * runtime memory location for this variable + **/ + Binding path_; + }; + } /*namespace scm*/ +} /*namespace xo*/ + +/* end DVarRef.hpp */ diff --git a/include/xo/expression2/VarRef.hpp b/include/xo/expression2/VarRef.hpp new file mode 100644 index 00000000..19f2d1d0 --- /dev/null +++ b/include/xo/expression2/VarRef.hpp @@ -0,0 +1,13 @@ +/** @file VarRef.hpp + * + * @author Roland Conybeare, Feb 2026 + **/ + +#pragma once + +#include "DVarRef.hpp" +#include "detail/IExpression_DVarRef.hpp" +#include "detail/IGCObject_DVarRef.hpp" +#include "detail/IPrintable_DVarRef.hpp" + +/* end VarRef.hpp */ diff --git a/include/xo/expression2/detail/IExpression_DVarRef.hpp b/include/xo/expression2/detail/IExpression_DVarRef.hpp new file mode 100644 index 00000000..332113c1 --- /dev/null +++ b/include/xo/expression2/detail/IExpression_DVarRef.hpp @@ -0,0 +1,66 @@ +/** @file IExpression_DVarRef.hpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IExpression_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_repr.hpp.j2] + * 3. idl for facet methods + * [idl/IExpression_DVarRef.json5] + **/ + +#pragma once + +#include "Expression.hpp" +#include "Expression.hpp" +#include "DVarRef.hpp" + +namespace xo { namespace scm { class IExpression_DVarRef; } } + +namespace xo { + namespace facet { + template <> + struct FacetImplementation + { + using ImplType = xo::scm::IExpression_Xfer + ; + }; + } +} + +namespace xo { + namespace scm { + /** @class IExpression_DVarRef + **/ + class IExpression_DVarRef { + public: + /** @defgroup scm-expression-dvarref-type-traits **/ + ///@{ + using TypeDescr = xo::scm::AExpression::TypeDescr; + using Copaque = xo::scm::AExpression::Copaque; + using Opaque = xo::scm::AExpression::Opaque; + ///@} + /** @defgroup scm-expression-dvarref-methods **/ + ///@{ + // const methods + /** expression type (constant | apply | ..) **/ + static exprtype extype(const DVarRef & self) noexcept; + /** placeholder for type giving possible values for this expression **/ + static TypeRef typeref(const DVarRef & self) noexcept; + /** type giving possible values for this expression. Maybe null before typecheck **/ + static TypeDescr valuetype(const DVarRef & self) noexcept; + + // non-const methods + /** assing to valuetype member. Useful when scaffolding expressions **/ + static void assign_valuetype(DVarRef & self, TypeDescr td) noexcept; + ///@} + }; + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end */ \ No newline at end of file diff --git a/include/xo/expression2/detail/IGCObject_DApplyExpr.hpp b/include/xo/expression2/detail/IGCObject_DApplyExpr.hpp new file mode 100644 index 00000000..10b08c10 --- /dev/null +++ b/include/xo/expression2/detail/IGCObject_DApplyExpr.hpp @@ -0,0 +1,67 @@ +/** @file IGCObject_DApplyExpr.hpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IGCObject_DApplyExpr.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_repr.hpp.j2] + * 3. idl for facet methods + * [idl/IGCObject_DApplyExpr.json5] + **/ + +#pragma once + +#include "GCObject.hpp" +#include +#include +#include "DApplyExpr.hpp" + +namespace xo { namespace scm { class IGCObject_DApplyExpr; } } + +namespace xo { + namespace facet { + template <> + struct FacetImplementation + { + using ImplType = xo::mm::IGCObject_Xfer + ; + }; + } +} + +namespace xo { + namespace scm { + /** @class IGCObject_DApplyExpr + **/ + class IGCObject_DApplyExpr { + public: + /** @defgroup scm-gcobject-dapplyexpr-type-traits **/ + ///@{ + using size_type = xo::mm::AGCObject::size_type; + using AAllocator = xo::mm::AGCObject::AAllocator; + using ACollector = xo::mm::AGCObject::ACollector; + using Copaque = xo::mm::AGCObject::Copaque; + using Opaque = xo::mm::AGCObject::Opaque; + ///@} + /** @defgroup scm-gcobject-dapplyexpr-methods **/ + ///@{ + // const methods + /** memory consumption for this instance **/ + static size_type shallow_size(const DApplyExpr & self) noexcept; + /** copy instance using allocator **/ + static Opaque shallow_copy(const DApplyExpr & self, obj mm) noexcept; + + // non-const methods + /** during GC: forward immdiate children **/ + static size_type forward_children(DApplyExpr & self, obj gc) noexcept; + ///@} + }; + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end */ \ No newline at end of file diff --git a/include/xo/expression2/detail/IGCObject_DVarRef.hpp b/include/xo/expression2/detail/IGCObject_DVarRef.hpp new file mode 100644 index 00000000..e991ebb8 --- /dev/null +++ b/include/xo/expression2/detail/IGCObject_DVarRef.hpp @@ -0,0 +1,67 @@ +/** @file IGCObject_DVarRef.hpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IGCObject_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_repr.hpp.j2] + * 3. idl for facet methods + * [idl/IGCObject_DVarRef.json5] + **/ + +#pragma once + +#include "GCObject.hpp" +#include +#include +#include "DVarRef.hpp" + +namespace xo { namespace scm { class IGCObject_DVarRef; } } + +namespace xo { + namespace facet { + template <> + struct FacetImplementation + { + using ImplType = xo::mm::IGCObject_Xfer + ; + }; + } +} + +namespace xo { + namespace scm { + /** @class IGCObject_DVarRef + **/ + class IGCObject_DVarRef { + public: + /** @defgroup scm-gcobject-dvarref-type-traits **/ + ///@{ + using size_type = xo::mm::AGCObject::size_type; + using AAllocator = xo::mm::AGCObject::AAllocator; + using ACollector = xo::mm::AGCObject::ACollector; + using Copaque = xo::mm::AGCObject::Copaque; + using Opaque = xo::mm::AGCObject::Opaque; + ///@} + /** @defgroup scm-gcobject-dvarref-methods **/ + ///@{ + // const methods + /** memory consumption for this instance **/ + static size_type shallow_size(const DVarRef & self) noexcept; + /** copy instance using allocator **/ + static Opaque shallow_copy(const DVarRef & self, obj mm) noexcept; + + // non-const methods + /** during GC: forward immdiate children **/ + static size_type forward_children(DVarRef & self, obj gc) noexcept; + ///@} + }; + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end */ \ No newline at end of file diff --git a/include/xo/expression2/detail/IPrintable_DVarRef.hpp b/include/xo/expression2/detail/IPrintable_DVarRef.hpp new file mode 100644 index 00000000..50034312 --- /dev/null +++ b/include/xo/expression2/detail/IPrintable_DVarRef.hpp @@ -0,0 +1,62 @@ +/** @file IPrintable_DVarRef.hpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IPrintable_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_repr.hpp.j2] + * 3. idl for facet methods + * [idl/IPrintable_DVarRef.json5] + **/ + +#pragma once + +#include "Printable.hpp" +#include +#include +#include "DVarRef.hpp" + +namespace xo { namespace scm { class IPrintable_DVarRef; } } + +namespace xo { + namespace facet { + template <> + struct FacetImplementation + { + using ImplType = xo::print::IPrintable_Xfer + ; + }; + } +} + +namespace xo { + namespace scm { + /** @class IPrintable_DVarRef + **/ + class IPrintable_DVarRef { + public: + /** @defgroup scm-printable-dvarref-type-traits **/ + ///@{ + using ppindentinfo = xo::print::APrintable::ppindentinfo; + using Copaque = xo::print::APrintable::Copaque; + using Opaque = xo::print::APrintable::Opaque; + ///@} + /** @defgroup scm-printable-dvarref-methods **/ + ///@{ + // const methods + /** Pretty-printing support for this object. +See [xo-indentlog/xo/indentlog/pretty.hpp] **/ + static bool pretty(const DVarRef & self, const ppindentinfo & ppii); + + // non-const methods + ///@} + }; + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end */ \ No newline at end of file diff --git a/include/xo/expression2/exprtype.hpp b/include/xo/expression2/exprtype.hpp index 3bcda3c4..8b1778fb 100644 --- a/include/xo/expression2/exprtype.hpp +++ b/include/xo/expression2/exprtype.hpp @@ -36,8 +36,10 @@ namespace xo { /** function definition **/ lambda, - /** variable reference **/ + /** variable definition **/ variable, + /** variabele reference (possibly non-local) **/ + varref, /** if-then-else **/ ifexpr, /** sequence **/ @@ -65,14 +67,16 @@ namespace xo { case exprtype::assign: return "assign"; #endif case exprtype::apply: return "apply"; -#ifdef NOT_YET case exprtype::lambda: return "lambda"; case exprtype::variable: return "variable"; + case exprtype::varref: return "varref"; case exprtype::ifexpr: return "if_expr"; case exprtype::sequence: return "sequence"; +#ifdef NOT_YET case exprtype::convert: return "convert"; #endif - default: break; + case exprtype::N: break; + //default: break; } return "???exprtype???"; diff --git a/src/expression2/Binding.cpp b/src/expression2/Binding.cpp new file mode 100644 index 00000000..6802ba69 --- /dev/null +++ b/src/expression2/Binding.cpp @@ -0,0 +1,41 @@ +/** @file Binding.cpp + * + * @author Roland Conybeare, Feb 2026 + **/ + +#include "Binding.hpp" +#include + +namespace xo { + namespace scm { + + Binding + Binding::relative(int32_t i_link, Binding def) + { + if (def.i_link_ == Binding::c_link_global) { + // for globally defined vars, i_link always -1 + return def; + } else if (def.i_link_ >= 0) { + return Binding(i_link + def.i_link_, def.j_slot_); + } else { + assert(false); + return Binding(); + } + } + + void + Binding::print(std::ostream & os) const + { + if (i_link_ == c_link_global) { + os << "{path:global}"; + } else if (i_link_ == c_link_sentinel) { + os << "{path}"; + } else { + os << "{path:" << i_link_ << ":" << j_slot_ << "}"; + } + } + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end Binding.cpp */ diff --git a/src/expression2/CMakeLists.txt b/src/expression2/CMakeLists.txt index 4f3d63c1..45b5a06e 100644 --- a/src/expression2/CMakeLists.txt +++ b/src/expression2/CMakeLists.txt @@ -3,9 +3,12 @@ set(SELF_LIB xo_expression2) set(SELF_SRCS init_expression2.cpp + expression2_register_facets.cpp + expression2_register_types.cpp DConstant.cpp DVariable.cpp + DVarRef.cpp DDefineExpr.cpp DLambdaExpr.cpp DApplyExpr.cpp @@ -13,6 +16,7 @@ set(SELF_SRCS DSequenceExpr.cpp TypeRef.cpp + Binding.cpp IExpression_Any.cpp @@ -24,10 +28,15 @@ set(SELF_SRCS IGCObject_DVariable.cpp IPrintable_DVariable.cpp + IExpression_DVarRef.cpp + IGCObject_DVarRef.cpp + IPrintable_DVarRef.cpp + IExpression_DDefineExpr.cpp IPrintable_DDefineExpr.cpp IExpression_DApplyExpr.cpp + IGCObject_DApplyExpr.cpp IPrintable_DApplyExpr.cpp IExpression_DLambdaExpr.cpp @@ -55,8 +64,6 @@ set(SELF_SRCS IGCObject_DUniqueString.cpp IPrintable_DUniqueString.cpp - expression2_register_facets.cpp - expression2_register_types.cpp ) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) diff --git a/src/expression2/DApplyExpr.cpp b/src/expression2/DApplyExpr.cpp index 751c1d85..d4e1fa55 100644 --- a/src/expression2/DApplyExpr.cpp +++ b/src/expression2/DApplyExpr.cpp @@ -102,6 +102,48 @@ namespace xo { typeref_.resolve(td); } + // ----- gcobject facet ----- + + std::size_t + DApplyExpr::shallow_size() const noexcept { + return sizeof(DApplyExpr) + (n_args_ * sizeof(obj)); + } + + DApplyExpr * + DApplyExpr::shallow_copy(obj mm) const noexcept { + DApplyExpr * copy = (DApplyExpr *)mm.alloc_copy((std::byte *)this); + + if (copy) { + copy->typeref_ = typeref_; + copy->fn_ = fn_; + copy->n_args_ = n_args_; + + constexpr auto c_obj_z = sizeof(obj); + + ::memcpy((void*)&(copy->args_[0]), (void*)&(args_[0]), n_args_ * c_obj_z); + } + + return copy; + } + + std::size_t + DApplyExpr::forward_children(obj gc) noexcept + { + for (size_type i = 0; i < n_args_; ++i) { + obj & arg = args_[i]; + + // runtime poly here + obj arg_gco = arg.to_facet(); + + // need the data address within *this + gc.forward_inplace(arg_gco.iface(), (void **)(&arg.data_)); + } + + return shallow_size(); + } + + // ----- printable facet ----- + bool DApplyExpr::pretty(const ppindentinfo & ppii) const { using xo::print::ppstate; diff --git a/src/expression2/DVarRef.cpp b/src/expression2/DVarRef.cpp new file mode 100644 index 00000000..afd9190e --- /dev/null +++ b/src/expression2/DVarRef.cpp @@ -0,0 +1,105 @@ +/** @file DVarRef.cpp + * + * @author Roland Conybeare, Feb 2026 + **/ + +#include "DVarRef.hpp" + +namespace xo { + using xo::mm::AGCObject; + using xo::reflect::TypeDescr; + + namespace scm { + + DVarRef::DVarRef(DVariable * vardef, + Binding path) + : vardef_{vardef}, + path_{path} + {} + + DVarRef * + DVarRef::make(obj mm, + DVariable * vardef, + int32_t link) + { + assert(vardef); + + void * mem = mm.alloc_for(); + + return new (mem) DVarRef(vardef, + Binding::relative(link, + vardef->path())); + } + + const DUniqueString * + DVarRef::name() const { + return vardef_->name(); + } + + TypeRef + DVarRef::typeref() const noexcept { + assert(vardef_); + + return vardef_->typeref(); + } + + TypeDescr + DVarRef::valuetype() const noexcept + { + return this->typeref().td(); + } + + void + DVarRef::assign_valuetype(TypeDescr td) noexcept + { + assert(vardef_); + vardef_->assign_valuetype(td); + } + + // gcobject facet + + std::size_t + DVarRef::shallow_size() const noexcept + { + return sizeof(DVarRef); + } + + DVarRef * + DVarRef::shallow_copy(obj mm) const noexcept + { + DVarRef * copy = (DVarRef *)mm.alloc_copy((std::byte *)this); + + if (copy) + *copy = *this; + + return copy; + } + + std::size_t + DVarRef::forward_children(obj gc) noexcept + { + // TODO: this can be helper in RCollector interface + auto iface = xo::facet::impl_for(); + gc.forward_inplace(&iface, (void **)vardef_); + + // TODO: concept to indicate that no gc pointers in Binding + + return shallow_size(); + } + + // printable facet + + bool + DVarRef::pretty(const ppindentinfo & ppii) const + { + return ppii.pps()->pretty_struct + (ppii, + "DVarRef", + refrtag("name", std::string_view(*(this->name()))), + refrtag("path", this->path_)); + } + + } +} /*namespace xo*/ + +/* end DVarRef.cpp */ diff --git a/src/expression2/IExpression_DVarRef.cpp b/src/expression2/IExpression_DVarRef.cpp new file mode 100644 index 00000000..843a0653 --- /dev/null +++ b/src/expression2/IExpression_DVarRef.cpp @@ -0,0 +1,45 @@ +/** @file IExpression_DVarRef.cpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IExpression_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_any.hpp.j2] + * 3. idl for facet methods + * [idl/IExpression_DVarRef.json5] +**/ + +#include "detail/IExpression_DVarRef.hpp" + +namespace xo { + namespace scm { + auto + IExpression_DVarRef::extype(const DVarRef & self) noexcept -> exprtype + { + return self.extype(); + } + + auto + IExpression_DVarRef::typeref(const DVarRef & self) noexcept -> TypeRef + { + return self.typeref(); + } + + auto + IExpression_DVarRef::valuetype(const DVarRef & self) noexcept -> TypeDescr + { + return self.valuetype(); + } + + auto + IExpression_DVarRef::assign_valuetype(DVarRef & self, TypeDescr td) noexcept -> void + { + self.assign_valuetype(td); + } + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end IExpression_DVarRef.cpp */ diff --git a/src/expression2/IGCObject_DApplyExpr.cpp b/src/expression2/IGCObject_DApplyExpr.cpp new file mode 100644 index 00000000..ad6571e0 --- /dev/null +++ b/src/expression2/IGCObject_DApplyExpr.cpp @@ -0,0 +1,39 @@ +/** @file IGCObject_DApplyExpr.cpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IGCObject_DApplyExpr.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_any.hpp.j2] + * 3. idl for facet methods + * [idl/IGCObject_DApplyExpr.json5] +**/ + +#include "detail/IGCObject_DApplyExpr.hpp" + +namespace xo { + namespace scm { + auto + IGCObject_DApplyExpr::shallow_size(const DApplyExpr & self) noexcept -> size_type + { + return self.shallow_size(); + } + + auto + IGCObject_DApplyExpr::shallow_copy(const DApplyExpr & self, obj mm) noexcept -> Opaque + { + return self.shallow_copy(mm); + } + + auto + IGCObject_DApplyExpr::forward_children(DApplyExpr & self, obj gc) noexcept -> size_type + { + return self.forward_children(gc); + } + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end IGCObject_DApplyExpr.cpp */ diff --git a/src/expression2/IGCObject_DVarRef.cpp b/src/expression2/IGCObject_DVarRef.cpp new file mode 100644 index 00000000..61596baa --- /dev/null +++ b/src/expression2/IGCObject_DVarRef.cpp @@ -0,0 +1,39 @@ +/** @file IGCObject_DVarRef.cpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IGCObject_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_any.hpp.j2] + * 3. idl for facet methods + * [idl/IGCObject_DVarRef.json5] +**/ + +#include "detail/IGCObject_DVarRef.hpp" + +namespace xo { + namespace scm { + auto + IGCObject_DVarRef::shallow_size(const DVarRef & self) noexcept -> size_type + { + return self.shallow_size(); + } + + auto + IGCObject_DVarRef::shallow_copy(const DVarRef & self, obj mm) noexcept -> Opaque + { + return self.shallow_copy(mm); + } + + auto + IGCObject_DVarRef::forward_children(DVarRef & self, obj gc) noexcept -> size_type + { + return self.forward_children(gc); + } + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end IGCObject_DVarRef.cpp */ diff --git a/src/expression2/IPrintable_DVarRef.cpp b/src/expression2/IPrintable_DVarRef.cpp new file mode 100644 index 00000000..a2027a2a --- /dev/null +++ b/src/expression2/IPrintable_DVarRef.cpp @@ -0,0 +1,28 @@ +/** @file IPrintable_DVarRef.cpp + * + * Generated automagically from ingredients: + * 1. code generator: + * [xo-facet/codegen/genfacet] + * arguments: + * --input [idl/IPrintable_DVarRef.json5] + * 2. jinja2 template for abstract facet .hpp file: + * [iface_facet_any.hpp.j2] + * 3. idl for facet methods + * [idl/IPrintable_DVarRef.json5] +**/ + +#include "detail/IPrintable_DVarRef.hpp" + +namespace xo { + namespace scm { + auto + IPrintable_DVarRef::pretty(const DVarRef & self, const ppindentinfo & ppii) -> bool + { + return self.pretty(ppii); + } + + + } /*namespace scm*/ +} /*namespace xo*/ + +/* end IPrintable_DVarRef.cpp */ diff --git a/src/expression2/expression2_register_facets.cpp b/src/expression2/expression2_register_facets.cpp index 26869d50..67944a08 100644 --- a/src/expression2/expression2_register_facets.cpp +++ b/src/expression2/expression2_register_facets.cpp @@ -16,13 +16,13 @@ #include #include +#include + #include #include #include -#include -//#include -#include +#include #include //#include @@ -63,6 +63,7 @@ namespace xo { // Expression // +- Constant // +- Variable + // +- VarRef // +- DefineExpr // +- ApplyExpr // +- LambdaExpr @@ -77,13 +78,20 @@ namespace xo { FacetRegistry::register_impl(); FacetRegistry::register_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); + FacetRegistry::register_impl(); + //FacetRegistry::register_impl(); FacetRegistry::register_impl(); FacetRegistry::register_impl(); + FacetRegistry::register_impl(); FacetRegistry::register_impl(); FacetRegistry::register_impl(); + //FacetRegistry::register_impl(); FacetRegistry::register_impl(); FacetRegistry::register_impl(); @@ -95,11 +103,13 @@ namespace xo { FacetRegistry::register_impl(); FacetRegistry::register_impl(); + //FacetRegistry::register_impl(); FacetRegistry::register_impl(); log && log(xtag("DUniqueString.tseq", typeseq::id())); log && log(xtag("DDefineExpr.tseq", typeseq::id())); log && log(xtag("DVariable.tseq", typeseq::id())); + log && log(xtag("DVarRef.tseq", typeseq::id())); log && log(xtag("DConstant.tseq", typeseq::id())); log && log(xtag("DApplyExpr.tseq", typeseq::id())); log && log(xtag("DLambdaExpr.tseq", typeseq::id())); @@ -108,7 +118,7 @@ namespace xo { log && log(xtag("DLocalSymtab.tseq", typeseq::id())); - log && log(xtag("AExpression.tqseq", typeseq::id())); + log && log(xtag("AExpression.tseq", typeseq::id())); log && log(xtag("ASymbolTable.tseq", typeseq::id())); return true;