Crafting Interpreters · Week 19

Ch 21: Global Variables

Updated

About 5 hoursVerified by tests

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.

Flashcards 6 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

4 steps
  1. Read the chapter

    Pay attention to how the canAssign flag prevents invalid assignment targets.

  2. Write the code in your lox repo

    Type it into clox/ (C) yourself as you read, and get it running.

  3. Do the challenges (1–3)

    Challenge 1 (stop adding duplicate name constants) and challenge 2 (faster global lookup) are both good engineering exercises.

  4. 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 you

Sign 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 posts

Questions 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.

Spot a mistake? Report it

Back to week 19