2025 · Solo · learning project
Lox, slowly
A tree-walking interpreter in TypeScript, built chapter by chapter from Crafting Interpreters.
- TypeScript
- Interpreters
- Node.js
The problem
I'd been writing code for a couple of years without really knowing what happens between typing something and a computer doing it. Programming languages felt like weather: they just existed.
Robert Nystrom's Crafting Interpreters was recommended in a forum thread, and I decided to work through the first half one chapter at a time, translating the Java into TypeScript instead of copying it.
What I built
The result is a complete interpreter for Lox, a small dynamically typed language: a scanner, a recursive-descent parser, a resolver for variable scope, and a tree-walking evaluator with closures and classes.
I added a browser playground so friends could try it without installing anything, plus a test suite that runs every example program from the book.
- Translated the book's visitor pattern into TypeScript discriminated unions
- Closures, classes, inheritance, and `super` all working
- Around 240 test programs checked against expected output
- Friendly error messages with line and column pointers
Tech stack
- TypeScriptDiscriminated unions stood in for Java's class hierarchy
- Node.jsREPL and file runner for the command line
- VitestSnapshot tests against the book's sample programs
What I learned
It took me four months, mostly because I kept stopping to reread chapters until they made sense. That was the point. I understand recursion and scope in a way I didn't before, and I stopped being afraid of reading long technical books.
Translating rather than copying forced me to understand each design choice. A few times my "improvement" broke something and I learned why the book did it the original way.