xo-reader2 xo-expresion2: work on define-expressions [WIP]

This commit is contained in:
Roland Conybeare 2026-01-19 21:25:30 -05:00
commit 516b0932ee
40 changed files with 711 additions and 72 deletions

View file

@ -15,6 +15,7 @@ namespace xo {
static constexpr int32_t s_link_global = -1;
public:
Binding() : i_link_{-2}, j_slot_{-1} {}
Binding(int32_t i_link, int32_t j_slot)
: i_link_{i_link}, j_slot_{j_slot} {}
@ -32,6 +33,7 @@ namespace xo {
* >= 0: number of parent links to traverse
* to a fixed-size frame
* -1: resolve globally
* -2: sentinel (binding info not computed)
**/
int32_t i_link_ = s_link_sentinel;
/** if @ref i_link_ >= 0, frame offset

View file

@ -0,0 +1,75 @@
/** @file DDefineExpr.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "Expression.hpp"
#include "DVariable.hpp"
#include <xo/alloc2/Allocator.hpp>
namespace xo {
namespace scm {
class DUniqueString; // see DUniqueString.hpp
/** @class DDefineExpr
* @brief an expression that introduces a variable.
*
* Variable may optionally be declared with a type,
* and may come with an expression specifying an initial value
**/
class DDefineExpr {
public:
using AAllocator = xo::mm::AAllocator;
using TypeDescr = xo::reflect::TypeDescr;
public:
/** create instance: define-expr using memory from @p mm
* with lhs name @p lhs_name and rhs expression @p rhs_expr
**/
static DDefineExpr * make(obj<AAllocator> mm,
const DUniqueString * lhs_name,
obj<AExpression> rhs_expr);
/** create empty skeleton. Rely on this for parsing
**/
static DDefineExpr * make_empty(obj<AAllocator> mm);
DVariable * lhs() const { return lhs_var_; }
obj<AExpression> rhs() const noexcept { return rhs_; }
const DUniqueString * name() const noexcept;
void assign_lhs_name(const DUniqueString * name);
/** CONCESSION. will use DUniqueString* once we have StringTable **/
void assign_lhs_name(std::string_view name);
/** @defgroup scm-defineexpr-expression-facet **/
///@{
exprtype extype() const noexcept { return exprtype::define; }
TypeRef typeref() const noexcept { return lhs_var_->typeref(); }
TypeDescr valuetype() const noexcept { return lhs_var_->typeref().td(); }
void assign_valuetype(TypeDescr td) noexcept;
///@}
private:
DDefineExpr(DVariable * lhs_var,
obj<AExpression> rhs);
private:
/** variable being defined by this expression.
**/
DVariable * lhs_var_ = nullptr;
/** expression for initial value of this expression
**/
obj<AExpression> rhs_;
// std::set<std::string> free_var_set_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end DDefineExpr.hpp */

View file

@ -0,0 +1,39 @@
/** @file DGlobalSymtab.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "Binding.hpp"
namespace xo {
namespace scm {
class DUniqueString;
/** @class DGlobalSymtab
* @brief symbol table for toplevel environment
**/
struct DGlobalSymtab {
public:
public:
/** @defgroup xo-expression2-symboltable-facet symboltable facet**/
///@{
/** true for global symbol table **/
bool is_global_symtab() const noexcept { return true; }
/** lookup binding for variable @p sym **/
Binding lookup_binding(const DUniqueString * sym) const noexcept;
///@}
private:
};
} /*namespace scm*/
} /*namespace xo*/
/* end DGlobalSymtab.hpp */

View file

@ -19,15 +19,33 @@ namespace xo {
**/
class DVariable {
public:
using AAllocator = xo::mm::AAllocator;
using TypeDescr = xo::reflect::TypeDescr;
public:
DVariable(const DUniqueString & name, const TypeRef & typeref, Binding path)
: name_{name}, typeref_{typeref}, path_{path} {}
/** create instance
* @p mm memory allocator
* @p name variable name
* @p typeref type information for legal values
* (possibly just placeholder when relying on inference)
* @p path binding path to runtime value.
* This may be computed after parsing;
* mnust be resolved before execution.
**/
static DVariable * make(obj<AAllocator> mm,
const DUniqueString * name,
const TypeRef & typeref,
Binding path = Binding());
const DUniqueString & name() const { return name_; }
DVariable(const DUniqueString * name,
const TypeRef & typeref,
Binding path);
const DUniqueString * name() const { return name_; }
Binding path() const { return path_; }
void assign_name(const DUniqueString * name) { this->name_ = name; }
/** @defgroup scm-variable-expression-facet**/
///@{
@ -40,7 +58,7 @@ namespace xo {
private:
/** symbol name **/
const DUniqueString & name_;
const DUniqueString * name_;
/** variable value always has type consistent
* with this description
**/

View file

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

View file

@ -23,8 +23,10 @@ namespace xo {
#ifdef NOT_YET
/** a literal constant that refers to a linkable named function **/
primitive,
#endif
/** variable/function definition **/
define,
#ifdef NOT_YET
/** variable assignment **/
assign,
/** function call **/
@ -55,7 +57,9 @@ namespace xo {
case exprtype::constant: return "constant";
#ifdef NOT_YET
case exprtype::primitive: return "primitive";
#endif
case exprtype::define: return "define";
#ifdef NOT_YET
case exprtype::assign: return "assign";
case exprtype::apply: return "apply";
case exprtype::lambda: return "lambda";