Crafting Interpreters · Week 21

Ch 23: Jumping Back and Forth

Updated

About 5 hoursVerified by tests

Summary

Crafting Interpreters chapter 23, Jumping Back and Forth: Control flow in bytecode: jump instructions with backpatching for if/else, logical operators, while loops with backward jumps, and for loops. Budget about 5 hours; the book's chap23_jumping 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

    Disassemble each control structure and draw the jump arrows.

  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 (a switch statement) and challenge 2 (continue) are both excellent.

  4. Push and let the tests verify it

    Your repo runs the book's chap23_jumping 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

Backpatching is how every assembler and compiler resolves forward jumps. You will see it again in real assemblers and in Knuth's MIX.

How to approach it

Disassemble each control structure and draw the jump arrows. Off-by-one jump offsets are the usual bug; the disassembler shows them immediately.

Challenges

3 challenges at the end of the chapter.

Challenge 1 (a switch statement) and challenge 2 (continue) are both excellent. Challenge 3 is an open design question. 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.

Common traps
  • Jump offsets off by the size of the operand bytes.
  • Not popping the condition value on both branches.

Spot a mistake? Report it

Back to week 21