diff --git a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp index d0889fa3..8807a72c 100644 --- a/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp +++ b/xo-interpreter2/src/interpreter2/VirtualSchematikaMachine.cpp @@ -5,6 +5,7 @@ #include "VirtualSchematikaMachine.hpp" #include "DPrimitive_gco_3_dict_string_gco.hpp" +#include "DPrimitive_gco_2_gco_gco.hpp" #include "VsmDefContFrame.hpp" #include "VsmApplyFrame.hpp" #include "VsmEvalArgsFrame.hpp" @@ -13,6 +14,7 @@ #include "VsmSeqContFrame.hpp" #include "VsmRcx.hpp" #include "Closure.hpp" +#include #include #include #include @@ -25,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -911,6 +914,26 @@ namespace xo { static DPrimitive_gco_0 s_cwd_pm("_cwd", &xfer_cwd); + // ----- primitive: fn_nth() ----- + + // TODO: seq_gc -> obj + // n_gco -> obj + // + obj + xfer_nth(obj rcx, + obj seq_gco, + obj n_gco) + { + (void)rcx; + + obj seq = seq_gco.to_facet(); + auto n = obj::from(n_gco); + + return seq.at(n->value()); + } + + static DPrimitive_gco_2_gco_gco s_nth_pm("_nth", &xfer_nth); + // ----- primitive: fn_n_args() ----- obj @@ -1003,6 +1026,18 @@ namespace xo { obj(&s_cwd_pm)); } + /* nth */ + { + const DUniqueString * name + = reader_.intern_string("nth"); + + global_env_->_upsert_value + (mm_.to_op(), + name, + Reflect::require(), + obj(&s_nth_pm)); + } + /* fn_n_args */ { const DUniqueString * name diff --git a/xo-reader2/include/xo/reader2/DExpectQLiteralSsm.hpp b/xo-reader2/include/xo/reader2/DExpectQLiteralSsm.hpp index b8452961..7073283a 100644 --- a/xo-reader2/include/xo/reader2/DExpectQLiteralSsm.hpp +++ b/xo-reader2/include/xo/reader2/DExpectQLiteralSsm.hpp @@ -51,6 +51,12 @@ namespace xo { void on_f64_token(const Token & tk, ParserStateMachine * p_psm); + /** update state for i64 token @p tk, with overall parser state in @p p_psm. + * delegates to parent ssm via @ref on_quoted_literal + **/ + void on_i64_token(const Token & tk, + ParserStateMachine * p_psm); + /** update state on incoming token @p tk, * with overall parser state in @p p_psm. * diff --git a/xo-reader2/src/reader2/DExpectQLiteralSsm.cpp b/xo-reader2/src/reader2/DExpectQLiteralSsm.cpp index 7dc30124..92309ddc 100644 --- a/xo-reader2/src/reader2/DExpectQLiteralSsm.cpp +++ b/xo-reader2/src/reader2/DExpectQLiteralSsm.cpp @@ -7,7 +7,7 @@ #include "ExpectQListSsm.hpp" #include "ExpectQArraySsm.hpp" #include -//#include "DExpectFormalArgSsm.hpp" +#include //#include "ssm/ISyntaxStateMachine_DExpectFormalArgSsm.hpp" //#include //#include @@ -91,6 +91,10 @@ namespace xo { this->on_f64_token(tk, p_psm); return; + case tokentype::tk_i64: + this->on_i64_token(tk, p_psm); + return; + case tokentype::tk_leftparen: this->on_leftparen_token(tk, p_psm); return; @@ -116,7 +120,6 @@ namespace xo { case tokentype::tk_colon: case tokentype::tk_singleassign: case tokentype::tk_string: - case tokentype::tk_i64: case tokentype::tk_bool: case tokentype::tk_semicolon: case tokentype::tk_invalid: @@ -161,6 +164,17 @@ namespace xo { p_psm->on_quoted_literal(literal); } + void + DExpectQLiteralSsm::on_i64_token(const Token & tk, + ParserStateMachine * p_psm) + { + auto literal = DInteger::box(p_psm->expr_alloc(), + tk.i64_value()); + + p_psm->pop_ssm(); + p_psm->on_quoted_literal(literal); + } + #ifdef NOT_YET void DExpectQLiteralSsm::_accept_formal(obj expr_alloc, diff --git a/xo-reader2/utest/SchematikaParser.test.cpp b/xo-reader2/utest/SchematikaParser.test.cpp index 3921148d..0b1fea58 100644 --- a/xo-reader2/utest/SchematikaParser.test.cpp +++ b/xo-reader2/utest/SchematikaParser.test.cpp @@ -1401,6 +1401,44 @@ namespace xo { log && fixture.log_memory_layout(&log); } + TEST_CASE("SchematikaParser-batch-qlist2", "[reader2][SchematikaParser]") + { + // top-level recursive function definition + + 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: + * + * #q{ (4 7.2) }; + * ^ ^ ^^ ^ ^ ^^ + * 0 1 2| 4 5 6| + * 3 7 + **/ + + std::vector tk_v{ + /* [ 0] */ Token::quote_token(), + /* [ 1] */ Token::leftbrace_token(), + /* [ 2] */ Token::leftparen_token(), + /* [ 3] */ Token::i64_token("4"), + /* [ 4] */ Token::f64_token("7.2"), + /* [ 5] */ Token::rightparen_token(), + /* [ 6] */ Token::rightbrace_token(), + /* [ 7] */ Token::semicolon_token(), + }; + + utest_tokenizer_loop(&parser, tk_v, c_debug_flag); + + log && fixture.log_memory_layout(&log); + } + TEST_CASE("SchematikaParser-batch-qarray", "[reader2][SchematikaParser]") { // top-level recursive function definition