Jump to ratings and reviews
Rate this book

Writing An Interpreter In Go

Rate this book
In this book we will create a programming language together.

We'll start with 0 lines of code and end up with a fully working interpreter for the Monkey programming language. Monkey has been specifically designed for this book: it's a language that looks a lot like C, has first class functions, closures, strings, hashes and arrays and its only implementation is the one we build in the book.

In contrast to text books on interpreters & compilers, the focus of this book is working code. Code is not just found in the appendix -- no, nearly every page contains a snippet! And not only that, but also tests. The code presented in the book is fully tested and the test suite is included.

For more information, check out: http://interpreterbook.com/

200 pages, ebook

Published November 23, 2016

283 people are currently reading
1023 people want to read

About the author

Thorsten Ball

2 books164 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
206 (63%)
4 stars
104 (31%)
3 stars
16 (4%)
2 stars
0 (0%)
1 star
0 (0%)
Displaying 1 - 30 of 47 reviews
Profile Image for William Roe.
15 reviews8 followers
October 13, 2018
I loved following this guide through from start to finish. For the most part I'd read and type out the code on my journeys to and from work on the train. There weren't very many places in the book where I had to stop and really concentrate without distractions, but that may be due to my having done compiler and interpreter courses at university a way back, as well as this being one of my main areas of interest in computer science.

When I followed this book through, my skill level with Go was as a not confident beginner, but I found the code relatively straightforward to follow and when I made a typo in typing it out, I never got stuck looking at a confounding compiler error. That's important because this book isn't about Go, but it means Go was a very good choice of implementation language because the exercise of building an interpreter never gets bogged down in Go's quirks or semantics (with few rare exceptions, it's not perfect!).

What really kept me working through this book with no appreciable waning of interest was its incremental nature. Although I was typing out the code in the book line by line, I felt like I was building a real system, tests and all.

After reading this book I immediately read the next mini-book called "The Lost Chapter: A Macro System for Monkey", which was also extremely good (and fun to implement). And then I moved onto the next book: Writing a Compiler in Go.
Profile Image for Matt.
Author 1 book25 followers
June 9, 2024
This is a really solid book and a great way to get an understanding of how many programming languages work.
Profile Image for Mr. Musale.
64 reviews10 followers
May 21, 2020
I enjoyed reading this book. Currently, I'm interested in programming languages and how they work and this book did a fantastic job explaining to me a lot of things. I especially liked the Pratt Parsing section; it had nice images and accompanying code that make the explanations very understandable.

Another thing I liked is the way Thorsten explains concepts and then if it's not in the scope of the book, he gives you nuggets about it and challenges you to take it up and implement it. I liked that as it helped me to gauge myself if I understood the concept.

For someone who is interested in programming languages, I would recommend this book for them.
34 reviews2 followers
January 3, 2023
I really enjoyed going through this book.

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.

The best pedagogical decision author has taken here is the use of Pratt parsing. Parsing is very hard to explain well but this decision, along with Thorsten's writing, made this topic much more palatable.

The use of Go was also a good decision. Go's standard libraries were enough to implement the interpreter. This self-containment is a great feature of this book.

The only place where the book got slightly boring at times was in test cases. I don't think anyone could make that part interesting. Moreover, the TDD approach actually helped me understand the concepts.

Overall, I had a great time with this book.
Profile Image for Hưng Đặng.
129 reviews71 followers
February 25, 2025
Viết 1 trình thông dịch tree-walking được tác giả trình bày khá đơn giản, t cảm thấy vậy. Về cơ bản là có khoảng 4 bước:

Source code -> Token -> Abstract syntax tree (AST) -> Evaluation

Giờ nhìn lại thấy mọi thứ trên lý thuyết không khó và nhờ có tài năng diễn giải của Thorsten, thực sự cảm giác viết lại cũng không quá khó. Ngoài ra cũng nên dành lời khen cho Go, một ngôn ngữ ổn định, syntax khá quen thuộc (t đi từ JS) và backward compatibility tốt.

Có một điều mà t không thực sự hài lòng là t không biết Go nên chưa thể thẩm được hết độ thú vị của sách. Nếu mục tiêu của t là có thể tự mình viết lại 1 chiếc interpreter vì thực sự ở thời điểm hiện tại, t chỉ có thể tạm hiểu được ngôn ngữ này.

Cuốn sách cũng làm t phóng chiếu đến GraphQL và AST của nó. Liệu chăng có điều gì khác biệt ở GraphQL hay nó cũng chỉ là 1 ngôn ngữ thông dịch tree-walking bình thường.

Pratt parser cũng là 1 câu chuyện thú vị. Sau khi được Vaughan Pratt viết ra từ 1973, trong 1 thời gian dài phương pháp parsing này bị bỏ qua cho đến khi Douglas Crockford sử dụng nó để viết jslint và Bob Nystrom.

Bài học đầu tiên là "peek" về phía trước để biến text thành token, nhưng cách này sẽ gặp phải vấn đề - như Thorsten nói - vì nó quá phức tạp vì sử dụng do phải gắn các hàm parsing với các quy tắc ngữ pháp (grammar rules theo BNF hoặc EBNF). Cách của Pratt là gắn 2 hàm parsing vào mỗi 1 loại token tuỳ theo vị trí của loại token đó là ở giữa (infix) hay ở đầu (prefix)

Có một chuyện xảy ra khi đang xây cây AST là một số các token cần biết các token phía trước và phía sau của nó (E.g dấu "+") nên "precedence" được sử dụng để biết mức độ dính của các token phía trước và phía sau của 1 token.

Phần evaluation cần sự cẩn trọng khi viết do mức độ ảnh hưởng cao của nó do sự thay đổi nhỏ của evaluator có thể gây ảnh hưởng lớn tới ngôn ngữ lập trình. Có thể điều này cũng là 1 phần lý do vì sao Thorsten viết cuốn sách theo phong cách TDD. Tiện nói đến evaluator, đôi khi để tăng tốc độ chạy code, thay vì thực hiện trực tiếp cây AST thì cây AST được biến thành bytecode và sau đó được hoặc không được biên dịch ra mã máy trước khi được thực thi. Khi một máy ảo biên dịch bytecode trước khi thực thi thì khi đó ta đã có 1 trình biên dịch JIT (just in time compiler).

Block scope yêu cầu cần có môi trường (environment) để lưu các giá trị sâu bên trong các scope để thực thi dần. Trên thực tế thì environment chỉ đơn thuần là 1 hash map để ghi nhớ thứ tự thực thi khi các ngoặc nhọn lồng nhau. Khi học về JS ES6, t đã ấn tượng với feature này, giờ mới được biết cụ thể.
379 reviews10 followers
December 31, 2019
This book is amazing! I tried multiple times to study how to write an interpreter or a compiler, and most books and tutorials are just too academic and get lost in the theory.
This goes straight to the point. The code is easy to follow, the descriptions are clear, the algorithms simple and effective.
A must if you are starting out. You can always go deeper in the theory later.
Profile Image for Fotis Koutoulakis.
117 reviews13 followers
November 12, 2021
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** (I have read a number of compilers/interpreter books, and most of them provide code that kinda works, which is terrible when you already have to handle the complexity of a system as complex as a compiler).

The book is built in a way that reading just a couple of pages every time is enough to give you some gratification (seeing as you built things that work and you can test it at every step of the way) and leaves you wanting more. It gets bonus points for following Test-Driven Design.

As an aside, the final system you end up with is perfect for small experimentation and further building up.

The only (minor) downside of the book is that it's light on theory. If you deeply want to understand Programming Language Implementation/Compilers, then you probably want to use this in conjunction with a more theory-heavy book.
Profile Image for JP.
1,064 reviews9 followers
December 4, 2021
You know, I’m always up for a good ‘writing an interpreter’ book. Making programming languages is a thing I’ve done a number of times before and really have been itching to get back into again. Add to that a desire to pick up a bit more Go syntax… well, perhaps this book is just about perfect.

In a nutshell, it’s a ‘writing an interpreter’ book. They go through lexing, parsing, and evaluating. On a plus side, they include closures and first-class functions. On the downside, they specifically didn’t get into garbage collection:


Unfortunately, no. We’d have to disable Go’s GC and find a way to take over all of its duties. That’s easier said than done. It’s a huge undertaking since we would also have to take care of allocating and freeing memory ourselves - in a language that per default prohibits exactly that.

That’s why I decided to not add a “Let’s write our own GC next to Go’s GC” section to this book and to instead reuse Go’s GC. Garbage collection itself is a huge topic and adding the dimension of working around an existing GC blows it out of the scope of this book. But still, I hope that this section gave you a rough idea of what a GC does and which problems it solves. Maybe you even know now what to do if you were to translate the interpreter we built here into another host language without garbage collection.


Fair enough. Something to think about when I get that far.

I think the weakest part of the book is just how much time is spent showing the tests failing every single time. It gets old. Finally around let statements, there are a few that don’t, but it really isn’t something I felt the book needed. On the other hand, that’s a relatively minor quibble and they’re easy enough to skip.

On the other other hand, running throughout the book the sense of humor is wonderful.

In ten years, when Monkey is a famous programming language and the discussion about research-ignoring dilettantes designing programming languages is still ongoing and we’re both rich and famous, someone will ask on StackOverflow why integer comparison in Monkey is slower than boolean comparison. The answer will be written by either you or me and one of us will say that Monkey’s object system doesn’t allow pointer comparison for integer objects. It has to unwrap the value before a comparison can be made. Thus the comparison between booleans is faster. We’ll add a “Source: I wrote it.” to the bottom of our answer and earn an unheard of amount of karma.


Overall, if you’re looking for a solid introduction to writing an interpreter that goes into a decent amount of depth, this is a solid choice. Especially if Go is already something you’re familiar with. Onward!
5 reviews
April 2, 2024
I had a conflict my senior year of college and could not take the compiler course. Two of the bullets on the book's website are:

* never took a compiler course in college
* want to get started with interpreters or compilers…

Well ... that was me for sure.

I always said I was going to get around to writing an interpreter or compiler and put it off for ... let's just say 20+ years (I'm old). I started and stopped "The Dragon Book" a few times and while it's probably great, I just wouldn't or couldn't commit the time to it.

I found this book to be outstanding. The book helped me grok lexers, parsing and evaluating the AST. The hands-on approach of writing the code with unit tests was great to.

Highly recommend if you have an interest in learning about interpreters or how programming languages work in general.

I'm looking forward to the author's "Writing a Compiler in Go" next.

One recommendation if you take this book on: Don't do what I did and try to do for just 1-2 hours one day each weekend. I started doing that, but the concepts leaked out of my head too quickly. Get at least a second day per week, even if just 30 minutes, to keep the learning fresh in your brain.
Profile Image for Povilas Balzaravičius.
28 reviews
November 30, 2017
Good and clear introduction into interpreters. Reader won't find a lot of theory in this book but instead going to build an actual and working interpreter for programming language called "Monkey". Which in my opinion is really good path to understand the way interpreters work. I was actually surprised with provided features of the language starting with basic data types, conditions, functions as first class citizens, closures and more. And everything is covered in this pretty-short book. Recommended for anybody who wants to understand how interpreters works.

Why 4 starts but not 5? Because of lack of theory & weak writing style. Personally I do not like unnecessary sentences which does not provide any meaningful information in tech literature books. I count those as wasting reader's time. And this book has plenty of those. Not too much, not too annoying (because the book is short) but still there are plenty of those. This definitely does not mean you should avoid the book. Read it! It is worth.
Profile Image for Ruairidh.
4 reviews1 follower
March 20, 2020
Really enjoyable and hands-on introduction to the subject.

Not trusting myself to mindlessly copy from the examples, I decided to follow this using Lua rather than Go, and it holds up surprisingly well. The whole book is written using the core Go packages, which makes the code very simple to read for somebody with no prior knowledge of the language. It was only at the very end when implementing hashmaps that I found myself having to deviate from the ideas presented, which seems to prove the value of using a simple language like Go for communicating concepts.

At the end of the book you're left with an almost complete implementation of an interpreter for the Monkey language. The features added in the final chapter are operational but quite bare-bones, but by this point you should have the confidence to finish these off without the book's direct guidance.

Can definitely recommend for anybody interested in the subject, or as a first "proper" project for a learner who's comfortable with the basic programming concepts but isn't sure what to do next.
Profile Image for Luca.
78 reviews16 followers
September 21, 2017
The book is a great introduction to a complex (and very broad topic), I only had a basic understanding of how an interpreter works and the book helped me deepening my knowledge. I liked the approach too: the book is about writing the code for an interpreter and and the author does a great job of guiding the reader into the wonderful details of such a process.

I didn't give it the fifth star because the writing style is pretty annoying, I have to confess that. There are grammar mistakes I couldn't help noticing, and there's a lot of clutter in the text that don't contribute to a good reading experience.

Having said that, I consider the book a must if you're new to compilers and interpreters. Highly recommended.
Profile Image for Hiep Pham.
57 reviews36 followers
April 6, 2022
Kudos to the brilliant practical book!

The author walks you through the process of writing an interpreter step by step following the test-driven approach. He writes in pure Go with clear syntax. He provides sufficient explanation without overwhelming with details. The goal is to get your feet wet by having done a simple interpreter.

I was in doubt when he chose to write an interpreter for his invented language. But it turned out this approach would pave the way for you to write your own simple language.

After finishing the book, I'm confident to write an interpreter for any common language.
Profile Image for Ioram Gordadze.
14 reviews
July 22, 2022
The book is impressive and genuinely demystifies how programming language compilers work.

I was always interested in that subject, but all the books and papers I found were very academic and full of theory, which was difficult to understand without prior knowledge.

This book takes the opposite direction and dives directly into the practice. This helps and removes a lot of mystery from terms like Lexer, Parser, and AST.

I sincerely enjoyed it and looking forward to reading the sequel - "Writing A Compiler In Go."
21 reviews
December 20, 2023
I really loved this book. It's incredibly approachable, even if you're never written go before (which was the case for me when I went through it). The writing is clear, witty, funny and understandable without being patronising, which it could have easily been given the banter-ey tone Ball employs. I also feel like I have a much better understanding of how programming languages are made now. While I'm of course not an expert, I could actually see myself digging into (e.g.) the C code for python after reading this. Well worth your time
Profile Image for Serge.
7 reviews
January 1, 2024
It just happened that I’ve written a couple of cross-compilers from scratch in Scala (I probably need to write a blog about it as well), so once un-magical AdSense showed me this book, I didn’t hesitate to get it. It’s fantastic — you start by writing a Pratt parser from scratch and extend it in a pure Test Driven Development (TDD) fashion until you have a working JavaScript-like line-by-line interpreter. It’s pretty fun and pretty niche. Please also check out Thorsten’s blog  — plenty of exciting things there.

Profile Image for Picasso.
60 reviews1 follower
November 19, 2019
A really nice introduction to the topic! I still remember implementing an interpreter for a functional language in college, and ouch was it a pain. I was impressed by the simplicity of the Pratt parser and by the fact that I had never heard of it... The writing was mostly concise, clear and fun to read. My only pet peeve were the test code dumps, I know they are there for a reason and it was nice at the beginning, but afterwards it felt a bit repetitive and my eyes started to glaze over.
Profile Image for Latiif alSharif.
6 reviews
June 18, 2020
It is a must if you want to deepen your understanding of how programming languages work; I suggest you follow it but also make your own design decisions along the way. I found myself spending hours on an end tinkering and fixing errors.

In the early phases, follow the book; but later on you can challenge yourself by trying to implement the functionalities on your own. It might backfire though but that's what makes it fun.
Profile Image for Krystian.
11 reviews
June 9, 2023
Very cool. The only thing I'd change would be to add simple flowcharts to behavior descriptions. One part in particular that I think would benefit greatly from it is the description of how Vaughan Pratt's parser works - there's code, there are multiple examples and rephrased descriptions, but I think adding a flowchart would get the point across way better than rephrasing descriptions. Still, very cool and highly recommended :)
Profile Image for Tomas Dabašinskas.
51 reviews10 followers
August 16, 2024
It doesn't matter if you don't write code in Go, or even if you're not planning to write an interpreter. If you write any code at all, you should read "Writing an Interpreter in Go." The book is so well-written that it not only demonstrates the power of TDD practices but also inspires you to sit down and start coding. Plus, it's not that long. I'm now excited to dive into the author's next book, "Writing a Compiler in Go."
9 reviews
July 6, 2020
This book is a fantastic look at how interpreters are written. It demystifies the entire process, providing a high level overview of each section, code, and tests. I went into this book knowing nothing about Golang, and I came out feeling confident that I could build a programming language if I wanted to.
Profile Image for Bassam Ismail.
4 reviews8 followers
March 4, 2021
Absolutely loved the book. The author has impressive skills in making a dense subject quite simple to digest. The book uses a small subset of the language with limited use of the standard library. Even though I have been using Go for a while there were various novel aspect which I got to learn from it.
Highly recommended to anyone who didn't get a chance to learn the subject at Uni.
Profile Image for Andromeda Robins.
48 reviews
July 14, 2022
I've read many technical books, followed along with programming throughout them, and still felt like I didn't get anything out of it.

This book is the exact opposite. I found that by the time I got partly through each chapter I was thinking, "why is he still talking, he already explained it perfectly. I totally understand." I was able to finish most of the final chapter without even reading his code and explanation because the topic had been so effectively set up and explained in previous chapters.

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.
Profile Image for Eric Younger.
1 review
March 27, 2023
Holy Cow! (or should i say monkey?) What a great book! Really felt that this gave me a tremendous insight into how interpreters work, how parsing is done, and how abstract syntax trees are constructed and then evaluated and then takes the form of a programming language. Looking forward to read his other book on writing a compiler. And hoping for a third book later on as well.
Profile Image for Roger.
27 reviews
November 4, 2023
Incredible work by Thorsten Ball in order to craft such an accessible and understandable book as this one, about one of the most complex topics in computer science such as interpreters.
By far, my favourite technical book I've ever read. I'd highly recommend it to anyone interested on building a simple interpreter, regardless of what tech stack you choose to build it.
Profile Image for Fábio Fortkamp.
165 reviews5 followers
April 4, 2024
One of the most amazing programming books I've read. This is not just a book about writing interpreters, or the Go language: it is a book about writing a complete software, from scratch. The methodology is sound: lots of testing, small functions that add up, data structures that are easy to understand. The text is incredibly well written, too.
Profile Image for Mariano UG.
10 reviews
January 11, 2025
Incredibly practical, almost to a fault, though I'll let it pass due to the breath of references provided at the end, which fill in the details. 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.
108 reviews1 follower
May 11, 2025
We started by building the lexer that turns strings entered into the REPL into tokens.After that, we built the parser, a top-down recursive-descent parser(often called a Pratt parser) that turns the tokens into an AST. Then we built an evaluator. With func Eval recursively walks down the AST and evaluates it, using the object system to produce val.
Displaying 1 - 30 of 47 reviews

Can't find what you're looking for?

Get help and learn more about the design.