Master the fundamental concepts of build a bytecode vm through this focused micro-challenge.
A compiler walks source text and emits opcodes your VM already understands. Recursive descent is the standard approach for small languages: one parsing function per grammar rule, each emitting instructions as it goes.
Expression compilation is postfix-friendly on a stack VM:
Control flow needs jump patching:
For example, `if 10 > 5 print 1 else print 0` compiles to a comparison, conditional jump, two print blocks, and two patched addresses.
This pipeline mirrors CPython's `compile.c` and Bob Nystrom's clox compiler in Crafting Interpreters.
Emit instructions in a single pass where possible, but keep the bytecode buffer growable. Real compilers use chunked buffers or vectors because function size is not known until parsing finishes. Reserve space for jump operands when you emit branch placeholders.
This exercise asks you to build a tokenizer, recursive-descent parser, and bytecode emitter with jump backpatching. You will implement compilation for arithmetic, `if/else`, and `print`, then run the result in your interpreter.
Write a compiler that translates simple expressions and statements into your VM's bytecode in C.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github