Capstone: Source to Bytecode to VM

Lesson, slides, and applied problem sets.

View Slides

Lesson

Capstone: Source to Bytecode to VM

Why this module exists

This capstone connects every stage:

  1. Lex source into tokens
  2. Parse tokens into a program AST
  3. Run semantic analysis
  4. Compile to bytecode
  5. Encode bytecode to a file format
  6. Decode it back
  7. Execute in the VM

That is the full pipeline of a real compiler + runtime.


1) Honest compilation

The key idea is separation:

  • The compiler produces bytecode
  • The VM consumes bytecode

They are separate components with a clear contract. This is what makes it "honest."


2) Error handling

Each stage can fail:

  • Lex and parse errors
  • Semantic errors (undefined names, const misuse, arity mismatch)
  • Runtime errors (type errors, division by zero)

The capstone should return the first error it encounters.


3) Open-ended next steps

From here, you can extend:

  • Closures and captured variables
  • Bytecode optimizations
  • A binary bytecode format
  • A simple JIT

Module Items