Skip to content

Commit

Permalink
fix: Interpreter now does an environment lookup for all identifier re…
Browse files Browse the repository at this point in the history
…ferences
  • Loading branch information
Asger Gitz-Johansen committed Aug 28, 2022
1 parent 0a7b2a2 commit e3250f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions include/drivers/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ namespace expr {
symbol_table_t result{};
symbol_value_t expression_result{};

static auto evaluate(const syntax_tree_t& tree, expr::arithmetic_operator& arith, expr::boolean_operator& boolean, expr::compare_operator& comparator) -> symbol_value_t;
static auto evaluate(const compiler::compiled_expr_collection_t& symbol_tree_map, expr::arithmetic_operator& arith, expr::boolean_operator& boolean, expr::compare_operator& comparator) -> symbol_table_t;
auto evaluate(const syntax_tree_t& tree) -> symbol_value_t;
auto evaluate(const compiler::compiled_expr_collection_t& tree) -> symbol_table_t;
static auto evaluate(const syntax_tree_t& tree, expr::arithmetic_operator& arith, expr::boolean_operator& boolean, expr::compare_operator& comparator, const symbol_table_t& symbols) -> symbol_value_t;
static auto evaluate(const compiler::compiled_expr_collection_t& symbol_tree_map, expr::arithmetic_operator& arith, expr::boolean_operator& boolean, expr::compare_operator& comparator, const symbol_table_t& symbols) -> symbol_table_t;

protected:
const symbol_table_t &environment{};
Expand Down
24 changes: 16 additions & 8 deletions src/drivers/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,27 @@ namespace expr {
}

void interpreter::add_tree(const syntax_tree_t& tree) {
expression_result = evaluate(tree, *this, *this, *this);
expression_result = evaluate(tree);
}

void interpreter::add_tree(const std::string& identifier, const syntax_tree_t& tree) {
result[identifier] = evaluate(tree, *this, *this, *this);
result[identifier] = evaluate(tree);
}

auto interpreter::evaluate(const syntax_tree_t& tree, arithmetic_operator& arith, boolean_operator& boolean, compare_operator& comparator) -> symbol_value_t {
auto interpreter::evaluate(const syntax_tree_t& tree) -> symbol_value_t {
return evaluate(tree, *this, *this, *this, environment);
}

auto interpreter::evaluate(const compiler::compiled_expr_collection_t& trees) -> symbol_table_t {
return evaluate(trees, *this, *this, *this, environment);
}

auto interpreter::evaluate(const syntax_tree_t& tree, arithmetic_operator& arith, boolean_operator& boolean, compare_operator& comparator, const symbol_table_t& symbols) -> symbol_value_t {
symbol_value_t v{};
auto eval_wrapper = [&](const syntax_tree_t& t) { return evaluate(t, arith, boolean, comparator); };
auto eval_wrapper = [&](const syntax_tree_t& t) { return evaluate(t, arith, boolean, comparator, symbols); };
std::visit(ya::overload(
[&v](const symbol_reference_t& r){ v = r->second; },
[&v](const c_symbol_reference_t& r){ v = r->second; },
[&](const symbol_reference_t& r){ v = symbols.at(r->first); }, // TODO: Should we look up every time? If so, what is the point of storing an iterator in the ast?
[&](const c_symbol_reference_t& r){ v = symbols.at(r->first); }, // TODO: Should we look up every time? If so, what is the point of storing an iterator in the ast?
[&](const operator_t& o) {
switch (o.operator_type) {
case operator_type_t::minus: v = arith.sub(eval_wrapper(tree.children[0]), eval_wrapper(tree.children[1])); break;
Expand Down Expand Up @@ -104,10 +112,10 @@ namespace expr {
return v;
}

auto interpreter::evaluate(const compiler::compiled_expr_collection_t& symbol_tree_map, expr::arithmetic_operator &arith, expr::boolean_operator &boolean, expr::compare_operator &comparator) -> symbol_table_t {
auto interpreter::evaluate(const compiler::compiled_expr_collection_t& symbol_tree_map, expr::arithmetic_operator &arith, expr::boolean_operator &boolean, expr::compare_operator &comparator, const symbol_table_t& symbols) -> symbol_table_t {
symbol_table_t result{};
for(auto& tree : symbol_tree_map)
result[tree.first] = evaluate(tree.second, arith, boolean, comparator);
result[tree.first] = evaluate(tree.second, arith, boolean, comparator, symbols);
return result;
}
}

0 comments on commit e3250f5

Please sign in to comment.