Crafting Interpreters · Week 4

Crafting Interpreters 7: Evaluating Expressions: reading guide

Read the chapterVerified by tests

You make expressions run: a tree-walking evaluator over the AST, mapping Lox values to Java objects, truthiness, equality, runtime type checks and runtime error reporting.

Time About 5 hours this week

Why it matters

This is the moment your interpreter actually computes something. Mapping a dynamic language's values onto a host language is the core design decision of any interpreter.

How to approach it

Write the evaluator, then try every operator with wrong types to see your runtime errors. Compare how Lox equality and truthiness work with your favorite language.

Challenges

3 challenges at the end of the chapter.

Challenge 2 (string concatenation with a non-string operand) and challenge 3 (division by zero) make you decide semantics and implement them. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.

Traps

  • Comparing Java Doubles with == instead of equals, or mishandling nil equality.
  • Printing doubles: whole numbers should print without a trailing .0, as the book shows. The test suite checks exact output.

Verified by the book's chap07_evaluating tests when you push your repo.

Links

Back to week 4