Crafting Interpreters · Week 5
Crafting Interpreters 9: Control Flow: reading guide
Read the chapterVerified by tests
Control flow: if/else, logical operators with short-circuiting, while loops, and for loops implemented by desugaring into while. Lox becomes Turing-complete.
Time About 4 hours this week
Why it matters
Desugaring (implementing a feature by rewriting it into simpler features) is a key compiler technique. Short-circuit evaluation shows up in every language.
How to approach it
After implementing for-loop desugaring, print the AST of a for loop to see the rewritten while loop.
Challenges
3 challenges at the end of the chapter.
Challenge 3 (add break) is the one to implement; it is a good exercise in parser state and in using an exception or flag to unwind. Challenges 1 and 2 are thought exercises. 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
- The dangling else: make sure else binds to the nearest if.
- Logical operators must return the operand value, not a converted boolean.
Verified by the book's chap09_control tests when you push your repo.