v3 Language: Collections and Closures
- Track aliases and closure environments across function return.
- Identify v3 lexical additions without assigning them parser meaning.
- Distinguish primitive values from VM heap objects.
1 / 6
Trace the value graph for:
fn make(start) {
var n = start;
fn next() { n = n + 1; return n; }
return next;
}
let counter = make(4);
let a = counter();
let b = counter();
The call to make creates environment E with start=4 and n=4. Creating next allocates a closure that refers to its code ID and E. Returning the closure keeps E reachable after make's call frame ends. The first counter call creates a new parameter environment whose parent is E, updates E.n to 5, and returns 5. The second updates the same binding to 6. Copying n=4 into each call would yield 5 twice and fail the shared-binding contract.
A returned counter closure observes the caller’s local n=100 instead of its defining n=4. What parent environment was chosen incorrectly? Would preserving only the function ID fix it?
Create two counters from make(4). Predict interleaved calls A, A, B, A and draw which environment each closure retains. Then tokenize a map whose value is an array without interpreting either container.
Carry the value graph and new punctuation into v3 parsing. Preserve source-order evaluation and shared-binding examples for the VM and GC stages.