+ xo-interpreter2 scaffold for constant expression [WIP]

This commit is contained in:
Roland Conybeare 2026-01-13 01:40:48 -05:00
commit 47239f98e5
24 changed files with 453 additions and 32 deletions

View file

@ -106,6 +106,7 @@ add_subdirectory(xo-ordinaltree)
#
add_subdirectory(xo-tokenizer2) # schematika tokenizer (fomo)
add_subdirectory(xo-expression2) # schematika expressions (fomo)
add_subdirectory(xo-interpreter2) # schematika interpreter (fomo)
#
add_subdirectory(xo-webutil)
add_subdirectory(xo-pywebutil)

View file

@ -8,6 +8,7 @@ include(CMakeFindDependencyMacro)
#
find_dependency(xo_gc)
find_dependency(reflect)
find_dependency(xo_object2)
find_dependency(xo_printable2)
find_dependency(xo_flatstring)
find_dependency(indentlog)

View file

@ -1,6 +1,9 @@
{
mode: "facet",
includes: ["\"TypeRef.hpp\""],
includes: [ "\"TypeRef.hpp\"",
"\"exprtype.hpp\"",
"<xo/reflect/TypeDescr.hpp>"],
namespace1: "xo",
namespace2: "scm",
facet: "Expression",
@ -10,8 +13,24 @@
doc: [
"Representation for executable Schematika expressions"
],
types: [],
types: [
// using TypeDescr = xo::reflect::TypeDescr;
{
name: "TypeDescr",
doc: ["struct describing a type"],
definition: "xo::reflect::TypeDescr"
},
],
const_methods: [
{
name: "extype",
doc: ["expression type (constant | apply | ..)"],
return_type: "exprtype",
args: [],
const: true,
noexcept: true,
attributes: [],
},
{
name: "typeref",
doc: ["placeholder for type giving possible values for this expression"],

View file

@ -3,7 +3,10 @@
* @author Roland Conybeare, Jan 2026
**/
#include <xo/reflect/TypeDescr.hpp>
#include "TypeRef.hpp"
#include "exprtype.hpp"
#include <xo/reflect/TaggedPtr.hpp>
#include <xo/gc/GCObject.hpp>
namespace xo {
namespace scm {
@ -12,24 +15,36 @@ namespace xo {
**/
struct DConstant {
public:
using TaggedPtr = xo::reflect::TaggedPtr;
using TypeDescr = xo::reflect::TypeDescr;
using AGCObject = xo::mm::AGCObject;
using typeseq = xo::reflect::typeseq;
public:
DConstant(TypeDescr td, void * value);
explicit DConstant(obj<AGCObject> value) noexcept;
TypeDescr value_td() const { return value_td; }
bool is_resolved() const noexcept { return typeref_.is_resolved(); }
TypeRef typeref() const noexcept;
TypeDescr valuetype() const noexcept;
void assign_valuetype(TypeDescr td) noexcept;
exprtype extype() const noexcept { return exprtype::constant; }
TypeDescr value_td() const noexcept { return typeref_.td(); }
TaggedPtr value_tp() const noexcept { return TaggedPtr(typeref_.td(), value_.data()); }
TypeRef typeref() const noexcept { return typeref_; }
TypeDescr valuetype() const noexcept { return typeref_.td(); }
obj<AGCObject> value() const noexcept { return value_; }
void assign_valuetype(TypeDescr td) noexcept { typeref_.resolve(td); }
private:
/** type for value of this expression **/
TypeRef type_ref_;
/** type description for destination *value_ **/
TypeDescr valuetype_;
static TypeDescr _lookup_td(typeseq tseq);
private:
/** type for value of this expression
* or unification breadcrumb before unification
**/
TypeRef typeref_;
/** literal value **/
void * value_;
obj<AGCObject> value_;
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -2,7 +2,7 @@
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Expression.json5]
* 2. jinja2 template for facet .hpp file:

View file

@ -26,6 +26,11 @@ namespace xo {
TypeRef() = default;
TypeRef(const type_var & id, TypeDescr td);
/** trivial typeref, where already resolved.
* Require: @p td non-null
**/
static TypeRef resolved(TypeDescr td);
/** if @p td is non-null
* -> type is already resolved
*
@ -43,7 +48,10 @@ namespace xo {
TypeDescr td() const noexcept { return td_; }
/** true iff type at this location has been resolved **/
bool is_concrete() const noexcept;
bool is_resolved() const noexcept;
/** resolve TypeRef by supplying final type-description **/
void resolve(TypeDescr td) noexcept { td_ = td; }
private:
/** unique (probably generated) name for type at this location **/

View file

@ -2,7 +2,7 @@
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Expression.json5]
* 2. jinja2 template for abstract facet .hpp file:
@ -15,6 +15,8 @@
// includes (via {facet_includes})
#include "TypeRef.hpp"
#include "exprtype.hpp"
#include <xo/reflect/TypeDescr.hpp>
#include <xo/facet/obj.hpp>
#include <xo/facet/facet_implementation.hpp>
#include <xo/facet/typeseq.hpp>
@ -35,6 +37,8 @@ public:
// types
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
/** struct describing a type **/
using TypeDescr = xo::reflect::TypeDescr;
///@}
/** @defgroup scm-expression-methods **/
@ -42,6 +46,8 @@ public:
// const methods
/** RTTI: unique id# for actual runtime data representation **/
virtual typeseq _typeseq() const noexcept = 0;
/** expression type (constant | apply | ..) **/
virtual exprtype extype(Copaque data) const noexcept = 0;
/** placeholder for type giving possible values for this expression **/
virtual TypeRef typeref(Copaque data) const noexcept = 0;
/** type giving possible values for this expression. Maybe null before typecheck **/

View file

@ -2,7 +2,7 @@
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Expression.json5]
* 2. jinja2 template for abstract facet .hpp file:
@ -44,6 +44,7 @@ namespace scm {
/** integer identifying a type **/
using typeseq = xo::facet::typeseq;
using TypeDescr = AExpression::TypeDescr;
///@}
/** @defgroup scm-expression-any-methods **/
@ -55,6 +56,7 @@ namespace scm {
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
[[noreturn]] exprtype extype(Copaque) const noexcept override { _fatal(); }
[[noreturn]] TypeRef typeref(Copaque) const noexcept override { _fatal(); }
[[noreturn]] TypeDescr valuetype(Copaque) const noexcept override { _fatal(); }

View file

@ -2,7 +2,7 @@
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Expression.json5]
* 2. jinja2 template for abstract facet .hpp file:
@ -14,6 +14,8 @@
#pragma once
#include "TypeRef.hpp"
#include "exprtype.hpp"
#include <xo/reflect/TypeDescr.hpp>
namespace xo {
namespace scm {
@ -28,6 +30,7 @@ namespace scm {
using Impl = IExpression_DRepr;
/** integer identifying a type **/
using typeseq = AExpression::typeseq;
using TypeDescr = AExpression::TypeDescr;
///@}
/** @defgroup scm-expression-xfer-methods **/
@ -40,6 +43,9 @@ namespace scm {
// const methods
typeseq _typeseq() const noexcept override { return s_typeseq; }
exprtype extype(Copaque data) const noexcept override {
return I::extype(_dcast(data));
}
TypeRef typeref(Copaque data) const noexcept override {
return I::typeref(_dcast(data));
}

View file

@ -2,7 +2,7 @@
*
* Generated automagically from ingredients:
* 1. code generator:
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
* arguments:
* --input [idl/Expression.json5]
* 2. jinja2 template for abstract facet .hpp file:
@ -31,6 +31,7 @@ public:
using ObjectType = Object;
using DataPtr = Object::DataPtr;
using typeseq = xo::reflect::typeseq;
using TypeDescr = AExpression::TypeDescr;
///@}
/** @defgroup scm-expression-router-ctors **/
@ -44,10 +45,13 @@ public:
// const methods
typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); }
TypeRef typeref() const noexcept override {
exprtype extype() const noexcept {
return O::iface()->extype(O::data());
}
TypeRef typeref() const noexcept {
return O::iface()->typeref(O::data());
}
TypeDescr valuetype() const noexcept override {
TypeDescr valuetype() const noexcept {
return O::iface()->valuetype(O::data());
}

View file

@ -0,0 +1,84 @@
/** @file exprtype.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include <ostream>
//#include <cstdint>
namespace xo {
namespace scm {
/** @enum exprtype
* @brief enum to identify subclasses of xo::scm::Expression.
*
**/
enum class exprtype {
/** sentinel value **/
invalid = -1,
/** literal constant. must satisfy both standard_layout_type + trivial **/
constant,
#ifdef NOT_YET
/** a literal constant that refers to a linkable named function **/
primitive,
/** variable/function definition **/
define,
/** variable assignment **/
assign,
/** function call **/
apply,
/** function definition **/
lambda,
/** variable reference **/
variable,
/** if-then-else **/
ifexpr,
/** sequence **/
sequence,
/** type conversion **/
convert,
#endif
/** not an expression. comes last, counts entries **/
N
};
inline const char *
expr2str(exprtype x)
{
switch(x) {
case exprtype::invalid: return "?exprtype";
case exprtype::constant: return "constant";
#ifdef NOT_YET
case exprtype::primitive: return "primitive";
case exprtype::define: return "define";
case exprtype::assign: return "assign";
case exprtype::apply: return "apply";
case exprtype::lambda: return "lambda";
case exprtype::variable: return "variable";
case exprtype::ifexpr: return "if_expr";
case exprtype::sequence: return "sequence";
case exprtype::convert: return "convert";
#endif
default: break;
}
return "???exprtype???";
}
/** @brief number of built-in expression types, repr convenient for array sizing **/
static constexpr std::size_t n_exprtype = static_cast<std::size_t>(exprtype::N);
inline std::ostream &
operator<<(std::ostream & os,
exprtype x)
{
os << expr2str(x);
return os;
}
} /*namespace scm*/
} /*namespace xo*/
/* end exprtype.hpp */

View file

@ -2,6 +2,7 @@
set(SELF_LIB xo_expression2)
set(SELF_SRCS
DConstant.cpp
TypeRef.cpp
#IExpression_Any.cpp
expression2_register_facets.cpp
@ -11,6 +12,7 @@ xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 $
# note: deps here must also appear in cmake/xo_expression2Config.cmake.in
xo_dependency(${SELF_LIB} xo_gc)
xo_dependency(${SELF_LIB} reflect)
xo_dependency(${SELF_LIB} xo_object2)
xo_dependency(${SELF_LIB} xo_printable2)
xo_dependency(${SELF_LIB} xo_flatstring)
xo_dependency(${SELF_LIB} indentlog)

View file

@ -0,0 +1,53 @@
/** @file DConstant.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "DConstant.hpp"
#include "TypeDescr.hpp"
#include <xo/object2/DFloat.hpp>
#include <xo/object2/DInteger.hpp>
#include <xo/reflect/Reflect.hpp>
#include <xo/reflectutil/typeseq.hpp>
namespace xo {
using xo::scm::DFloat;
using xo::scm::DInteger;
using xo::reflect::Reflect;
using xo::reflect::TypeDescr;
using xo::reflect::typeseq;
namespace scm {
DConstant::DConstant(obj<AGCObject> value) noexcept
:
//typeref_{TypeRef::resolved(td)},
value_{value}
{
// todo: use ObjectConverter here
auto tseq = value_._typeseq();
TypeDescr td = this->_lookup_td(tseq);
if (td) {
typeref_ = TypeRef::resolved(td);
}
}
TypeDescr
DConstant::_lookup_td(typeseq tseq)
{
if (tseq == typeseq::id<DFloat>()) {
/* double */
return Reflect::require<DFloat::value_type>();
} else if (tseq == typeseq::id<DInteger>()) {
/* long */
return Reflect::require<DInteger::value_type>();
}
return nullptr;
}
} /*namespace scm*/
} /*namespace xo*/
/* end DConstant.cpp */

View file

@ -11,6 +11,15 @@ namespace xo {
: id_{id}, td_{td}
{}
TypeRef
TypeRef::resolved(TypeDescr td)
{
assert(td);
type_var null;
return TypeRef(null, td);
}
TypeRef
TypeRef::dwim(prefix_type prefix, TypeDescr td)
{
@ -18,8 +27,7 @@ namespace xo {
/* type already resolved
* -> we don't need a type variable name
*/
type_var null;
return TypeRef(null, td);
return TypeRef::resolved(td);
} else {
/* type is not resolved yet.
* -> give it a unique name,
@ -49,7 +57,7 @@ namespace xo {
}
bool
TypeRef::is_concrete() const noexcept
TypeRef::is_resolved() const noexcept
{
return (td_ != nullptr);
}

View file

@ -56,7 +56,7 @@ public:
// const methods
typeseq _typeseq() const noexcept { return O::iface()->_typeseq(); }
{% for md in const_methods %}
{{md.return_type}} {{md.name}}({{md.args | argsnodata}}) {{md | qualifiers}} override {
{{md.return_type}} {{md.name}}({{md.args | argsnodata}}) {{md | qualifiers}} {
return O::iface()->{{md.name}}({{md.args | argrouting}});
}
{% endfor %}

View file

@ -20,14 +20,9 @@ add_definitions(${PROJECT_CXX_FLAGS})
# ----------------------------------------------------------------
# output targets
add_subdirectory(src/interpreter2)
#add_subdirectory(utest)
# ----------------------------------------------------------------
# header-only library
set(SELF_LIB xo_interpreter2)
xo_add_headeronly_library(${SELF_LIB})
xo_install_library4(${SELF_LIB} ${PROJECT_NAME}Targets)
xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets)
# ----------------------------------------------------------------

View file

@ -6,7 +6,8 @@ include(CMakeFindDependencyMacro)
# must coordinate with xo_dependency() calls
# in CMakeLists.txt
#
#find_dependency(xo_flatstring)
find_dependency(xo_expression2)
find_dependency(xo_gc)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

View file

@ -0,0 +1,78 @@
/** @file VirtualSchematikaMachine.hpp
*
* @author Roland Conybare, Jan 2026
**/
#pragma once
#include "VsmInstr.hpp"
#include <xo/expression2/Expression.hpp>
#include <xo/gc/GCObject.hpp>
namespace xo {
namespace scm {
/** @class VirtualSchematikaMachine
* @brief virtual machine for schematika
**/
class VirtualSchematikaMachine {
public:
// will be DArenaVector<obj<StackFrame>> probably
using Stack = void *;
using AGCObject = xo::mm::AGCObject;
public:
VirtualSchematikaMachine();
/** borrow calling thread to run indefinitely,
* until halt instruction
**/
void run();
/** execute vsm instruction in @ref pc_.
* @retval instruction count. 1 unless pc_ is halt.
**/
bool execute_one();
private:
/** Require:
* - expression in @ref expr_
**/
void _do_eval_op();
/** evaluate a constant expression
* Require:
* - expression in @ref expr_
**/
void _do_eval_constant_op();
private:
/*
* Some registers are preserved by evaluation:
* stack_
* cont_
*
* Other registers are not preserved
* pc_
* expr_
* value_
*/
/** program counter **/
VsmInstr pc_ = VsmInstr::halt();
/** stack pointer **/
Stack stack_;
/** expression register **/
obj<AExpression> expr_;
/** result register **/
obj<AGCObject> value_;
/** continuation register **/
VsmInstr cont_ = VsmInstr::halt();
};
} /*namespace scm*/
} /*namespace xo*/
/* end VirtualSchematikaMachine.hpp */

View file

@ -0,0 +1,27 @@
/** @file VsmInstr.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "VsmOpcode.hpp"
namespace xo {
namespace scm {
class VsmInstr {
public:
explicit VsmInstr(vsm_opcode oc) : opcode_{oc} {}
static VsmInstr halt() { return VsmInstr{vsm_opcode::halt}; }
static VsmInstr eval() { return VsmInstr{vsm_opcode::eval}; }
vsm_opcode opcode() const noexcept { return opcode_; }
private:
vsm_opcode opcode_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end VsmInstr.hpp */

View file

@ -0,0 +1,29 @@
/** @file VsmOpcode.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include <cstdint>
namespace xo {
namespace scm {
/** Opcode for a virtual schematika expression;
* exeucted by VirtualSchematikaMachine
**/
enum class vsm_opcode {
/** Immediately halt virtual schematika machine. **/
halt,
/** Evaluate expression in expr register **/
eval,
/** sentinel, counts number of opcodes **/
N,
};
static constexpr uint32_t n_opcode = static_cast<uint32_t>(vsm_opcode::N);
} /*namespace scm*/
} /*namespace xo*/
/* end VsmOpcode.hpp */

View file

@ -0,0 +1,17 @@
# interpreter2/CMakeLists.txt
set(SELF_LIB xo_interpreter2)
set(SELF_SRCS
VirtualSchematikaMachine.cpp
#IExpression_Any.cpp
#interpreter2_register_facets.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
# note: deps here must also appear in cmake/xo_interpreter2Config.cmake.in
xo_dependency(${SELF_LIB} xo_expression2)
xo_dependency(${SELF_LIB} xo_gc)
#xo_dependency(${SELF_LIB} reflect)
#xo_dependency(${SELF_LIB} xo_printable2)
#xo_dependency(${SELF_LIB} xo_flatstring)
#xo_dependency(${SELF_LIB} indentlog)

View file

@ -0,0 +1,63 @@
/** @file VirtualSchematikaMachine.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "VirtualSchematikaMachine.hpp"
#include <xo/expression2/DConstant.hpp>
#include <cassert>
namespace xo {
namespace scm {
VirtualSchematikaMachine::VirtualSchematikaMachine()
{}
void
VirtualSchematikaMachine::run()
{
while (this->execute_one())
;
}
bool
VirtualSchematikaMachine::execute_one()
{
switch (pc_.opcode()) {
case vsm_opcode::halt:
case vsm_opcode::N:
return false;
case vsm_opcode::eval:
_do_eval_op();
}
return true;
}
void
VirtualSchematikaMachine::_do_eval_op()
{
switch(expr_.extype()) {
case exprtype::invalid:
case exprtype::N:
break;
case exprtype::constant:
_do_eval_constant_op();
break;
}
}
void
VirtualSchematikaMachine::_do_eval_constant_op()
{
auto expr
= obj<AExpression,DConstant>::from(expr_);
this->value_ = expr.data()->value();
this->pc_ = this->cont_;
}
} /*namespace scm*/
} /*namespace xo*/
/* end VirtualSchematikaMachine.hpp */

View file

@ -14,6 +14,7 @@ namespace xo {
struct DFloat {
using AAllocator = xo::mm::AAllocator;
using ppindentinfo = xo::print::ppindentinfo;
using value_type = double;
explicit DFloat(double x) : value_{x} {}

View file

@ -15,6 +15,7 @@ namespace xo {
struct DInteger {
using AAllocator = xo::mm::AAllocator;
using ppindentinfo = xo::print::ppindentinfo;
using value_type = long;
explicit DInteger(long x) : value_{x} {}