xo-reader: distinguish interactive sessions

+ allow top-level i64 literals
This commit is contained in:
Roland Conybeare 2025-07-04 10:10:40 -05:00
commit a12a236bc1
16 changed files with 196 additions and 13 deletions

View file

@ -97,6 +97,8 @@ namespace xo {
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_rightparen_token(const token_type & tk, virtual void on_rightparen_token(const token_type & tk,
parserstatemachine * p_psm) override; 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, virtual void on_f64_token(const token_type & tk,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;

View file

@ -41,6 +41,9 @@ namespace xo {
virtual void on_symbol_token(const token_type & tk, virtual void on_symbol_token(const token_type & tk,
parserstatemachine * p_psm) override; 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, virtual void on_f64_token(const token_type & tk,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;

View file

@ -10,15 +10,37 @@
namespace xo { namespace xo {
namespace scm { 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 /** @class exprseq_xs
* @brief parsing state-machine for top-level expression sequence * @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 { class exprseq_xs : public exprstate {
public: public:
exprseq_xs(); explicit exprseq_xs(exprseqtype seqtype);
static void start(parserstatemachine * p_psm); static void start(exprseqtype seqtype, parserstatemachine * p_psm);
public: public:
// ----- token input methods ----- // ----- token input methods -----
@ -27,6 +49,8 @@ namespace xo {
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_symbol_token(const token_type & tk, virtual void on_symbol_token(const token_type & tk,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_i64_token(const token_type & tk,
parserstatemachine * p_psm) override;
// ----- victory methods ----- // ----- victory methods -----
@ -34,9 +58,14 @@ namespace xo {
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_expr(ref::brw<Expression> expr, virtual void on_expr(ref::brw<Expression> expr,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_expr_with_semicolon(ref::brw<Expression> expr,
parserstatemachine * p_psm) override;
private: 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 scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -12,6 +12,8 @@
namespace xo { namespace xo {
namespace scm { namespace scm {
/** Identify current state in an expression state machine
**/
enum class exprstatetype { enum class exprstatetype {
invalid = -1, invalid = -1,
@ -172,6 +174,10 @@ namespace xo {
virtual void on_operator_token(const token_type & tk, virtual void on_operator_token(const token_type & tk,
parserstatemachine * p_psm); 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 **/ /** handle incoming floating-point-literal token **/
virtual void on_f64_token(const token_type & tk, virtual void on_f64_token(const token_type & tk,
parserstatemachine * p_psm); parserstatemachine * p_psm);

View file

@ -64,6 +64,8 @@ namespace xo {
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;
virtual void on_rightparen_token(const token_type & tk, virtual void on_rightparen_token(const token_type & tk,
parserstatemachine * p_psm) override; 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, virtual void on_f64_token(const token_type & tk,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;

View file

@ -59,10 +59,16 @@ namespace xo {
* } * }
* *
* Grammar: * 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>] * type-decl = decltype $typename [<$tp1 .. $tpn>]
* expression = define-expr * expression = type-decl
* | define-expr
* | literal-expr * | literal-expr
* | variable-expr * | variable-expr
* | apply-expr * | apply-expr
@ -185,6 +191,10 @@ namespace xo {
**/ **/
bool has_incomplete_expr() const; 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 /** put parser into state for beginning of a translation unit
* (i.e. input stream) * (i.e. input stream)
**/ **/

View file

@ -89,6 +89,9 @@ namespace xo {
virtual void on_operator_token(const token_type & tk, virtual void on_operator_token(const token_type & tk,
parserstatemachine * p_psm) override; 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, virtual void on_f64_token(const token_type & tk,
parserstatemachine * p_psm) override; parserstatemachine * p_psm) override;

View file

@ -67,6 +67,15 @@ namespace xo {
public: public:
reader() = default; 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(): /** call once before calling .read_expr():
* 1. with new reader * 1. with new reader
* 2. if last read_expr() call had eof=true * 2. if last read_expr() call had eof=true

View file

@ -245,6 +245,18 @@ namespace xo {
this->illegal_input_error(self_name, tk); 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 void
define_xs::on_f64_token(const token_type & tk, define_xs::on_f64_token(const token_type & tk,
parserstatemachine * /*p_psm*/) parserstatemachine * /*p_psm*/)

View file

@ -172,6 +172,17 @@ namespace xo {
return; 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 void
expect_expr_xs::on_f64_token(const token_type & tk, expect_expr_xs::on_f64_token(const token_type & tk,

View file

@ -3,25 +3,28 @@
#include "exprseq_xs.hpp" #include "exprseq_xs.hpp"
#include "parserstatemachine.hpp" #include "parserstatemachine.hpp"
#include "exprstatestack.hpp" #include "exprstatestack.hpp"
#include "exprseq_xs.hpp"
#include "expect_expr_xs.hpp"
#include "define_xs.hpp" #include "define_xs.hpp"
#include "expect_symbol_xs.hpp" #include "expect_symbol_xs.hpp"
namespace xo { namespace xo {
namespace scm { namespace scm {
std::unique_ptr<exprseq_xs> 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 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() exprseq_xs::exprseq_xs(exprseqtype x)
: exprstate(exprstatetype::expect_toplevel_expression_sequence) : exprstate(exprstatetype::expect_toplevel_expression_sequence),
xseqtype_{x}
{ {
} }
@ -51,6 +54,24 @@ namespace xo {
this->illegal_input_error(c_self_name, tk); 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 void
exprseq_xs::on_typedescr(TypeDescr /*td*/, exprseq_xs::on_typedescr(TypeDescr /*td*/,
parserstatemachine * /*p_psm*/) parserstatemachine * /*p_psm*/)
@ -73,6 +94,21 @@ namespace xo {
*p_emit_expr = expr.promote(); *p_emit_expr = expr.promote();
} /*on_expr*/ } /*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 scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -251,6 +251,18 @@ namespace xo {
this->illegal_input_error(self_name, tk); 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 void
exprstate::on_f64_token(const token_type & tk, exprstate::on_f64_token(const token_type & tk,
parserstatemachine * /*p_psm*/) parserstatemachine * /*p_psm*/)
@ -284,7 +296,7 @@ namespace xo {
return; return;
case tokentype::tk_i64: case tokentype::tk_i64:
assert(false); this->on_i64_token(tk, p_psm);
return; return;
case tokentype::tk_f64: case tokentype::tk_f64:

View file

@ -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 void
paren_xs::on_f64_token(const token_type & tk, paren_xs::on_f64_token(const token_type & tk,
parserstatemachine * /*p_psm*/) parserstatemachine * /*p_psm*/)

View file

@ -29,6 +29,16 @@ namespace xo {
return !xs_stack_.empty(); 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 void
parser::begin_translation_unit() { parser::begin_translation_unit() {
/* note: not using emit expr here */ /* note: not using emit expr here */
@ -36,7 +46,7 @@ namespace xo {
&env_stack_, &env_stack_,
nullptr /*p_emit_expr*/); nullptr /*p_emit_expr*/);
exprseq_xs::start(&psm); exprseq_xs::start(exprseqtype::toplevel_batch, &psm);
} }
rp<Expression> rp<Expression>

View file

@ -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 void
progress_xs::on_f64_token(const token_type & tk, progress_xs::on_f64_token(const token_type & tk,
parserstatemachine * p_psm) parserstatemachine * p_psm)

View file

@ -4,6 +4,16 @@
namespace xo { namespace xo {
namespace scm { 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 void
reader::begin_translation_unit() { reader::begin_translation_unit() {
parser_.begin_translation_unit(); parser_.begin_translation_unit();