xo-reader2 stack: + xo-numeric + setup multi dispatch for *,/

This commit is contained in:
Roland Conybeare 2026-02-18 21:47:02 -08:00
commit d6bf210714
37 changed files with 1057 additions and 83 deletions

View file

@ -99,11 +99,11 @@ add_subdirectory(xo-callback)
add_subdirectory(xo-printable2) # experiment w/ facet object model
add_subdirectory(xo-alloc)
add_subdirectory(xo-alloc2) # experiment w/ facet object model
add_subdirectory(xo-numeric) # experiment w/ facet object model
add_subdirectory(xo-gc)
add_subdirectory(xo-object)
add_subdirectory(xo-object2) # experiment w/ facet object model
add_subdirectory(xo-procedure2) # schematika procedure abstraction + runtime context (fomo)
add_subdirectory(xo-numeric) # experiment w/ facet object model
add_subdirectory(xo-ordinaltree)
#
add_subdirectory(xo-tokenizer2) # schematika tokenizer (fomo)

View file

@ -50,7 +50,8 @@ namespace xo {
// combine the two seqno values
std::size_t h1 = std::hash<int32_t>{}(k.first.seqno());
std::size_t h2 = std::hash<int32_t>{}(k.second.seqno());
return h1 ^ (h2 << 1);
return h1 ^ (h2 << 3);
}
};

View file

@ -9,6 +9,9 @@
#include <xo/facet/FacetRegistry.hpp>
#include <replxx.hxx>
#include <iostream>
#ifdef __APPLE__
#include <unistd.h> // for STDIN_FILENO on OSX
#endif
namespace xo {
using xo::scm::VirtualSchematikaMachine;
@ -239,4 +242,3 @@ main (int argc, char * argv[])
} /*main*/
/* end skreplxx.cpp */

View file

@ -73,8 +73,8 @@ namespace xo {
};
aux_mm_.arena_.visit_pools(visitor);
FacetRegistry::instance().visit_pools(visitor);
TypeRegistry::instance().visit_pools(visitor);
FacetRegistry::instance().visit_pools(visitor);
vsm_.visit_pools(visitor);
return true;
@ -189,6 +189,37 @@ namespace xo {
log && vsm_fixture.log_memory_layout(&log);
}
TEST_CASE("VirtualSchematikaMachine-arith2", "[interpreter2][VSM]")
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
VsmFixture vsm_fixture(testname, c_debug_flag);
auto & vsm = vsm_fixture.vsm_;
bool eof_flag = false;
vsm.begin_interactive_session();
VsmResultExt res = vsm.read_eval_print(span_type::from_cstr("3.14159265 / 0.5;"), eof_flag);
REQUIRE(res.is_value());
REQUIRE(res.value());
log && log(xtag("res.tseq", res.value()->_typeseq()));
auto x = obj<AGCObject,DFloat>::from(*res.value());
REQUIRE(x);
REQUIRE(x.data()->value() == 6.2831853);
REQUIRE(res.remaining_.size() == 1);
REQUIRE(*res.remaining_.lo() == '\n');
log && vsm_fixture.log_memory_layout(&log);
}
TEST_CASE("VirtualSchematikaMachine-cmp1", "[interpreter2][VSM]")
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();

View file

@ -29,14 +29,32 @@ xo_add_genfacet(
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-numeric-facetimpl-numeric-float
FACET_PKG xo_numeric
FACET Numeric
REPR Float
INPUT idl/INumeric_DFloat.json5
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-numeric-facetimpl-numeric-integer
FACET_PKG xo_numeric
FACET Numeric
REPR Integer
INPUT idl/INumeric_DInteger.json5
)
# ----------------------------------------------------------------
xo_add_genfacet_all(xo-numeric-genfacet-all)
# ----------------------------------------------------------------
#add_subdirectory(src/numeric) # NOTE: will need cmake export here
#add_subdirectory(utest)
add_subdirectory(src/numeric)
add_subdirectory(utest)
# ----------------------------------------------------------------
# cmake export

View file

@ -1,44 +0,0 @@
/** @file NumericDispatch.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
//#include <xo/facet/FacetRegistry.hpp> // probably will need this at some point?
#include <xo/arena/DArenaHashMap.hpp>
#include <utility>
namespace xo {
namespace scm {
/** type-erased arithmetic dispatch.
**/
struct NumericOps {
obj<ANumeric>
};
class NumericDispatch {
public:
using KeyType = std::pair<typeseq, typeseq>;
using MapType = xo::map::DArenaHashMap<KeyType, MappedType, KeyHash>
/** combine two typeseq's to get hash value **/
struct KeyHash {
std::size_t operator()(const key_type & k) const noexcept {
// combine the two seqno values
std::size_t h1 = std::hash<int32_t>{}(k.first.seqno());
std::size_t h2 = std::hash<int32_t>{}(k.second.seqno());
return h1 ^ (h2 << 1);
}
};
};
} /*namespace scm*/
} /*namespace xo*/
/* end NumericDispqatch.hpp */

View file

@ -7,7 +7,7 @@ include(CMakeFindDependencyMacro)
# in CMakeLists.txt
#
#find_dependency(xo_gc)
find_dependency(xo_facet)
find_dependency(xo_procedure2)
find_dependency(subsys)
#find_dependency(indentlog)

View file

@ -0,0 +1,18 @@
{
mode: "implementation",
output_cpp_dir: "src/numeric",
output_hpp_dir: "include/xo/numeric",
output_impl_subdir: "float",
includes: [
// "<xo/printable2/Printable.hpp>",
// "<xo/printable2/detail/IPrintable_Xfer.hpp>"
],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Numeric.json5",
brief: "provide ANumeric interface for DFloat",
using_doxygen: true,
repr: "DFloat",
doc: [ "implement ANumeric for DFloat" ],
}

View file

@ -0,0 +1,18 @@
{
mode: "implementation",
output_cpp_dir: "src/numeric",
output_hpp_dir: "include/xo/numeric",
output_impl_subdir: "integer",
includes: [
// "<xo/printable2/Printable.hpp>",
// "<xo/printable2/detail/IPrintable_Xfer.hpp>"
],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Numeric.json5",
brief: "provide ANumeric interface for DInteger",
using_doxygen: true,
repr: "DInteger",
doc: [ "implement ANumeric for DInteger" ],
}

View file

@ -0,0 +1,42 @@
/** @file FloatIntegerOps.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "Numeric.hpp"
#include <xo/procedure2/RuntimeContext.hpp>
#include <xo/object2/Float.hpp>
#include <xo/object2/Integer.hpp>
namespace xo {
namespace scm {
class FloatIntegerOps {
public:
using AGCObject = xo::mm::AGCObject;
public:
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
};
class IntegerFloatOps {
public:
using AGCObject = xo::mm::AGCObject;
public:
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
};
}
}
/* end FloatIntegerOps.hpp */

View file

@ -0,0 +1,30 @@
/** @file FloatOps.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "Numeric.hpp"
#include <xo/procedure2/RuntimeContext.hpp>
#include <xo/object2/Float.hpp>
namespace xo {
namespace scm {
class FloatOps {
public:
using AGCObject = xo::mm::AGCObject;
public:
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
};
}
}
/* end FloatOps.hpp */

View file

@ -0,0 +1,31 @@
/** @file IntegerOps.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "Numeric.hpp"
#include <xo/procedure2/RuntimeContext.hpp>
#include <xo/object2/Integer.hpp>
namespace xo {
namespace scm {
class IntegerOps {
public:
using AGCObject = xo::mm::AGCObject;
public:
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
};
}
}
/* end IntegerOps.hpp */

View file

@ -5,13 +5,93 @@
#pragma once
#include "NumericOps.hpp"
#include <xo/procedure2/RuntimeContext.hpp>
#include <xo/gc/GCObject.hpp>
#include <xo/arena/DArenaHashMap.hpp>
#include <xo/reflectutil/typeseq.hpp>
namespace xo {
namespace scm {
/** Runtime polymoprhic multimethod dispatch.
* Hash on argument types to get function table
**/
class NumericDispatch {
public:
//using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
using MemorySizeVisitor = xo::mm::MemorySizeVisitor;
using typeseq = xo::reflect::typeseq;
using KeyType = std::pair<typeseq, typeseq>;
using MappedType = AnonymizedNumericOps;
/** hash function for key_type **/
struct KeyHash {
std::size_t operator()(const KeyType & k) const noexcept {
// combine the two seqno values
std::size_t h1 = std::hash<int32_t>{}(k.first.seqno());
std::size_t h2 = std::hash<int32_t>{}(k.second.seqno());
return h1 ^ (h2 << 7);
}
};
using MapType = xo::map::DArenaHashMap<KeyType,
MappedType,
KeyHash,
std::equal_to<void>>;
public:
NumericDispatch(uint32_t hint_max_capacity)
: dispatch_{"numeric-dispatch",
hint_max_capacity,
false /*!debug_flag*/} {}
/** @p hint_max_capacity only honored the first time instance()
* is called.
**/
static NumericDispatch & instance(uint32_t hint_max_capacity = 64) {
static NumericDispatch s_instance(hint_max_capacity);
return s_instance;
}
/** multiply w/ runtime polymorphism (double-dispatch)
**/
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y);
/** divide w/ runtime polymorphism (double-dispatch)
**/
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y);
/** report memory use for owned arenas to @p visitor **/
void visit_pools(const MemorySizeVisitor & visitor);
/** Use:
* Need to have overload
* multiply(obj<AAllocator>, DRepr1, DRepr2) -> obj<ANumeric>
* available
**/
template <typename DRepr1, typename DRepr2>
void register_impl(typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl mul_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl div_fn) {
KeyType key(typeseq::id<DRepr1>().seqno(),
typeseq::id<DRepr2>().seqno());
// note: copying op table so they're in proximity
this->dispatch_[key] = NumericOps<DRepr1, DRepr2>::make(mul_fn, div_fn);
}
private:
/** 2d dispatch for arithmetic **/
MapType dispatch_;
};
} /*namespace scm*/

View file

@ -1,44 +1,52 @@
/** @file NumericOps.hpp
*
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include "Numeric.hpp"
#include <xo/numeric/Numeric.hpp>
#include <xo/procedure2/RuntimeContext.hpp>
#include <xo/gc/GCObject.hpp>
#include <xo/facet/obj.hpp>
namespace xo {
namespace scm {
class INumericOps {
class AnonymizedNumericOps {
public:
using BinaryOp1 = obj<ANumeric> (*)(obj<AAllocator> mm, void * x, void * y);
using ARuntimeContext = xo::scm::ARuntimeContext;
using AGCObject = xo::mm::AGCObject;
using BinaryOp = obj<AGCObject> (*)(obj<ARuntimeContext> mm, void * x, void * y);
public:
explicit INumericOp9s(BinaryOp1 multiply) : multiply_{multiply} {}
/** note: null ctor load-bearing for membership in DArenaHashTable **/
AnonymizedNumericOps() = default;
/** @p multiply to multiply (x,y); allocate from mm **/
explicit AnonymizedNumericOps(BinaryOp multiply,
BinaryOp divide)
: multiply_{multiply}, divide_{divide} {}
/** multiply (x,y); allocate from mm **/
BinaryOp1 multiply_;
BinaryOp multiply_ = nullptr;
BinaryOp divide_ = nullptr;
};
/** Convenience template. To use, provide implementation
* for
* _multiply() ...
*
**/
template <typename DRepr1, typename DRepr2>
class NumericOps : public INumericOps {
class NumericOps {
public:
using BinaryOp1_Impl = obj<ANumeric> (*)(obj<AAllocator> mm, DRepr1 * x, DRepr2 * y);
using ARuntimeContext = xo::scm::ARuntimeContext;
using AGCObject = xo::mm::AGCObject;
using BinaryOp_Impl = obj<AGCObject> (*)(obj<ARuntimeContext> rcx, DRepr1 * x, DRepr2 * y);
using BinaryOp_Anon = AnonymizedNumericOps::BinaryOp;
public:
explicit NumericOps(BinaryOp1_Impl multiply)
: INumericOps(reinterpret_cast<INumericOps::BinaryOp1>(multiply))
{}
static AnonymizedNumericOps make(BinaryOp_Impl multiply,
BinaryOp_Impl divide) {
return AnonymizedNumericOps(reinterpret_cast<BinaryOp_Anon>(multiply),
reinterpret_cast<BinaryOp_Anon>(divide));
}
};
} /*namespace scm*/
} /*namespace xo*/
/* end NumericOps.hpp */

View file

@ -0,0 +1,23 @@
/** @file NumericDispatch.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include <xo/procedure2/DPrimitive_gco_2_gco_gco.hpp>
namespace xo {
namespace scm {
/** @brief primitives using multidispatch
**/
class NumericPrimitives {
public:
/** poly divide **/
static DPrimitive_gco_2_gco_gco s_div_gco_gco_pm;
};
}
}
/* end NumericDispatch.hpp */

View file

@ -0,0 +1,56 @@
/** @file INumeric_DFloat.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/INumeric_DFloat.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/INumeric_DFloat.json5]
**/
#pragma once
#include "Numeric.hpp"
#include "DFloat.hpp"
namespace xo { namespace scm { class INumeric_DFloat; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::ANumeric,
xo::scm::DFloat>
{
using ImplType = xo::scm::INumeric_Xfer
<xo::scm::DFloat,
xo::scm::INumeric_DFloat>;
};
}
}
namespace xo {
namespace scm {
/** @class INumeric_DFloat
**/
class INumeric_DFloat {
public:
/** @defgroup scm-numeric-dfloat-type-traits **/
///@{
using Copaque = xo::scm::ANumeric::Copaque;
using Opaque = xo::scm::ANumeric::Opaque;
///@}
/** @defgroup scm-numeric-dfloat-methods **/
///@{
// const methods
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,21 @@
/** @file init_numeric.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
#include <xo/subsys/Subsystem.hpp>
namespace xo {
/* tag to represent the xo-numeric/ subsystem within ordered initialization */
enum S_numeric_tag {};
template <>
struct InitSubsys<S_numeric_tag> {
static void init();
static InitEvidence require();
};
}
/* end init_numeric.hpp */

View file

@ -0,0 +1,56 @@
/** @file INumeric_DInteger.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/INumeric_DInteger.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/INumeric_DInteger.json5]
**/
#pragma once
#include "Numeric.hpp"
#include "DInteger.hpp"
namespace xo { namespace scm { class INumeric_DInteger; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::ANumeric,
xo::scm::DInteger>
{
using ImplType = xo::scm::INumeric_Xfer
<xo::scm::DInteger,
xo::scm::INumeric_DInteger>;
};
}
}
namespace xo {
namespace scm {
/** @class INumeric_DInteger
**/
class INumeric_DInteger {
public:
/** @defgroup scm-numeric-dinteger-type-traits **/
///@{
using Copaque = xo::scm::ANumeric::Copaque;
using Opaque = xo::scm::ANumeric::Opaque;
///@}
/** @defgroup scm-numeric-dinteger-methods **/
///@{
// const methods
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,16 @@
/** @file numeric_register_facets.hpp
*
* @author Roland Conybeare, Feb 2026
**/
#pragma once
namespace xo {
namespace scm {
/** Setup numeric facet dispatch **/
bool numeric_register_facets();
}
}
/* end numeric_register_facets.hpp */

View file

@ -0,0 +1,25 @@
# numeric/CMakeLists.txt
set(SELF_LIB xo_numeric)
set(SELF_SRCS
init_numeric.cpp
numeric_register_facets.cpp
NumericPrimitives.cpp
NumericDispatch.cpp
INumeric_Any.cpp
FloatIntegerOps.cpp
FloatOps.cpp
IntegerOps.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
xo_dependency(${SELF_LIB} xo_procedure2)
xo_dependency(${SELF_LIB} subsys)
xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets)
# ----------------------------------------------------------------
# docs targets depend on other library/utest/exec targets above,
# --> must come after them.
#
#add_subdirectory(docs)

View file

@ -0,0 +1,48 @@
/** @file FloatIntegerOps.cp
*
* @author Roland Conybeare, Feb 2206
**/
#include "FloatIntegerOps.hpp"
#include "float/INumeric_DFloat.hpp"
namespace xo {
using xo::mm::AGCObject;
namespace scm {
// ----- Float op Integer -----
obj<AGCObject>
FloatIntegerOps::multiply(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() * y->value());
}
obj<AGCObject>
FloatIntegerOps::divide(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
// ----- Integer op Float -----
obj<AGCObject>
IntegerFloatOps::multiply(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() * y->value());
}
obj<AGCObject>
IntegerFloatOps::divide(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
}
}
/* end FloatIntegerOps.cpp */

View file

@ -0,0 +1,31 @@
/** @file FloatOps.cp
*
* @author Roland Conybeare, Feb 2206
**/
#include "FloatOps.hpp"
#include "float/INumeric_DFloat.hpp"
namespace xo {
using xo::mm::AGCObject;
namespace scm {
obj<AGCObject>
FloatOps::multiply(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() * y->value());
}
obj<AGCObject>
FloatOps::divide(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
}
}
/* end FloatOps.cpp */

View file

@ -0,0 +1,22 @@
/** @file INumeric_DFloat.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/INumeric_DFloat.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/INumeric_DFloat.json5]
**/
#include "float/INumeric_DFloat.hpp"
namespace xo {
namespace scm {
} /*namespace scm*/
} /*namespace xo*/
/* end INumeric_DFloat.cpp */

View file

@ -0,0 +1,22 @@
/** @file INumeric_DInteger.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/INumeric_DInteger.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/INumeric_DInteger.json5]
**/
#include "integer/INumeric_DInteger.hpp"
namespace xo {
namespace scm {
} /*namespace scm*/
} /*namespace xo*/
/* end INumeric_DInteger.cpp */

View file

@ -0,0 +1,31 @@
/** @file IntegerOps.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "IntegerOps.hpp"
#include "integer/INumeric_DInteger.hpp"
namespace xo {
using xo::mm::AGCObject;
namespace scm {
obj<AGCObject>
IntegerOps::multiply(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y)
{
return DInteger::box<AGCObject>(rcx.allocator(), x->value() * y->value());
}
obj<AGCObject>
IntegerOps::divide(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y)
{
return DInteger::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
}
}
/* end IntegerOps.cpp */

View file

@ -0,0 +1,59 @@
/** @file NumericDispatch.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "NumericDispatch.hpp"
#include <xo/indentlog/scope.hpp>
namespace xo {
using xo::mm::AGCObject;
namespace scm {
void
NumericDispatch::visit_pools(const MemorySizeVisitor & visitor)
{
dispatch_.visit_pools(visitor);
}
obj<AGCObject>
NumericDispatch::multiply(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y)
{
KeyType key(x._typeseq(), y._typeseq());
auto target_fn
= NumericDispatch::instance().dispatch_[key].multiply_;
return (*target_fn)(rcx, x.data(), y.data());
}
obj<AGCObject>
NumericDispatch::divide(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y)
{
scope log(XO_DEBUG(true));
KeyType key(x._typeseq(), y._typeseq());
log && log(xtag("x.tseq", x._typeseq().seqno()),
xtag("y.tseq", y._typeseq().seqno()));
auto target_fn
= NumericDispatch::instance().dispatch_[key].divide_;
log && log(xtag("target_fn", target_fn));
assert(target_fn);
return (*target_fn)(rcx, x.data(), y.data());
}
} /*namespace scm*/
} /*namespace xo*/
/* end NumericDispatch.cpp **/

View file

@ -0,0 +1,22 @@
/** @file NumericPrimitives.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "NumericPrimitives.hpp"
#include "NumericDispatch.hpp"
namespace xo {
using xo::mm::AGCObject;
namespace scm {
DPrimitive_gco_2_gco_gco
NumericPrimitives::s_div_gco_gco_pm("_div",
&NumericDispatch::divide);
} /*namespace scm*/
} /*namespace xo*/
/* end NumericDispatch.cpp */

View file

@ -0,0 +1,36 @@
/** @file init_numeric.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "init_numeric.hpp"
#include <xo/procedure2/init_procedure2.hpp>
#include "Subsystem.hpp"
#include "numeric_register_facets.hpp"
namespace xo {
using xo::scm::numeric_register_facets;
void
InitSubsys<S_numeric_tag>::init()
{
numeric_register_facets();
}
InitEvidence
InitSubsys<S_numeric_tag>::require()
{
InitEvidence retval;
/* direct subsystem deps for xo-numeric/ */
retval ^= InitSubsys<S_procedure2_tag>::require();
/* xo-numeric/'s own initialization code */
retval ^= Subsystem::provide<S_numeric_tag>("numeric", &init);
return retval;
}
} /*namespace xo*/
/* end init_numeric.cpp */

View file

@ -0,0 +1,61 @@
/** @file numeric_register_facets.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "numeric_register_facets.hpp"
#include "NumericDispatch.hpp"
#include "Numeric.hpp"
#include "FloatIntegerOps.hpp"
#include "FloatOps.hpp"
#include "float/INumeric_DFloat.hpp"
#include "IntegerOps.hpp"
#include "integer/INumeric_DInteger.hpp"
#include <xo/object2/DFloat.hpp>
#include <xo/object2/Integer.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/reflectutil/typeseq.hpp>
#include <xo/indentlog/scope.hpp>
namespace xo {
using xo::facet::FacetRegistry;
using xo::reflect::typeseq;
namespace scm {
bool
numeric_register_facets()
{
scope log(XO_DEBUG(true));
FacetRegistry::register_impl<ANumeric, DInteger>();
NumericDispatch::instance().register_impl<DFloat, DFloat>
(&FloatOps::multiply,
&FloatOps::divide);
NumericDispatch::instance().register_impl<DFloat, DInteger>
(&FloatIntegerOps::multiply,
&FloatIntegerOps::divide);
NumericDispatch::instance().register_impl<DInteger, DFloat>
(&IntegerFloatOps::multiply,
&IntegerFloatOps::divide);
NumericDispatch::instance().register_impl<DInteger, DInteger>
(&IntegerOps::multiply,
&IntegerOps::divide);
log && log(xtag("ANumeric.tseq", typeseq::id<ANumeric>()));
return true;
}
} /*namespace scm*/
} /*namespace xo*/
/* end numeric_register_facets.cpp */

View file

@ -0,0 +1,12 @@
# built unittest xo-numeric/utest
set(UTEST_EXE utest.numeric)
set(UTEST_SRCS
numeric_utest_main.cpp
Numeric.test.cpp
)
xo_add_utest_executable(${UTEST_EXE} ${UTEST_SRCS})
xo_self_dependency(${UTEST_EXE} xo_numeric)
#xo_dependency(${UTEST_EXE} randomgen)
xo_external_target_dependency(${UTEST_EXE} Catch2 Catch2::Catch2)

View file

@ -0,0 +1,76 @@
/** @file Numeric.test.cpp
*
* @author Roland Conybeare, Feb 2026
**/
#include "init_numeric.hpp"
#include "NumericDispatch.hpp"
#include <xo/facet/FacetRegistry.hpp>
#include <xo/facet/TypeRegistry.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::scm::NumericDispatch;
//using xo::mm::AAllocator;
using xo::mm::DArena;
using xo::mm::ArenaConfig;;
using xo::mm::MemorySizeInfo;
using xo::facet::FacetRegistry;
using xo::facet::TypeRegistry;
//using xo::facet::with_facet;
//using xo::facet::obj;
namespace ut {
namespace {
struct Fixture {
explicit Fixture(const std::string & testname,
std::size_t aux_arena_size = 16 * 1024)
: aux_arena_(
ArenaConfig().with_name(testname).with_size(aux_arena_size))
{}
bool log_memory_layout(scope * p_log) {
auto visitor = [p_log](const MemorySizeInfo & info) {
*p_log && (*p_log)(xtag("resource", info.resource_name_),
xtag("used", info.used_),
xtag("alloc", info.allocated_),
xtag("commit", info.committed_),
xtag("resv", info.reserved_));
};
aux_arena_.visit_pools(visitor);
TypeRegistry::instance().visit_pools(visitor);
FacetRegistry::instance().visit_pools(visitor);
NumericDispatch::instance().visit_pools(visitor);
return true;
}
DArena aux_arena_;
};
} /*namespace*/
static InitEvidence s_init = (InitSubsys<S_numeric_tag>::require());
TEST_CASE("Numeric-init" "[numeric]")
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
Fixture fixture(testname);
{
// real purpose: ensure s_init sutvives static linking
REQUIRE(s_init.evidence());
}
log && fixture.log_memory_layout(&log);
}
} /*namespace ut*/
} /*namespace xo*/
/* end Numeric.test.cpp */

View file

@ -0,0 +1,24 @@
/* file numeric_utest_main.cpp */
#include <xo/subsys/Subsystem.hpp>
#define CATCH_CONFIG_RUNNER
#include "catch2/catch.hpp"
int
main(int argc, char* argv[])
{
using xo::Subsystem;
// Your custom initialization code here
Subsystem::initialize_all();
// Run Catch2's test session
int result = Catch::Session().run(argc, argv);
// cleanup here, if any
return result;
}
/* end numeric_utest_main.cpp */

View file

@ -6,6 +6,7 @@ include(CMakeFindDependencyMacro)
# must coordinate with xo_dependency() calls
# in CMakeLists.txt
#
find_dependency(xo_numeric)
find_dependency(xo_procedure2)
find_dependency(xo_gc)
find_dependency(xo_tokenizer2)

View file

@ -73,6 +73,7 @@ set(SELF_SRCS
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
# note: deps here must also appear in cmake/xo_expression2Config.cmake.in
xo_dependency(${SELF_LIB} xo_numeric)
xo_dependency(${SELF_LIB} xo_procedure2)
xo_dependency(${SELF_LIB} xo_gc)
xo_dependency(${SELF_LIB} xo_tokenizer2)

View file

@ -12,6 +12,8 @@
#include "ApplySsm.hpp"
#include "ParenSsm.hpp"
#include <xo/numeric/NumericPrimitives.hpp>
#include <xo/expression2/DApplyExpr.hpp>
#include <xo/expression2/detail/IExpression_DApplyExpr.hpp>
@ -118,15 +120,15 @@ namespace xo {
switch (tktype) {
case tokentype::tk_assign:
return optype::op_assign;
case tokentype::tk_plus:
case tokentype::tk_plus: // [+]
return optype::op_add;
case tokentype::tk_minus:
case tokentype::tk_minus: // [-]
return optype::op_subtract;
case tokentype::tk_star:
case tokentype::tk_star: // [*]
return optype::op_multiply;
case tokentype::tk_slash:
case tokentype::tk_slash: // [/]
return optype::op_divide;
case tokentype::tk_cmpeq:
case tokentype::tk_cmpeq: // [==]
return optype::op_equal;
case tokentype::tk_cmpne:
return optype::op_not_equal;
@ -297,12 +299,12 @@ namespace xo {
break;
case tokentype::tk_star:
case tokentype::tk_slash:
case tokentype::tk_minus:
case tokentype::tk_cmpeq:
this->on_operator_token(tk, p_psm);
return;
case tokentype::tk_slash:
case tokentype::tk_cmpne:
case tokentype::tk_type:
case tokentype::tk_lambda:
@ -1283,9 +1285,40 @@ namespace xo {
break;
case optype::op_divide:
// TODO: implement binary operator expression assembly
assert(false);
{
auto pm_obj = (with_facet<AGCObject>::mkobj
(&NumericPrimitives::s_div_gco_gco_pm));
auto fn_expr = (DConstant::make
(p_psm->expr_alloc(), pm_obj));
/* note:
* 1. don't assume we know lhs_ / rhs_ value types yet.
* perhaps have expression like
* f(..) * g(..)
* where f is the function that contains current ssm.
*
* 2. consequence: we need representation for
* polymorphic multiply on unknown numeric arguments.
*
* 3. TypeRef::dwim(..) is a placeholder.
* Plan to later provide abstract interpreter
* (ie compiler pass :) to drive type inference/unification
*
* 4. Alternatively could supply type-annotation syntax
* so human can assist inference; context here is we want
* to automate the boring stuff
*/
TypeRef tref = TypeRef::dwim
(TypeRef::prefix_type::from_chars("_div_gco"),
nullptr);
return DApplyExpr::make2(p_psm->expr_alloc(),
tref, fn_expr, lhs_, rhs_);
}
break;
case optype::op_subtract: /* editor bait: op_minus */
{
auto pm_obj = (with_facet<AGCObject>::mkobj

View file

@ -8,6 +8,7 @@
#include "reader2_register_types.hpp"
#include <xo/expression2/init_expression2.hpp>
#include <xo/numeric/init_numeric.hpp>
#include <xo/gc/CollectorTypeRegistry.hpp>
namespace xo {
@ -30,6 +31,7 @@ namespace xo {
/* direct subsystem deps for xo-reader2/ */
retval ^= InitSubsys<S_expression2_tag>::require();
retval ^= InitSubsys<S_numeric_tag>::require();
/* xo-reader2/'s own initialization code */
retval ^= Subsystem::provide<S_reader2_tag>("reader2", &init);

View file

@ -578,6 +578,67 @@ namespace xo {
log && fixture.log_memory_layout(&log);
}
TEST_CASE("SchematikaParser-interactive-arith2", "[reader2][SchematikaParser]")
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();
constexpr bool c_debug_flag = false;
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
ParserFixture fixture(testname, c_debug_flag);
auto & parser = *(fixture.parser_);
parser.begin_interactive_session();
/** Walkthrough parsing input equivalent to:
*
* 3.14159265 / 10.0 ;
*
**/
std::vector<Token> tk_v{
Token::f64_token("3.14159265"),
Token::slash_token(),
Token::f64_token("10.0"),
Token::semicolon_token(),
};
INFO(testname);
utest_tokenizer_loop(&parser, tk_v, c_debug_flag);
const auto & result = parser.result();
{
auto expr = obj<AExpression,DApplyExpr>::from(result.result_expr());
REQUIRE(expr);
REQUIRE(expr->n_args() == 2);
auto fn = obj<AExpression,DConstant>::from(expr->fn());
REQUIRE(fn);
auto pm = obj<AGCObject,DPrimitive_gco_2_gco_gco>::from(fn->value());
REQUIRE(pm);
REQUIRE(pm->name() == "_div");
auto lhs = obj<AExpression,DConstant>::from(expr->arg(0));
REQUIRE(lhs);
auto lhs_f64 = obj<AGCObject,DFloat>::from(lhs->value());
REQUIRE(lhs_f64);
REQUIRE(lhs_f64->value() == 3.14159265);
auto rhs = obj<AExpression,DConstant>::from(expr->arg(1));
REQUIRE(rhs);
auto rhs_f64 = obj<AGCObject,DFloat>::from(rhs->value());
REQUIRE(rhs_f64);
REQUIRE(rhs_f64->value() == 10.0);
}
log && fixture.log_memory_layout(&log);
}
TEST_CASE("SchematikaParser-interactive-cmp", "[reader2][SchematikaParser]")
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();