NonfictionProgramming language implementation
Crafting Interpreters
by Robert Nystrom
Write two programs that run the same language and see what changes when the second has to run faster.
What does a computer do with an expression you type? Nystrom puts a small language called Lox on the workbench and implements it twice. First you can watch Java code walk the branches of a syntax tree. Then you can step through compact instructions in a virtual machine written in C.
$37.98 Kindle ·
Amazon US · Affiliate link
Follow one expression through two machines
Try a small expression such as 1 + 2 * 3. In a syntax tree, the multiplication sits below the addition. In a stack machine, the multiplication instruction can run first, leaving its result ready for addition. You can trace the order of operations in both versions before moving on to functions or classes.
At the keyboard
- Get something running early
- The tree-walk project builds a scanner, parser and evaluator in stages. Work with a small Lox expression while each stage is in view before trying to trace a whole program by eye.Nystrom’s manuscript and interpreter code
- Read the bytecode as data
- The second build needs a way to inspect instructions as it produces them. In the bytecode chapter, a disassembler prints what the virtual machine will execute. That output makes a useful check on the compiler you are building.Read the chapter on bytecode
How readers found it
Williams completed both interpreters and tried chapter challenges. Bendersky brought prior interpreter experience and ported much of the second build to Rust, identifying parts that were harder to translate early in the project.
· Chidi Williams’s experience building both interpreters · Eli Bendersky on following the implementation
Questions before you read
Is this a suitable first programming book?
Nystrom assumes you can already read and write code. The early chapters introduce language implementation, then the projects use Java and C. Someone still learning variables and loops has a distinct extra task before the interpreter lessons will pay off.
Can I stop after the Java interpreter?
Yes. The first project reaches a working tree-walk interpreter with functions, closures and classes. The second starts the implementation over in C. It is the place to continue if you want to study bytecode, memory management and performance.
Why build Lox twice?
The Java interpreter preserves a program as a tree and visits its nodes to evaluate it. In the C version you compile the program into bytecode and run those instructions on a stack-based virtual machine. Holding the language steady makes it easier to see what each implementation costs.
What readers noticed
Chidi Williams built both interpreters over roughly six months of part-time study. He particularly enjoyed the challenges that let him add language features, including anonymous functions and a conditional expression.
Williams describes completing the projects and extending Lox. · Chidi Williams’s experience building both interpreters
Eli Bendersky reimplemented about two-thirds of the C interpreter in Rust. He advises other readers changing implementation languages to look ahead, since an odd-looking field sometimes matters only several chapters later. He also wanted more guidance on testing a partial interpreter.
Bendersky’s account of working through the code. · Eli Bendersky on following the implementation
Which programming project fits your next step?
Both guides build something you can run. The level of programming experience they assume is quite different.
Crafting InterpretersIf you can already program, build the interpreter that executes a language. Java and C are the implementation tools.
Unity From Zero to Proficiency (Beginner)If you want a first programming project, write C# that makes objects and characters respond inside a Unity game.
Read Nystrom’s opening chapter · Patrick Felicia’s first Unity projects
A programmer completed both implementations
Chidi Williams described building and extending the interpreters, including the time the project took him.
About this edition
- Publisher
- Genever Benning
- Format
- Kindle ebook
- US edition
- B09BCCVLCL