Finished 'Writing an interpreter in Go'

I did it. I finished a non-fiction book and completed all the exercises in the book. I just completed 'Writing an interpreter in Go' by Thorsten Ball. I read the entire book, cover to cover.

With a fulltime job, wife, kids and a house it is not often I find the time to finish a book like this. Sure, I read a bit here and there - but I don't always find the time to sit down and do the actual work of the books that I read. So completing the last page of the book felt like an achievement of sorts. It helped me along the way that it was a cool subject and that Ball is able to convey the content in a clear way. He writes very conversationally while still delivering a good theoretical background.

About the book

The subject of the book should not come as a surprise after having read the title. 'Writing an interpreter in Go' is about.... writing an interpreter in Go. And one of the goals Ball sets is to use no 3rd party libraries. And he actually keeps to this promise. Most of the time when I see such promises in books there is always exceptions and they end up using one or two libraries anyways. This goal makes it very easy to translate the implementation from Go to any other language. You can see on the Monkey Language webpage that the interpreter has been implemented in pretty much all languages. Using no 3rd party libraries also helps you understand every bit of the interpreter: Nothing is hidden behind a magic function.

The language that you implement an interpreter for is the Monkey Language. It looks a bit like javascript and behaves similarly. But it is easy to read and write and does not have too much fuzz about it. This makes it easy to follow along on the examples and code snippets because you do not have to waste time figuring out why the language behaves strangely or why a line of Monkey language code does something that seems like magic. If you know how to program in pretty much any language, you should be able to easily decipher the Monkey language.

You will start from scratch with no code, no tests and no project files and 250 pages later you will have a fully functioning interpreter. There is a lot of code in the book and everything is done in TDD fashion.

If you are interested in compilers or interpreters and want an easy-to-follow project, this is definitely the book for you. It was fun and leaves you with a fully working interpreter that you can easily extend by yourself. Ball describes the reasoning for all the steps and provides you enough theory to take new concepts and add them to the finished codebase.

Writing an interpreter in Go ... in Java?

I chose to write the implementation of the interpreter in Java. I know, I know, Java is not very sexy and sooo last year (decade?). All the cool kids are either doing it in Go or Rust today. I actually started doing it in Rust and with the pattern matching in Rust it felt like a great fit. But then I decided that I wanted a chance to test out the new features in recent Java version that I do not get to try out at work. It is also rare that I create a Java project from scratch so that was a small exercise. Trying out pattern match for switch (as mentioned here) was a fun experience. Java might not be sooo last year anymore.

This might be sacrilegious to say but I definitely prefer the code structure of Java compared to Rust and Go. I have many small logical files with clear names instead of bigger files containing lots and lots of structs. And with Functional Interfaces, streams, pattern matching and records I do not really feel like I am missing out on the modern cool stuff that attracts people to Go and Rust.

What's next?

I left a bunch of TODO's in my code that I need to fix. I also have a few ideas for how to make the code prettier and easier to read. So I will work on that. I think it could be fun to throw the code into a backend and then create a website where you can REPL the Monkey language and explore the AST. So there is definitely still work to be done on the code even though I am done with the book.

Thorsten Ball has also written a successor to this book. It is called 'Writing a compiler in Go'. That is a project I will start maybe first thing in the new year. It is a little thicker than his interpreter book with around 330 pages.

I think you should read it too

The subject of the book was easy to digest because of the writing style of Ball. It is far from dry and he talks you through thing rather than just stating facts. If you are able and willing to do the code in the book you are rewarded with a working interpreter and a lot of green checkmarks due to the test driven development style of the book.

Discussion, links, and tweets

I'm a developer and CS graduate. Follow me on Twitter; you'll enjoy my tweets.

I also keep an "ask me anything" type of project in a repository on GitHub (naturally!). Feel free to ask me a question.