feat: implement closures with upvalue support and statement/expression separation

Add closure objects that wrap functions with captured upvalues, enabling
first-class closures that close over local variables from enclosing scopes.
Introduce new bytecode instructions (CODE_CLOSURE, CODE_LOAD_UPVALUE,
CODE_STORE_UPVALUE, CODE_CLOSE_UPVALUE, CODE_RETURN) and a CompilerUpvalue
tracking system with a MAX_UPVALUES limit of 256. Refactor the grammar to
strictly separate statements from expressions, fixing a bug where block
expressions incorrectly popped locals while temporaries remained on the
stack. Rename internal functions (wrenCallFunction, wrenDebugDumpInstruction,
wrenDebugDumpStack) and add Upvalue allocation, closure creation, and
debug dump support for the new instructions.
This commit is contained in:
Bob Nystrom
2013-12-04 15:43:50 +00:00
parent 7e7a508096
commit 5b05c477c6
32 changed files with 1063 additions and 416 deletions
+6 -2
View File
@@ -1,9 +1,13 @@
// Create a local scope for the 'then' expression.
var a = "out"
if (true) var a = "in"
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"
if (false) "dummy" else {
var b = "in"
}
io.write(b) // expect: out
+3 -3
View File
@@ -1,7 +1,7 @@
// Body has its own scope.
var a = "outer"
var i = 0
while ((i = i + 1) <= 1) var a = "inner"
while ((i = i + 1) <= 1) {
var a = "inner"
}
io.write(a) // expect: outer
// TODO(bob): What about condition?