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

Add missing tests for SymbolTable

parent d1b7dc82
Branches
Tags
1 merge request!104Add missing tests for ASTNodeBuiltinFunctionExpressionBuilder
...@@ -285,10 +285,8 @@ class SymbolTable ...@@ -285,10 +285,8 @@ class SymbolTable
std::vector<Symbol> builtin_function_symbol_list; std::vector<Symbol> builtin_function_symbol_list;
for (auto i_stored_symbol : m_symbol_list) { for (auto i_stored_symbol : m_symbol_list) {
if (use_position.byte < i_stored_symbol.attributes().position().byte)
continue;
// Symbol must be defined before the call // Symbol must be defined before the call
if (use_position.byte >= i_stored_symbol.attributes().position().byte) {
std::string_view stored_symbol_name = i_stored_symbol.name(); std::string_view stored_symbol_name = i_stored_symbol.name();
if ((stored_symbol_name.size() > symbol.size()) and (stored_symbol_name[symbol.size()] == ':')) { if ((stored_symbol_name.size() > symbol.size()) and (stored_symbol_name[symbol.size()] == ':')) {
if (stored_symbol_name.substr(0, symbol.size()) == symbol) { if (stored_symbol_name.substr(0, symbol.size()) == symbol) {
...@@ -296,6 +294,7 @@ class SymbolTable ...@@ -296,6 +294,7 @@ class SymbolTable
} }
} }
} }
}
if (m_parent_table) { if (m_parent_table) {
return m_parent_table->getBuiltinFunctionSymbolList(symbol, use_position); return m_parent_table->getBuiltinFunctionSymbolList(symbol, use_position);
...@@ -308,10 +307,8 @@ class SymbolTable ...@@ -308,10 +307,8 @@ class SymbolTable
has(const std::string& symbol, const TAO_PEGTL_NAMESPACE::position& use_position) has(const std::string& symbol, const TAO_PEGTL_NAMESPACE::position& use_position)
{ {
for (auto i_stored_symbol : m_symbol_list) { for (auto i_stored_symbol : m_symbol_list) {
if (use_position.byte < i_stored_symbol.attributes().position().byte)
continue;
// Symbol must be defined before the call // Symbol must be defined before the call
if (use_position.byte >= i_stored_symbol.attributes().position().byte) {
std::string_view stored_symbol_name = i_stored_symbol.name(); std::string_view stored_symbol_name = i_stored_symbol.name();
if ((stored_symbol_name.size() == symbol.size()) or if ((stored_symbol_name.size() == symbol.size()) or
(stored_symbol_name.size() > symbol.size() and (stored_symbol_name[symbol.size()] == ':'))) { (stored_symbol_name.size() > symbol.size() and (stored_symbol_name[symbol.size()] == ':'))) {
...@@ -320,6 +317,7 @@ class SymbolTable ...@@ -320,6 +317,7 @@ class SymbolTable
} }
} }
} }
}
if (m_parent_table) { if (m_parent_table) {
return m_parent_table->has(symbol, use_position); return m_parent_table->has(symbol, use_position);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <language/utils/BuiltinFunctionEmbedder.hpp> #include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <language/utils/SymbolTable.hpp> #include <language/utils/SymbolTable.hpp>
#include <language/utils/TypeDescriptor.hpp>
#include <pegtl/internal/iterator.hpp> #include <pegtl/internal/iterator.hpp>
...@@ -186,6 +187,29 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -186,6 +187,29 @@ TEST_CASE("SymbolTable", "[language]")
value_output << symbol.attributes(); value_output << symbol.attributes();
REQUIRE(value_output.str() == "function_id:2"); REQUIRE(value_output.str() == "function_id:2");
} }
{
const SymbolTable::Symbol symbol_decl{i_symbol_a->name(), i_symbol_a->attributes()};
SymbolTable::Attributes attributes_b = attributes_a;
SymbolTable::Symbol symbol("foo", attributes_b);
symbol = symbol_decl;
std::stringstream value_output;
value_output << symbol.attributes();
REQUIRE(value_output.str() == "function_id:2");
}
{
SymbolTable::Attributes attributes_b = attributes_a;
SymbolTable::Symbol symbol("foo", attributes_b);
symbol = SymbolTable::Symbol{i_symbol_a->name(), i_symbol_a->attributes()};
std::stringstream value_output;
value_output << symbol.attributes();
REQUIRE(value_output.str() == "function_id:2");
}
} }
SECTION("FunctionTable") SECTION("FunctionTable")
...@@ -220,4 +244,20 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -220,4 +244,20 @@ TEST_CASE("SymbolTable", "[language]")
REQUIRE(builtin_function_table.size() == 1); REQUIRE(builtin_function_table.size() == 1);
REQUIRE(const_builtin_function_table.size() == 1); REQUIRE(const_builtin_function_table.size() == 1);
} }
SECTION("TypeEmbedderTable")
{
std::shared_ptr root_st = std::make_shared<SymbolTable>();
auto& type_table = root_st->typeEmbedderTable();
REQUIRE(type_table.size() == 0);
const auto& const_type_table = static_cast<const SymbolTable&>(*root_st).typeEmbedderTable();
REQUIRE(const_type_table.size() == 0);
type_table.add(std::make_shared<TypeDescriptor>("a_type"));
REQUIRE(type_table.size() == 1);
REQUIRE(const_type_table.size() == 1);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment