xo-reader: feat: + var lookup in envframestack, psm
This commit is contained in:
parent
3b57a1f142
commit
eed5cdf691
4 changed files with 51 additions and 0 deletions
|
|
@ -13,12 +13,21 @@ namespace xo {
|
|||
* @brief A stack of envframe objects
|
||||
**/
|
||||
class envframestack {
|
||||
public:
|
||||
using Variable = xo::ast::Variable;
|
||||
|
||||
public:
|
||||
envframestack() {}
|
||||
|
||||
bool empty() const { return stack_.empty(); }
|
||||
std::size_t size() const { return stack_.size(); }
|
||||
|
||||
/** lookup variable in environment stack.
|
||||
* Visit frames in fifo order, report first match;
|
||||
* nullptr if no matches.
|
||||
**/
|
||||
rp<Variable> lookup(const std::string & x) const;
|
||||
|
||||
envframe & top_envframe();
|
||||
void push_envframe(envframe x);
|
||||
void pop_envframe();
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ namespace xo {
|
|||
class parserstatemachine {
|
||||
public:
|
||||
using Expression = xo::ast::Expression;
|
||||
using Variable = xo::ast::Variable;
|
||||
|
||||
public:
|
||||
parserstatemachine(exprstatestack * p_stack,
|
||||
|
|
@ -33,6 +34,14 @@ namespace xo {
|
|||
exprstate & top_exprstate();
|
||||
void push_exprstate(std::unique_ptr<exprstate> x);
|
||||
|
||||
/** lookup variable name in lexical context represented by
|
||||
* this psm. nullptr if not found
|
||||
**/
|
||||
rp<Variable> lookup_var(const std::string & x) const;
|
||||
|
||||
void push_envframe(envframe x);
|
||||
void pop_envframe();
|
||||
|
||||
public:
|
||||
/** stack of incomplete parser work.
|
||||
* generally speaking, push when to start new work for nested content;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
#include "envframestack.hpp"
|
||||
|
||||
namespace xo {
|
||||
using xo::ast::Variable;
|
||||
|
||||
namespace scm {
|
||||
envframe &
|
||||
envframestack::top_envframe() {
|
||||
|
|
@ -50,6 +52,20 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
rp<Variable>
|
||||
envframestack::lookup(const std::string & x) const {
|
||||
for (std::size_t i = 0, z = this->size(); i < z; ++i) {
|
||||
const auto & frame = (*this)[i];
|
||||
|
||||
auto retval = frame.lookup(x);
|
||||
|
||||
if (retval)
|
||||
return retval;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
envframestack::print(std::ostream & os) const {
|
||||
os << "<envframestack"
|
||||
|
|
|
|||
|
|
@ -7,7 +7,14 @@
|
|||
#include "exprstatestack.hpp"
|
||||
|
||||
namespace xo {
|
||||
using xo::ast::Variable;
|
||||
|
||||
namespace scm {
|
||||
rp<Variable>
|
||||
parserstatemachine::lookup_var(const std::string & x) const {
|
||||
return p_env_stack_->lookup(x);
|
||||
}
|
||||
|
||||
std::unique_ptr<exprstate>
|
||||
parserstatemachine::pop_exprstate() {
|
||||
return p_stack_->pop_exprstate();
|
||||
|
|
@ -22,6 +29,16 @@ namespace xo {
|
|||
parserstatemachine::push_exprstate(std::unique_ptr<exprstate> x) {
|
||||
p_stack_->push_exprstate(std::move(x));
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::push_envframe(envframe x) {
|
||||
p_env_stack_->push_envframe(std::move(x));
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::pop_envframe() {
|
||||
p_env_stack_->pop_envframe();
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue