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 911818e957
5 changed files with 106 additions and 8 deletions

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();