Crafting Interpreters · Week 6
Crafting Interpreters 11: Resolving and Binding: reading guide
Read the chapterVerified by tests
A static resolver pass that walks the AST before execution and records, for each variable use, how many scopes up its definition is. This fixes a closure bug caused by mutable environments and adds compile-time errors like reading a local in its own initializer.
Time About 7 hours this week
Why it matters
This is your first semantic analysis pass. Linters, type checkers and compilers all work this way: a separate walk over the tree that computes facts about it.
How to approach it
Before reading the fix, run the example that shows the bug and make sure you understand why it misbehaves. Then implement the resolver. This chapter is subtle; plan for extra time.
Challenges
4 challenges at the end of the chapter.
Challenge 3 (error on unused locals) is a nice small analysis to add. Challenge 4 (store locals in arrays indexed by slot instead of maps) is a real performance improvement and previews clox. 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
- Forgetting to resolve some node type, which leaves variables silently treated as globals.
- Mismatched beginScope/endScope calls.
- Resolving a function body in the wrong function type context, which breaks later return checks.
Verified by the book's chap11_resolving tests when you push your repo.