Cover of Writing A Compiler In Go by Thorsten Ball

Book 2 of the Monkey books

Writing A Compiler In Go

This is the sequel to Writing An Interpreter In Go.

We're picking up right where we left off and write a compiler and a virtual machine for Monkey.

Runnable and tested code front and center, built from the ground up, step by step, just like before. But this time, we're going to define bytecode, compile Monkey and execute it in our very own virtual machine. It's the next step in Monkey's evolution.

eBook includes PDF, ePub, Mobi (Kindle) and HTML. Read a free sample.

Rated 4.7 ★ on Amazon and Goodreads · Translated into Chinese · Version 1.2, changelog

This is a very polished pair of books that together give a steady path to follow for learning some of the real techniques that are used to implement programming languages. They're both well above average for their depth, technical clarity, and accessibility. I've been recommending them to everyone I work with who wants to get involved in compilers.

Chris Seaton Chris Seaton @ChrisGSeaton
Researcher at Shopify, PhD in Ruby, Founder of Truffle Ruby

The series

Two books, one programming language

In Writing An Interpreter In Go we build Monkey from scratch. In this book we take that interpreter and turn it into a bytecode compiler and a virtual machine.

Cover of Writing An Interpreter In Go

Book 1 · 2016

Writing An Interpreter In Go

Build an interpreter for the Monkey programming language from scratch: lexer, Pratt parser, AST, a tree-walking evaluator and a REPL.

Visit interpreterbook.com →
Cover of Writing A Compiler In Go

Book 2 · 2018 · You are here

Writing A Compiler In Go

The sequel: turn Monkey into a bytecode compiler and a stack-based virtual machine that runs it, closures and all, 3x faster.

Get both books as a bundle. Writing An Interpreter In Go and Writing A Compiler In Go in ePub, Mobi, PDF and HTML, plus all the code, for a reduced price.

Buy the bundle $50

The sequel

It's the sequel to … a programming language

Writing A Compiler In Go is the sequel to Writing An Interpreter In Go. It starts right where the first one stopped, with a fully-working, fully-tested Monkey interpreter in hand, connecting both books seamlessly, ready to build a compiler and a virtual machine for Monkey.

In this book, we use the codebase (included in the book!) from the first part and extend it. We take the lexer, the parser, the AST, the REPL and the object system and use them to build a new, faster implementation of Monkey, right next to the tree-walking evaluator we built in the first book.

The approach is unchanged, too. Working, tested code is the focus, we build everything from scratch, do baby steps, write tests firsts, use no 3rd-party-libraries and see and understand how all the pieces fit together.

It’s a continuation in prose and in code.

Do you need to read the first part before this one? If you’re okay with treating the code from the first book as a black box, then no. But that’s not what these books are about; they’re about opening up black boxes, looking inside and shining a light. You’ll have the best understanding of where we’re going in this book, if you know where we started.

What you'll learn

Learn how to write a compiler and a virtual machine

Our main goal in in this book is to evolve Monkey. We change its architecture and turn it into a bytecode compiler and virtual machine.

We’ll take the lexer, the parser, the AST and the object system we wrote in the first book and use them to build our own Monkey compiler and virtual machine … from scratch! We’ll build them side-by-side so that we’ll always have a running system we can steadily evolve.

What we end up with is not only much closer to the programming languages we use every day, giving us a better understanding of how they work, but also 3x faster. And that’s without explicitly aiming for performance.

Here’s what we’ll do:

  • We define our own bytecode instructions, specifying their operands and their encoding. Along the way, we also build a mini-disassembler for them.
  • We write a compiler that takes in a Monkey AST and turns it into bytecode by emitting instructions
  • At the same time we build a stack-based virtual machine that executes the bytecode in its main loop

We’ll learn a lot about computers, how they work, what machine code and opcodes are, what the stack is and how to work with stack pointers and frame pointers, what it means to define a calling convention, and much more.

We also

  • build a symbol table and a constant pool
  • do stack arithmetic
  • generate jump instructions
  • build frames into our VM to execute functions with local bindings and arguments!
  • add built-in functions to the VM
  • get real closures working in the virtual machine and learn why closure-compilation is so tricky
The Monkey programming language logo
The official Monkey logo

The language

The Monkey Programming Language

The implementation of Monkey we build in this book is markedly different from the tree-walking interpreter we built in Writing An Interpreter In Go, but Monkey stays the same.

At the end, Monkey will still look and work like this:

// Integers & arithmetic expressions
let version = 1 + (50 / 2) - (8 * 3);

// Strings
let name = "The Monkey programming language";

// Booleans
let isMonkeyFastNow = true;

// Arrays & Hashes
let people = [{"name": "Anna", "age": 24}, {"name": "Bob", "age": 99}];

// Functions
let getName = fn(person) { person["name"]; };
getName(people[0]); // => "Anna"
getName(people[1]); // => "Bob"

And it will still support recursive functions, conditionals, implicit and explicit returning of values:

let fibonacci = fn(x) {
  if (x == 0) {
    0
  } else {
    if (x == 1) {
      return 1;
    } else {
      fibonacci(x - 1) + fibonacci(x - 2);
    }
  }
};

The crown-jewel we’ll build into our new Monkey implementation are closures:

// `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

Yes, we’ll compile all of that to bytecode and execute it in a stack-based virtual machine. That not only works, but is also tremendous fun to build.

And Monkey has taken on a life of its own: readers have built nearly 100 Monkey implementations in more than 25 languages, from Rust and TypeScript to Zig, OCaml and Haskell.

Reviews

What readers are saying about this book

Rated 4.7 ★ on Amazon and Goodreads.

This book is incredible. … It's like the practical, up-to-date version of the Dragon book. If I was trapped on an island and forced to build a programming language (hint hint!), this is the only book I would take.

Joanna M
Verified purchase

on Amazon

Thorsten has a wonderful gift — as you read his books, you'll feel as though you've discovered compilers, all on your own! As a self-taught engineer without a CS degree, I found myself led on a journey of learning, driven by the nuggets of 'why' that Thorsten shares alongside code.

I've recommended these books to many, and I won't hesitate to continue. Compilers, interpreters and programming languages may seem esoteric, but if you look closely, they're everywhere. The Monkey language is my go-to project whenever I learn a new programming language. There's no better way to take a new language through its paces!

Lauren Tan Lauren Tan @potetotes
Software Engineer

Thorsten has done an amazing job at taking a complicated subject, distilling it down into digestible parts and taking the reader along (a fun) journey to implement a compiler in Go.

10/10 I would highly recommend this book.

Avi
Verified purchase

on Amazon

This book has made the art of implementing compiler and bytecode virtual machines broadly accessible. … I don't think there is any other book like this.

Saurabh Jha
on Goodreads

Longing for implementing a compiler, but lacking the guidance? Then this book is for you. Thorsten offers invaluable mentorship for tackling the multiple challenges that arise when writing a single-pass compiler and a virtual machine to execute the resulting code, in a pragmatic and test-driven approach. After going over this book, you'll certainly feel inspired to go further, such as extending the compiler with code transformations of your own.

Ricardo Nabinger Sanchez Ricardo Nabinger Sanchez @rnsanchez
Performance Engineer at Taghos Tecnologia

I went over "Writing an Interpreter in Go" and "Writing a Compiler in Go" but instead of doing it in Go, I did it in C. … I really recommend Writing an Interpreter/Compiler in Go.

Krzysztof Gabis kgabis
Creator of the Ape programming language

on Hacker News

Finally, a compiler book that you don't have to fight each and every page to get through! Other books burden the reader with so many details and subtleties that you'll never make progress. But if you want to make an actual compiler, read this. I'll definitely be recommending this to my students.

Austin Henley Austin Henley @austinzhenley
Assistant Professor, Department of Electrical Engineering & Computer Science, University of Tennessee

Making is understanding. The book carefully teaches us how VM-backed interpreters work, while building our own compiler and virtual machine. Written in a TDD style, a step-by-step manner and with a humane tone. I've enjoyed the wonderful journey!

Yoji Shidara Yoji Shidara @darashi
Programmer, English/Japanese Translator of Writing An Interpreter In Go

I think that it's one of the best self-published books on the market I've ever read, not only in Go area, but whole technical market.

Bart Smykla Bart Smykla @bartsmykla
Open Source engineer at VMWare
Read 14 more reviews

If you are scared of Dragons but want to understand how compilers work, this book is for you. Not only does it make a daunting subject approachable but it also make it fun and enjoyable.

Franck JEANNIN Franck JEANNIN @frje
Founder DataNuage

The Monkey books are everything I've ever wanted in programming books, but could never find until now. They are a real labor of love and it shows on every page. The books are fun to read and showcase the Go programming language, but what makes them extraordinary is the fact that they're completely focused on giving the reader an accessible, conceptual understanding of the implementation of interpreters and compilers. It's hard to overstate just how rare yet extremely valuable that focus on understanding is in an educational book.

Brandon Ramos Brandon Ramos
Product Owner/Developer

Just awesome books. If we had books like this on other subjects (search engines, DB systems), I think developers would have less fear of contributing to large open source projects. - Thank you @thorstenball. This is already paying off in my DB internals course.

Evance Soumaoro Evance Soumaoro @evanxg852000
Software Engineer | System Programming Enthusiast

Yes! Thorsten's books are great!

Bob Nystrom
Author of Crafting Interpreters and Game Programming Patterns

on Hacker News

…the Interpreter Book and Compiler Book, both written for Go as the implementation language. They were all a huge help for me learning the ropes initially.

Linus Lee thesephist
on Hacker News

I found them exceedingly approachable and great in any language (I did mine in Typescript).

David Brownman xavdid
Tech Lead for SDKs at Stripe

on Lobsters

Writing an Interpreter in Go / Writing a Compiler in Go, the two part series by Thorsten Ball, was what I really cut my teeth on.

cdcarter
Works on the Apex compiler and runtime at Salesforce

on Hacker News

Absolutely best books I've ever read on the subject.

prologic
on Hacker News

I cannot recommend Thorsten Ball's books highly enough. They're really fantastic, step-by-step tutorials, and you certainly don't need a CS degree to understand what he's doing.

WaltPurvis
on Hacker News

If you want to dive into compiler/interpreters but do not want to go straight into the computer science of it I wholeheartedly recommend: https://compilerbook.com and https://interpreterbook.com

Nicolás Hatcher nhatcher
Creator of IronCalc

on Hacker News

An excellent tutorial on writing a bytecode compiler and VM for a modern language, with implementation code in Go. Gets 5 stars for the execution, humour, language and code completeness.

Fotis Koutoulakis
on Goodreads

Once I am done finishing it, feels like as if I went thru a course in Computer Science.

Michael Chan
on Goodreads

…continues to showcase the author’s excellent teaching style. Like the first book, it offers clear explanations and practical coding examples.

Tomas Dabašinskas
on Goodreads

Both books are wonderfully written. Code in both book work as advertised.

Ever since I left school I wanted to someday write a compiler. I did it! :-)

kdfuser
Verified purchase

on Amazon

What readers said about the first book

Writing An Interpreter In Go is rated 4.6 ★ on Amazon and Goodreads.

I wrote "Crafting Interpreters" and read and very much enjoyed Thorsten's interpreters book. … Both are written in an informal friendly style and focus on working code. … His will get you to a working interpreter with less prose to wade through.

Bob Nystrom
Author of Crafting Interpreters and Game Programming Patterns

on Hacker News

Compilers was the most surprisingly useful university course I ever took. Learning to write a parser and runtime for a toy language helps take away a lot of "magic" in various parts of computer science. I recommend any engineer who isn't familiar with lexers, parsers, and evaluators to read Thorsten's book.

Mitchell Hashimoto Mitchell Hashimoto @mitchellh
Founder of HashiCorp

Great book. I loved it because everything is built by hand, so you get to think about all the details, and it does so in a gradual way, which is didactic. The implementation itself is also nice and simple 🙌

Xavier Noria Xavier Noria @fxn
Everlasting student, Rails Core Team, Ruby Hero, Freelance, Live lover

Thorsten took a topic that is usually very dry and CS heavy and made it accessible and easy to understand. After reading this book I felt confident enough to write Plush, the templating language I’ve always wanted in Go! If you have yet to read Thorsten's book, I can't recommend it enough. Please go and buy it!

Mark Bates Mark Bates @markbates
Creator of gobuffalo.io

This book demystifies and makes the topic of interpreters approachable and fun. Don't be surprised if you become a better Go programmer after working your way through it.

Johnny Boursiquot Johnny Boursiquot @jboursiquot
Go Programmer, @BaltimoreGolang Organizer, @GolangBridge Core Member

Thorsten's writing style is fun and easy to understand with detailed explanations and even better code. Even if you've written an interpreter before, this book is a great refresher. I picked it up as a project to learn Rust, translating the example Go code into Rust as I read through. Lexers, parsers, and interpreters are such a fundamental part of CS, these skills are valuable to more than just programmers implementing programming languages. As a project for picking up a new language, this book is perfect because it only requires the standard library and has extensive test driven development, which means you get quick feedback as you go along. I highly recommend it for programmers wanting to learn more about lexers, parsers, and interpreters or Go programmers picking up a new language looking for a project to learn through.

Paul Dix Paul Dix @pauldix
CTO of InfluxDB
Read 29 more reviews

Amazing book! Besides satisfying my curiosity with clear writing and code examples, the book inspired me to apply those skills to a new http testing tool I’m working on.

Felix Geisendörfer Felix Geisendörfer @felixge
Prolific Open Source Contributor, Creator of GoDrone, Node.js Core Alumni

I really enjoyed the modern, practical approach of this book. Diving into the world of interpreters, by getting your hands dirty right from the beginning.

Christian Bäuerlein Christian Bäuerlein @fabrik42
Developer, Organizer & Curator of MECHANICON

Great writing and explanations. The practical focus of the book kept me coding for a week straight. Definitely the best book to get into the magical world of compilers and interpreters.

Arthur Tabatchnic LinkedIn
Senior Cloud Solutions Developer

We use parsers and interpreters on a daily basis, just think of JavaScript and JSON. This book not only helped me to better understand how they work but will come in handy the next time I have to implement a parser for an obscure data format.

Robin Mehner Robin Mehner @rmehner
Developer, Organizer of BerlinJS, Reject.JS & NodeCopter.

This book clearly, and elegantly explains the different pieces needed to make a language. From lexing and parsing to actually executing the code, this book does a great job explaining to the reader the purpose of each element and how they interact.

Lee Keitel lfkeitel
Systems Programmer & Network Technician

I loved this book and it remains one of my favorite #golang books to this day.

Brian Downs Brian Downs @bdowns328
Software Engineer & Organizer of Golang Phoenix

I only wish this book was available ten years ago! At the time I was using Appel's Java book and trying wade through the dragon book too. It's so refreshing to have a TDD-based tutorial to learn the concepts in a language you might reasonably use to build an interpreter.

Robert Gravina Robert Gravina @robertgravina
Programmer

It has been one of the funnest experiences in my programming career. I recommended your books to all my friends in the industry. I recently finished 'Writing An Interpreter In Go' and yesterday I purchased 'Writing A Compiler In Go'. Once again thank you! I was blind and now I can see, thanks to you!

Rodrigo Lessa

This book is not only educational, but the code quality is incredible, which allows the reader to move seamlessly from chapter to chapter without the need to scratch their head over what the code means or how it works. It is cleanly separated, well optimized, highly readable, and very precise in its functionality. Because of this, it provides an excellent example for both novices and veterans of the Go programming language, and will serve readers beyond a purely intellectual understanding of programming language design and functionality; the code used in the book will also provide a solid foundation in Go programming that can be practically applied right away. This balance is tough to achieve, and made the book a joy to read.

Aaron Hnatiw Aaron Hnatiw @TheHackerDev
Hacker, educator, software developer

I was completely hooked by your book on writing an interpreter and read it in 3 days. It might be the best book on programming I've ever read, and I read a lot of them. I love how all of the concepts are explained simply through very readable code and I love how the product turned out so real and useful. I wish more books were written in this style and I look forward to diving into the sequel!

Ludvig Gislason Ludvig Gislason @ludviggislason
Software Engineer

The best thing to do on a maternity leave when the baby is sleeping? Write an interpreter based on Thorsten Ball's book Writing An Interpreter In Go. I had so much fun! Now off to writing a compiler!

Alena Varkockova Alena Varkockova @alenkacz
Distributed systems enthusiast, working at Mesosphere

It's been the most fun I've had in years.

Danny van Kooten

I can't recommend this book more highly. Not just as an example of interpreter construction or engineering in Go, but as a perfect example of technical writing.

Andromeda Robins
on Goodreads

This is a great book. It's clear, concise and packed full of learnings.

The result is impressive, and I am somewhat astonished by just how much content is actually in here, and how well written said content is.

Simon Larsén
Senior Software Engineer at GitButler

on slar.se

I really love Writing An Interpreter In Go. The book presents the material in a smooth and clear progression and anticipates the questions the reader may have.

Paolo Amoroso
on his journal

I am a 66-year-old retired programmer. I just finished this month of reading and coding along with reading your remarkable book, which gave me moments of real joy.

Liviu Moraru
on Register Spill

I have gone through the dragon book before and have been working on compilers optimisations for a while. But Thorsten's book made me comfortable in compiler frontend.

Saurabh Jha
on Goodreads

Read this if you're bored by the first few chapters of literally ANY compiler book in existence and just want to write a working parser you actually understand.

Mariano UG
on Goodreads

By far, my favourite technical book I've ever read.

Roger
on Goodreads

A simply outstanding book on Programming Language implementation. Thorsten Ball has done an amazing job writing a book with clear explanations, easy to follow code, code that works

Fotis Koutoulakis
on Goodreads

This book is excellent. It is well written, and the code extracts compile and work. That is not something I can say about all the computer books I have had over the years.

I am an experienced developer who has read many computer books, including ones on compilers and interpreters. This one is probably at the top of my list of well written computer programming books.

Sam Brown
Verified purchase

on Amazon

Perhaps the most approachable literature I have seen on the topic, everything is explained clearly, in detail, and without skimping on specifics - but it's not at all overwhelming.

Ellie Huxtable
on Amazon

I learned so much from working through this book, and not just about interpreters - about dealing with complexity, writing proper unit tests, writing a REPL, etc.

Josh Weston
Verified purchase

on Amazon

…this strikes the perfect balance between a theory laden dissertation and a trivial example. It's clear, concise and hands on.

Joel Thormeier
Verified purchase

on Amazon

I've read many books and web articles on writing compilers and interpreters and this is one of the most accessible yet. … The book contains a lot of simple elegant code that is a joy to read.

I would love it if the author writes a second volume focused on optimization or compiling down to native instruction.

F. Huynh
Verified purchase

on Amazon

The book definitely delivers on the promise - it builds the interpreter from the ground up, from lexer to parser to evaluator, with tests written first to give the reader an immediate view of what's coming.

I especially liked the fact that the author doesn't cut corners on writing test harness for the code, and doesn't abuse the "<such and such> is left as an exercise for the reader" phrase - that alone is an indicator of thoroughness and no-nonsense approach to writing.

Sergey Ten
Verified purchase

on Amazon

This is probably the 9th book about compilers I bought and read in the last two years... And I loved it.

…it is a book about writing an interpreter in Go, exactly as the title says, and I love it.

cprieto
Verified purchase

on Amazon

The writing is very accessible, and the Pratt Parser implemented is thoughtfully constructed.

Also, I really like the fact that the interpreter is built TDD-style. That makes it easy to spot errors early, and also to extend the implementation painlessly.

Dr Funk
Verified purchase

on Amazon

Thorsten illustrates some of the joy and elegance of programming small clever things, before we got bogged down in frameworks and enterprise jargon.

ak
Verified purchase

on Amazon

Get the book

Build your own compiler and virtual machine

eBook

$29

  • The book in ePub, Mobi (Kindle), PDF and HTML
  • Around 260 pages of content with code and illustrations
  • All the code presented in the book, with tests
Buy the eBook

Paperback

$39

  • The book in a beautiful 360 pages paperback version
  • All the code, with tests, as a download
  • Amazon support: all of Amazon's money-back guarantees and shipping options
Buy the paperback

All the code Runnable, organized by chapter, MIT licensed and including the complete test suite.

Free updates eBook buyers get every update for the lifetime of that edition of the book.

Money-back guarantee I want you to enjoy this book. If you, for any reason, are not happy with it, just send me an email. You'll keep what you bought and get your money back.

Current version: 1.2. Last updated 7 May 2020. See the changelog.

FAQ

Frequently asked questions

Do I need to read the first book, Writing An Interpreter In Go, before this one?

The short answer: Yes.

The slightly longer version is this: Writing A Compiler In Go is the sequel to Writing An Interpreter In Go and starts right where the other one stopped. It also uses the code from the first book (the lexer, the parser, the AST, the object system) and extends it.

If you're okay with treating the code as a black box, then no, you don't need to start with the first book. But that's not what I recommend or what these books are about; they're about opening up black boxes, looking inside and shining a light. You'll have the best understanding of where we're going in this book, if you know where we started.

Does the code still work with today's Go?

Yes. The code comes with go.mod files, uses only the standard library and its complete test suite passes with current Go releases.

Can I buy a bundle of the eBook and the paperback?

I'm sorry to say it, but no, I cannot bundle eBooks with paperbacks. It's not that I don't want to (I do!) but I can't. The eBooks are sold and distributed through LemonSqueezy, where I have a lot of influence on the process, but the paperback editions are being printed, sold and shipped by Amazon and I don't have many options there. I can't tell Amazon to bundle digital files with the paperback. Sorry!

I found a typo/mistake/error in the book. What now?

Take a look at the changelog to see whether I've already fixed it. If I haven't or you're not sure I have, please send me an email to me @ thorstenball.com. I really appreciate it!

The books are too expensive for me. Can you help me out?

Sure, just send me a picture! I'm always fascinated by new places and love seeing where people live, so here's my proposal.

You go outside, take a picture of where you live and send it to me to me @ thorstenball.com. Tell me what you feel comfortable paying for the book(s) and we'll make that happen.

Thorsten Ball

About the author

Hello there! My name is Thorsten Ball. I’m a programmer living in Germany and a co-founder of Amp, the coding agent. Before Amp, I worked at Sourcegraph from 2019 to 2024 and then at Zed, the code editor.

In my spare time, I like to do what I call recreational programming, where I dig deep into various topics and their codebases, taking them apart, recreating them in my own code, trying to peel away layers of what seems like magic, hoping to come out the other end with a better understanding of what it is that I do when I program.

Writing an interpreter from scratch and writing a book about that has been one of the most wonderful and satisfying things I ever did as a programmer. It has been so much fun, in fact, that I couldn’t stop doing it, which is why you’re looking at the next book.

Every week I write Register Spill, a newsletter with thoughts about software engineering that are too ephemeral for blog posts and too long for social media. I’ve also talked about interpreters, compilers and, lately, coding agents on podcasts such as Go Time, CoRecursive, Software Engineering Radio, CodeNewbie and Latent Space. You can find all of them on my website.

You can also follow me on Twitter, Bluesky and Mastodon, or take a look at my GitHub profile.

Any questions?

If you have any questions, send me an email. I promise, you'll make my day. I love getting email from you: me @ thorstenball.com

Select your Amazon store

The paperback is printed, sold and shipped by Amazon.