Skip to content

Build a Programming Language from Scratch

Build a complete programming language from first principles. You'll implement a scanner, recursive descent parser, tree-walk interpreter, bytecode compiler, stack-based virtual machine, garbage collector, closures, classes with inheritance, and a type inference engine. This is the definitive course on programming language implementation — based on the same techniques used by Python, Ruby, Lua, and JavaScript engines.

advanced38 lessons9 chapters38 graded exercisesPython

No sign-up needed for lesson 1 · certificate on completion · sign up to save progress

What you’ll have built

Chapter by chapter. Every step is a graded exercise.

  • Scanning & Tokens
  • Parsing Expressions
  • Statements & State
  • Control Flow & Functions
  • Closures & Classes
  • Bytecode Virtual Machine
  • Compiling to Bytecode
  • Garbage Collection
  • Type System & Optimization
Starts in Python — solve in the language you choose in the editor, same tests either way.

Curriculum

9 chapters, 38 lessons. Each lesson is a short read, one graded exercise, and a quiz.

  1. 1The Scanner — Breaking Source into TokensRead · exercise · quiz
  2. 2Token Types — Keywords, Literals & OperatorsRead · exercise · quiz
  3. 3String & Number LiteralsRead · exercise · quiz
Loading reference solution…
Going further

When the tests are green, keep going.

Read

  • Crafting Interpreters (Robert Nystrom) — the canonical text for this material. Free online at craftinginterpreters.com. Build the bytecode VM next; it's roughly 20× faster than the tree-walker.
  • Structure and Interpretation of Computer Programs (Abelson + Sussman) — for a more theoretical foundation, especially around closures and continuations.
  • The Garbage Collection Handbook (Jones + Hosking + Moss) — when you add heap-allocated values, you'll need GC. This is the book.

Build next

  • Functions + closures: extend the AST with Function and Call nodes. Capture the enclosing environment when a function value is created.
  • Bytecode compiler + stack VM: translate the AST into a flat instruction stream (PUSH, ADD, JUMP, CALL, RETURN). Run on a stack-machine. ~10× speedup over walking the tree.
  • Garbage collector: mark-and-sweep first, then incremental, then generational.
  • Type system: Hindley-Milner inference is the rabbit hole.

Continue with our courses

  • Build a C Compiler — same pipeline, but lowering all the way to x86-64 assembly. Real compilation, not interpretation.
  • Build a Lisp Interpreter — a smaller, more uniform language to test the same ideas.
Build a Programming Language from Scratch