ReefVM/examples/closure.reef
2025-10-05 22:07:26 -07:00

25 lines
387 B
Plaintext

; Closure example: counter function that captures state
; outer() returns inner() which increments and returns captured count
MAKE_FUNCTION () #10
PUSH 0
PUSH 0
CALL
STORE counter_fn
LOAD counter_fn
PUSH 0
PUSH 0
CALL
HALT
; Outer function body
PUSH 0
STORE count
MAKE_FUNCTION () #14
RETURN
; Inner function body (closure over count)
LOAD count
PUSH 1
ADD
STORE count
LOAD count
RETURN