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

Fix symbol table management for do...while and while loops

In case of empty blocks or single instruction statement, the symbol
table could be cleared unexpectedly.
parent d450c057
No related branches found
No related tags found
1 merge request!145git subrepo clone git@gitlab.com:OlMon/org-themes.git packages/org-themes
......@@ -8,7 +8,8 @@
void
ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>& symbol_table)
{
if (n.is_type<language::block>() or (n.is_type<language::for_statement>())) {
if (n.is_type<language::block>() or (n.is_type<language::for_statement>()) or
(n.is_type<language::while_statement>()) or (n.is_type<language::do_while_statement>())) {
if (!n.children.empty()) {
std::shared_ptr block_symbol_table = std::make_shared<SymbolTable>(symbol_table);
n.m_symbol_table = block_symbol_table;
......
......@@ -105,6 +105,41 @@ do {
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 12ul);
}
SECTION("do-while lifetime variable")
{
std::string_view data = R"(
let i:N, i = 3;
let j:N, j = 0;
do {
j = 5;
let j:N, j = 2;
i = j;
} while(false);
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 2ul);
CHECK_WHILE_PROCESSOR_RESULT(data, "j", 5ul);
}
SECTION("empty do-while symbol table untouched")
{
std::string_view data = R"(
let i:N, i = 3;
do {} while(false);
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 3ul);
}
SECTION("single instruction do-while symbol table untouched")
{
std::string_view data = R"(
let i:N, i = 3;
do
i = 2;
while(false);
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 2ul);
}
SECTION("errors")
{
SECTION("bad test type")
......
......@@ -101,6 +101,40 @@ while(i<10) {
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 12ul);
}
SECTION("while lifetime variable")
{
std::string_view data = R"(
let i:N, i = 3;
let j:N, j = 0;
while(i != 2) {
j = 5;
let j:N, j = 2;
i = j;
}
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 2ul);
CHECK_WHILE_PROCESSOR_RESULT(data, "j", 5ul);
}
SECTION("while symbol table untouched")
{
std::string_view data = R"(
let i:N, i = 3;
while(i != 3);
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 3ul);
}
SECTION("single instruction while symbol table untouched")
{
std::string_view data = R"(
let i:N, i = 3;
while (i == 3)
i = 2;
)";
CHECK_WHILE_PROCESSOR_RESULT(data, "i", 2ul);
}
SECTION("errors")
{
SECTION("bad test type")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment