Writing an Interpreter in Go
212 pages
English
PDF
4.5

Writing an Interpreter in Go

Writing an Interpreter in Go

Author:

Year:

Summary:

Introduction

The book «Writing an Interpreter in Go» by Thorsten Ball is a unique practical guide that teaches you step by step how to create a full-fledged interpreter for the fictional programming language Monkey. Unlike academic textbooks on compilers, this book focuses on real code and minimal theory, allowing the reader to immediately start writing a working interpreter in Go.

The book was published in 2018, contains 212 pages, and is aimed at programmers who want to gain a deeper understanding of how programming languages work under the hood. It does not require prior knowledge of compilers, but assumes a solid command of Go.

What This Book Is About

The book systematically guides the reader through all stages of creating an interpreter: from lexical analysis to code execution. The main topics include:

  • Lexical analyzer (lexer) — breaking source code into tokens.
  • Parser — building an abstract syntax tree (AST) using recursive descent and the Pratt parsing method.
  • Object system — representing values (numbers, strings, booleans, functions, arrays, hashes).
  • Evaluator (eval) — traversing the AST and executing expressions and statements.
  • Closures and first-class functions — implementing scopes and closures.
  • Built-in functions — adding standard operations (len, puts, first, rest, etc.).
  • Type system — dynamic typing with runtime checks.

Each chapter ends with working code that can be tested immediately. In the end, the reader gets an interpreter capable of executing programs in Monkey — a language with a syntax reminiscent of a mix of C, JavaScript, and Python.

Who This Book Is For

This book will be useful for:

  • Go developers looking to deepen their understanding of the language and its capabilities.
  • Programmers interested in compilers — those who want to learn how parsers and interpreters work.
  • Students and teachers — as a practical supplement to courses on programming languages.
  • Any developer who wants to create their own language or DSL (domain-specific language).

Important: the book does not teach Go from scratch — the reader should be able to write Go at an intermediate level (knowledge of structs, interfaces, pointers is expected; goroutines are not required).

What the Reader Will Learn

After reading, you will be able to:

  • Write a lexer from scratch that breaks strings into tokens.
  • Implement a parser using recursive descent with operator precedence.
  • Build and traverse an AST.
  • Create a value system and environments (execution contexts).
  • Implement closures and higher-order functions.
  • Add support for arrays, hashes, and string operations.
  • Understand how dynamic dispatch and error handling work.
  • Obtain ready-to-use interpreter code that can be extended.

Strengths of the Book

Practical focus. Every concept is immediately backed by code. You don't just read theory — you write the interpreter line by line.

Step-by-step presentation. The book is divided into small chapters, each adding new functionality. This makes it easy to return to previous sections.

Clean Go code. The author uses idiomatic Go, interfaces, and composition, making the code both educational and production-ready.

No unnecessary theory. Unlike the "Dragon Book" (Aho, Lam, Sethi, Ullman), there are no formal grammars or proofs — only what is needed for a working interpreter.

Recommendations