xo-interpreter2 stack: support op!= + trial numeric refactor

This commit is contained in:
Roland Conybeare 2026-02-19 11:56:06 -08:00
commit 0f17c52ce8
15 changed files with 172 additions and 17 deletions

View file

@ -30,6 +30,8 @@ namespace xo {
static obj<AGCObject> cmp_equal(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
static obj<AGCObject> cmp_notequal(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
};
class IntegerFloatOps {
@ -48,6 +50,8 @@ namespace xo {
static obj<AGCObject> cmp_equal(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
static obj<AGCObject> cmp_notequal(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
};
}

View file

@ -29,6 +29,8 @@ namespace xo {
static obj<AGCObject> cmp_equal(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
static obj<AGCObject> cmp_notequal(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
};
}

View file

@ -31,6 +31,8 @@ namespace xo {
static obj<AGCObject> cmp_equal(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
static obj<AGCObject> cmp_notequal(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
};

View file

@ -25,6 +25,7 @@ namespace xo {
using typeseq = xo::reflect::typeseq;
using KeyType = std::pair<typeseq, typeseq>;
using MappedType = AnonymizedNumericOps;
using BinaryOp = AnonymizedNumericOps::BinaryOp;
/** hash function for key_type **/
struct KeyHash {
@ -58,6 +59,16 @@ namespace xo {
return s_instance;
}
/** multi-dispatch driver.
* Invoke @p member_ptr in AnonymizedNumericOps
**/
static obj<AGCObject> dispatch(obj<ARuntimeContext> rcx,
const char * caller,
const char * error_headline,
BinaryOp AnonymizedNumericOps::* member_ptr,
obj<AGCObject> x,
obj<AGCObject> y);
/** multiply w/ runtime polymorphism (double-dispatch)
**/
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
@ -87,6 +98,11 @@ namespace xo {
obj<AGCObject> x,
obj<AGCObject> y);
/** compare two numeric values for inequality **/
static obj<AGCObject> cmp_notequal(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y);
/** report memory use for owned arenas to @p visitor **/
void visit_pools(const MemorySizeVisitor & visitor);
@ -100,7 +116,8 @@ namespace xo {
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl div_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl add_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl sub_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl cmpeq_fn) {
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl cmpeq_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl cmpne_fn) {
KeyType key(typeseq::id<DRepr1>().seqno(),
typeseq::id<DRepr2>().seqno());
@ -111,7 +128,8 @@ namespace xo {
div_fn,
add_fn,
sub_fn,
cmpeq_fn);
cmpeq_fn,
cmpne_fn);
}
private:

View file

@ -26,9 +26,10 @@ namespace xo {
BinaryOp divide,
BinaryOp add,
BinaryOp subtract,
BinaryOp cmpeq)
BinaryOp cmpeq,
BinaryOp cmpne)
: multiply_{multiply}, divide_{divide}, add_{add}, subtract_{subtract},
cmpeq_{cmpeq} {}
cmpeq_{cmpeq}, cmpne_{cmpne} {}
BinaryOp multiply_ = nullptr;
BinaryOp divide_ = nullptr;
@ -37,6 +38,8 @@ namespace xo {
/** compare numerics for equality **/
BinaryOp cmpeq_ = nullptr;
/** compare numerics for inequality **/
BinaryOp cmpne_ = nullptr;
};
template <typename DRepr1, typename DRepr2>
@ -52,12 +55,14 @@ namespace xo {
BinaryOp_Impl divide,
BinaryOp_Impl add,
BinaryOp_Impl subtract,
BinaryOp_Impl cmpeq) {
BinaryOp_Impl cmpeq,
BinaryOp_Impl cmpne) {
return AnonymizedNumericOps(reinterpret_cast<BinaryOp_Anon>(multiply),
reinterpret_cast<BinaryOp_Anon>(divide),
reinterpret_cast<BinaryOp_Anon>(add),
reinterpret_cast<BinaryOp_Anon>(subtract),
reinterpret_cast<BinaryOp_Anon>(cmpeq));
reinterpret_cast<BinaryOp_Anon>(cmpeq),
reinterpret_cast<BinaryOp_Anon>(cmpne));
}
};

View file

@ -25,6 +25,8 @@ namespace xo {
/** polymorphic (in both arguments) compare (==) **/
static DPrimitive_gco_2_gco_gco s_cmpeq_gco_gco_pm;
/** polymorphic (in both arguments) compare (!=) **/
static DPrimitive_gco_2_gco_gco s_cmpne_gco_gco_pm;
};
}
}

View file

@ -50,6 +50,14 @@ namespace xo {
x->value() == DFloat::value_type(y->value()));
}
obj<AGCObject>
FloatIntegerOps::cmp_notequal(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y)
{
return DBoolean::box<AGCObject>(rcx.allocator(),
x->value() != DFloat::value_type(y->value()));
}
// ----- Integer op Float -----
obj<AGCObject>
@ -84,8 +92,16 @@ namespace xo {
IntegerFloatOps::cmp_equal(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(),
DFloat::value_type(x->value() == y->value()));
return DBoolean::box<AGCObject>(rcx.allocator(),
DFloat::value_type(x->value()) == y->value());
}
obj<AGCObject>
IntegerFloatOps::cmp_notequal(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DBoolean::box<AGCObject>(rcx.allocator(),
DFloat::value_type(x->value()) != y->value());
}
}

View file

@ -47,6 +47,13 @@ namespace xo {
return DBoolean::box<AGCObject>(rcx.allocator(), x->value() == y->value());
}
obj<AGCObject>
FloatOps::cmp_notequal(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y)
{
return DBoolean::box<AGCObject>(rcx.allocator(), x->value() != y->value());
}
}
}

View file

@ -48,6 +48,13 @@ namespace xo {
return DBoolean::box<AGCObject>(rcx.allocator(), x->value() == y->value());
}
obj<AGCObject>
IntegerOps::cmp_notequal(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y)
{
return DBoolean::box<AGCObject>(rcx.allocator(), x->value() != y->value());
}
}
}

View file

@ -4,10 +4,13 @@
**/
#include "NumericDispatch.hpp"
#include <xo/object2/RuntimeError.hpp>
#include <xo/facet/TypeRegistry.hpp>
#include <xo/indentlog/scope.hpp>
namespace xo {
using xo::mm::AGCObject;
using xo::facet::TypeRegistry;
namespace scm {
@ -17,6 +20,35 @@ namespace xo {
dispatch_.visit_pools(visitor);
}
obj<AGCObject>
NumericDispatch::dispatch(obj<ARuntimeContext> rcx,
const char * caller,
const char * error_headline,
BinaryOp AnonymizedNumericOps::* member_ptr,
obj<AGCObject> x,
obj<AGCObject> y)
{
KeyType key(x._typeseq(), y._typeseq());
auto target_fn
= NumericDispatch::instance().dispatch_[key].*member_ptr;
if (!target_fn) {
// FIXME: use {fmt} here
std::stringstream ss;
tosn(ss,
error_headline,
xtag("x.type", TypeRegistry::id2name(x._typeseq())),
xtag("y.type", TypeRegistry::id2name(y._typeseq())));
return DRuntimeError::make(rcx.allocator(),
caller,
ss.str().c_str());
}
return (*target_fn)(rcx, x.data(), y.data());
}
obj<AGCObject>
NumericDispatch::multiply(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
@ -91,12 +123,56 @@ namespace xo {
auto target_fn
= NumericDispatch::instance().dispatch_[key].cmpeq_;
if (!target_fn)
assert(false);
if (!target_fn) {
// FIXME: use {fmt} here
std::stringstream ss;
tosn(ss,
"incomparable types in x==y",
xtag("x.type", TypeRegistry::id2name(x._typeseq())),
xtag("y.type", TypeRegistry::id2name(y._typeseq())));
return DRuntimeError::make(rcx.allocator(),
"NumericDispatch::cmp_equal",
ss.str().c_str());
}
return (*target_fn)(rcx, x.data(), y.data());
}
obj<AGCObject>
NumericDispatch::cmp_notequal(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y)
{
return dispatch(rcx,
"NumericDispatch::cmp_notequal",
"incomparable types in x!=y",
&AnonymizedNumericOps::cmpne_,
x, y);
#ifdef OBSOLETE
KeyType key(x._typeseq(), y._typeseq());
auto target_fn
= NumericDispatch::instance().dispatch_[key].cmpne_;
if (!target_fn) {
// FIXME: use {fmt} here
std::stringstream ss;
tosn(ss,
"incomparable types in x==y",
xtag("x.type", TypeRegistry::id2name(x._typeseq())),
xtag("y.type", TypeRegistry::id2name(y._typeseq())));
return DRuntimeError::make(rcx.allocator(),
"NumericDispatch::cmp_notequal",
ss.str().c_str());
}
return (*target_fn)(rcx, x.data(), y.data());
#endif
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -31,6 +31,10 @@ namespace xo {
NumericPrimitives::s_cmpeq_gco_gco_pm("_cmpeq",
&NumericDispatch::cmp_equal);
DPrimitive_gco_2_gco_gco
NumericPrimitives::s_cmpne_gco_gco_pm("_cmpne",
&NumericDispatch::cmp_notequal);
} /*namespace scm*/
} /*namespace xo*/

View file

@ -39,28 +39,32 @@ namespace xo {
&FloatOps::divide,
&FloatOps::add,
&FloatOps::subtract,
&FloatOps::cmp_equal);
&FloatOps::cmp_equal,
&FloatOps::cmp_notequal);
NumericDispatch::instance().register_impl<DFloat, DInteger>
(&FloatIntegerOps::multiply,
&FloatIntegerOps::divide,
&FloatIntegerOps::add,
&FloatIntegerOps::subtract,
&FloatIntegerOps::cmp_equal);
&FloatIntegerOps::cmp_equal,
&FloatIntegerOps::cmp_notequal);
NumericDispatch::instance().register_impl<DInteger, DFloat>
(&IntegerFloatOps::multiply,
&IntegerFloatOps::divide,
&IntegerFloatOps::add,
&IntegerFloatOps::subtract,
&IntegerFloatOps::cmp_equal);
&IntegerFloatOps::cmp_equal,
&IntegerFloatOps::cmp_notequal);
NumericDispatch::instance().register_impl<DInteger, DInteger>
(&IntegerOps::multiply,
&IntegerOps::divide,
&IntegerOps::add,
&IntegerOps::subtract,
&IntegerOps::cmp_equal);
&IntegerOps::cmp_equal,
&IntegerOps::cmp_notequal);
log && log(xtag("ANumeric.tseq", typeseq::id<ANumeric>()));