fix: properly dispose loop-scoped variables and exit scopes on break

Replace the single `loopBody` index with a `Loop` struct that tracks scope depth, enabling the compiler to emit correct scope-exit code when a `break` statement is encountered inside nested blocks. Also add `WREN_DUMP_COMPILED_CODE` debug flag and new test cases for closures capturing loop variables after break, return inside loops, and nested loop scoping.
This commit is contained in:
Bob Nystrom
2014-01-06 16:01:04 +00:00
parent 488dd3521f
commit 0ba6456214
13 changed files with 208 additions and 62 deletions
-1
View File
@@ -2,7 +2,6 @@ var list = []
for (i in [1, 2, 3]) {
list.add(fn IO.print(i))
//list.add(fn IO.print(i))
}
for (f in list) f.call
+9
View File
@@ -0,0 +1,9 @@
var f = fn {
for (i in [1, 2, 3]) {
return fn IO.print(i)
}
}
var g = f.call
g.call
// expect: 1
+8
View File
@@ -0,0 +1,8 @@
var f = fn {
for (i in [1, 2, 3]) {
return i
}
}
IO.print(f.call)
// expect: 1
@@ -18,3 +18,6 @@ for
// expect: 1
// expect: 2
// expect: 3
// TODO: Return from inside for loop.
// TODO: Return closure from inside for loop.