builds / interpreter / stage-3SHEET 3 / 6 · REV ASIGN IN
ASSEMBLY DIAGRAM — YOUR INTERPRETERSCALE: LEARNING
source filethe outside worldSCANNER✓ builtPARSER✓ builtEVALUATOR⚙ buildingENV ROOMSstage 4CLOSURE CELLSstage 5DIAG PANELstage 6
BUILTUNDER CONSTRUCTIONNOT YET IMAGINED INTO EXISTENCE
STAGE 3 · THE EVALUATOR

The Machine Wakes

Source in, answer out — the assembly line runs end to end for the first time.

What you're wiring up

Evaluation is a tree walk. To evaluate (1 + (2 * 3)) you evaluate the left child, evaluate the right child, then apply the operator. Recursion does all the work, because the parser already baked the order of operations into the tree's shape. That is the payoff for Stage 2.

You also make your first genuine language-design decisions. Values get their own object system — Integer, Boolean, Null behind one interface — and you decide what counts as truthy. In blu, everything is truthy except false and null, and an if with no taken branch yields null. Those are your calls, and they are now the spec.

This stage boots the REPL too. From here on you have a live prompt into your own language, which turns every later stage from a test-running exercise into something you can play with.

Assembly steps

[ 01 ]
Define the value system in an object package: Integer, Boolean, Null behind one Object interface with Type and Inspect.
hint

Create exactly one TRUE, one FALSE, one NULL and reuse the pointers. Comparisons become pointer equality and the evaluator gets simpler, not just faster.

[ 02 ]
Write Eval(node) as one switch over node types: literals return their object, prefix operators evaluate the operand then apply, infix evaluates both sides then dispatches on operand types.
[ 03 ]
Implement if/else as an expression that yields a value, and define truthiness.
hint

This is the one place you must not evaluate both branches eagerly. Evaluate the condition, then walk only the branch you take.

[ 04 ]
Wire blu run to evaluate a file and print the final value, and the bare blu command to run a read-lex-parse-eval-print loop.
hint

The REPL is about fifteen lines that reuse everything you already have. Never build anything REPL-specific into the evaluator.

Go deeper (after it passes)

Recursive descent plus a tree walk is a masterclass in one-job functions — pair with Clean Code M2 — Functions: Small, Then Smaller Still. The REPL is the same read-parse-execute loop that build-your-own-shell runs over processes instead of expressions.