Week 4 of 125

Crafting Interpreters, chapters 7 and 8

Sign up or sign in to tick these off and give this week a due date.

  • Build Ch 7: Evaluating Expressions Verified 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.

    Reading guide

  • Build Ch 8: Statements and State Verified by tests

    Statements and state: expression and print statements, global variable declarations, the environment that maps names to values, assignment, and block scope with nested environments.

    Time About 6 hours this week

    Why it matters

    Scopes and environments are where interpreters get interesting, and every closure bug later traces back to understanding this chapter.

    How to approach it

    Draw the environment chain for a few nested blocks. Run the shadowing examples in the chapter and predict the output before running.

    Challenges

    3 challenges at the end of the chapter.

    Challenge 1 (make the REPL print bare expressions) is a nice usability win. Challenge 2 (runtime error for uninitialized variables) changes semantics, so keep it on a branch. Challenge 3 is a scoping puzzle; answer it before running.

    Traps

    • Not restoring the previous environment when a block throws. Use try/finally as the book does.
    • Treating assignment to an undefined variable as an implicit declaration.

    Reading guide

  • Two problems from the NeetCode 150, in order. One box for the pair.