Writing a Compiler in Go
246 pages
English
PDF
4.5

Writing a Compiler in Go

Writing a Compiler in Go

Author:

Year:

Summary:

Introduction

The book "Writing a Compiler in Go" is the second part of Thorsten Ball's famous duology dedicated to creating an interpreter and compiler for the Monkey language. While the first book ("Writing an Interpreter in Go") teaches you to build an interpreter, this volume leads the reader further — towards creating a full-fledged compiler that generates bytecode, and its own virtual machine (VM).

The book was published in 2019, contains 246 pages, and is aimed at programmers proficient in Go (Golang). It is ideal for those who want to deeply understand how compilers, bytecode, and virtual machines work in practice.

What the book is about

Thorsten Ball shows step by step how to turn the interpreter from the first book into a compiler. The main idea: instead of executing the AST (Abstract Syntax Tree) directly, we compile it into bytecode instructions and then execute them on a virtual machine.

The book covers the following topics:

  • Designing an instruction system (opcodes) for bytecode.
  • Implementing a compiler that transforms an AST into a sequence of bytecode.
  • Creating a stack-based virtual machine (VM).
  • Handling variables, functions, closures, arrays, hashes, and built-in functions.
  • Optimizing and debugging the compiler.
  • Testing each stage with unit tests.

Who this book is for

The book is intended for intermediate and advanced Go developers who are already familiar with the basics of the language and want to delve into systems programming. It will also be useful for anyone interested in compilers, virtual machines, and the internal workings of programming languages. If you have read the first book, this is a mandatory continuation. If not, the author recommends starting with it, but you can also jump straight in, although it will be more challenging.

What the reader will learn

  • How compilation of AST into bytecode works. You will learn to design an instruction system and write code that generates bytecode from the parse tree.
  • How a stack-based virtual machine is structured. You will implement a VM that executes bytecode, manages the stack, function calls, and memory.
  • How to handle closures. One of the most complex topics — capturing variables from an outer context — is covered in great detail.
  • How to test the compiler and VM. The author pays a lot of attention to testing, making the book practical.
  • How to debug bytecode. You will learn to output traces and find errors in the compiler.

Strengths

  • Practical focus. Each chapter contains code that you can immediately run and modify. You don't just read theory; you write a real compiler.
  • Step-by-step presentation. The author does not overwhelm the reader with complex concepts all at once. Each new element is introduced gradually, with examples and tests.
  • Clean and understandable Go code. The code is written in a style close to production-ready, with an emphasis on readability and modularity.
  • Deep dive into the topic. You will learn not only the "how" but also the "why" — the author explains trade-offs and alternative approaches.
  • Self-contained. The book contains everything you need: from design to final implementation. At the end, you get a working compiler and VM.

Who is recommended

The book will be useful for:

  • Go developers looking to broaden their horizons and understand how programming languages work internally.
  • Students and teachers of compiler courses — as excellent practical material.
  • Developers in other languages (Python, Java, C++) familiar with Go — the concepts are universal.

Recommendations