xo-expression: generalize envframestack + handle explicit lm retturn
This commit is contained in:
parent
3c0dffe019
commit
44f463e359
9 changed files with 55 additions and 43 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 &
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(); }
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace xo {
|
|||
using xo::scm::Variable;
|
||||
|
||||
namespace scm {
|
||||
bp<LocalEnv>
|
||||
bp<Environment>
|
||||
envframestack::top_envframe() const {
|
||||
std::size_t z = stack_.size();
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ namespace xo {
|
|||
}
|
||||
|
||||
void
|
||||
envframestack::push_envframe(const rp<LocalEnv> & frame)
|
||||
envframestack::push_envframe(const rp<Environment> & frame)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag),
|
||||
|
|
@ -38,7 +38,7 @@ namespace xo {
|
|||
stack_[z] = frame;
|
||||
}
|
||||
|
||||
rp<LocalEnv>
|
||||
rp<Environment>
|
||||
envframestack::pop_envframe() {
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
|
@ -48,7 +48,7 @@ namespace xo {
|
|||
if (z > 0) {
|
||||
//std::unique_ptr<exprstate> top = std::move(stack_[z-1]);
|
||||
|
||||
rp<LocalEnv> retval = stack_.at(z-1);
|
||||
rp<Environment> retval = stack_.at(z-1);
|
||||
|
||||
stack_.resize(z-1);
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
bp<Variable>
|
||||
bp<Expression>
|
||||
envframestack::lookup(const std::string & x) const {
|
||||
for (std::size_t i = 0, z = this->size(); i < z; ++i) {
|
||||
const auto & frame = (*this)[i];
|
||||
|
|
|
|||
|
|
@ -156,6 +156,7 @@ namespace xo {
|
|||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr const char * c_self_name = "lambda_xs::on_typedescr";
|
||||
scope log(XO_DEBUG(p_psm->debug_flag()));
|
||||
|
||||
assert(td);
|
||||
|
||||
|
|
@ -189,13 +190,17 @@ namespace xo {
|
|||
&& (p_psm->env_stack_size() >= 2)
|
||||
)
|
||||
{
|
||||
bp<LocalEnv> def_env = p_psm->lookup_envframe(1);
|
||||
const define_xs * def_xs = dynamic_cast<const define_xs*>(&(p_psm->lookup_exprstate(2)));
|
||||
|
||||
assert(def_env->n_arg() == 1);
|
||||
assert(def_xs);
|
||||
|
||||
bp<Variable> def_var = def_env->lookup_arg(0).get();
|
||||
bp<Variable> def_var = def_xs->lhs_variable();
|
||||
|
||||
if (def_var->valuetype() == nullptr) {
|
||||
log && log("assign discovered lambda type T to enclosing define",
|
||||
xtag("lhs", def_var),
|
||||
xtag("T", print::unq(this->lambda_td_->canonical_name())));
|
||||
|
||||
def_var->assign_valuetype(lambda_td_);
|
||||
} else {
|
||||
/* don't need to unify here. if def already hasa a type,
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@
|
|||
#include "xo/expression/DefineExpr.hpp"
|
||||
#include "xo/expression/Constant.hpp"
|
||||
#include "xo/expression/ConvertExpr.hpp"
|
||||
//#include "xo/expression/GlobalEnv.hpp"
|
||||
#include "xo/expression/LocalEnv.hpp"
|
||||
#include "xo/expression/GlobalEnv.hpp"
|
||||
//#include "xo/expression/LocalEnv.hpp"
|
||||
//#include <regex>
|
||||
#include <stdexcept>
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ namespace xo {
|
|||
: psm_{debug_flag}
|
||||
{
|
||||
/* top-level environment. initially empty */
|
||||
rp<LocalEnv> toplevel_env = LocalEnv::make_empty();
|
||||
rp<Environment> toplevel_env = GlobalEnv::make_empty();
|
||||
|
||||
this->psm_.env_stack_.push_envframe(toplevel_env);
|
||||
}
|
||||
|
|
@ -70,6 +70,12 @@ namespace xo {
|
|||
return psm_.result_;
|
||||
} /*include_token*/
|
||||
|
||||
void
|
||||
parser::reset_result()
|
||||
{
|
||||
psm_.result_ = parser_result::none();
|
||||
}
|
||||
|
||||
void
|
||||
parser::reset_to_idle_toplevel()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace xo {
|
|||
namespace scm {
|
||||
bp<Variable>
|
||||
parserstatemachine::lookup_var(const std::string & x) const {
|
||||
return env_stack_.lookup(x);
|
||||
return Variable::from(env_stack_.lookup(x));
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -45,30 +45,28 @@ namespace xo {
|
|||
xs_stack_.push_exprstate(std::move(x));
|
||||
}
|
||||
|
||||
bp<LocalEnv>
|
||||
bp<Environment>
|
||||
parserstatemachine::top_envframe() const {
|
||||
return env_stack_.top_envframe();
|
||||
}
|
||||
|
||||
bp<LocalEnv>
|
||||
bp<Environment>
|
||||
parserstatemachine::lookup_envframe(std::size_t i) const {
|
||||
return env_stack_[i];
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::push_envframe(const rp<LocalEnv> & x) {
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("frame", x));
|
||||
|
||||
env_stack_.push_envframe(x);
|
||||
}
|
||||
|
||||
rp<LocalEnv>
|
||||
rp<Environment>
|
||||
parserstatemachine::pop_envframe() {
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
return env_stack_.pop_envframe();
|
||||
}
|
||||
|
|
@ -76,8 +74,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_expr(bp<Expression> x)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("x", x),
|
||||
xtag("psm", this));
|
||||
|
|
@ -88,8 +85,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_expr_with_semicolon(bp<Expression> x)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("x", x),
|
||||
xtag("psm", this));
|
||||
|
|
@ -102,8 +98,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_symbol(const std::string & x)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("x", x),
|
||||
xtag("psm", this));
|
||||
|
|
@ -114,8 +109,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_semicolon_token(const token_type & tk)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("tk", tk),
|
||||
xtag("psm", this));
|
||||
|
|
@ -126,8 +120,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_operator_token(const token_type & tk)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("tk", tk),
|
||||
xtag("psm", this));
|
||||
|
|
@ -138,8 +131,7 @@ namespace xo {
|
|||
void
|
||||
parserstatemachine::on_leftbrace_token(const token_type & tk)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
scope log(XO_DEBUG(debug_flag_));
|
||||
|
||||
log && log(xtag("tk", tk),
|
||||
xtag("psm", this));
|
||||
|
|
|
|||
|
|
@ -63,6 +63,10 @@ namespace xo {
|
|||
log && log(xtag("outcome", "victory!"),
|
||||
xtag("expr", parser_result.result_expr()));
|
||||
|
||||
rp<Expression> result_expr = parser_result.result_expr();
|
||||
|
||||
this->parser_.reset_result();
|
||||
|
||||
/* token completes an expression -> victory */
|
||||
return reader_result(parser_result.result_expr(),
|
||||
expr_span, parser_.stack_size(), reader_error());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue