xo-reader: wip: parsing lambda expressions [wip, non-functional]

This commit is contained in:
Roland Conybeare 2024-08-17 01:09:17 -04:00
commit 0841fd7dbd
10 changed files with 289 additions and 5 deletions

View file

@ -19,6 +19,9 @@ namespace xo {
static std::unique_ptr<expect_expr_xs> expect_rhs_expression();
virtual void on_lambda_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_leftparen_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;

View file

@ -0,0 +1,55 @@
/* file expect_formal_arglist_xs.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "exprstate.hpp"
#include "formal_arg.hpp"
#include <vector>
namespace xo {
namespace scm {
enum class formalarglstatetype {
invalid = -1,
argl_0,
argl_1,
n_formalarglstatetype,
};
/** @class expect_formal_arglist
* @brief parser state-machine for a formal parameter list
*
* ( name(1) : type(1), ..., )
* ^
* |
* argl_0
**/
class expect_formal_arglist_xs : public exprstate {
public:
expect_formal_arglist_xs();
static std::unique_ptr<expect_formal_arglist_xs> make();
const std::vector<formal_arg> & argl() const { return argl_; }
virtual void on_leftparen_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
private:
/** parsing state-machine state **/
formalarglstatetype farglxs_type_;
/** populate with (parmaeter-name, parameter-type) list
* as they're encountered
**/
std::vector<formal_arg> argl_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end expect_formal_arglist_xs.hpp */

View file

@ -59,6 +59,7 @@ namespace xo {
class exprstate {
public:
using Expression = xo::ast::Expression;
using Variable = xo::ast::Variable;
using exprtype = xo::ast::exprtype;
using token_type = token<char>;
using TypeDescr = xo::reflect::TypeDescr;
@ -92,9 +93,13 @@ namespace xo {
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** update exprstate when expecting a formal parameter **/
virtual void on_formal(const formal_arg & formal,
virtual void on_formal(const rp<Variable> & formal,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** update expression when epecting a formal parameter list **/
virtual void on_formal_arglist(const std::vector<rp<Variable>> & argl,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** print human-readable representation on @p os **/
virtual void print(std::ostream & os) const;
@ -103,6 +108,10 @@ namespace xo {
/** handle incoming 'def' token **/
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack);
/** handle incoming 'lambda' token **/
virtual void on_lambda_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** handle incoming symbol token **/
virtual void on_symbol_token(const token_type & tk,
exprstatestack * p_stack,

View file

@ -10,14 +10,60 @@
namespace xo {
namespace scm {
/**
* lambda ( name(1) : type(1), ..., ) body-expr ;
* ^ ^ ^ ^
* | | | |
* lm_0 lm_1 lm_2 lm_3
*
* lm_0 --on_lambda_token()--> lm_1
* lm_1 --on_formal_arglist()--> lm_2
* lm_2 --on_expr()--> lm_3
* lm_3 --on_semicolon_token()--> (done)
**/
enum class lambdastatetype {
invalid = -1,
lm_0,
lm_1,
lm_2,
lm_3,
n_lambdastatetype
};
/** @class lambda_xs
* @brief parsing state-machine for a lambda-expression
*
**/
class lambda_xs : public exprstate {
public:
lambda_xs();
static std::unique_ptr<lambda_xs> make();
virtual void on_lambda_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_formal_arglist(const std::vector<rp<Variable>> & argl,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_expr(ref::brw<Expression> expr,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_semicolon_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
private:
/** parsing state-machine state **/
lambdastatetype lmxs_type_;
/** formal parameter list **/
std::vector<rp<Variable>> argl_;
/** body expression **/
rp<Expression> body_;
};
} /*namespace scm*/
} /*namespace xo*/