Crafting Interpreters · Week 11

Crafting Interpreters 21: Global Variables: reading guide

Read the chapterVerified by tests

Statements and global variables in clox: print and expression statements, declarations, global variable get and set via the hash table, and assignment with correct precedence.

Time About 5 hours this week

Why it matters

Shows how a single-pass compiler handles declarations and l-values, a trickier problem than in jlox.

How to approach it

Pay attention to how the canAssign flag prevents invalid assignment targets. Test with expressions like a * b = c.

Challenges

3 challenges at the end of the chapter.

Challenge 1 (stop adding duplicate name constants) and challenge 2 (faster global lookup) are both good engineering exercises. Challenge 3 (REPL-friendly forward references) is a design puzzle.

Traps

  • Invalid assignment targets silently accepted when canAssign is not threaded through correctly.

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

Links

Back to week 11