v3 Bytecode: Closures and Indexing
Lesson, slides, and applied problem sets.
View SlidesLesson
v3 Bytecode: Closures + Collections
Why this module exists
We now compile to a richer bytecode that supports:
- closures (function values)
- arrays and maps
- indexed reads and writes
This is the real compiler step for v3.
1) Closures
A function declaration compiles into:
- a
Functionbytecode object - a runtime closure creation
Bytecode uses:
MAKE_CLOSURE <id>to push a closure valueCALL <arity>to call a function value on the stack
2) Collections
Arrays and maps live on the heap.
MAKE_ARRAY <n>popsnvalues and pushes an arrayMAKE_MAP <n>pops2nvalues (key, value, key, value...)GET_INDEXreadsa[i]SET_INDEXwritesa[i] = v
3) Control flow
Same as v2: JUMP, JUMP_IF_FALSE, ENTER_SCOPE, EXIT_SCOPE.