xo-reader2 stack: handle comparison expression (x == y)
This commit is contained in:
parent
20cee5db7d
commit
cf9930a54a
8 changed files with 216 additions and 55 deletions
|
|
@ -227,7 +227,7 @@ namespace xo {
|
||||||
|
|
||||||
TEST_CASE("VirtualSchematikaMachine-apply2", "[interpreter2][VSM]")
|
TEST_CASE("VirtualSchematikaMachine-apply2", "[interpreter2][VSM]")
|
||||||
{
|
{
|
||||||
scope log(XO_DEBUG(true));
|
scope log(XO_DEBUG(false));
|
||||||
|
|
||||||
VsmConfig cfg;
|
VsmConfig cfg;
|
||||||
VirtualSchematikaMachine vsm(cfg);
|
VirtualSchematikaMachine vsm(cfg);
|
||||||
|
|
@ -269,6 +269,7 @@ namespace xo {
|
||||||
FacetRegistry::instance().visit_pools(visitor);
|
FacetRegistry::instance().visit_pools(visitor);
|
||||||
vsm.visit_pools(visitor);
|
vsm.visit_pools(visitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
} /*namespace ut*/
|
} /*namespace ut*/
|
||||||
} /*namespace xo*/
|
} /*namespace xo*/
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,12 @@ namespace xo {
|
||||||
**/
|
**/
|
||||||
static DPrimitive_gco_2_gco_gco s_mul_gco_gco_pm;
|
static DPrimitive_gco_2_gco_gco s_mul_gco_gco_pm;
|
||||||
|
|
||||||
|
/** polymorphic equality comparison
|
||||||
|
*
|
||||||
|
* TODO: this will want to move to x-numeric/
|
||||||
|
**/
|
||||||
|
static DPrimitive_gco_2_gco_gco s_equal_gco_gco_pm;
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
static Primitive_f64_1_f64 s_neg_f64_pm;
|
static Primitive_f64_1_f64 s_neg_f64_pm;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,11 @@
|
||||||
|
|
||||||
#include "init_primitives.hpp"
|
#include "init_primitives.hpp"
|
||||||
#include "DPrimitive.hpp"
|
#include "DPrimitive.hpp"
|
||||||
#include <xo/object2/DFloat.hpp>
|
#include <xo/object2/Float.hpp>
|
||||||
#include <xo/object2/number/IGCObject_DFloat.hpp>
|
//#include <xo/object2/number/IGCObject_DFloat.hpp>
|
||||||
#include <xo/object2/DInteger.hpp>
|
#include <xo/object2/Integer.hpp>
|
||||||
#include <xo/object2/number/IGCObject_DInteger.hpp>
|
//#include <xo/object2/number/IGCObject_DInteger.hpp>
|
||||||
|
#include <xo/object2/Boolean.hpp>
|
||||||
#include <xo/gc/GCObjectConversion.hpp>
|
#include <xo/gc/GCObjectConversion.hpp>
|
||||||
#include <xo/object2/number/GCObjectConversion_DFloat.hpp>
|
#include <xo/object2/number/GCObjectConversion_DFloat.hpp>
|
||||||
#include <xo/object2/number/GCObjectConversion_DInteger.hpp>
|
#include <xo/object2/number/GCObjectConversion_DInteger.hpp>
|
||||||
|
|
@ -54,9 +55,6 @@ namespace xo {
|
||||||
// 2. at that point will require polymorphic dispatch
|
// 2. at that point will require polymorphic dispatch
|
||||||
// on argument representations, analogous to dispatch
|
// on argument representations, analogous to dispatch
|
||||||
// in FacetRegistry
|
// in FacetRegistry
|
||||||
// 3. Need concept of a 'runtime context'.
|
|
||||||
// This will need to be part of the AProcedure api
|
|
||||||
// e.g. passed to apply_nocheck
|
|
||||||
|
|
||||||
typeseq x_tseq = x_gco._typeseq();
|
typeseq x_tseq = x_gco._typeseq();
|
||||||
typeseq y_tseq = y_gco._typeseq();
|
typeseq y_tseq = y_gco._typeseq();
|
||||||
|
|
@ -101,6 +99,65 @@ namespace xo {
|
||||||
return obj<AGCObject>();
|
return obj<AGCObject>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj<AGCObject>
|
||||||
|
equal_gco_gco(obj<ARuntimeContext> rcx,
|
||||||
|
obj<AGCObject> x_gco,
|
||||||
|
obj<AGCObject> y_gco)
|
||||||
|
{
|
||||||
|
using xo::reflect::typeseq;
|
||||||
|
|
||||||
|
obj<AAllocator> mm = rcx.allocator();
|
||||||
|
|
||||||
|
// PLACEHOLDER
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// 1. move this to xo-numeric2/ when available
|
||||||
|
// 2. at that point will require polymorphic dispatch on argument representations.
|
||||||
|
//
|
||||||
|
|
||||||
|
typeseq x_tseq = x_gco._typeseq();
|
||||||
|
typeseq y_tseq = y_gco._typeseq();
|
||||||
|
|
||||||
|
// FOR NOW: just test runtime values
|
||||||
|
//
|
||||||
|
if (x_tseq == typeseq::id<DInteger>()) {
|
||||||
|
// i64 * ..
|
||||||
|
long x = GCObjectConversion<long>::from_gco(mm, x_gco);
|
||||||
|
|
||||||
|
if (y_tseq == typeseq::id<DInteger>()) {
|
||||||
|
// i64 == i64
|
||||||
|
long y = GCObjectConversion<long>::from_gco(mm, y_gco);
|
||||||
|
|
||||||
|
return DBoolean::box<AGCObject>(mm, x == y);
|
||||||
|
} else if (y_tseq == typeseq::id<DFloat>()) {
|
||||||
|
// i64 == f64
|
||||||
|
double y = GCObjectConversion<double>::from_gco(mm, y_gco);
|
||||||
|
|
||||||
|
return DFloat::box<AGCObject>(mm, static_cast<double>(x) == y);
|
||||||
|
}
|
||||||
|
} else if (x_tseq == typeseq::id<DFloat>()) {
|
||||||
|
if (y_tseq == typeseq::id<DInteger>()) {
|
||||||
|
// f64 == i64.
|
||||||
|
double x = GCObjectConversion<double>::from_gco(mm, x_gco);
|
||||||
|
long y = GCObjectConversion<long>::from_gco(mm, y_gco);
|
||||||
|
|
||||||
|
return DFloat::box<AGCObject>(mm, x == static_cast<double>(y));
|
||||||
|
} else if (y_tseq == typeseq::id<DFloat>()) {
|
||||||
|
// f64 * f64.
|
||||||
|
double x = GCObjectConversion<double>::from_gco(mm, x_gco);
|
||||||
|
double y = GCObjectConversion<double>::from_gco(mm, y_gco);
|
||||||
|
|
||||||
|
return DFloat::box<AGCObject>(mm, x == y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// here: error
|
||||||
|
throw std::runtime_error(tostr("mul_gco_gco: unexpected argument types xt,yt",
|
||||||
|
xtag("x.tseq", x_tseq),
|
||||||
|
xtag("y.tseq", y_tseq)));
|
||||||
|
return obj<AGCObject>();
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
double
|
double
|
||||||
mul_f64_f64(double x, double y) {
|
mul_f64_f64(double x, double y) {
|
||||||
|
|
@ -141,6 +198,9 @@ namespace xo {
|
||||||
DPrimitive_gco_2_gco_gco
|
DPrimitive_gco_2_gco_gco
|
||||||
Primitives::s_mul_gco_gco_pm("_mul", &mul_gco_gco);
|
Primitives::s_mul_gco_gco_pm("_mul", &mul_gco_gco);
|
||||||
|
|
||||||
|
DPrimitive_gco_2_gco_gco
|
||||||
|
Primitives::s_equal_gco_gco_pm("_equal", &equal_gco_gco);
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
Primitive_f64_1_f64
|
Primitive_f64_1_f64
|
||||||
Primitives::s_neg_f64_pm("_neg_d",
|
Primitives::s_neg_f64_pm("_neg_d",
|
||||||
|
|
|
||||||
|
|
@ -193,6 +193,9 @@ namespace xo {
|
||||||
/** top of parser stack **/
|
/** top of parser stack **/
|
||||||
obj<ASyntaxStateMachine> top_ssm() const;
|
obj<ASyntaxStateMachine> top_ssm() const;
|
||||||
|
|
||||||
|
/** current parser result. Value of last return from @ref on_token **/
|
||||||
|
const ParserResult & result() const;
|
||||||
|
|
||||||
/** visit parser-owned memory pools; invoke visitor(info) for each **/
|
/** visit parser-owned memory pools; invoke visitor(info) for each **/
|
||||||
void visit_pools(const MemorySizeVisitor & visitor) const;
|
void visit_pools(const MemorySizeVisitor & visitor) const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -292,11 +292,11 @@ namespace xo {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case tokentype::tk_star:
|
case tokentype::tk_star:
|
||||||
|
case tokentype::tk_cmpeq:
|
||||||
this->on_operator_token(tk, p_psm);
|
this->on_operator_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case tokentype::tk_slash:
|
case tokentype::tk_slash:
|
||||||
case tokentype::tk_cmpeq:
|
|
||||||
case tokentype::tk_cmpne:
|
case tokentype::tk_cmpne:
|
||||||
case tokentype::tk_type:
|
case tokentype::tk_type:
|
||||||
case tokentype::tk_lambda:
|
case tokentype::tk_lambda:
|
||||||
|
|
@ -1177,6 +1177,10 @@ namespace xo {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// make_builtin_apply_pm2
|
||||||
|
}
|
||||||
|
|
||||||
obj<AExpression>
|
obj<AExpression>
|
||||||
DProgressSsm::assemble_expr(ParserStateMachine * p_psm)
|
DProgressSsm::assemble_expr(ParserStateMachine * p_psm)
|
||||||
{
|
{
|
||||||
|
|
@ -1209,7 +1213,28 @@ namespace xo {
|
||||||
return this->lhs_;
|
return this->lhs_;
|
||||||
|
|
||||||
case optype::op_assign:
|
case optype::op_assign:
|
||||||
|
assert(false);
|
||||||
|
break;
|
||||||
|
|
||||||
case optype::op_equal:
|
case optype::op_equal:
|
||||||
|
{
|
||||||
|
auto pm_obj = (with_facet<AGCObject>::mkobj
|
||||||
|
(&Primitives::s_equal_gco_gco_pm));
|
||||||
|
auto fn_expr = (DConstant::make
|
||||||
|
(p_psm->expr_alloc(), pm_obj));
|
||||||
|
|
||||||
|
// see note on op_multiply
|
||||||
|
|
||||||
|
TypeRef tref = TypeRef::dwim
|
||||||
|
(TypeRef::prefix_type::from_chars("_equal_gco"),
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
return DApplyExpr::make2(p_psm->expr_alloc(),
|
||||||
|
tref,
|
||||||
|
fn_expr, lhs_, rhs_);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case optype::op_not_equal:
|
case optype::op_not_equal:
|
||||||
case optype::op_less:
|
case optype::op_less:
|
||||||
case optype::op_less_equal:
|
case optype::op_less_equal:
|
||||||
|
|
@ -1217,6 +1242,7 @@ namespace xo {
|
||||||
case optype::op_great_equal:
|
case optype::op_great_equal:
|
||||||
case optype::op_add:
|
case optype::op_add:
|
||||||
case optype::op_subtract:
|
case optype::op_subtract:
|
||||||
|
assert(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case optype::op_multiply:
|
case optype::op_multiply:
|
||||||
|
|
@ -1255,6 +1281,7 @@ namespace xo {
|
||||||
break;
|
break;
|
||||||
case optype::op_divide:
|
case optype::op_divide:
|
||||||
// TODO: implement binary operator expression assembly
|
// TODO: implement binary operator expression assembly
|
||||||
|
assert(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,12 @@ namespace xo {
|
||||||
return psm_.top_ssm();
|
return psm_.top_ssm();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ParserResult &
|
||||||
|
SchematikaParser::result() const
|
||||||
|
{
|
||||||
|
return psm_.result();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SchematikaParser::visit_pools(const MemorySizeVisitor & visitor) const
|
SchematikaParser::visit_pools(const MemorySizeVisitor & visitor) const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -411,6 +411,39 @@ namespace xo {
|
||||||
//REQUIRE(result.error_description());
|
//REQUIRE(result.error_description());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
utest_tokenizer_loop(SchematikaParser * parser, std::vector<Token> & tk_v, bool debug_flag)
|
||||||
|
{
|
||||||
|
scope log(XO_DEBUG(debug_flag));
|
||||||
|
|
||||||
|
size_t i_tk = 0;
|
||||||
|
size_t n_tk = tk_v.size();
|
||||||
|
for (const auto & tk : tk_v) {
|
||||||
|
INFO(tostr(xtag("i_tk", i_tk), xtag("tk", tk)));
|
||||||
|
|
||||||
|
auto & result = parser->on_token(tk);
|
||||||
|
|
||||||
|
log && log("after token", xtag("i_tk", i_tk), xtag("tk", tk));
|
||||||
|
log && log(xtag("parser", parser));
|
||||||
|
log && log(xtag("result", result));
|
||||||
|
|
||||||
|
if (i_tk + 1 < n_tk) {
|
||||||
|
REQUIRE(parser->has_incomplete_expr() == true);
|
||||||
|
REQUIRE(!result.is_error());
|
||||||
|
REQUIRE(result.is_incomplete());
|
||||||
|
} else {
|
||||||
|
/* last token in tk_v[] */
|
||||||
|
|
||||||
|
REQUIRE(parser->has_incomplete_expr() == false);
|
||||||
|
REQUIRE(!result.is_error());
|
||||||
|
REQUIRE(result.is_expression());
|
||||||
|
REQUIRE(result.result_expr());
|
||||||
|
}
|
||||||
|
|
||||||
|
++i_tk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("SchematikaParser-interactive-arith", "[reader2][SchematikaParser]")
|
TEST_CASE("SchematikaParser-interactive-arith", "[reader2][SchematikaParser]")
|
||||||
{
|
{
|
||||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||||
|
|
@ -435,54 +468,17 @@ namespace xo {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
std::vector<Token> tk_v{
|
||||||
|
Token::f64_token("3.14159265"),
|
||||||
|
Token::star_token(),
|
||||||
|
Token::f64_token("0.5"),
|
||||||
|
Token::semicolon_token(),
|
||||||
|
};
|
||||||
|
|
||||||
|
utest_tokenizer_loop(&parser, tk_v, c_debug_flag);
|
||||||
|
|
||||||
|
const auto & result = parser.result();
|
||||||
{
|
{
|
||||||
auto & result = parser.on_token(Token::f64_token("3.14159265"));
|
|
||||||
|
|
||||||
log && log("after float(3.14159265) token:");
|
|
||||||
log && log(xtag("parser", &parser));
|
|
||||||
log && log(xtag("result", result));
|
|
||||||
|
|
||||||
REQUIRE(parser.has_incomplete_expr() == true);
|
|
||||||
REQUIRE(!result.is_error());
|
|
||||||
REQUIRE(result.is_incomplete());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
auto & result = parser.on_token(Token::star_token());
|
|
||||||
|
|
||||||
log && log("after star token:");
|
|
||||||
log && log(xtag("parser", &parser));
|
|
||||||
log && log(xtag("result", result));
|
|
||||||
|
|
||||||
REQUIRE(parser.has_incomplete_expr() == true);
|
|
||||||
REQUIRE(!result.is_error());
|
|
||||||
REQUIRE(result.is_incomplete());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
auto & result = parser.on_token(Token::f64_token("0.5"));
|
|
||||||
|
|
||||||
log && log("after float(0.5) token:");
|
|
||||||
log && log(xtag("parser", &parser));
|
|
||||||
log && log(xtag("result", result));
|
|
||||||
|
|
||||||
REQUIRE(parser.has_incomplete_expr() == true);
|
|
||||||
REQUIRE(!result.is_error());
|
|
||||||
REQUIRE(result.is_incomplete());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
auto & result = parser.on_token(Token::semicolon_token());
|
|
||||||
|
|
||||||
log && log("after semicolon token:");
|
|
||||||
log && log(xtag("parser", &parser));
|
|
||||||
log && log(xtag("result", result));
|
|
||||||
|
|
||||||
REQUIRE(parser.has_incomplete_expr() == false);
|
|
||||||
REQUIRE(!result.is_error());
|
|
||||||
REQUIRE(result.is_expression());
|
|
||||||
REQUIRE(result.result_expr());
|
|
||||||
|
|
||||||
auto expr = obj<AExpression,DApplyExpr>::from(result.result_expr());
|
auto expr = obj<AExpression,DApplyExpr>::from(result.result_expr());
|
||||||
REQUIRE(expr);
|
REQUIRE(expr);
|
||||||
|
|
||||||
|
|
@ -509,10 +505,69 @@ namespace xo {
|
||||||
REQUIRE(rhs_f64);
|
REQUIRE(rhs_f64);
|
||||||
REQUIRE(rhs_f64->value() == 0.5);
|
REQUIRE(rhs_f64->value() == 0.5);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//REQUIRE(result.is_error());
|
TEST_CASE("SchematikaParser-interactive-cmp", "[reader2][SchematikaParser]")
|
||||||
//// illegal input on token
|
{
|
||||||
//REQUIRE(result.error_description());
|
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||||
|
|
||||||
|
constexpr bool c_debug_flag = true;
|
||||||
|
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||||
|
|
||||||
|
ArenaConfig config;
|
||||||
|
config.name_ = "test-arena";
|
||||||
|
config.size_ = 16 * 1024;
|
||||||
|
|
||||||
|
DArena expr_arena = DArena::map(config);
|
||||||
|
obj<AAllocator> expr_alloc = with_facet<AAllocator>::mkobj(&expr_arena);
|
||||||
|
|
||||||
|
SchematikaParser parser(config, 4096, expr_alloc, false /*debug_flag*/);
|
||||||
|
|
||||||
|
parser.begin_interactive_session();
|
||||||
|
|
||||||
|
/** Walkthrough parsing input equivalent to:
|
||||||
|
*
|
||||||
|
* 312 == 312 ;
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
std::vector<Token> tk_v{
|
||||||
|
Token::i64_token("312"),
|
||||||
|
Token::cmpeq_token(),
|
||||||
|
Token::i64_token("312"),
|
||||||
|
Token::semicolon_token(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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() == "_equal");
|
||||||
|
|
||||||
|
auto lhs = obj<AExpression,DConstant>::from(expr->arg(0));
|
||||||
|
REQUIRE(lhs);
|
||||||
|
|
||||||
|
auto lhs_i64 = obj<AGCObject,DInteger>::from(lhs->value());
|
||||||
|
REQUIRE(lhs_i64);
|
||||||
|
REQUIRE(lhs_i64->value() == 312);
|
||||||
|
|
||||||
|
auto rhs = obj<AExpression,DConstant>::from(expr->arg(1));
|
||||||
|
REQUIRE(rhs);
|
||||||
|
|
||||||
|
auto rhs_i64 = obj<AGCObject,DInteger>::from(rhs->value());
|
||||||
|
REQUIRE(rhs_i64);
|
||||||
|
REQUIRE(rhs_i64->value() == 312);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("SchematikaParser-interactive-lambda", "[reader2][SchematikaParser]")
|
TEST_CASE("SchematikaParser-interactive-lambda", "[reader2][SchematikaParser]")
|
||||||
|
|
@ -1017,7 +1072,7 @@ namespace xo {
|
||||||
|
|
||||||
TEST_CASE("SchematikaParser-interactive-apply", "[reader2][SchematikaParser]")
|
TEST_CASE("SchematikaParser-interactive-apply", "[reader2][SchematikaParser]")
|
||||||
{
|
{
|
||||||
constexpr bool c_debug_flag = true;
|
constexpr bool c_debug_flag = false;
|
||||||
scope log(XO_DEBUG(c_debug_flag));
|
scope log(XO_DEBUG(c_debug_flag));
|
||||||
|
|
||||||
ArenaConfig config;
|
ArenaConfig config;
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,9 @@ namespace xo {
|
||||||
/** token for @c "/" **/
|
/** token for @c "/" **/
|
||||||
static Token slash_token() { return Token(tokentype::tk_slash); }
|
static Token slash_token() { return Token(tokentype::tk_slash); }
|
||||||
|
|
||||||
|
/** token for @c "==" **/
|
||||||
|
static Token cmpeq_token() { return Token(tokentype::tk_cmpeq); }
|
||||||
|
|
||||||
/** token representing keyword @c type **/
|
/** token representing keyword @c type **/
|
||||||
static Token type() { return Token(tokentype::tk_type); }
|
static Token type() { return Token(tokentype::tk_type); }
|
||||||
/** token representing keyword @c def **/
|
/** token representing keyword @c def **/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue