feat: add block scope support with push/pop macros and scope tracking in compiler

Introduce Scope struct and PUSH_SCOPE/POP_SCOPE macros to manage nested variable scopes during compilation. Update declareVariable to distinguish global declarations from local ones based on scope presence rather than parent compiler. Add truncateSymbolTable helper to clean up local symbols when exiting a scope. Include new test files verifying scoped variable isolation in if, while, and block constructs.
This commit is contained in:
Bob Nystrom
2013-11-18 17:19:03 +00:00
parent dc2f053d76
commit ed9f4ec9f3
8 changed files with 127 additions and 14 deletions
+8
View File
@@ -36,3 +36,11 @@ if
// Newline after "else".
if (false) io.write("bad") else
io.write("good") // expect: good
// Definition in then arm.
if (true) var a = io.write("ok") // expect: ok
if (true) class Foo {} // no error
// Definition in else arm.
if (false) null else var a = io.write("ok") // expect: ok
if (true) null else class Foo {} // no error
+9
View File
@@ -0,0 +1,9 @@
// Create a local scope for the 'then' expression.
var a = "out"
if (true) var a = "in"
io.write(a) // expect: out
// Create a local scope for the 'else' expression.
var b = "out"
if (false) "dummy" else var b = "in"
io.write(b) // expect: out
@@ -0,0 +1,9 @@
{
var a = "first"
io.write(a) // expect: first
}
{
var a = "second"
io.write(a) // expect: second
}
+7
View File
@@ -0,0 +1,7 @@
// Body has its own scope.
var a = "outer"
var i = 0
while ((i = i + 1) <= 1) var a = "inner"
io.write(a) // expect: outer
// TODO(bob): What about condition?
+4
View File
@@ -29,3 +29,7 @@ var f = while (e < 3) {
e = e + 1
}
io.write(f) // expect: null
// Definition body.
while (false) var a = "ok" // no error
while (false) class Foo {} // no error