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

Change declaration treatment to mimic C++

Actually in C++
``
double x = 3;
{
  double x = x+1;
}
``
in the instruction, ``double x = x+1;``, the right-hand side `x` IS the
left-hand side and not the global one, which lead to a "use of non-initialized"
warning.

It is not clear to me why the RHS x is not the global one, and I think the
previous implementation was a possibility. One just can say that the chosen C++
policy implies that the above code is equivalent to
``
double x = 3;
{
  double x; x = x+1;
}
``
which was not the case for the previous pugs implementation, and that `x` is
found in the local symbol table as soon as it is declared (right after the `=`
symbol)
parent cbbf958d
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -19,7 +19,7 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable> ...@@ -19,7 +19,7 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>
if (n.has_content()) { if (n.has_content()) {
if (n.is<language::declaration>()) { if (n.is<language::declaration>()) {
const std::string& symbol = n.children[1]->string(); const std::string& symbol = n.children[1]->string();
auto [i_symbol, success] = symbol_table->add(symbol, n.children[1]->begin(), n.end()); auto [i_symbol, success] = symbol_table->add(symbol, n.children[1]->begin());
if (not success) { if (not success) {
std::ostringstream error_message; std::ostringstream error_message;
error_message << "symbol '" << rang::fg::red << symbol << rang::fg::reset << '\'' << " was already defined!"; error_message << "symbol '" << rang::fg::red << symbol << rang::fg::reset << '\'' << " was already defined!";
......
...@@ -19,7 +19,6 @@ class SymbolTable ...@@ -19,7 +19,6 @@ class SymbolTable
ASTNodeDataVariant m_value; ASTNodeDataVariant m_value;
TAO_PEGTL_NAMESPACE::position m_position; TAO_PEGTL_NAMESPACE::position m_position;
TAO_PEGTL_NAMESPACE::position m_end_of_declaration;
public: public:
auto& auto&
...@@ -58,12 +57,6 @@ class SymbolTable ...@@ -58,12 +57,6 @@ class SymbolTable
return m_position; return m_position;
} }
auto
endOfDeclaration() const
{
return m_end_of_declaration;
}
void void
setDataType(const ASTNodeDataType& data_type) setDataType(const ASTNodeDataType& data_type)
{ {
...@@ -87,9 +80,7 @@ class SymbolTable ...@@ -87,9 +80,7 @@ class SymbolTable
return os; return os;
} }
Attributes(const TAO_PEGTL_NAMESPACE::position& position, const TAO_PEGTL_NAMESPACE::position& end_of_declaration) Attributes(const TAO_PEGTL_NAMESPACE::position& position) : m_position(position) {}
: m_position(position), m_end_of_declaration(end_of_declaration)
{}
Attributes(const Attributes&) = default; Attributes(const Attributes&) = default;
}; };
...@@ -122,8 +113,7 @@ class SymbolTable ...@@ -122,8 +113,7 @@ class SymbolTable
} }
} }
if (i_symbol != m_symbol_list.end() and (use_position.byte > i_symbol->second.endOfDeclaration().byte or if (i_symbol != m_symbol_list.end() and (use_position.byte >= i_symbol->second.position().byte)) {
use_position.byte == i_symbol->second.position().byte)) {
return std::make_pair(i_symbol, true); return std::make_pair(i_symbol, true);
} else { } else {
if (m_parent_table) { if (m_parent_table) {
...@@ -135,9 +125,7 @@ class SymbolTable ...@@ -135,9 +125,7 @@ class SymbolTable
} }
auto auto
add(const std::string& symbol, add(const std::string& symbol, const TAO_PEGTL_NAMESPACE::position& symbol_position)
const TAO_PEGTL_NAMESPACE::position& symbol_position,
const TAO_PEGTL_NAMESPACE::position& declaration_end)
{ {
for (auto i_stored_symbol = m_symbol_list.begin(); i_stored_symbol != m_symbol_list.end(); ++i_stored_symbol) { for (auto i_stored_symbol = m_symbol_list.begin(); i_stored_symbol != m_symbol_list.end(); ++i_stored_symbol) {
if (i_stored_symbol->first == symbol) { if (i_stored_symbol->first == symbol) {
...@@ -145,7 +133,7 @@ class SymbolTable ...@@ -145,7 +133,7 @@ class SymbolTable
} }
} }
return std::make_pair(m_symbol_list.emplace(m_symbol_list.end(), return std::make_pair(m_symbol_list.emplace(m_symbol_list.end(),
std::make_pair(symbol, Attributes(symbol_position, declaration_end))), std::make_pair(symbol, Attributes(symbol_position))),
true); true);
} }
......
...@@ -13,13 +13,12 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -13,13 +13,12 @@ TEST_CASE("SymbolTable", "[language]")
using namespace TAO_PEGTL_NAMESPACE; using namespace TAO_PEGTL_NAMESPACE;
position begin_position{internal::iterator{"fixture"}, "fixture"}; position begin_position{internal::iterator{"fixture"}, "fixture"};
position end_declaration{internal::iterator{"fixture"}, "fixture"};
auto [i_symbol_a, created_a] = root_st->add("a", begin_position, end_declaration); auto [i_symbol_a, created_a] = root_st->add("a", begin_position);
REQUIRE(created_a); REQUIRE(created_a);
// Check that one cannot build another "a" in this table // Check that one cannot build another "a" in this table
REQUIRE(not root_st->add("a", begin_position, end_declaration).second); REQUIRE(not root_st->add("a", begin_position).second);
position use_position{internal::iterator{"fixture"}, "fixture"}; position use_position{internal::iterator{"fixture"}, "fixture"};
auto [i_search_a, found_a] = root_st->find("a", use_position); auto [i_search_a, found_a] = root_st->find("a", use_position);
...@@ -96,7 +95,7 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -96,7 +95,7 @@ TEST_CASE("SymbolTable", "[language]")
position begin_position{internal::iterator{"fixture"}, "fixture"}; position begin_position{internal::iterator{"fixture"}, "fixture"};
position end_declaration{internal::iterator{"fixture"}, "fixture"}; position end_declaration{internal::iterator{"fixture"}, "fixture"};
auto [i_root_symbol_a, created_root_a] = root_st->add("a", begin_position, end_declaration); auto [i_root_symbol_a, created_root_a] = root_st->add("a", begin_position);
REQUIRE(created_root_a); REQUIRE(created_root_a);
std::shared_ptr nested_st = std::make_shared<SymbolTable>(root_st); std::shared_ptr nested_st = std::make_shared<SymbolTable>(root_st);
...@@ -107,7 +106,7 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -107,7 +106,7 @@ TEST_CASE("SymbolTable", "[language]")
// symbol "a" is the one defined in root_st // symbol "a" is the one defined in root_st
REQUIRE(i_root_symbol_a == i_search_a); REQUIRE(i_root_symbol_a == i_search_a);
auto [i_nested_symbol_a, created_nested_a] = nested_st->add("a", begin_position, end_declaration); auto [i_nested_symbol_a, created_nested_a] = nested_st->add("a", begin_position);
REQUIRE(created_nested_a); REQUIRE(created_nested_a);
auto [i_search_nested_a, found_nested_a] = nested_st->find("a", use_position); auto [i_search_nested_a, found_nested_a] = nested_st->find("a", use_position);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment