v3 Bytecode: Closures and Indexing

Lesson, slides, and applied problem sets.

View Slides

Lesson

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:

  1. a Function bytecode object
  2. a runtime closure creation

Bytecode uses:

  • MAKE_CLOSURE <id> to push a closure value
  • CALL <arity> to call a function value on the stack

2) Collections

Arrays and maps live on the heap.

  • MAKE_ARRAY <n> pops n values and pushes an array
  • MAKE_MAP <n> pops 2n values (key, value, key, value...)
  • GET_INDEX reads a[i]
  • SET_INDEX writes a[i] = v

3) Control flow

Same as v2: JUMP, JUMP_IF_FALSE, ENTER_SCOPE, EXIT_SCOPE.


Module Items