xo-interpreter2 stack: + literal array parsing
This commit is contained in:
parent
f2a9aa3f52
commit
680416d077
18 changed files with 812 additions and 51 deletions
|
|
@ -73,6 +73,10 @@ set(SELF_SRCS
|
|||
ISyntaxStateMachine_DExpectQListSsm.cpp
|
||||
IPrintable_DExpectQListSsm.cpp
|
||||
|
||||
DExpectQArraySsm.cpp
|
||||
ISyntaxStateMachine_DExpectQArraySsm.cpp
|
||||
IPrintable_DExpectQArraySsm.cpp
|
||||
|
||||
DProgressSsm.cpp
|
||||
ISyntaxStateMachine_DProgressSsm.cpp
|
||||
IPrintable_DProgressSsm.cpp
|
||||
|
|
|
|||
222
src/reader2/DExpectQArraySsm.cpp
Normal file
222
src/reader2/DExpectQArraySsm.cpp
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
/** @file DExpectQArraySsm.cpp
|
||||
*
|
||||
* @author Roland Conybeare, Mar 2026
|
||||
**/
|
||||
|
||||
#include "ExpectQArraySsm.hpp"
|
||||
#include "ExpectQLiteralSsm.hpp"
|
||||
#include <xo/object2/Array.hpp>
|
||||
#include <xo/facet/FacetRegistry.hpp>
|
||||
#include <xo/indentlog/print/pretty.hpp>
|
||||
|
||||
namespace xo {
|
||||
using xo::print::APrintable;
|
||||
using xo::facet::FacetRegistry;
|
||||
using xo::mm::AGCObject;
|
||||
|
||||
namespace scm {
|
||||
const char *
|
||||
QArrayXst::_descr(enum code x)
|
||||
{
|
||||
switch (x) {
|
||||
case code::invalid: break;
|
||||
case code::qarray_0: return "qarray_0";
|
||||
case code::qarray_1a: return "qarray_1a";
|
||||
case code::qarray_2: return "qarray_2";
|
||||
case code::N: break;
|
||||
}
|
||||
|
||||
return "?QArrayXst";
|
||||
}
|
||||
|
||||
DExpectQArraySsm::DExpectQArraySsm() : state_{QArrayXst::code::qarray_0} {}
|
||||
|
||||
obj<ASyntaxStateMachine,DExpectQArraySsm>
|
||||
DExpectQArraySsm::make(DArena & parser_mm)
|
||||
{
|
||||
return obj<ASyntaxStateMachine,DExpectQArraySsm>(_make(parser_mm));
|
||||
}
|
||||
|
||||
DExpectQArraySsm *
|
||||
DExpectQArraySsm::_make(DArena & parser_mm)
|
||||
{
|
||||
void * mem = parser_mm.alloc_for<DExpectQArraySsm>();
|
||||
|
||||
return new (mem) DExpectQArraySsm();
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQArraySsm::start(ParserStateMachine * p_psm)
|
||||
{
|
||||
DArena::Checkpoint ckp = p_psm->parser_alloc().checkpoint();
|
||||
|
||||
p_psm->push_ssm(ckp, DExpectQArraySsm::make(p_psm->parser_alloc()));
|
||||
}
|
||||
|
||||
syntaxstatetype
|
||||
DExpectQArraySsm::ssm_type() const noexcept {
|
||||
return syntaxstatetype::expect_qarray;
|
||||
}
|
||||
|
||||
std::string_view
|
||||
DExpectQArraySsm::get_expect_str() const {
|
||||
switch (state_.code()) {
|
||||
case QArrayXst::code::qarray_0:
|
||||
return "leftparen";
|
||||
case QArrayXst::code::qarray_1a:
|
||||
return "qliteral|rightparen";
|
||||
case QArrayXst::code::qarray_2:
|
||||
return "(done)";
|
||||
case QArrayXst::code::invalid:
|
||||
case QArrayXst::code::N:
|
||||
break;
|
||||
}
|
||||
|
||||
return "?DExpectQArraySsm";
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQArraySsm::on_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
switch(tk.tk_type())
|
||||
{
|
||||
case tokentype::tk_leftbracket:
|
||||
this->on_leftbracket_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_rightbracket:
|
||||
this->on_rightbracket_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_comma:
|
||||
case tokentype::tk_lambda:
|
||||
case tokentype::tk_def:
|
||||
case tokentype::tk_if:
|
||||
case tokentype::tk_symbol:
|
||||
case tokentype::tk_colon:
|
||||
case tokentype::tk_singleassign:
|
||||
case tokentype::tk_string:
|
||||
case tokentype::tk_f64:
|
||||
case tokentype::tk_i64:
|
||||
case tokentype::tk_bool:
|
||||
case tokentype::tk_semicolon:
|
||||
case tokentype::tk_invalid:
|
||||
case tokentype::tk_quote:
|
||||
case tokentype::tk_leftparen:
|
||||
case tokentype::tk_rightparen:
|
||||
case tokentype::tk_leftbrace:
|
||||
case tokentype::tk_rightbrace:
|
||||
case tokentype::tk_leftangle:
|
||||
case tokentype::tk_rightangle:
|
||||
case tokentype::tk_lessequal:
|
||||
case tokentype::tk_greatequal:
|
||||
case tokentype::tk_dot:
|
||||
case tokentype::tk_doublecolon:
|
||||
case tokentype::tk_assign:
|
||||
case tokentype::tk_yields:
|
||||
case tokentype::tk_plus:
|
||||
case tokentype::tk_minus:
|
||||
case tokentype::tk_star:
|
||||
case tokentype::tk_slash:
|
||||
case tokentype::tk_cmpeq:
|
||||
case tokentype::tk_cmpne:
|
||||
case tokentype::tk_type:
|
||||
case tokentype::tk_then:
|
||||
case tokentype::tk_else:
|
||||
case tokentype::tk_let:
|
||||
case tokentype::tk_in:
|
||||
case tokentype::tk_end:
|
||||
case tokentype::N:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQArraySsm::on_leftbracket_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (state_.code() == QArrayXst::code::qarray_0) {
|
||||
this->state_ = QArrayXst(QArrayXst::code::qarray_1a);
|
||||
|
||||
this->array_ = DArray::empty(p_psm->expr_alloc(),
|
||||
8 /*heuristic starting capacity*/);
|
||||
|
||||
DExpectQLiteralSsm::start(p_psm,
|
||||
false /*cxl_on_rightparen*/,
|
||||
true /*cxl_on_rightbracket*/);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::illegal_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQArraySsm::on_rightbracket_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (state_.code() == QArrayXst::code::qarray_1a) {
|
||||
this->state_ = QArrayXst(QArrayXst::code::qarray_2);
|
||||
|
||||
array_->shrink_to_fit();
|
||||
|
||||
obj<AGCObject> lit = obj<AGCObject,DArray>(array_);
|
||||
|
||||
p_psm->pop_ssm();
|
||||
p_psm->on_quoted_literal(lit);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::illegal_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQArraySsm::on_quoted_literal(obj<AGCObject> lit,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if(state_.code() == QArrayXst::code::qarray_1a) {
|
||||
// append lit at the end of array_
|
||||
{
|
||||
assert(array_);
|
||||
|
||||
// ensure sufficient capacity
|
||||
if (array_->size() == array_->capacity()) {
|
||||
assert(array_->capacity() > 0);
|
||||
|
||||
/* reallocate w/ 2x capacity */
|
||||
this->array_ = DArray::copy(p_psm->expr_alloc(),
|
||||
array_,
|
||||
2 * array_->capacity());
|
||||
}
|
||||
|
||||
bool ok = array_->push_back(lit);
|
||||
|
||||
assert(ok);
|
||||
}
|
||||
|
||||
// start syntax to receive next literal
|
||||
DExpectQLiteralSsm::start(p_psm,
|
||||
false /*cxl_on_rightparen*/,
|
||||
true /*cxl_on_rightbracket*/);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::illegal_quoted_literal(lit, p_psm);
|
||||
}
|
||||
|
||||
bool
|
||||
DExpectQArraySsm::pretty(const ppindentinfo & ppii) const
|
||||
{
|
||||
obj<AGCObject,DArray> array(array_);
|
||||
auto array_pr = FacetRegistry::instance().variant<APrintable,AGCObject>(array);
|
||||
|
||||
return ppii.pps()->pretty_struct(ppii,
|
||||
"DExpectQArraySsm",
|
||||
refrtag("state", state_),
|
||||
refrtag("expect", this->get_expect_str()),
|
||||
refrtag("array", array_pr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* end DExpectQArraySsm.cpp */
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "ExpectQLiteralSsm.hpp"
|
||||
#include "ExpectQListSsm.hpp"
|
||||
#include "ExpectQArraySsm.hpp"
|
||||
#include <xo/object2/Float.hpp>
|
||||
//#include "DExpectFormalArgSsm.hpp"
|
||||
//#include "ssm/ISyntaxStateMachine_DExpectFormalArgSsm.hpp"
|
||||
|
|
@ -25,13 +26,16 @@ namespace xo {
|
|||
// using xo::reflect::typeseq;
|
||||
|
||||
namespace scm {
|
||||
DExpectQLiteralSsm::DExpectQLiteralSsm(bool cxl_on_rightparen)
|
||||
: cxl_on_rightparen_{cxl_on_rightparen}
|
||||
DExpectQLiteralSsm::DExpectQLiteralSsm(bool cxl_on_rightparen,
|
||||
bool cxl_on_rightbracket)
|
||||
: cxl_on_rightparen_{cxl_on_rightparen},
|
||||
cxl_on_rightbracket_{cxl_on_rightbracket}
|
||||
{}
|
||||
|
||||
DExpectQLiteralSsm *
|
||||
DExpectQLiteralSsm::_make(DArena & arena,
|
||||
bool cxl_on_rightparen)
|
||||
bool cxl_on_rightparen,
|
||||
bool cxl_on_rightbracket)
|
||||
{
|
||||
/* out-of-order so argl follows ssm in arena,
|
||||
* consistent with any subsequent arglist realloc.
|
||||
|
|
@ -40,26 +44,30 @@ namespace xo {
|
|||
|
||||
void * mem = arena.alloc_for<DExpectQLiteralSsm>();
|
||||
|
||||
return new (mem) DExpectQLiteralSsm(cxl_on_rightparen);
|
||||
return new (mem) DExpectQLiteralSsm(cxl_on_rightparen, cxl_on_rightbracket);
|
||||
}
|
||||
|
||||
obj<ASyntaxStateMachine,DExpectQLiteralSsm>
|
||||
DExpectQLiteralSsm::make(DArena & arena,
|
||||
bool cxl_on_rightparen)
|
||||
bool cxl_on_rightparen,
|
||||
bool cxl_on_rightbracket)
|
||||
{
|
||||
obj<ASyntaxStateMachine,DExpectQLiteralSsm> retval(_make(arena,
|
||||
cxl_on_rightparen));
|
||||
cxl_on_rightparen,
|
||||
cxl_on_rightbracket));
|
||||
return retval;
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQLiteralSsm::start(ParserStateMachine * p_psm,
|
||||
bool cxl_on_rightparen)
|
||||
bool cxl_on_rightparen,
|
||||
bool cxl_on_rightbracket)
|
||||
{
|
||||
DArena::Checkpoint ckp = p_psm->parser_alloc().checkpoint();
|
||||
|
||||
p_psm->push_ssm(ckp, DExpectQLiteralSsm::make(p_psm->parser_alloc(),
|
||||
cxl_on_rightparen));
|
||||
cxl_on_rightparen,
|
||||
cxl_on_rightbracket));
|
||||
}
|
||||
|
||||
syntaxstatetype
|
||||
|
|
@ -78,6 +86,7 @@ namespace xo {
|
|||
ParserStateMachine * p_psm)
|
||||
{
|
||||
switch (tk.tk_type()) {
|
||||
|
||||
case tokentype::tk_f64:
|
||||
this->on_f64_token(tk, p_psm);
|
||||
return;
|
||||
|
|
@ -90,6 +99,14 @@ namespace xo {
|
|||
this->on_rightparen_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_leftbracket:
|
||||
this->on_leftbracket_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_rightbracket:
|
||||
this->on_rightbracket_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_comma:
|
||||
case tokentype::tk_lambda:
|
||||
case tokentype::tk_def:
|
||||
|
|
@ -103,8 +120,6 @@ namespace xo {
|
|||
case tokentype::tk_semicolon:
|
||||
case tokentype::tk_invalid:
|
||||
case tokentype::tk_quote:
|
||||
case tokentype::tk_leftbracket:
|
||||
case tokentype::tk_rightbracket:
|
||||
case tokentype::tk_leftbrace:
|
||||
case tokentype::tk_rightbrace:
|
||||
case tokentype::tk_leftangle:
|
||||
|
|
@ -200,22 +215,6 @@ namespace xo {
|
|||
p_psm->on_token(tk);
|
||||
}
|
||||
|
||||
#ifdef NOT_YET
|
||||
void
|
||||
DExpectQLiteralSsm::on_comma_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (fastate_ == formalarglstatetype::argl_1b) {
|
||||
this->fastate_ = formalarglstatetype::argl_1a;
|
||||
|
||||
DExpectFormalArgSsm::start(p_psm);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::on_token(tk, p_psm);
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
DExpectQLiteralSsm::on_rightparen_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
|
|
@ -226,7 +225,31 @@ namespace xo {
|
|||
return;
|
||||
}
|
||||
|
||||
Super::on_token(tk, p_psm);
|
||||
Super::illegal_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQLiteralSsm::on_leftbracket_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
// replace self with specialized version for parsing a literal array
|
||||
|
||||
p_psm->pop_ssm();
|
||||
DExpectQArraySsm::start(p_psm);
|
||||
p_psm->on_token(tk);
|
||||
}
|
||||
|
||||
void
|
||||
DExpectQLiteralSsm::on_rightbracket_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
if (cxl_on_rightbracket_) {
|
||||
p_psm->pop_ssm();
|
||||
p_psm->on_token(tk);
|
||||
return;
|
||||
}
|
||||
|
||||
Super::illegal_token(tk, p_psm);
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
|
|||
28
src/reader2/IPrintable_DExpectQArraySsm.cpp
Normal file
28
src/reader2/IPrintable_DExpectQArraySsm.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/** @file IPrintable_DExpectQArraySsm.cpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DExpectQArraySsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DExpectQArraySsm.json5]
|
||||
**/
|
||||
|
||||
#include "ssm/IPrintable_DExpectQArraySsm.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
auto
|
||||
IPrintable_DExpectQArraySsm::pretty(const DExpectQArraySsm & self, const ppindentinfo & ppii) -> bool
|
||||
{
|
||||
return self.pretty(ppii);
|
||||
}
|
||||
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end IPrintable_DExpectQArraySsm.cpp */
|
||||
79
src/reader2/ISyntaxStateMachine_DExpectQArraySsm.cpp
Normal file
79
src/reader2/ISyntaxStateMachine_DExpectQArraySsm.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/** @file ISyntaxStateMachine_DExpectQArraySsm.cpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/ISyntaxStateMachine_DExpectQArraySsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_any.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/ISyntaxStateMachine_DExpectQArraySsm.json5]
|
||||
**/
|
||||
|
||||
#include "ssm/ISyntaxStateMachine_DExpectQArraySsm.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::ssm_type(const DExpectQArraySsm & self) noexcept -> syntaxstatetype
|
||||
{
|
||||
return self.ssm_type();
|
||||
}
|
||||
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::get_expect_str(const DExpectQArraySsm & self) noexcept -> std::string_view
|
||||
{
|
||||
return self.get_expect_str();
|
||||
}
|
||||
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_token(DExpectQArraySsm & self, const Token & tk, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_token(tk, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_symbol(DExpectQArraySsm & self, std::string_view sym, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_symbol(sym, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_typedescr(DExpectQArraySsm & self, TypeDescr td, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_typedescr(td, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_formal(DExpectQArraySsm & self, const DUniqueString * param_name, TypeDescr param_type, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_formal(param_name, param_type, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_formal_with_token(DExpectQArraySsm & self, const DUniqueString * param_name, TypeDescr param_type, const Token & tk, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_formal_with_token(param_name, param_type, tk, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_formal_arglist(DExpectQArraySsm & self, DArray * arglist, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_formal_arglist(arglist, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_expression(DExpectQArraySsm & self, obj<AExpression> expr, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_expression(expr, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_parsed_expression_with_token(DExpectQArraySsm & self, obj<AExpression> expr, const Token & tk, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_parsed_expression_with_token(expr, tk, p_psm);
|
||||
}
|
||||
auto
|
||||
ISyntaxStateMachine_DExpectQArraySsm::on_quoted_literal(DExpectQArraySsm & self, obj<AGCObject> lit, ParserStateMachine * p_psm) -> void
|
||||
{
|
||||
self.on_quoted_literal(lit, p_psm);
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end ISyntaxStateMachine_DExpectQArraySsm.cpp */
|
||||
|
|
@ -23,6 +23,7 @@
|
|||
#include "ExpectExprSsm.hpp"
|
||||
#include "ExpectQLiteralSsm.hpp"
|
||||
#include "ExpectQListSsm.hpp"
|
||||
#include "ExpectQArraySsm.hpp"
|
||||
|
||||
#include <xo/printable2/detail/APrintable.hpp>
|
||||
#include <xo/facet/FacetRegistry.hpp>
|
||||
|
|
@ -87,6 +88,9 @@ namespace xo {
|
|||
FacetRegistry::register_impl<ASyntaxStateMachine, DExpectQListSsm>();
|
||||
FacetRegistry::register_impl<APrintable, DExpectQListSsm>();
|
||||
|
||||
FacetRegistry::register_impl<ASyntaxStateMachine, DExpectQArraySsm>();
|
||||
FacetRegistry::register_impl<APrintable, DExpectQArraySsm>();
|
||||
|
||||
// misc types showing up in aux arena
|
||||
TypeRegistry::register_type<SchematikaParser>();
|
||||
// misc types showing up in parser stack arena
|
||||
|
|
@ -103,6 +107,7 @@ namespace xo {
|
|||
log && log(xtag("DExpectExprSsm.tseq", typeseq::id<DExpectExprSsm>()));
|
||||
log && log(xtag("DExpectQLiteralSsm.tseq", typeseq::id<DExpectQLiteralSsm>()));
|
||||
log && log(xtag("DExpectQListSsm.tseq", typeseq::id<DExpectQListSsm>()));
|
||||
log && log(xtag("DExpectQArraySsm.tseq", typeseq::id<DExpectQArraySsm>()));
|
||||
|
||||
log && log(xtag("DProgressSsm.tseq", typeseq::id<DProgressSsm>()));
|
||||
log && log(xtag("DParenSsm.tseq", typeseq::id<DParenSsm>()));
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ namespace xo {
|
|||
return "expect-qliteral";
|
||||
case syntaxstatetype::expect_qlist:
|
||||
return "expect-qlist";
|
||||
case syntaxstatetype::expect_qarray:
|
||||
return "expect-qarray";
|
||||
case syntaxstatetype::N:
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue