xo-reader: distinguish interactive sessions
+ allow top-level i64 literals
This commit is contained in:
parent
2025969068
commit
a12a236bc1
16 changed files with 196 additions and 13 deletions
|
|
@ -97,6 +97,8 @@ namespace xo {
|
|||
parserstatemachine * p_psm) override;
|
||||
virtual void on_rightparen_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,9 @@ namespace xo {
|
|||
virtual void on_symbol_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -10,15 +10,37 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
enum class exprseqtype {
|
||||
/** toplevel interactive sequence. Most permissive **/
|
||||
toplevel_interactive,
|
||||
/** toplevel non-interactive sequence.
|
||||
* This used for top-level expressions in a translation unit.
|
||||
**/
|
||||
toplevel_batch,
|
||||
/** nested sequence, for example in function body **/
|
||||
nested,
|
||||
};
|
||||
|
||||
/** @class exprseq_xs
|
||||
* @brief parsing state-machine for top-level expression sequence
|
||||
*
|
||||
* expression sequences come in several types:
|
||||
* 1. top-level interactive
|
||||
* 2. top-level batch
|
||||
* 3. nested
|
||||
*
|
||||
* 1 2 3
|
||||
* +--------
|
||||
* def | y y y
|
||||
* symbol | y n n 1: evaluate as variable
|
||||
* i64 | y n n 1: evaluate as constant
|
||||
*
|
||||
**/
|
||||
class exprseq_xs : public exprstate {
|
||||
public:
|
||||
exprseq_xs();
|
||||
explicit exprseq_xs(exprseqtype seqtype);
|
||||
|
||||
static void start(parserstatemachine * p_psm);
|
||||
static void start(exprseqtype seqtype, parserstatemachine * p_psm);
|
||||
|
||||
public:
|
||||
// ----- token input methods -----
|
||||
|
|
@ -27,6 +49,8 @@ namespace xo {
|
|||
parserstatemachine * p_psm) override;
|
||||
virtual void on_symbol_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
// ----- victory methods -----
|
||||
|
||||
|
|
@ -34,9 +58,14 @@ namespace xo {
|
|||
parserstatemachine * p_psm) override;
|
||||
virtual void on_expr(ref::brw<Expression> expr,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_expr_with_semicolon(ref::brw<Expression> expr,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
private:
|
||||
static std::unique_ptr<exprseq_xs> make();
|
||||
static std::unique_ptr<exprseq_xs> make(exprseqtype seqtype);
|
||||
|
||||
/** context for this expression sequence **/
|
||||
exprseqtype xseqtype_;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** Identify current state in an expression state machine
|
||||
**/
|
||||
enum class exprstatetype {
|
||||
invalid = -1,
|
||||
|
||||
|
|
@ -172,6 +174,10 @@ namespace xo {
|
|||
virtual void on_operator_token(const token_type & tk,
|
||||
parserstatemachine * p_psm);
|
||||
|
||||
/** handle incoming integer-literal token **/
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm);
|
||||
|
||||
/** handle incoming floating-point-literal token **/
|
||||
virtual void on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm);
|
||||
|
|
|
|||
|
|
@ -64,6 +64,8 @@ namespace xo {
|
|||
parserstatemachine * p_psm) override;
|
||||
virtual void on_rightparen_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -59,10 +59,16 @@ namespace xo {
|
|||
* }
|
||||
*
|
||||
* Grammar:
|
||||
* toplevel-program = $expression(1); ..; $expression(n)
|
||||
* toplevel-program = $toplevel-expression(1); ..; $toplevel-expression(n)
|
||||
*
|
||||
* if interactive:
|
||||
* toplevel-expression = expression
|
||||
* else
|
||||
* toplevel-expression = type-decl | define-expr
|
||||
*
|
||||
* type-decl = decltype $typename [<$tp1 .. $tpn>]
|
||||
* expression = define-expr
|
||||
* expression = type-decl
|
||||
* | define-expr
|
||||
* | literal-expr
|
||||
* | variable-expr
|
||||
* | apply-expr
|
||||
|
|
@ -185,6 +191,10 @@ namespace xo {
|
|||
**/
|
||||
bool has_incomplete_expr() const;
|
||||
|
||||
/** put parser into state for beginning an interactive session.
|
||||
**/
|
||||
void begin_interactive_session();
|
||||
|
||||
/** put parser into state for beginning of a translation unit
|
||||
* (i.e. input stream)
|
||||
**/
|
||||
|
|
|
|||
|
|
@ -89,6 +89,9 @@ namespace xo {
|
|||
virtual void on_operator_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -67,6 +67,15 @@ namespace xo {
|
|||
public:
|
||||
reader() = default;
|
||||
|
||||
/** call once before calling .read_expr()
|
||||
* for a new interactive session
|
||||
**/
|
||||
void begin_interactive_session();
|
||||
|
||||
/** counterpart to .begin_interactive_session()
|
||||
**/
|
||||
reader_result end_interactive_session();
|
||||
|
||||
/** call once before calling .read_expr():
|
||||
* 1. with new reader
|
||||
* 2. if last read_expr() call had eof=true
|
||||
|
|
|
|||
|
|
@ -245,6 +245,18 @@ namespace xo {
|
|||
this->illegal_input_error(self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
define_xs::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
constexpr const char * self_name = "define_xs::on_i64";
|
||||
|
||||
this->illegal_input_error(self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
define_xs::on_f64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
|
|
|
|||
|
|
@ -172,6 +172,17 @@ namespace xo {
|
|||
return;
|
||||
}
|
||||
|
||||
void
|
||||
expect_expr_xs::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
progress_xs::start
|
||||
(Constant<int64_t>::make(tk.i64_value()),
|
||||
p_psm);
|
||||
}
|
||||
|
||||
void
|
||||
expect_expr_xs::on_f64_token(const token_type & tk,
|
||||
|
|
|
|||
|
|
@ -3,25 +3,28 @@
|
|||
#include "exprseq_xs.hpp"
|
||||
#include "parserstatemachine.hpp"
|
||||
#include "exprstatestack.hpp"
|
||||
#include "exprseq_xs.hpp"
|
||||
#include "expect_expr_xs.hpp"
|
||||
#include "define_xs.hpp"
|
||||
#include "expect_symbol_xs.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
std::unique_ptr<exprseq_xs>
|
||||
exprseq_xs::make()
|
||||
exprseq_xs::make(exprseqtype seqtype)
|
||||
{
|
||||
return std::make_unique<exprseq_xs>(exprseq_xs());
|
||||
return std::make_unique<exprseq_xs>(exprseq_xs(seqtype));
|
||||
}
|
||||
|
||||
void
|
||||
exprseq_xs::start(parserstatemachine * p_psm)
|
||||
exprseq_xs::start(exprseqtype seqtype, parserstatemachine * p_psm)
|
||||
{
|
||||
p_psm->push_exprstate(exprseq_xs::make());
|
||||
p_psm->push_exprstate(exprseq_xs::make(seqtype));
|
||||
}
|
||||
|
||||
exprseq_xs::exprseq_xs()
|
||||
: exprstate(exprstatetype::expect_toplevel_expression_sequence)
|
||||
exprseq_xs::exprseq_xs(exprseqtype x)
|
||||
: exprstate(exprstatetype::expect_toplevel_expression_sequence),
|
||||
xseqtype_{x}
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -51,6 +54,24 @@ namespace xo {
|
|||
this->illegal_input_error(c_self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
exprseq_xs::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
constexpr const char * c_self_name = "exprseq_xs::on_i64_token";
|
||||
|
||||
if (xseqtype_ == exprseqtype::toplevel_interactive)
|
||||
{
|
||||
expect_expr_xs::start(p_psm);
|
||||
p_psm->top_exprstate().on_i64_token(tk, p_psm);
|
||||
} else {
|
||||
this->illegal_input_error(c_self_name, tk);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
exprseq_xs::on_typedescr(TypeDescr /*td*/,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
|
|
@ -73,6 +94,21 @@ namespace xo {
|
|||
*p_emit_expr = expr.promote();
|
||||
} /*on_expr*/
|
||||
|
||||
void
|
||||
exprseq_xs::on_expr_with_semicolon(ref::brw<Expression> expr,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
/* toplevel expression sequence accepts an
|
||||
* arbitrary number of expressions.
|
||||
*
|
||||
* semicolons are sometimes mandatory to avoid ambiguity.
|
||||
*/
|
||||
|
||||
auto p_emit_expr = p_psm->p_emit_expr_;
|
||||
|
||||
*p_emit_expr = expr.promote();
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
|
|
@ -251,6 +251,18 @@ namespace xo {
|
|||
this->illegal_input_error(self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
exprstate::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
constexpr const char * self_name = "exprstate::on_i64";
|
||||
|
||||
this->illegal_input_error(self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
exprstate::on_f64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
|
|
@ -284,7 +296,7 @@ namespace xo {
|
|||
return;
|
||||
|
||||
case tokentype::tk_i64:
|
||||
assert(false);
|
||||
this->on_i64_token(tk, p_psm);
|
||||
return;
|
||||
|
||||
case tokentype::tk_f64:
|
||||
|
|
|
|||
|
|
@ -170,6 +170,18 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
paren_xs::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
constexpr const char * c_self_name = "paren_xs::on_i64";
|
||||
|
||||
this->illegal_input_error(c_self_name, tk);
|
||||
}
|
||||
|
||||
void
|
||||
paren_xs::on_f64_token(const token_type & tk,
|
||||
parserstatemachine * /*p_psm*/)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,16 @@ namespace xo {
|
|||
return !xs_stack_.empty();
|
||||
}
|
||||
|
||||
void
|
||||
parser::begin_interactive_session() {
|
||||
/* note: not using emit expr here */
|
||||
parserstatemachine psm(&xs_stack_,
|
||||
&env_stack_,
|
||||
nullptr /*p_emit_expr*/);
|
||||
|
||||
exprseq_xs::start(exprseqtype::toplevel_interactive, &psm);
|
||||
}
|
||||
|
||||
void
|
||||
parser::begin_translation_unit() {
|
||||
/* note: not using emit expr here */
|
||||
|
|
@ -36,7 +46,7 @@ namespace xo {
|
|||
&env_stack_,
|
||||
nullptr /*p_emit_expr*/);
|
||||
|
||||
exprseq_xs::start(&psm);
|
||||
exprseq_xs::start(exprseqtype::toplevel_batch, &psm);
|
||||
}
|
||||
|
||||
rp<Expression>
|
||||
|
|
|
|||
|
|
@ -401,6 +401,22 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
progress_xs::on_i64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
constexpr const char * self_name = "progress_xs::on_i64";
|
||||
|
||||
if (this->op_type_ == optype::invalid) {
|
||||
this->illegal_input_error(self_name, tk);
|
||||
} else {
|
||||
exprstate::on_i64_token(tk, p_psm);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
progress_xs::on_f64_token(const token_type & tk,
|
||||
parserstatemachine * p_psm)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,16 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
void
|
||||
reader::begin_interactive_session() {
|
||||
parser_.begin_interactive_session();
|
||||
}
|
||||
|
||||
reader_result
|
||||
reader::end_interactive_session() {
|
||||
return this->read_expr(span_type(nullptr, nullptr), true /*eof*/);
|
||||
}
|
||||
|
||||
void
|
||||
reader::begin_translation_unit() {
|
||||
parser_.begin_translation_unit();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue