I’ve read hundreds of technical books over the last 40 years. Crafting Interpreters is an instant classic, and far more readable and fun than many of the classics.
Nystrom covers a lot of ground in this book, building two very different interpreters for Lox, a small dynamic language of his own design. He takes us through every line of jlox, a Java-based tree-walk interpreter, and of clox, a bytecode virtual machine written in C.
For the first implementation, jlox, he covers such topics as scanning, parsing expressions with recursive descent, evaluating expressions, control flow, functions and closures, classes, and inheritance.
Starting with an empty slate, Nystrom adds just enough code to implement the topic of each chapter, having a working albeit incomplete implementation of the interpreter by the end of the chapter. He adds new code as he goes, inserting an extra case into a switch here or writing a new function there, or replacing a few lines of an earlier implementation with something that’s just been explained. Knuth’s Literate Programming explains a finished implementation, broken into separate pieces for exposition. Nystrom’s continual, ever-evolving exposition is slower to get to the point, but it’s excellent pedagogy. I would be remiss if I didn’t mention the hundreds of hand-drawn illustrations, which add a quirky flavor to the tone of the book. He has a blog post on how he pulled this organization off and another on how he created a physical book from the text.
clox is a very different second implementation of a Lox interpreter. Instead of a slow interpreter walking an abstract syntax tree, he develops a stack-based virtual machine, compiles Lox into bytecode, and interprets the bytecode. He covers theory and practical considerations for creating a bytecode virtual machine, makes use of Pratt’s “top-down operator precedence parsing”, and implements closures and classes in C. In jlox, he used Java’s HashMap to manage identifiers and relied on Java’s garbage collection for memory management. For clox, he implements a hash table and a mark-and-sweep garbage collector. Although he has to cover similar topics (parsing, local variables, closures) each time, he finds a fresh perspective for the second implementation.
I read the entire book for free at https://craftinginterpreters.com/, but I liked it so much that I’ve ordered a physical copy. In fact, I actually read much of the book on the website in 2020, but life intervened and I didn’t finish it, so this month, I read it again from the start.
This book is not a textbook and you don’t get an exhaustive introduction to building interpreters, much less compilers. In the final year of my Computer Science degree at Trinity College Dublin in 1986–87, I studied the Dragon Book when the first edition was brand new. Crafting Interpreters is a lot more fun than the Dragon Book.