xo-expression: generalize envframestack + handle explicit lm retturn

This commit is contained in:
Roland Conybeare 2025-07-28 13:16:09 -04:00
commit 44f463e359
9 changed files with 55 additions and 43 deletions

View file

@ -79,6 +79,7 @@ namespace xo {
static void start(parserstatemachine * p_psm);
defexprstatetype defxs_type() const { return defxs_type_; }
bp<Variable> lhs_variable() const { return def_expr_->lhs_variable(); }
/** @return expected input in current state **/
virtual const char * get_expect_str() const override;

View file

@ -14,8 +14,6 @@ namespace xo {
**/
class envframestack {
public:
using LocalEnv = xo::scm::LocalEnv;
using Variable = xo::scm::Variable;
using ppstate = xo::print::ppstate;
using ppindentinfo = xo::print::ppindentinfo;
@ -29,23 +27,23 @@ namespace xo {
* Visit frames in fifo order, report first match;
* nullptr if no matches.
**/
bp<Variable> lookup(const std::string & x) const;
bp<Expression> lookup(const std::string & x) const;
/** update/replace binding for variable @p target.
* New binding may have a different type.
**/
void upsert(bp<Variable> target);
bp<LocalEnv> top_envframe() const;
void push_envframe(const rp<LocalEnv> & x);
rp<LocalEnv> pop_envframe();
bp<Environment> top_envframe() const;
void push_envframe(const rp<Environment> & x);
rp<Environment> pop_envframe();
void reset_to_toplevel() { stack_.resize(1); }
/** relative to top-of-stack.
* 0 -> top (last in), z-1 -> bottom (first in)
**/
bp<LocalEnv> operator[](std::size_t i) {
bp<Environment> operator[](std::size_t i) {
std::size_t z = stack_.size();
assert(i < z);
@ -53,7 +51,7 @@ namespace xo {
return stack_[z - i - 1].get();
}
bp<LocalEnv> operator[](std::size_t i) const {
bp<Environment> operator[](std::size_t i) const {
std::size_t z = stack_.size();
assert(i < z);
@ -65,7 +63,7 @@ namespace xo {
bool pretty_print(const ppindentinfo & ppii) const;
private:
std::vector<rp<LocalEnv>> stack_;
std::vector<rp<Environment>> stack_;
};
inline std::ostream &

View file

@ -219,6 +219,12 @@ namespace xo {
**/
const parser_result & include_token(const token_type & tk);
/** reset parsed result expression; use using return value from
* @ref include_token. Complicating api here to avoid copying parser_result
* on each token
**/
void reset_result();
/** reset to starting parsing state.
* use this after encountering an error, to avoid cascade of
* spurious secondary errors.. particularly important when

View file

@ -59,13 +59,13 @@ namespace xo {
void upsert_var(bp<Variable> x);
/** @return available variable bindings in current parsing state **/
bp<LocalEnv> top_envframe() const;
bp<Environment> top_envframe() const;
/** @return frame @p i levels from the top **/
bp<LocalEnv> lookup_envframe(std::size_t i) const;
bp<Environment> lookup_envframe(std::size_t i) const;
/** push frame @p x (with new variable bindings) onto environment stack **/
void push_envframe(const rp<LocalEnv> & x);
/** @return pop innermost environment frame and return it **/
rp<LocalEnv> pop_envframe();
rp<Environment> pop_envframe();
/** @return number of stacked environment frames **/
size_t env_stack_size() const { return env_stack_.size(); }