xo-reader2 stack: top-level lambda w/ apply parses

This commit is contained in:
Roland Conybeare 2026-02-05 15:45:40 -05:00
commit 60b8fda134
26 changed files with 858 additions and 20 deletions

View file

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

View file

@ -5,14 +5,15 @@
#pragma once
#include <iostream>
#include <cstdint>
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*/

View file

@ -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<AAllocator> mm) const noexcept;
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
/** @defgroup scm-applyexpr-printable-facet **/

View file

@ -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<int32_t>(size_));

View file

@ -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<AAllocator> 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<AAllocator> mm) const noexcept;
size_t forward_children(obj<ACollector> 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 */

View file

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

View file

@ -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<xo::scm::AExpression,
xo::scm::DVarRef>
{
using ImplType = xo::scm::IExpression_Xfer
<xo::scm::DVarRef,
xo::scm::IExpression_DVarRef>;
};
}
}
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 */

View file

@ -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 <xo/gc/GCObject.hpp>
#include <xo/alloc2/Allocator.hpp>
#include "DApplyExpr.hpp"
namespace xo { namespace scm { class IGCObject_DApplyExpr; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DApplyExpr>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DApplyExpr,
xo::scm::IGCObject_DApplyExpr>;
};
}
}
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<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DApplyExpr & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -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 <xo/gc/GCObject.hpp>
#include <xo/alloc2/Allocator.hpp>
#include "DVarRef.hpp"
namespace xo { namespace scm { class IGCObject_DVarRef; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DVarRef>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DVarRef,
xo::scm::IGCObject_DVarRef>;
};
}
}
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<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DVarRef & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -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 <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DVarRef.hpp"
namespace xo { namespace scm { class IPrintable_DVarRef; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DVarRef>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DVarRef,
xo::scm::IPrintable_DVarRef>;
};
}
}
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 */

View file

@ -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???";