freeprogrammingbooks.com

Crafting Interpreters

By Robert Nystrom (2021)

Crafting Interpreters book cover

Every programming language you use runs on top of machinery that transforms human-readable code into executable instructions. Compilers and interpreters are the invisible infrastructure of modern software, and understanding how they work changes the way you think about code, performance, and debugging.

Languages continue to multiply — Python for AI pipelines, Rust for systems programming, TypeScript for large-scale frontends — the principles of language implementation remain largely the same. Crafting Interpreters is the definitive hands-on guide to building that machinery yourself, from the first token to a full garbage collector.

About the book

This book teaches you how to design and implement a complete programming language by building one from scratch. Robert Nystrom, an engineer on the Dart team at Google, walks you through two complete implementations of the same language.

The first is a simple tree-walk interpreter written in Java, perfect for understanding core concepts. The second is a high-performance bytecode virtual machine in C, closer to what real production language runtimes look like.

You start with nothing and end with a working language called Lox that supports dynamic typing, lexical scope, closures, classes, and inheritance. The book needs no prior compiler or language implementation experience — just the ability to read code and a willingness to write a lot of it.

What you will learn

  • How to write a lexical scanner that breaks source code into tokens
  • How to build a recursive descent parser and handle grammar rules
  • How to represent and evaluate abstract syntax trees
  • How a stack-based bytecode virtual machine works
  • How to implement variables, control flow, functions, closures, and classes in both an interpreter and a compiler
  • How to design a garbage collector, from simple mark-sweep to generational collection
  • How to optimize a bytecode compiler for real-world performance
  • How closures, first-class functions, and class inheritance work at the implementation level

Table of contents

Part I: Welcome

  • Introduction
  • A Map of the Territory
  • The Lox Language

Part II: A Tree-Walk Interpreter

  • Scanning
  • Representing Code
  • Parsing Expressions
  • Evaluating Expressions
  • Statements and State
  • Control Flow
  • Functions
  • Resolving and Binding
  • Classes
  • Inheritance

Part III: A Bytecode Virtual Machine

  • Chunks of Bytecode
  • A Virtual Machine
  • Scanning on Demand
  • Compiling Expressions
  • Types of Values
  • Strings
  • Hash Tables
  • Global Variables
  • Local Variables
  • Jumping Back and Forth
  • Calls and Functions
  • Closures
  • Garbage Collection
  • Classes and Instances
  • Methods and Initializers
  • Superclasses
  • Optimization

Appendices

  • Lox Grammar
  • Generated Syntax Tree Classes

Book details

  • Title: Crafting Interpreters
  • Author(s): Robert Nystrom
  • Publication year: 2021
  • Publisher: Genever Benning
  • Pages: 640
  • PDF size: N/A (free online edition available)
  • Estimated reading time: ~16 h
  • Level: Intermediate
  • Main category: Programming
  • Subcategory: (none)
  • Language: English
  • License: CC BY-NC-ND 4.0 (text and illustrations), MIT (source code)

More books in: Programming


Legal notice: This book is shared for educational purposes only. The content is distributed under Creative Commons licenses or with explicit permission from the author. FreeProgrammingBooks may host files that comply with their respective licenses.

Want more free books?

Join our channels and receive free books, courses and tech news.