xo-reader2: recursive top-level function definition works
This commit is contained in:
parent
c7d820a92b
commit
4a3c8e794a
7 changed files with 222 additions and 13 deletions
|
|
@ -51,6 +51,12 @@ namespace xo {
|
||||||
/** @defgroup scm-expectexpr-methods general methods **/
|
/** @defgroup scm-expectexpr-methods general methods **/
|
||||||
///@{
|
///@{
|
||||||
|
|
||||||
|
/** update state for this syntax on incoming leftparen token @p tk,
|
||||||
|
* with overall parser state in @p p_psm
|
||||||
|
**/
|
||||||
|
void on_leftparen_token(const Token & tk,
|
||||||
|
ParserStateMachine * p_psm);
|
||||||
|
|
||||||
/** update state for this syntax on incoming leftbrace token @p tk,
|
/** update state for this syntax on incoming leftbrace token @p tk,
|
||||||
* with overall parser state in @p p_psm
|
* with overall parser state in @p p_psm
|
||||||
**/
|
**/
|
||||||
|
|
@ -81,8 +87,18 @@ namespace xo {
|
||||||
void on_string_token(const Token & tk,
|
void on_string_token(const Token & tk,
|
||||||
ParserStateMachine * p_psm);
|
ParserStateMachine * p_psm);
|
||||||
|
|
||||||
|
/** update state for this syntax on incoming if-token @p tk,
|
||||||
|
* overall parser state in @p p_psm.
|
||||||
|
*
|
||||||
|
* action: start nested if-else ssm
|
||||||
|
**/
|
||||||
|
void on_if_token(const Token & tk,
|
||||||
|
ParserStateMachine * p_psm);
|
||||||
|
|
||||||
/** update state for this syntax on incoming lambda token @p tk,
|
/** update state for this syntax on incoming lambda token @p tk,
|
||||||
* overall parser state in @p p_psm
|
* overall parser state in @p p_psm
|
||||||
|
*
|
||||||
|
* action: start nested lambda ssm
|
||||||
**/
|
**/
|
||||||
void on_lambda_token(const Token & tk,
|
void on_lambda_token(const Token & tk,
|
||||||
ParserStateMachine * p_psm);
|
ParserStateMachine * p_psm);
|
||||||
|
|
|
||||||
|
|
@ -116,6 +116,14 @@ namespace xo {
|
||||||
void on_parsed_expression(obj<AExpression> expr,
|
void on_parsed_expression(obj<AExpression> expr,
|
||||||
ParserStateMachine * p_psm);
|
ParserStateMachine * p_psm);
|
||||||
|
|
||||||
|
/** update ssm for expression @p expr (emitted by nested ssm)
|
||||||
|
* that's immediately followed by token @p tk
|
||||||
|
* with overall parser state in @p p_psm
|
||||||
|
**/
|
||||||
|
void on_parsed_expression_with_token(obj<AExpression> expr,
|
||||||
|
const Token & tk,
|
||||||
|
ParserStateMachine * p_psmn);
|
||||||
|
|
||||||
///@}
|
///@}
|
||||||
/** @defgroup scm-parenssm-printable-facet printable facet methods **/
|
/** @defgroup scm-parenssm-printable-facet printable facet methods **/
|
||||||
///@{
|
///@{
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include "SyntaxStateMachine.hpp"
|
#include "SyntaxStateMachine.hpp"
|
||||||
#include "ssm/ISyntaxStateMachine_DProgressSsm.hpp"
|
#include "ssm/ISyntaxStateMachine_DProgressSsm.hpp"
|
||||||
#include "DSequenceSsm.hpp"
|
#include "DSequenceSsm.hpp"
|
||||||
|
#include "IfElseSsm.hpp"
|
||||||
#include "LambdaSsm.hpp"
|
#include "LambdaSsm.hpp"
|
||||||
#include "syntaxstatetype.hpp"
|
#include "syntaxstatetype.hpp"
|
||||||
#include <xo/expression2/Variable.hpp>
|
#include <xo/expression2/Variable.hpp>
|
||||||
|
|
@ -103,9 +104,9 @@ namespace xo {
|
||||||
DExpectExprSsm::get_expect_str() const noexcept
|
DExpectExprSsm::get_expect_str() const noexcept
|
||||||
{
|
{
|
||||||
if (allow_defs_) {
|
if (allow_defs_) {
|
||||||
return "def|lambda|lparen|lbrace|literal|var";
|
return "def|if|lambda|lparen|lbrace|literal|var";
|
||||||
} else {
|
} else {
|
||||||
return "lambda|lparen|lbrace|literal|var";
|
return "if|lambda|lparen|lbrace|literal|var";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,6 +117,10 @@ namespace xo {
|
||||||
scope log(XO_DEBUG(p_psm->debug_flag()), xtag("tk", tk));
|
scope log(XO_DEBUG(p_psm->debug_flag()), xtag("tk", tk));
|
||||||
|
|
||||||
switch (tk.tk_type()) {
|
switch (tk.tk_type()) {
|
||||||
|
case tokentype::tk_leftparen:
|
||||||
|
this->on_leftparen_token(tk, p_psm);
|
||||||
|
return;
|
||||||
|
|
||||||
case tokentype::tk_leftbrace:
|
case tokentype::tk_leftbrace:
|
||||||
this->on_leftbrace_token(tk, p_psm);
|
this->on_leftbrace_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
|
|
@ -144,17 +149,19 @@ namespace xo {
|
||||||
this->on_bool_token(tk, p_psm);
|
this->on_bool_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
case tokentype::tk_if:
|
||||||
|
this->on_if_token(tk, p_psm);
|
||||||
|
return;
|
||||||
|
|
||||||
case tokentype::tk_lambda:
|
case tokentype::tk_lambda:
|
||||||
this->on_lambda_token(tk, p_psm);
|
this->on_lambda_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// all the not-yet handled cases
|
// all the not-yet handled cases
|
||||||
case tokentype::tk_invalid:
|
case tokentype::tk_invalid:
|
||||||
case tokentype::tk_if:
|
|
||||||
case tokentype::tk_singleassign:
|
case tokentype::tk_singleassign:
|
||||||
case tokentype::tk_colon:
|
case tokentype::tk_colon:
|
||||||
case tokentype::tk_semicolon:
|
case tokentype::tk_semicolon:
|
||||||
case tokentype::tk_leftparen:
|
|
||||||
case tokentype::tk_rightparen:
|
case tokentype::tk_rightparen:
|
||||||
case tokentype::tk_leftbracket:
|
case tokentype::tk_leftbracket:
|
||||||
case tokentype::tk_rightbracket:
|
case tokentype::tk_rightbracket:
|
||||||
|
|
@ -187,6 +194,22 @@ namespace xo {
|
||||||
Super::on_token(tk, p_psm);
|
Super::on_token(tk, p_psm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DExpectExprSsm::on_leftparen_token(const Token & tk,
|
||||||
|
ParserStateMachine * p_psm)
|
||||||
|
{
|
||||||
|
// need progress ssm here because this is allowed:
|
||||||
|
//
|
||||||
|
// if (foo) > 5 then ...
|
||||||
|
//
|
||||||
|
// Start progress ssm, delegate incoming token thereto
|
||||||
|
//
|
||||||
|
|
||||||
|
DProgressSsm::start(p_psm->parser_alloc(),
|
||||||
|
p_psm);
|
||||||
|
p_psm->on_token(tk);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
DExpectExprSsm::on_leftbrace_token(const Token & tk,
|
DExpectExprSsm::on_leftbrace_token(const Token & tk,
|
||||||
ParserStateMachine * p_psm)
|
ParserStateMachine * p_psm)
|
||||||
|
|
@ -388,6 +411,18 @@ namespace xo {
|
||||||
p_psm);
|
p_psm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DExpectExprSsm::on_if_token(const Token & tk,
|
||||||
|
ParserStateMachine * p_psm)
|
||||||
|
{
|
||||||
|
(void)tk;
|
||||||
|
|
||||||
|
DIfElseSsm::start(p_psm->parser_alloc(),
|
||||||
|
p_psm->expr_alloc(),
|
||||||
|
p_psm);
|
||||||
|
// TODO: should send if-token here
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
DExpectExprSsm::on_lambda_token(const Token & tk,
|
DExpectExprSsm::on_lambda_token(const Token & tk,
|
||||||
ParserStateMachine * p_psm)
|
ParserStateMachine * p_psm)
|
||||||
|
|
|
||||||
|
|
@ -423,14 +423,71 @@ namespace xo {
|
||||||
// TODO: may consider allowing if-else to terminate on other particular tokens
|
// TODO: may consider allowing if-else to terminate on other particular tokens
|
||||||
// e.g. ')'
|
// e.g. ')'
|
||||||
|
|
||||||
if ((tk.tk_type() == tokentype::tk_then)
|
switch (ifstate_) {
|
||||||
|| (tk.tk_type() == tokentype::tk_else)
|
case ifexprstatetype::invalid:
|
||||||
|| (tk.tk_type() == tokentype::tk_semicolon))
|
case ifexprstatetype::N:
|
||||||
{
|
// impossible
|
||||||
|
assert(false);
|
||||||
|
break;
|
||||||
|
case ifexprstatetype::if_0:
|
||||||
|
// also unreachable
|
||||||
|
break;
|
||||||
|
case ifexprstatetype::if_1:
|
||||||
|
// only ok if tk-type is tk_then.
|
||||||
|
if (tk.tk_type() == tokentype::tk_then) {
|
||||||
|
// advance to if_2 -then-> if_3
|
||||||
this->on_parsed_expression(expr, p_psm);
|
this->on_parsed_expression(expr, p_psm);
|
||||||
this->on_token(tk, p_psm);
|
this->on_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case ifexprstatetype::if_2:
|
||||||
|
// illegal, not expecting expression
|
||||||
|
break;
|
||||||
|
case ifexprstatetype::if_3:
|
||||||
|
// incoming expr argument is sufficient to complete this if-else
|
||||||
|
// but may continue on else-token
|
||||||
|
if (tk.tk_type() == tokentype::tk_else) {
|
||||||
|
// advance to if_4 -else-> if_5
|
||||||
|
this->on_parsed_expression(expr, p_psm);
|
||||||
|
this->on_token(tk, p_psm);
|
||||||
|
return;
|
||||||
|
} else if ((tk.tk_type() == tokentype::tk_semicolon)
|
||||||
|
|| (tk.tk_type() == tokentype::tk_rightparen)
|
||||||
|
|| (tk.tk_type() == tokentype::tk_rightbrace)) {
|
||||||
|
|
||||||
|
this->on_parsed_expression(expr, p_psm);
|
||||||
|
p_psm->pop_ssm();
|
||||||
|
p_psm->on_parsed_expression_with_token(if_expr_, tk);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case ifexprstatetype::if_4:
|
||||||
|
// illegal, not expecting expression
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ifexprstatetype::if_5:
|
||||||
|
// incoming expr argument completes this if-else
|
||||||
|
// advance to if_6
|
||||||
|
if ((tk.tk_type() == tokentype::tk_semicolon)
|
||||||
|
|| (tk.tk_type() == tokentype::tk_rightparen)
|
||||||
|
|| (tk.tk_type() == tokentype::tk_rightbrace))
|
||||||
|
{
|
||||||
|
// attaches expr as else- branch
|
||||||
|
this->on_parsed_expression(expr, p_psm);
|
||||||
|
|
||||||
|
p_psm->pop_ssm();
|
||||||
|
p_psm->on_parsed_expression_with_token(if_expr_, tk);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ifexprstatetype::if_6:
|
||||||
|
// illegal, not expecting expression
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Super::on_parsed_expression_with_token(expr, tk, p_psm);
|
Super::on_parsed_expression_with_token(expr, tk, p_psm);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -364,7 +364,23 @@ namespace xo {
|
||||||
}
|
}
|
||||||
|
|
||||||
Super::on_parsed_expression(expr, p_psm);
|
Super::on_parsed_expression(expr, p_psm);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DParenSsm::on_parsed_expression_with_token(obj<AExpression> expr,
|
||||||
|
const Token & tk,
|
||||||
|
ParserStateMachine * p_psm)
|
||||||
|
{
|
||||||
|
if (parenstate_ == parenexprstatetype::lparen_1) {
|
||||||
|
this->parenstate_ = parenexprstatetype::lparen_2;
|
||||||
|
this->expr_ = expr;
|
||||||
|
|
||||||
|
this->on_token(tk, p_psm);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Super::on_parsed_expression(expr, p_psm);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ namespace xo {
|
||||||
using xo::scm::Variable;
|
using xo::scm::Variable;
|
||||||
using xo::scm::Apply;
|
using xo::scm::Apply;
|
||||||
#endif
|
#endif
|
||||||
|
using xo::mm::AGCObject;
|
||||||
using xo::print::APrintable;
|
using xo::print::APrintable;
|
||||||
using xo::facet::FacetRegistry;
|
using xo::facet::FacetRegistry;
|
||||||
using xo::facet::with_facet;
|
using xo::facet::with_facet;
|
||||||
|
|
@ -214,7 +215,7 @@ namespace xo {
|
||||||
if (!lhs_) {
|
if (!lhs_) {
|
||||||
return "expr1|leftparen";
|
return "expr1|leftparen";
|
||||||
} else if (op_type_ == optype::invalid) {
|
} else if (op_type_ == optype::invalid) {
|
||||||
return "oper|semicolon|rightparen|righbrace";
|
return "oper|semicolon|rightparen|rightbrace";
|
||||||
} else {
|
} else {
|
||||||
return "expr2|leftparen";
|
return "expr2|leftparen";
|
||||||
}
|
}
|
||||||
|
|
@ -293,10 +294,10 @@ namespace xo {
|
||||||
case tokentype::tk_assign:
|
case tokentype::tk_assign:
|
||||||
case tokentype::tk_yields:
|
case tokentype::tk_yields:
|
||||||
case tokentype::tk_plus:
|
case tokentype::tk_plus:
|
||||||
case tokentype::tk_minus:
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case tokentype::tk_star:
|
case tokentype::tk_star:
|
||||||
|
case tokentype::tk_minus:
|
||||||
case tokentype::tk_cmpeq:
|
case tokentype::tk_cmpeq:
|
||||||
this->on_operator_token(tk, p_psm);
|
this->on_operator_token(tk, p_psm);
|
||||||
return;
|
return;
|
||||||
|
|
@ -1244,7 +1245,6 @@ namespace xo {
|
||||||
case optype::op_great:
|
case optype::op_great:
|
||||||
case optype::op_great_equal:
|
case optype::op_great_equal:
|
||||||
case optype::op_add:
|
case optype::op_add:
|
||||||
case optype::op_subtract:
|
|
||||||
assert(false);
|
assert(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -1286,6 +1286,24 @@ namespace xo {
|
||||||
// TODO: implement binary operator expression assembly
|
// TODO: implement binary operator expression assembly
|
||||||
assert(false);
|
assert(false);
|
||||||
break;
|
break;
|
||||||
|
case optype::op_subtract: /* editor bait: op_minus */
|
||||||
|
{
|
||||||
|
auto pm_obj = (with_facet<AGCObject>::mkobj
|
||||||
|
(&Primitives::s_sub_gco_gco_pm));
|
||||||
|
auto fn_expr = (DConstant::make
|
||||||
|
(p_psm->expr_alloc(), pm_obj));
|
||||||
|
|
||||||
|
// see comment on op_multiply re need for poly impl
|
||||||
|
|
||||||
|
TypeRef tref = TypeRef::dwim
|
||||||
|
(TypeRef::prefix_type::from_chars("_sub_gco"),
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
return DApplyExpr::make2(p_psm->expr_alloc(),
|
||||||
|
tref, fn_expr, lhs_, rhs_);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
#ifdef NOT_YET
|
#ifdef NOT_YET
|
||||||
case optype::op_assign:
|
case optype::op_assign:
|
||||||
|
|
@ -1308,6 +1326,7 @@ case optype::op_equal:
|
||||||
if (lhs_->valuetype()->is_i64() && rhs_->valuetype()->is_i64()) {
|
if (lhs_->valuetype()->is_i64() && rhs_->valuetype()->is_i64()) {
|
||||||
return Apply::make_cmp_eq_i64(lhs_, rhs_);
|
return Apply::make_cmp_eq_i64(lhs_, rhs_);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
this->apply_type_error(c_self_name,
|
this->apply_type_error(c_self_name,
|
||||||
op_type_, lhs_, rhs_, p_psm);
|
op_type_, lhs_, rhs_, p_psm);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
||||||
|
|
@ -274,7 +274,7 @@ namespace xo {
|
||||||
{
|
{
|
||||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||||
|
|
||||||
constexpr bool c_debug_flag = true;
|
constexpr bool c_debug_flag = false;
|
||||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||||
|
|
||||||
ParserFixture fixture(testname, c_debug_flag);
|
ParserFixture fixture(testname, c_debug_flag);
|
||||||
|
|
@ -809,7 +809,7 @@ namespace xo {
|
||||||
|
|
||||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||||
|
|
||||||
constexpr bool c_debug_flag = true;
|
constexpr bool c_debug_flag = false;
|
||||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||||
|
|
||||||
ParserFixture fixture(testname, c_debug_flag);
|
ParserFixture fixture(testname, c_debug_flag);
|
||||||
|
|
@ -861,6 +861,64 @@ namespace xo {
|
||||||
log && fixture.log_memory_layout(&log);
|
log && fixture.log_memory_layout(&log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SchematikaParser-batch-def2", "[reader2][SchematikaParser]")
|
||||||
|
{
|
||||||
|
// top-level recursive function definition
|
||||||
|
|
||||||
|
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||||
|
|
||||||
|
constexpr bool c_debug_flag = true;
|
||||||
|
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:
|
||||||
|
*
|
||||||
|
* def fact = lambda (n) { if (n == 0) then 1 else n * fact(n - 1) };
|
||||||
|
* ^ ^ ^ ^ ^^^ ^ ^ ^^ ^ ^^ ^ ^ ^ ^ ^ ^ ^^ ^ ^^ ^^
|
||||||
|
* 0 1 2 3 4|6 7 8 9| b c| e f g h i j k| m n| p|
|
||||||
|
* 5 a d l o q
|
||||||
|
**/
|
||||||
|
|
||||||
|
std::vector<Token> tk_v{
|
||||||
|
/* [ 0] */ Token::def_token(),
|
||||||
|
|
||||||
|
/* [ 1] */ Token::symbol_token("fact"),
|
||||||
|
/* [ 2] */ Token::singleassign_token(),
|
||||||
|
/* [ 3] */ Token::lambda_token(),
|
||||||
|
/* [ 4] */ Token::leftparen_token(),
|
||||||
|
/* [ 5] */ Token::symbol_token("n"),
|
||||||
|
/* [ 6] */ Token::rightparen_token(),
|
||||||
|
/* [ 7] */ Token::leftbrace_token(),
|
||||||
|
/* [ 8] */ Token::if_token(),
|
||||||
|
/* [ 9] */ Token::leftparen_token(),
|
||||||
|
/* [ a] */ Token::symbol_token("n"),
|
||||||
|
/* [ b] */ Token::cmpeq_token(),
|
||||||
|
/* [ c] */ Token::i64_token("0"),
|
||||||
|
/* [ d] */ Token::rightparen_token(),
|
||||||
|
/* [ e] */ Token::then_token(),
|
||||||
|
/* [ f] */ Token::i64_token("1"),
|
||||||
|
/* [ g] */ Token::else_token(),
|
||||||
|
/* [ h] */ Token::symbol_token("n"),
|
||||||
|
/* [ i] */ Token::star_token(),
|
||||||
|
/* [ j] */ Token::symbol_token("fact"),
|
||||||
|
/* [ k] */ Token::leftparen_token(),
|
||||||
|
/* [ l] */ Token::symbol_token("n"),
|
||||||
|
/* [ m] */ Token::minus_token(),
|
||||||
|
/* [ n] */ Token::i64_token("1"),
|
||||||
|
/* [ o] */ Token::rightparen_token(),
|
||||||
|
/* [ p] */ Token::rightbrace_token(),
|
||||||
|
/* [ q] */ Token::semicolon_token(),
|
||||||
|
};
|
||||||
|
|
||||||
|
utest_tokenizer_loop(&parser, tk_v, c_debug_flag);
|
||||||
|
|
||||||
|
log && fixture.log_memory_layout(&log);
|
||||||
|
}
|
||||||
|
|
||||||
} /*namespace ut*/
|
} /*namespace ut*/
|
||||||
} /*namespace xo*/
|
} /*namespace xo*/
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue