Crafting Interpreters · Week 4
Crafting Interpreters 8: Statements and State: reading guide
Read the chapterVerified 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.
Verified by the book's chap08_statements tests when you push your repo.