THORSTEN // FIELD MAP
← the map
THE MONKEY BOOKS · PAGE 08 OF 10lexer · Pratt parser · bytecode VM · 2016–2018

The Monkey books: Writing An Interpreter In Go and Writing A Compiler In Go

Two self-published books, one invented language, and a codebase the reader builds from an empty file to a bytecode virtual machine.
TL;DR — Thorsten wrote both books himself and sold them himself. Writing An Interpreter In Go (November 2016) starts from zero lines of code and ends with a working tree-walking interpreter for Monkey, a small C-like language he designed for the book. Writing A Compiler In Go (July 2018) keeps that lexer, parser and AST and replaces the evaluator with a bytecode compiler and a stack-based virtual machine, which the book's site says runs Monkey 3x faster. Both are written tests first, use only the Go standard library, and put working code at the centre of every page. Readers have since written nearly a hundred Monkey implementations in other languages. In 2026 he notes, with some amusement, that he wrote books built on TDD and no longer writes tests by hand.

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.

Monkey source flows through a shared lexer, parser and AST, then either a tree-walking evaluator (book 1) or a compiler, bytecode and virtual machine (book 2) SHARED FRONT END · built in book 1, reused in book 2 source code let x = 5 + 5; lexer ch. 1 · characters into tokens tokens LET IDENTIFIER INTEGER … parser ch. 2 · recursive descent + Pratt AST statements and expressions The Lost Chapter (2017, free) a macro phase rewrites the AST before evaluation BOOK 1 · WRITING AN INTERPRETER IN GO · 2016 tree-walking evaluator ch. 3–4 · visits each node in turn objects → REPL environment, built-ins, output BOOK 2 · WRITING A COMPILER IN GO · 2018 compiler emits instructions bytecode opcodes + operands stack VM frames, closures
Our drawing of the architecture both books describe. The left column and the amber row are book one; book two keeps the left column, the REPL and the object system and adds the blue row next to the evaluator. Chapter numbers are from the book one table of contents.

Book one: from characters to a running interpreter

The table of contents has four chapters, each one a layer of the pipeline.

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.

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 Go1.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 Go1.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)
Next: How Thorsten works · Back to the map.