forked from defunkt/ReefVM
27 lines
386 B
Plaintext
27 lines
386 B
Plaintext
; Closure example: counter function that captures state
|
|
; outer() returns inner() which increments and returns captured count
|
|
MAKE_FUNCTION () .outer_body
|
|
PUSH 0
|
|
PUSH 0
|
|
CALL
|
|
STORE counter_fn
|
|
LOAD counter_fn
|
|
PUSH 0
|
|
PUSH 0
|
|
CALL
|
|
HALT
|
|
|
|
.outer_body:
|
|
PUSH 0
|
|
STORE count
|
|
MAKE_FUNCTION () .inner_body
|
|
RETURN
|
|
|
|
.inner_body:
|
|
LOAD count
|
|
PUSH 1
|
|
ADD
|
|
STORE count
|
|
LOAD count
|
|
RETURN
|