From Interpretation to Compilation: Staging, Intermediate Representation
What This Concept Is
An interpreter walks the source expression every time it runs. A compiler does the walking once, up front, and emits a lower-level program that a simpler runtime (or a CPU) can execute directly.
Between the two extremes live a small family of standard moves:
- Syntactic analysis separated from execution (SICP §4.1.7). Instead of
eval exp env, produceanalyze(exp) -> λenv. result. The first phase walks the syntax; the second phase takes an environment and runs. Repeated runs skip the syntactic phase. - Intermediate representations (IRs). The AST is one IR; a core language (desugared subset) is another; a control-flow graph or SSA form is another; bytecode is another; machine code is the last. Each is a deliberate choice about what is easy and what is hard at that level.
- Staging / partial evaluation. When some inputs are known earlier than others, bake the early ones in.
(analyze exp)stages the interpreter: what depends only onexpruns now; what needsenvruns at call time. - Compilation proper (SICP §5.5). Emit machine (or register-machine) code directly from the AST. No more tree-walking at run time.
This concept is SUPPORTING because the module's centre of gravity is writing the interpreter; compilation is the natural next step, not a demand for this module.
Why It Matters Here
Three reasons to see the gradient now:
- Performance intuition. The speedup from tree-walking to bytecode is often 10-100×; from bytecode to native is another 2-10×. Knowing which level you are at explains the cost.
- Language design. Many real systems sit at a middle rung: CPython compiles to bytecode and interprets that; the JVM interprets bytecode and JIT-compiles hot paths. Knowing the rungs is how you read architecture docs of any mature runtime.
- Engineering leverage. Almost every production interpreter eventually moves toward "compile once, run many" -- template engines pre-compile templates; SQL engines build plans; regex libraries compile patterns. The staging move is universal.