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;
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;

View file

@ -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;

View file

@ -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*/

View file

@ -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);

View file

@ -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;

View file

@ -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)
**/

View file

@ -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;

View file

@ -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