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

Loading interface...
Loading interface...

About the author

Thorsten Ball

2 books93 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
126 (61%)
4 stars
71 (34%)
3 stars
9 (4%)
2 stars
0 (0%)
1 star
0 (0%)
Displaying 1 - 29 of 29 reviews
Profile Image for William Roe.
14 reviews4 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 Mr. Musale.
63 reviews8 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.
25 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.
379 reviews6 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.
79 reviews9 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.
954 reviews5 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!
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.
2 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.
77 reviews13 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.
48 reviews28 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.
13 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."
Profile Image for Picasso.
49 reviews
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.
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.
4 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 :)
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 Andey Robins.
19 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 Truong Hoang.
11 reviews3 followers
July 5, 2021
With this book, I wrote an understandable and well-working interpreter. Cool ideas behind an interpreter are clearly explained, and honestly, this is one of the most fun books about programming I've read so far.
Profile Image for Pavel Durov.
2 reviews
October 30, 2022
This book is fun, by far the most approachable book on the language structure I had. Although its not go in-depth into theoretical topics, I found it refreshing and fun to work with!
Highly recommend it to anyone interested in understanding what happens behind the scenes of interpreted languages.
5 reviews
September 16, 2018
For the beginner and explains how to implement an interpreter in Go from first principles. Very little theory is covered making it accessible to most programmers.
Profile Image for Carlos Becker.
23 reviews21 followers
January 29, 2020
Awesome for people curious about how interpreters work and who want a practical book about it.
Profile Image for Amr Hesham.
27 reviews3 followers
April 21, 2021
I enjoyed reading this book and i loved how he implemented the interpreter with TDD approach, I recommended it
Profile Image for TK.
67 reviews44 followers
March 23, 2023
The book has very thorough explanations. And even though it's a complex topic, I found it super accessible and easy to understand even if you don't have experience with golang. But you probably need to be comfortable with programming in general, at least the basic stuff.

I liked the explanations of the Pratt parser even though I think it would be easier to explain with animation but it's impossible to do it with books. With this limited book aspect, I think it did a really pretty good job explaining this complex part of the interpreter.

Another interesting thing was the implementation of the REPL. For every little implementation I did, the lexer, parser, or evaluator, I could see it in action and experiment with it on the REPL.

The testing part was a bit confusing for me because I don't have experience with tests in golang. But it starts becoming easier and easier to parse in your head over time as you start getting familiar with how you test code in golang.

Still, about testing, I liked the TDD approach but I would also like to see a chapter on proofs using something like Coq. Maybe it's out of scope but it would be nice to see proofs implemented for the PL.

I highly recommend it to people who want to learn more about interpreters and compilers or want to do something hard.

These are my contributions:

- My own implementation: Crafting an Interpreter github repository
- Blog posts about my journey implementing the interpreter
7 reviews
June 2, 2018
I had a great time reading this book. He manages to cover an enormous and daunting subject in a short book, with an approachable style. I felt like I learnt to approach the topic at a reasonably advanced level, while understanding the limitations of that level. There's oodles more to learn, but the platform is solid.

It feels like he learnt the content recently enough to guide you through his own learning process. This is my favourite style of teaching.

Finally, the 'toy' language he created - Monkey - is really enjoyable. It makes for a nice base for a wide range of languages, which can be found on github. Several derived works are out there, and that number is growing.
1 review1 follower
November 30, 2016
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.
Displaying 1 - 29 of 29 reviews

Can't find what you're looking for?

Get help and learn more about the design.