Crafting Interpreters · Week 19
Ch 21: Global Variables
Updated
Summary
Crafting Interpreters chapter 21, Global Variables: 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. Budget about 5 hours; the book's chap21_global tests check your code.
This week's steps
4 stepsPay attention to how the canAssign flag prevents invalid assignment targets.
Write the code in your lox repo
Type it into clox/ (C) yourself as you read, and get it running.
Challenge 1 (stop adding duplicate name constants) and challenge 2 (faster global lookup) are both good engineering exercises.
Push and let the tests verify it
Your repo runs the book's chap21_global tests on every push; passing marks this done.
Sign in to track this: tick each step off and it counts toward your week.
Your notes
Only youSign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.
Discussion
0 postsQuestions about this chapter, tips, and what tripped you up. Hints over answers: full solutions go in a spoiler.
No posts yet. Ask a question or share a tip.
Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Reference
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.
Common traps
- Invalid assignment targets silently accepted when canAssign is not threaded through correctly.