Skip to content
Snippets Groups Projects
Commit 56028cb9 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Fix function arguments scope

Function has now its own symbol table so that arguments are locally defined
parent 51d2adcd
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -10,10 +10,25 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>
if (!n.children.empty()) {
std::shared_ptr block_symbol_table = std::make_shared<SymbolTable>(symbol_table);
n.m_symbol_table = block_symbol_table;
for (auto& child : n.children) {
this->buildSymbolTable(*child, block_symbol_table);
}
}
} else if (n.is<language::let_declaration>()) {
std::shared_ptr local_symbol_table = std::make_shared<SymbolTable>(symbol_table);
n.m_symbol_table = local_symbol_table;
const std::string& symbol = n.children[0]->string();
auto [i_symbol, success] = symbol_table->add(symbol, n.children[0]->begin());
if (not success) {
std::ostringstream error_message;
error_message << "symbol '" << rang::fg::red << symbol << rang::fg::reset << '\'' << " was already defined!";
throw parse_error(error_message.str(), std::vector{n.begin()});
}
for (auto& child : n.children) {
this->buildSymbolTable(*child, local_symbol_table);
}
} else {
n.m_symbol_table = symbol_table;
if (n.has_content()) {
......@@ -25,14 +40,6 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>
error_message << "symbol '" << rang::fg::red << symbol << rang::fg::reset << '\'' << " was already defined!";
throw parse_error(error_message.str(), std::vector{n.begin()});
}
} else if (n.is<language::let_declaration>()) {
const std::string& symbol = n.children[0]->string();
auto [i_symbol, success] = symbol_table->add(symbol, n.children[0]->begin());
if (not success) {
std::ostringstream error_message;
error_message << "symbol '" << rang::fg::red << symbol << rang::fg::reset << '\'' << " was already defined!";
throw parse_error(error_message.str(), std::vector{n.begin()});
}
} else if (n.is<language::function_definition>()) {
const std::string& symbol = n.children[0]->string();
auto [i_symbol, success] = symbol_table->add(symbol, n.children[0]->begin());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment