Every developer uses an interpreter or a compiler daily, and for most of us it stays a black box. We know there is a parser, something about a syntax tree, maybe "bytecode". But how does a closure keep a variable alive after its function returned? What does super.method() actually look up? Why is a bytecode VM faster than walking the tree?
Robert Nystrom's book Crafting Interpreters answers those questions by building one small language, Lox, twice: first as a tree-walk interpreter in Java ("jlox"), then as a bytecode virtual machine in C ("clox"). rlox is my implementation of both in Rust, in one repository, sharing one scanner and one test suite. This post is about what I built and what it teaches.
What I built
Lox is a small dynamic language with a C-like syntax. Both back ends in rlox support the full language: variables, blocks, if / while / for , functions, closures, classes, fields, methods, initializers, this , inheritance and super , plus the clock() native function.
A shared scanner.…