See it

Visual explainers

The path's core ideas as small pictures you can step through, one sentence per step. Each sits on the reading guides it belongs to; here they are all together.

Crafting Interpreters · Ch 4 Scanning: characters into tokens Watch start and current walk the source, the lexeme grow, and each token appear the moment its lexeme ends.

start 6current 8lexeme >=

Tokens

  1. IFif
  2. LEFT_PAREN(
  3. IDENTIFIERn
  4. GREATER_EQUAL>=

The next character is '=', so match() consumes it: one GREATER_EQUAL token, not two.

In the guides: Crafting Interpreters 4: ScanningCrafting Interpreters 16: Scanning on Demand

Crafting Interpreters · Ch 6 Parsing: precedence becomes depth Recursive descent turns 1 + 2 * 3 - 4 into a tree; the tighter an operator binds, the deeper its node sits.

Grammar, lowest precedence first

  1. expression → term
  2. term → factor (( "+" | "-" ) factor)*
  3. factor → primary (( "*" | "/" ) primary)*
  4. primary → NUMBER

Call stack

  1. expression()

Built so far

(- (+ 1 (* 2 3)) 4)

No tokens left: expression returns the whole tree, (- (+ 1 (* 2 3)) 4).

In the guides: Crafting Interpreters 5: Representing CodeCrafting Interpreters 6: Parsing ExpressionsCrafting Interpreters 17: Compiling Expressions

Crafting Interpreters · Ch 8 to 11 Environments and a closure Each call gets an environment that points to its enclosing one, and a closure keeps the environment it was born in alive.
  1. fun makeCounter() {
  2. var i = 0;
  3. fun count() {
  4. i = i + 1;
  5. print i;
  6. }
  7. return count;
  8. }
  9. var c = makeCounter();
  10. c();
  11. c();

Output nothing yet

Looking up i: it is not in the call's environment, so follow the enclosing pointer and find i = 0 one level up.

In the guides: Crafting Interpreters 8: Statements and StateCrafting Interpreters 10: FunctionsCrafting Interpreters 11: Resolving and BindingCrafting Interpreters 25: Closures

Crafting Interpreters · Ch 14 and 15 A bytecode VM, one instruction at a time The instruction pointer walks the chunk while values are pushed and popped on the stack.

Bytes

00000001010500020306

Disassembly

0000OP_CONSTANT0 '1'
0002OP_CONSTANT1 '2'
0004OP_ADD
ip0005OP_NEGATE
0006OP_CONSTANT2 '3'
0008OP_MULTIPLY
0009OP_RETURN

Constants

  1. 1
  2. 2
  3. 3

Stack

  1. 3

stackTop

Printed

nothing yet

OP_ADD pops 2 and then 1, adds them and pushes 3.

In the guides: Crafting Interpreters 14: Chunks of BytecodeCrafting Interpreters 15: A Virtual Machine

Crafting Interpreters · Ch 20 · TAOCP 6.4 A hash table with linear probing Collisions probe to the next slot, deletions leave tombstones, and a full enough table doubles. Type your own keys.

find figcount 4live 3capacity 8load 0.50

  1. 0empty
  2. 1kiwihome
  3. 2empty
  4. 3empty
  5. 4empty
  6. 5tombstone
  7. 6fighome 5
  8. 7applehome

Slot 6 holds "fig": found, because probing went on past the tombstone at slot 5.

In the guides: Crafting Interpreters 20: Hash TablesTAOCP 6.4 Hashing (part 1)TAOCP 6.4 Hashing (part 2)

Crafting Interpreters · Ch 26 · TAOCP 2.3.5 Mark and sweep Gray objects wait on a stack to be traced; whatever is still white when marking ends gets swept away, cycles included.

Gray stack

  1. empty
  • white: not reached yet
  • gray: reached, references not traced
  • black: done

Pop C and blacken it: it references nothing new; the gray stack is empty, so marking is done.

In the guides: Crafting Interpreters 26: Garbage CollectionTAOCP 2.3.5 Lists and Garbage Collection

TAOCP 2.2.3 Linked stacks and queues Knuth's pointer assignments one at a time: push and pop on a stack, insert and delete on a queue.
  1. P ⇐ AVAIL
  2. INFO(P) ← Y
  3. LINK(P) ← T
  4. T ← P
  5. (pop)
  6. P ← T
  7. T ← LINK(P)
  8. Y ← INFO(P)
  9. AVAIL ⇐ P

Y none

LINK(P) ← T: the new node points to the old top, B.

In the guides: TAOCP 2.2.1 Stacks, Queues, and DequesTAOCP 2.2.3 Linked Allocation

TAOCP 2.3.1 Traversing a binary tree One walk touches each node three times; preorder, inorder and postorder differ only in which touch visits.

Inorder so far

DBAEG

Stack (the path from the root)

ACEG

Back at G after its left subtree (empty): inorder visits G now, between its two subtrees.

In the guides: TAOCP 2.3.1 Traversing Binary Trees

TAOCP 5.2 Three sorts, one array Straight insertion, heapsort and two-way merging side by side, one comparison per step, so you can see who finishes first.

Straight insertion 5.2.118 comparisons

  1. 1
  2. 2
  3. 4
  4. 3
  5. 6
  6. 7
  7. 9
  8. 10
  9. 8
  10. 5

Heapsort 5.2.318 comparisons

  1. 9
  2. 8
  3. 7
  4. 6
  5. 5
  6. 1
  7. 2
  8. 3
  9. 4
  10. 10

Two-way merge 5.2.418 comparisons

  1. 1
  2. 2
  3. 3
  4. 4
  5. 6
  6. 7
  7. 9
  8. 10
  9. 5
  10. 8

Straight insertion: 6 > 3, so it moves right; heapsort: 7 rises above 2; two-way merge: 9 ≤ 10, so 9 goes next.

In the guides: TAOCP 5.2.1 Sorting by InsertionTAOCP 5.2.3 Sorting by SelectionTAOCP 5.2.4 Sorting by Merging

TAOCP 3.2.1 and 3.3.4 A linear congruential generator Change a, c and m, watch the period, and see the lattice the spectral test measures in the plot of each pair.

Each dot is a pair (Xn, Xn+1)

Xn+1 = (a·Xn + c) mod m

(137·221 + 187) mod 256 = 0

Sequence

1361312146513295146221

Period 256 of a possible 256

Theorem A: a full period needs all three

  • c and m share no factor (gcd 1): yes
  • a − 1 is divisible by every prime of m (2): yes
  • a − 1 is a multiple of 4, since 4 divides m: yes

X256 = 0 was seen before, so the period is 256, the full modulus, and every pair lies on a few parallel lines whose spacing the spectral test measures.

In the guides: TAOCP 3.2.1 The Linear Congruential MethodTAOCP 3.3.4 The Spectral Test

TAOCP 7.2.2.1 Exact cover by backtracking Algorithm X on a tiny example: choose the item with the fewest options, cover, recurse, and undo on a dead end.
Optionabcdefg
c e
a d g
b c f
a d f
b g
d e g

Chosen, one per level

  1. {a d f}
  2. {b g}
  3. {c e}

Items still to cover

none: solved

Every item is covered: {a d f}, {b g}, {c e} is an exact cover.

In the guides: TAOCP 7.2.2.1 Dancing Links (part 1)TAOCP 7.2.2.1 Dancing Links (part 2)TAOCP 7.2.2.1 Dancing Links (part 3)TAOCP 7.2.2.1 Dancing Links (part 4)

Watch along

Free talks and lectures for each phase, on the speakers' and universities' own channels. Each guide shows the ones for its week.

Crafting Interpreters · weeks 1 to 28

TAOCP Vol 1: Fundamental Algorithms · weeks 29 to 63

TAOCP Vol 2: Seminumerical Algorithms · weeks 64 to 89

TAOCP Vol 3: Sorting and Searching · weeks 90 to 117

TAOCP Vol 4A: Combinatorial Algorithms, Part 1 · weeks 118 to 133

TAOCP Vol 4B: Combinatorial Algorithms, Part 2 · weeks 134 to 145