Free through lesson 3

Build Your Own Interpreter

From lexing, parsing, and tree-walking evaluation to bytecode and a virtual machine, you'll build one small programming language, Mame, all the way through in Python. Over 30 lessons, it grows variables, functions, closures, arrays, exceptions, and a REPL, and ends up with a test suite and a written spec. Every piece of code was actually run with Python on a local machine.

about 3 hours in total (estimate)

Curriculum

The 30 lessons are split into 6 chapters. We recommend working through them in order starting from Chapter 1: each chapter grows the same interpreter. * Short experiments run in your browser. You'll build the interpreter itself with Python 3.12 or later on your own machine (all output in this course was generated with Python 3.13.18). No third-party packages are used.

Chapter 1 — Before We Build (lessons 1–5)

See how an interpreter turns source into tokens, then a tree, then a value, using Python itself and a small calculator. By the end, you'll write the Mame lexer and point at mistakes with a ^.

Chapter 2 — Parsing (lessons 6–10)

Write Mame's grammar in BNF, design the shape of the tree (AST), then build the parser with recursive descent and Pratt parsing. By the end, the parser keeps reading after a mistake and reports several syntax errors at once.

Chapter 3 — Evaluation (lessons 11–15)

Build an evaluator that walks the tree from the root to compute values, then add variables and scope, if and while, functions and return, and closures. By the end of the chapter, the goal.mame from lesson 1 runs all the way through.

Chapter 4 — Data (lessons 16–20)

Add string indexing, arrays, and hashes, and reorganize the growing set of built-ins into a registered table. Pass failures to callers with throw, try, and catch, and finish with a REPL for trying things one line at a time.

Chapter 5 — Making It Fast (lessons 21–25)

Find the slow spots with a benchmark and a profiler, then compile the tree to bytecode and run it on a stack VM. Cut instructions further with constant folding, and finally see how GC cleans up unused values, using Mame's environments.

Chapter 6 — Finishing Touches (lessons 26–30)

Close the hole from lesson 15 with a static check that settles where names point before running, and guard both ways of running with a test suite. Split programs into files with modules, write the rules down in a spec, and finish Mame.

Once you've finished all 30 lessons, head to How Computers Work to learn what runs underneath a language, or to Rust Fundamentals, a good language for rewriting your interpreter to be fast. A membership unlocks every course.