xo-expression2: + LambdaExpr ++ LocalSymtab

This commit is contained in:
Roland Conybeare 2026-01-27 22:35:22 -05:00
commit 666482a945
15 changed files with 621 additions and 9 deletions

View file

@ -150,6 +150,32 @@ xo_add_genfacetimpl(
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-expression2-facetimpl-expression-lambdaexpr
FACET_PKG xo_expression2
FACET Expression
REPR LambdaExpr
INPUT idl/IExpression_DLambdaExpr.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-lambdaexpr
FACET_PKG xo_printable2
FACET Printable
REPR LambdaExpr
INPUT idl/IPrintable_DLambdaExpr.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-ifelseexpr

View file

@ -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 DLambdaExpr state",
using_doxygen: true,
repr: "DLambdaExpr",
doc: ["doc for IExpression+DLambdaExpr" ],
}

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 DLambdaExpr",
using_doxygen: true,
repr: "DLambdaExpr",
doc: [ "implement APrintable for DLambdaExpr" ],
}

View file

@ -19,6 +19,7 @@ namespace xo {
Binding(int32_t i_link, int32_t j_slot)
: i_link_{i_link}, j_slot_{j_slot} {}
static Binding null() { return Binding(); }
/** global bindings are located by symbol name **/
static Binding global() { return Binding(s_link_global, 0); }
static Binding local(int32_t j_slot) { return Binding(0, j_slot); }

View file

@ -0,0 +1,111 @@
/** @file DLambdaExpr.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "Expression.hpp"
#include "TypeRef.hpp"
#include "exprtype.hpp"
#include "DLocalSymtab.hpp"
#include "DString.hpp"
#include <xo/facet/obj.hpp>
namespace xo {
namespace scm {
/** @class DLambdaExpr
* @brief syntax tree for a function/procedure definition
*
**/
class DLambdaExpr {
public:
using AAllocator = xo::mm::AAllocator;
using TypeDescr = xo::reflect::TypeDescr;
using ppindentinfo = xo::print::ppindentinfo;
public:
/** @defgroup scm-lambdaexpr-ctors **/
///@{
DLambdaExpr(TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body);
#ifdef NOT_YET
/** create instance using memory from @p mm **/
static obj<AExpression,DLambdaExpr> make(obj<AAllocator> mm,
TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body);
#endif
/** create instance, using memory from @p mm **/
static DLambdaExpr * _make(obj<AAllocator> mm,
TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body);
///@}
/** @defgroup scm-lambdaexpr-methods **/
///@{
// get_free_variables()
// visit_preorder()
// visit_layer()
// xform_layer()
// attach_envs(SymbolTable*)
///@}
/** @defgroup scm-lambdaexpr-expression-facet **/
///@{
exprtype extype() const noexcept;
TypeRef typeref() const noexcept;
TypeDescr valuetype() const noexcept;
void assign_valuetype(TypeDescr td) noexcept;
///@}
/** @defgroup scm-lambdaexpr-printable-facet **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** expression value always has type consistent
* with description here
**/
TypeRef typeref_;
/** name for this lambda (generated if necessary) **/
const DUniqueString * name_ = nullptr;
#ifdef NOT_YET
/** e.g.
* i64(f64,string)
* for function of two arguments with types (f64, string) respectively,
* that returns an i64.
**/
const DUniqueString * type_name_str_ = nullptr;
#endif
/** symbol table for lambda arguments **/
DLocalSymtab * local_symtab_ = nullptr;;
/** expression for function body **/
obj<AExpression> body_expr_;
// free_var_set
// captured_var_set
// layer_var_map
// nested_lambda_map
};
} /*namespace scm*/
} /*namespace xo*/
/* end DLambdaExpr.hpp */

View file

@ -6,6 +6,7 @@
#pragma once
#include "Binding.hpp"
#include "DVariable.hpp"
#include "DUniqueString.hpp"
//#include "exprtype.hpp"
//#include <xo/reflect/TaggedPtr.hpp>
@ -23,15 +24,61 @@ namespace xo {
// using AGCObject = xo::mm::AGCObject;
// using typeseq = xo::reflect::typeseq;
using ppindentinfo = xo::print::ppindentinfo;
using AAllocator = xo::mm::AAllocator;
/* note: uint16_t would be fine too */
using size_type = std::uint32_t;
struct Slot {
// obj<Expression,DVariable> var_;
Binding binding_;
Slot() = default;
explicit Slot(const 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;
};
public:
// explicit DLocalSymtab(obj<AGCObject> value) noexcept;
/** @defgroup scm-lambdaexpr-constructors **/
///@{
/** @defgroup xo-expression2-symboltable-facet symboltable facet**/
/** empty instance with capacity for n slots.
* Caller must ensure that slots_[0..n) are actually addressable
**/
DLocalSymtab(size_type n);
/** scaffold empty symtab instance,
* with capacity for @p n slots, using memory from allocator @p mm
**/
static DLocalSymtab * _make_empty(obj<AAllocator> mm, size_type n);
///@}
/** @defgroup scm-lambdaexpr-methods **/
///@{
size_type capacity() const noexcept { return capacity_; }
size_type size() const noexcept { return size_; }
const DVariable * lookup_var(Binding ix) const noexcept {
assert(ix.i_link() == 0);
assert(ix.j_slot() < static_cast<int32_t>(size_));
return slots_[ix.j_slot()].var_;
}
/** increase slot size (provided beleow capacity) to append
* binding for one local variable. Local variable will be allocated
* from @p mm, named @p name, with type described by @p typeref.
**/
Binding append_var(obj<AAllocator> mm,
const DUniqueString * name,
TypeRef typeref);
///@}
/** @defgroup xo-localsymtab-symboltable-facet symboltable facet**/
///@{
/** true for global symbol table **/
@ -41,9 +88,20 @@ namespace xo {
Binding lookup_binding(const DUniqueString * sym) const noexcept;
///@}
/** @defgroup xo-localsymtab-printable-facet printable facet **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** actual range of slots_[] array. Can use inices in [0,..,n) **/
size_type capacity_ = 0;
/** number of slots in use **/
size_type size_ = 0;
/** memory for names and bindings **/
Slot slots_[];
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -0,0 +1,66 @@
/** @file IExpression_DLambdaExpr.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IExpression_DLambdaExpr.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IExpression_DLambdaExpr.json5]
**/
#pragma once
#include "Expression.hpp"
#include "Expression.hpp"
#include "DLambdaExpr.hpp"
namespace xo { namespace scm { class IExpression_DLambdaExpr; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::AExpression,
xo::scm::DLambdaExpr>
{
using ImplType = xo::scm::IExpression_Xfer
<xo::scm::DLambdaExpr,
xo::scm::IExpression_DLambdaExpr>;
};
}
}
namespace xo {
namespace scm {
/** @class IExpression_DLambdaExpr
**/
class IExpression_DLambdaExpr {
public:
/** @defgroup scm-expression-dlambdaexpr-type-traits **/
///@{
using TypeDescr = xo::scm::AExpression::TypeDescr;
using Copaque = xo::scm::AExpression::Copaque;
using Opaque = xo::scm::AExpression::Opaque;
///@}
/** @defgroup scm-expression-dlambdaexpr-methods **/
///@{
// const methods
/** expression type (constant | apply | ..) **/
static exprtype extype(const DLambdaExpr & self) noexcept;
/** placeholder for type giving possible values for this expression **/
static TypeRef typeref(const DLambdaExpr & self) noexcept;
/** type giving possible values for this expression. Maybe null before typecheck **/
static TypeDescr valuetype(const DLambdaExpr & self) noexcept;
// non-const methods
/** assing to valuetype member. Useful when scaffolding expressions **/
static void assign_valuetype(DLambdaExpr & self, TypeDescr td) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

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

View file

@ -32,10 +32,10 @@ namespace xo {
#endif
/** function call **/
apply,
#ifdef NOT_YET
/** function definition **/
lambda,
#endif
/** variable reference **/
variable,
/** if-then-else **/

View file

@ -7,6 +7,7 @@ set(SELF_SRCS
DConstant.cpp
DVariable.cpp
DDefineExpr.cpp
DLambdaExpr.cpp
DApplyExpr.cpp
DIfElseExpr.cpp
@ -26,6 +27,9 @@ set(SELF_SRCS
IExpression_DApplyExpr.cpp
IPrintable_DApplyExpr.cpp
IExpression_DLambdaExpr.cpp
IPrintable_DLambdaExpr.cpp
IExpression_DIfElseExpr.cpp
IPrintable_DIfElseExpr.cpp

View file

@ -0,0 +1,129 @@
/** @file DLambda.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "DLambdaExpr.hpp"
// #include "detail/IExpression_DLambdaExpr.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::print::APrintable;
using xo::facet::FacetRegistry;
using xo::reflect::TypeDescr;
using xo::reflect::typeseq;
namespace scm {
#ifdef NOT_YET
TypeDescr
assemble_lambda_td()
{
std::vector<TypeDescr> arg_td_v;
{
arg_td_v.reserve(local_symtab->size());
for (DLocalSymtab::size_type i = 0, n = local_symtab->size(); i < n; ++i) {
const DVariable * var = local_symtab->lookup_var(i);
if (!var)
break;
TypeDescr arg_td = var->valuetype();
if (!arg_td)
break;
arg_td_v.push_back(arg_td);
}
}
}
#endif
DLambdaExpr::DLambdaExpr(TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body) : typeref_{typeref},
name_{name},
local_symtab_{local_symtab},
body_expr_{body}
{
}
#ifdef NOT_YET
obj<AExpression,DLambdaExpr>
DLambdaExpr::make(obj<AAllocator> mm,
TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body)
{
return obj<AExpression,DLambdaExpr>(_make(mm, typeref,
name, local_symtab, body);
}
#endif
DLambdaExpr *
DLambdaExpr::_make(obj<AAllocator> mm,
TypeRef typeref,
const DUniqueString * name,
DLocalSymtab * local_symtab,
obj<AExpression> body)
{
// in general we're not going to know argument types yet.
// perhaps want to delay this until after type resolution.
void * mem = mm.alloc(typeseq::id<DLambdaExpr>(), sizeof(DLambdaExpr));
return new (mem) DLambdaExpr(typeref,
name,
local_symtab,
body);
}
exprtype
DLambdaExpr::extype() const noexcept {
return exprtype::lambda;
}
TypeRef
DLambdaExpr::typeref() const noexcept {
return typeref_;
}
TypeDescr
DLambdaExpr::valuetype() const noexcept {
return typeref_.td();
}
void
DLambdaExpr::assign_valuetype(TypeDescr td) noexcept {
typeref_.resolve(td);
}
bool
DLambdaExpr::pretty(const ppindentinfo & ppii) const
{
auto body
= FacetRegistry::instance().try_variant<APrintable,
AExpression>(body_expr_);
if (name_ && body) {
return ppii.pps()->pretty_struct(ppii,
"LambdaExpr",
refrtag("name", name_),
//refrtag("argv", local_env_->argv()),
refrtag("body", body));
} else {
return ppii.pps()->pretty_struct(ppii,
"LambdaExpr");
}
}
} /*namespace scm*/
} /*namespace xo*/
/* end DLambda.cpp */

View file

@ -8,13 +8,62 @@
#include <xo/indentlog/scope.hpp>
namespace xo {
using xo::facet::typeseq;
namespace scm {
DLocalSymtab::DLocalSymtab(size_type n) : capacity_{n}, size_{0}
{
for (size_type i = 0; i < n; ++i) {
void * mem = &slots_[i];
new (mem) Slot();
}
}
DLocalSymtab *
DLocalSymtab::_make_empty(obj<AAllocator> mm, size_type n)
{
void * mem = mm.alloc(typeseq::id<DLocalSymtab>(),
sizeof(DLocalSymtab) + (n * sizeof(Slot)));
return new (mem) DLocalSymtab(n);
}
Binding
DLocalSymtab::append_var(obj<AAllocator> mm,
const DUniqueString * name,
TypeRef typeref)
{
assert(name);
if (size_ >= capacity_ || !name) {
assert(false);
return Binding::null();
} else {
size_type i_slot = (this->size_)++;
Binding binding = Binding::local(i_slot);
DVariable * var = DVariable::make(mm, name, typeref, binding);
this->slots_[i_slot] = Slot(var);
return binding;
}
}
Binding
DLocalSymtab::lookup_binding(const DUniqueString * sym) const noexcept
{
scope log(XO_DEBUG(true), "stub impl");
log && log(xtag("sym", std::string_view(*sym)));
assert(sym);
if (sym) {
for (size_type i = 0; i < size_; ++i) {
const Slot & slot = slots_[i];
if (*sym == *(slot.var_->name()))
return slot.var_->path();
}
}
return Binding();
}

View file

@ -0,0 +1,45 @@
/** @file IExpression_DLambdaExpr.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IExpression_DLambdaExpr.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IExpression_DLambdaExpr.json5]
**/
#include "detail/IExpression_DLambdaExpr.hpp"
namespace xo {
namespace scm {
auto
IExpression_DLambdaExpr::extype(const DLambdaExpr & self) noexcept -> exprtype
{
return self.extype();
}
auto
IExpression_DLambdaExpr::typeref(const DLambdaExpr & self) noexcept -> TypeRef
{
return self.typeref();
}
auto
IExpression_DLambdaExpr::valuetype(const DLambdaExpr & self) noexcept -> TypeDescr
{
return self.valuetype();
}
auto
IExpression_DLambdaExpr::assign_valuetype(DLambdaExpr & self, TypeDescr td) noexcept -> void
{
self.assign_valuetype(td);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IExpression_DLambdaExpr.cpp */

View file

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

View file

@ -20,6 +20,9 @@
#include <xo/expression2/detail/IExpression_DApplyExpr.hpp>
#include <xo/expression2/detail/IPrintable_DApplyExpr.hpp>
#include <xo/expression2/detail/IExpression_DLambdaExpr.hpp>
#include <xo/expression2/detail/IPrintable_DLambdaExpr.hpp>
#include <xo/expression2/detail/IExpression_DIfElseExpr.hpp>
#include <xo/expression2/detail/IPrintable_DIfElseExpr.hpp>
@ -48,6 +51,7 @@ namespace xo {
// +- Variable
// +- DefineExpr
// +- ApplyExpr
// +- LambdaExpr
// \- IfElseExpr
FacetRegistry::register_impl<AExpression, DConstant>();
@ -62,6 +66,9 @@ namespace xo {
FacetRegistry::register_impl<AExpression, DApplyExpr>();
FacetRegistry::register_impl<APrintable, DApplyExpr>();
FacetRegistry::register_impl<AExpression, DLambdaExpr>();
FacetRegistry::register_impl<APrintable, DLambdaExpr>();
FacetRegistry::register_impl<AExpression, DIfElseExpr>();
FacetRegistry::register_impl<APrintable, DIfElseExpr>();
@ -70,6 +77,7 @@ namespace xo {
log && log(xtag("DVariable.tseq", typeseq::id<DVariable>()));
log && log(xtag("DConstant.tseq", typeseq::id<DConstant>()));
log && log(xtag("DApplyExpr.tseq", typeseq::id<DApplyExpr>()));
log && log(xtag("DLambdaExpr.tseq", typeseq::id<DLambdaExpr>()));
log && log(xtag("DIfElseExpr.tseq", typeseq::id<DIfElseExpr>()));
log && log(xtag("AExpression.tqseq", typeseq::id<AExpression>()));