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

@ -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 **/