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.
26 lines
298 B
Plaintext
26 lines
298 B
Plaintext
var f = null
|
|
var g = null
|
|
|
|
{
|
|
var local = "local"
|
|
f = fn {
|
|
io.write(local)
|
|
local = "after f"
|
|
io.write(local)
|
|
}
|
|
|
|
g = fn {
|
|
io.write(local)
|
|
local = "after g"
|
|
io.write(local)
|
|
}
|
|
}
|
|
|
|
f.call
|
|
// expect: local
|
|
// expect: after f
|
|
|
|
g.call
|
|
// expect: after f
|
|
// expect: after g
|