FIX: nested block scopes.

main
bog 2023-09-12 06:46:07 +02:00
parent aaeedb1ba9
commit 12f42a86f6
3 changed files with 24 additions and 7 deletions

View File

@ -1,2 +1,14 @@
(assert-eq? 6 (: 2 4 6))
(assert-eq? 9 (: 2 4 (: 3 0 9)))
(:
($ a 4)
(assert-eq? 4 a)
(:
($ a 7)
(assert-eq? 7 a)
)
(assert-eq? 4 a)
)

View File

@ -54,8 +54,6 @@ namespace grino
program.push_instr(OPCODE_POP);
}
}
std::cout << "-------------------------" << std::endl;
std::cout << sym.string() << std::endl;
leave_scope();
sym.purge(m_scope.size());
@ -86,7 +84,7 @@ namespace grino
case NODE_LAMBDA: {
auto params = node->child(0).lock();
enter_scope();
enter_scope(true);
for (size_t i=0; i<params->size(); i++)
{
@ -177,10 +175,17 @@ namespace grino
return m_scope.back() - 1;
}
void Compiler::enter_scope()
void Compiler::enter_scope(bool reset)
{
if (m_scope.empty() || reset)
{
m_scope.push_back(0);
}
else
{
m_scope.push_back(m_scope.back());
}
}
void Compiler::leave_scope()
{

View File

@ -35,7 +35,7 @@ namespace grino
std::vector<size_t> m_scope;
size_t get_local_address();
void enter_scope();
void enter_scope(bool reset=false);
void leave_scope();
};
}