refactor(frontends/lean): simplify local_decls data-structure

This is the first step for fixing #584
This commit is contained in:
Leonardo de Moura 2015-05-07 11:10:15 -07:00
parent 5113f6b9d8
commit 5fdf140096
3 changed files with 16 additions and 30 deletions

View file

@ -13,7 +13,7 @@ Author: Leonardo de Moura
namespace lean {
/**
\brief A "scoped" map from name to values.
\brief A map from name to values.
It also supports the operation \c find_idx that returns "when a declaration was inserted into the map".
*/
@ -21,20 +21,17 @@ template<typename V>
class local_decls {
typedef name_map<pair<V, unsigned>> map;
typedef list<pair<name, V>> entries;
typedef std::tuple<map, unsigned, entries> scope;
typedef list<scope> scopes;
map m_map;
unsigned m_counter;
scopes m_scopes;
entries m_entries;
unsigned m_counter;
public:
local_decls():m_counter(1) {}
local_decls(local_decls const & d):
m_map(d.m_map), m_counter(d.m_counter), m_scopes(d.m_scopes), m_entries(d.m_entries) {}
m_map(d.m_map), m_entries(d.m_entries), m_counter(d.m_counter) {}
void insert(name const & k, V const & v) {
m_map.insert(k, mk_pair(v, m_counter));
m_counter++;
m_entries = cons(mk_pair(k, v), m_entries);
m_counter++;
}
void update(name const & k, V const & v) {
if (auto it = m_map.find(k)) {
@ -47,18 +44,6 @@ public:
unsigned find_idx(name const & k) const { auto it = m_map.find(k); return it ? it->second : 0; }
bool contains(name const & k) const { return m_map.contains(k); }
entries const & get_entries() const { return m_entries; }
void push() { m_scopes = cons(scope(m_map, m_counter, m_entries), m_scopes); }
void pop() {
lean_assert(!is_nil(m_scopes));
std::tie(m_map, m_counter, m_entries) = head(m_scopes);
m_scopes = tail(m_scopes);
}
bool has_scopes() const { return !is_nil(m_scopes); }
bool empty() const { return m_map.empty(); }
struct mk_scope {
local_decls & m_d;
mk_scope(local_decls & d):m_d(d) { m_d.push(); }
~mk_scope() { m_d.pop(); }
};
};
}

View file

@ -451,28 +451,26 @@ expr parser::mk_app(std::initializer_list<expr> const & args, pos_info const & p
}
void parser::push_local_scope(bool save_options) {
m_local_level_decls.push();
m_local_decls.push();
optional<options> opts;
if (save_options)
opts = m_ios.get_options();
m_parser_scope_stack = cons(parser_scope_stack_elem(opts, m_level_variables, m_variables, m_include_vars,
m_undef_ids.size(), m_has_params),
m_undef_ids.size(), m_has_params, m_local_level_decls,
m_local_decls),
m_parser_scope_stack);
}
void parser::pop_local_scope() {
if (!m_local_level_decls.has_scopes()) {
if (!m_parser_scope_stack) {
throw parser_error("invalid 'end', there is no open namespace/section", pos());
}
m_local_level_decls.pop();
m_local_decls.pop();
lean_assert(!is_nil(m_parser_scope_stack));
auto s = head(m_parser_scope_stack);
if (s.m_options) {
m_ios.set_options(*s.m_options);
updt_options();
}
m_local_level_decls = s.m_local_level_decls;
m_local_decls = s.m_local_decls;
m_level_variables = s.m_level_variables;
m_variables = s.m_variables;
m_include_vars = s.m_include_vars;
@ -912,8 +910,8 @@ void parser::parse_binders_core(buffer<expr> & r, buffer<notation_entry> * nentr
local_environment parser::parse_binders(buffer<expr> & r, buffer<notation_entry> * nentries,
bool & last_block_delimited, bool allow_empty, unsigned rbp,
bool simple_only) {
flet<environment> save(m_env, m_env); // save environment
local_expr_decls::mk_scope scope(m_local_decls);
flet<environment> save1(m_env, m_env); // save environment
flet<local_expr_decls> save2(m_local_decls, m_local_decls);
unsigned old_sz = r.size();
parse_binders_core(r, nentries, last_block_delimited, rbp, simple_only);
if (!allow_empty && old_sz == r.size())

View file

@ -54,10 +54,13 @@ struct parser_scope_stack_elem {
name_set m_include_vars;
unsigned m_num_undef_ids;
bool m_has_params;
local_level_decls m_local_level_decls;
local_expr_decls m_local_decls;
parser_scope_stack_elem(optional<options> const & o, name_set const & lvs, name_set const & vs, name_set const & ivs,
unsigned num_undef_ids, bool has_params):
unsigned num_undef_ids, bool has_params,
local_level_decls const & lld, local_expr_decls const & led):
m_options(o), m_level_variables(lvs), m_variables(vs), m_include_vars(ivs),
m_num_undef_ids(num_undef_ids), m_has_params(has_params) {}
m_num_undef_ids(num_undef_ids), m_has_params(has_params), m_local_level_decls(lld), m_local_decls(led) {}
};
typedef list<parser_scope_stack_elem> parser_scope_stack;