The Monkey books: Writing An Interpreter In Go and Writing A Compiler In Go
Before Sourcegraph, Zed and Amp, many programmers knew Thorsten as the person who taught them how a programming language works. This page covers what the books contain, how he made them, and how he talks about them now.
Monkey, the language that lives in books
Monkey is a language he designed for the first book. The Monkey site describes it as having no official implementation: it exists in the two books, and each reader builds it, first as an interpreter and then as a compiler plus virtual machine. It has C-like syntax, let bindings, integers, booleans, strings, arrays and hashes, prefix and infix operators, conditionals, implicit and explicit returns, first-class and higher-order functions, and closures.
The design is small on purpose. Each feature makes you build one recognisable piece of a real language implementation and nothing extra. Closures, for example, force you to deal with environments in the first book and free variables in the second. This is the example the compiler book's site uses as its showpiece:
// `newAdder` returns a closure that makes use of the free variables `a` and `b`:
let newAdder = fn(a, b) {
fn(c) { a + b + c };
};
// This constructs a new `adder` function:
let adder = newAdder(1, 2);
adder(8); // => 11
One front end, two back ends
The two books share one codebase. Book one builds the whole pipeline. Book two keeps everything up to the syntax tree and puts a different engine behind it. The compiler book's sample chapter sets the goal as getting the expression 1 + 2 through every stage: tokenize and parse it with the packages from the first book, compile the resulting AST to bytecode, and have the new virtual machine turn it into 3.
Book one: from characters to a running interpreter
The table of contents has four chapters, each one a layer of the pipeline.
- 1 · Lexing. Define the token types, write a lexer that turns a string of characters into tokens, extend it with two-character operators, and finish with the start of a REPL that prints tokens. The free sample is the beginning of this chapter, and it is a fair test of whether the style suits you.
- 2 · Parsing. A section titled "Why not a parser generator?" explains why the parser is written by hand. Statements are parsed by recursive descent, and expressions by top-down operator precedence, also called Pratt parsing. A Pratt parser attaches a parsing function to each token type, one for when the token starts an expression and one for when it appears between two operands. A precedence table decides how tightly each operator binds. It is the longest chapter, and a section titled "How Pratt Parsing Works" walks through the mechanism. The chapter ends with a read-parse-print loop.
- 3 · Evaluation. A section on strategies of evaluation comes before the tree-walking interpreter itself. Then build an object system to represent values, then evaluate expressions, conditionals, return statements, errors, bindings in an environment, and functions with calls. Closures appear here, as a function value that holds on to the environment it was defined in. The last section, "Who's taking the trash out?", turns to memory.
- 4 · Extending the Interpreter. Strings, built-in functions such as len, arrays with index expressions, and hashes, each added through every layer from lexer to evaluator. That repetition is the point: by the fourth data type you know where each change goes.
A short "Going Further" section points at the Lost Chapter and the sequel. The book's site now puts it at around 250 pages, most of them code. His launch post said around 200.
Book two: bytecode, a stack machine and closures
The sequel opens with an introduction called "Evolving Monkey", with sections on the language's past and present and on its future. Its table of contents runs to ten chapters.
- 1 · Compilers & Virtual Machines. What a real machine does, what a virtual one is, why you would build one, what bytecode is, and the plan, in a section subtitled "the Duality of VM and Compiler": every feature gets a compiler half and a VM half, built side by side so there is always a running system.
- 2 · Hello Bytecode! Define opcodes and their operand encoding, build the smallest compiler, a mini disassembler, and a VM that can add two numbers. The sample explains the key choice: a stack machine, because it has fewer moving parts than a register machine and the book cares more about understanding than speed.
- 3–6. Infix, prefix and comparison expressions; conditionals compiled to jump instructions (and the return of null); names, with a symbol table and globals in the VM; then strings, arrays, hashes and the index operator.
- 7 · Functions. Function literals and calls, local bindings, and arguments. This is where the VM gets frames with stack and frame pointers and the book defines a calling convention.
- 8 · Built-in Functions. The same built-ins as book one, moved into the VM.
- 9 · Closures. The site calls this the "crown-jewel". The chapter titles show the approach: "Everything's a closure", compiling and resolving free variables, creating closures at run time, and recursive closures. The site says it will show "why closure-compilation is so tricky". The difficulty is that a tree walker gets closures almost for free, because the evaluator already holds the environment. A compiler has to work out at compile time which variables a function captures and emit code that copies them into a closure object.
- 10 · Taking Time. The closing chapter.
The speed claim reads like this: the result is "much closer to the programming languages we use every day" and "also 3x faster. And that's without explicitly aiming for performance." That sentence is from compilerbook.com, which gives no further detail, so we do not either. The eBook is around 260 pages and the bundle of both is around 480.
The method: tests first, standard library only, code on every page
The sites list four promises: working code is the focus, the book is short, there are no third-party libraries, and everything is tested, "Sometimes in TDD style, sometimes with the tests written after." The books page is blunter: "This book is not about theory, its focus is runnable, working code."
Each rule removes a reason to stop reading. A parser generator, or an existing library, would leave a box you cannot see into. Tests written before the code tell you what the next step is supposed to do, and a passing test tells you that you got there. Code in an appendix, or code that no longer compiles, would break the main promise that you can run every step. He describes the pain this caused him in The Tools I Use To Write Books. Every snippet lives twice, once in the Markdown chapter and once in the code folder, and is kept in sync by hand. He did this because no tool could show a change to an existing function the way he wanted. He ends with "two books and zero tools", and he counts that as a success.
Go was a deliberate choice. In 2024, eight years after the first release, he downloaded the original code and ran it:
Code written in 2016 using Go 1.7, frozen in time — compiling and running today, using Go 1.23. No warnings, no errors.— Thorsten Ball, Glad I did it in Go (Register Spill, 2024)
In that post he quotes the book's "Why Go?" section: no other tools beyond what Go ships, a formatter with no settings, a test framework built in, and code that should map closely to lower-level languages. The last point turned out to matter most. Readers who did not know Go translated the code into their own language as they read. Some of them told him they never typed out the Go version at all.
Why he wrote them, and how he shipped them
He tells the origin story in Why I Wrote a Book About Interpreters. In 2015 he worked through SICP and was taken with its meta-circular evaluator, a Lisp interpreter in about 400 lines of Scheme. Then he noticed that it skips the parser. He dislikes skipping anything, and Steve Yegge's Rich Programmer Food had just told him to write a compiler. So he set out to build one with no compiler course and no computer science degree. What he found was either theory-heavy tomes or toy tutorials that hid the hard parts. The book was his answer: every component built by hand, with tests, readable by any programmer. When he left Sourcegraph in 2023 he called this full circle: Yegge's essay got him into compilers, compilers got him into language tooling, and that got him a job at Sourcegraph reporting to Yegge.
It took eleven months: four building Monkey, seven writing. What I didn't do to write a book is a cheerful confession that he had no system. His to-do lists were scattered and his notes ended up wherever he was typing. The one constant was getting up at 5:45 every morning and moving the book forward, sometimes by a single sentence starting with "FIXME:".
The publishing side is in the tools post. He wrote the chapters in Markdown under git, ran them through pandoc with custom templates (LaTeX for the PDF) and used KindleGen for Mobi. In 2018 he sold the eBooks through Gumroad and printed paperbacks on demand through Amazon KDP. The sites now sell the eBooks through LemonSqueezy. The paperback post from February 2017 says the paperback was mostly reader demand, and that he would not print it until Monkey had a logo, so he ran a design contest for one.
The Lost Chapter: macros as a free fifth chapter
A few months after the release he went back to Monkey and added a Lisp-style macro system, after Racket's macros caught his attention. He wrote it up as The Lost Chapter: A Macro System For Monkey, published free in June 2017 and close to 50 pages as a PDF. It picks up where chapter four ends. quote and unquote let code handle syntax trees as values, and a separate macro-expansion phase rewrites the AST before evaluation. The book site compares it to Elixir's macro system and gives this example:
let unless = macro(condition, consequence, alternative) {
quote(if (!(unquote(condition))) {
unquote(consequence);
} else {
unquote(alternative);
});
};
unless(10 > 5, puts("not greater"), puts("greater"));
// outputs only: "greater"
His post about it gives two reasons for keeping it out of the book. A paperback cannot be updated for people who already bought it. And macros felt to him less like Monkey canon than a "bizarro expansion pack". The same post says Monkey work had also produced "a project I'm not ready to talk about yet". The compiler book came out a year later; the post does not say that was the project.
How they became the standard recommendation
| Writing An Interpreter In Go | 1.0 on 23 November 2016; current 1.7 (7 May 2020). Rated 4.6 on Amazon and Goodreads; translated into Japanese and Chinese, per interpreterbook.com. |
|---|---|
| Writing A Compiler In Go | 1.0 on 31 July 2018; current 1.2 (7 May 2020). Rated 4.7; translated into Chinese, per compilerbook.com. |
| Monkeys in the wild | "Nearly 100 Monkey implementations in more than 25 languages", per monkeylang.org. |
Three things put the books on every list of where to start with interpreters. They filled the gap he describes in his launch post, between the 50-line Lisp blog post and the Dragon Book, and you finish with a language you wrote yourself. The endorsements on the sites come from people in the field. Mitchell Hashimoto recommends the first book to any engineer who does not know lexers, parsers and evaluators. Bob Nystrom, author of Crafting Interpreters, says it gets you to a working interpreter with less prose than his own book. And Monkey became an exercise that outlives the books. Several reviewers on the sites say it is the project they build whenever they learn a new language, because it needs only a standard library and the test suite tells them when they are done.
Seven years on, the project still stands out for him. In Nothing in my way except myself (2023) he says the Monkey interpreter "was the most joy-inducing programming I had ever done". His leaving-Sourcegraph post mentions job candidates leaving the video frame and returning with one of his books. In 2024 he took apart c4, a C compiler in four functions, and was delighted to find it is a bytecode compiler and VM, the same architecture as his second book.
Why they matter now that agents write the code
He links the books to the agent era himself, and the link is uncomfortable for him. Looking at what changed between his first ten years as a programmer and the last two, he lists TDD among the casualties:
I wrote books that use TDD! I loved TDD! And yet at some point in the last 2 years I wrote my last test by hand— Thorsten Ball, Joy & Curiosity #96 (August 2026)
In the same issue he notes that "AI and agents could be super compilers". On the podcast in April 2025, a co-host reminds him that he wrote books about how to write code that writes code. He says that is why he couldn't sleep the night before: he was drafting a post called There's Beauty in AI in his head, along with the disclaimer it would need so readers would not take him for a hype merchant. The disclaimer was that he has written books about code and written compilers, and:
I truly care about code. That's the whole disclaimer.— Raising an Agent, Season 1 Episode 4, 23:37
The post came out about a month later as There's Beauty in AI. It explains his earlier resistance in terms the books would recognise. Programming had always been black boxes he knew he could open, and LLMs at first looked like oracles he could not. What changed his mind was fine-tuning a model at Zed and watching the data move its behaviour. Once he could see some of the wiring, he started to enjoy it again.
Our reading of those two pieces: the habit the books teach, opening a system down to its parts, is the habit that eventually won him over to models. The method they model, tests as proof of each step, is the part he says he has stopped doing by hand. Both fit his September 2026 list of beliefs about software, where the craft of writing code disappears and the craft of building software matters more. He does not mention the books there. One parallel is ours, not his: his Amp note How to Build an Agent has the books' shape. It starts from zero lines of Go, has no hidden parts, and asks you to type the code out.
How to read them
He states his own advice in the first book's introduction, which he quoted in Cadence of Code:
This book is meant to be read from start to finish and I recommend that you follow along by reading, typing out and modifying the presented code.— Thorsten Ball, quoting Writing An Interpreter In Go, in Cadence of Code (2023)
- Read the free sample first. The interpreter sample is the start of the lexer chapter. If that pace suits you, the rest will too.
- Type it, don't paste it. He types out the code of every book he truly wants to learn from, and in Cadence of Code he says the introduction does not set rules but nudges you toward this one.
- Run the tests at every step. Each section adds tests before or with the code. A red test that turns green is how you know you followed correctly. When something breaks, compare against the bundled code for that chapter, which is MIT-licensed and organised by chapter.
- Spend extra time on chapter 2. Pratt parsing is the idea most readers have not met before, and the rest of the book rests on it. Trace one expression such as -1 + 2 * 3 by hand through the "How Pratt Parsing Works" section.
- Consider a second language. Implementing Monkey in a language you know better than Go forces you to understand each step instead of copying it. Nearly a hundred readers have published one; monkeylang.org lists their versions to compare against.
- Read in order, then do the Lost Chapter. Finish book one, read the free macro chapter as a test of how well you know your own AST, then start the compiler book, which assumes the interpreter's code.