Capstone: Source to Bytecode to VM
Lesson, slides, and applied problem sets.
View SlidesLesson
Capstone: Source to Bytecode to VM
Why this module exists
This capstone connects every stage:
- Lex source into tokens
- Parse tokens into a program AST
- Run semantic analysis
- Compile to bytecode
- Encode bytecode to a file format
- Decode it back
- 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