From 12f42a86f670fd406f736dd142239fd411fc943f Mon Sep 17 00:00:00 2001 From: bog Date: Tue, 12 Sep 2023 06:46:07 +0200 Subject: [PATCH] FIX: nested block scopes. --- examples/block.gri | 14 +++++++++++++- src/Compiler.cpp | 15 ++++++++++----- src/Compiler.hpp | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/block.gri b/examples/block.gri index 4ae230d..b1dae9f 100644 --- a/examples/block.gri +++ b/examples/block.gri @@ -1,2 +1,14 @@ (assert-eq? 6 (: 2 4 6)) -(assert-eq? 9 (: 2 4 (: 3 0 9))) \ No newline at end of file +(assert-eq? 9 (: 2 4 (: 3 0 9))) + +(: + ($ a 4) + (assert-eq? 4 a) + + (: + ($ a 7) + (assert-eq? 7 a) + ) + + (assert-eq? 4 a) +) \ No newline at end of file diff --git a/src/Compiler.cpp b/src/Compiler.cpp index 07cadba..a9e196f 100644 --- a/src/Compiler.cpp +++ b/src/Compiler.cpp @@ -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; isize(); i++) { @@ -177,9 +175,16 @@ namespace grino return m_scope.back() - 1; } - void Compiler::enter_scope() + void Compiler::enter_scope(bool reset) { - m_scope.push_back(0); + if (m_scope.empty() || reset) + { + m_scope.push_back(0); + } + else + { + m_scope.push_back(m_scope.back()); + } } void Compiler::leave_scope() diff --git a/src/Compiler.hpp b/src/Compiler.hpp index 523a486..522a8ae 100644 --- a/src/Compiler.hpp +++ b/src/Compiler.hpp @@ -35,7 +35,7 @@ namespace grino std::vector m_scope; size_t get_local_address(); - void enter_scope(); + void enter_scope(bool reset=false); void leave_scope(); }; }