xo-reader: handle sequence with embedded local vars

This commit is contained in:
Roland Conybeare 2024-08-23 10:52:51 -04:00
commit 7fad60290d
5 changed files with 225 additions and 8 deletions

View file

@ -0,0 +1,57 @@
/* file let1_xs.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "exprstate.hpp"
namespace xo {
namespace scm {
class let1_xs : public exprstate {
public:
/** given local definition equivalent to
* def lhs_name = rhs
* rest...
* parse sequence of incoming expressions rest... (until '}')
*
* Result expression creates and inits @p lhs_name,
* then evaluates expressions that follow definition
* up to same-level '}'
**/
static void start(const std::string & lhs_name,
const rp<Expression> & rhs,
parserstatemachine * p_psm);
virtual void on_expr(ref::brw<Expression> expr,
parserstatemachine * p_psm) override;
virtual void on_rightbrace_token(const token_type & tk,
parserstatemachine * p_psm) override;
private:
let1_xs(std::string lhs_name,
rp<Expression> rhs);
/** named ctor idiom **/
static std::unique_ptr<let1_xs> make(std::string lhs_name,
rp<Expression> rhs);
private:
/** name for new local variable **/
std::string lhs_name_;
/** set initial value for @ref lhs_name_ from value of this expression **/
rp<Expression> rhs_;
/** evaluate expressions in this sequence, in order, in environment
* with variable @ref lhs_name_ defined
**/
std::vector<rp<Expression>> expr_v_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end let1_xs.hpp */

View file

@ -6,13 +6,22 @@
#pragma once
#include "exprstate.hpp"
#include <vector>
namespace xo {
namespace ast { class Sequence; }
namespace ast { class Lambda; }
namespace scm {
class sequence_xs : public exprstate {
public:
sequence_xs();
using Sequence = xo::ast::Sequence;
using Lambda = xo::ast::Lambda;
public:
/** start parsing a sequence-expr.
* input begins with first expression in the sequence.
**/
static void start(parserstatemachine * p_psm);
virtual void on_expr(ref::brw<Expression> expr,
@ -22,9 +31,13 @@ namespace xo {
parserstatemachine * p_psm) override;
private:
sequence_xs();
/** named ctor idiom **/
static std::unique_ptr<sequence_xs> make();
private:
/** will build SequenceExpr from in-order contents of this vector **/
std::vector<rp<Expression>> expr_v_;
};
} /*namespace scm*/