minizig

This book is a collection of problems. You solve them, one by one, and by the end you'll have built a programming language from scratch — interpreter, compiler, virtual machine, the whole thing. The compiler will compile its own source code.

Most compiler books invent a toy language. We use a subset of Zig — every program in this book compiles with the real Zig compiler. Most compiler books invent a bytecode. We target WebAssembly — an actual bytecode that runs in browsers, servers, and standalone runtimes. Most books show you how to interpret or compile. We do both, in parallel, because understanding one makes the other obvious. Then we build a virtual machine for the bytecode ourselves, because why not.

Type the code by hand. Resist the urge to copy-paste or ask an AI to write it for you. The hands do the learning, not the eyes. Every function you type yourself is one you'll remember when you need it three chapters later. This book was written by a human with the help of AI tools — but the best way to read it is to be as human as you can. Type it, break it, fix it, understand it. You'll learn more and feel good about it.

start →

A number

A real parser

Variables

Control flow

Functions

The Compiler

The VM

Feeding the Snake

Global Variables

Completing the Language

The Compiler, in Its Own Language

The Snake Eats Its Tail

Appendix A: A Simple Allocator

Appendix B: Floating Point