Exercise 2: Reading One Global 300 Times, Without Deduplication — Possible Solution ==================================================================== THE SETUP ------------------------------ class ChunkNoDedup: def add_constant(self, value): self.constants.append(value) # no "if value in self.constants" check return len(self.constants) - 1 # var g = 1; then g; three hundred times program = [VarStmt('g', Literal(1.0), line=1)] for _ in range(300): program.append(ExpressionStmt(Variable('g'), line=1)) RESULT ------------------------------ ValueError: byte must be in range(0, 256) WHY THIS WORKS AS AN ANSWER ------------------------------ The program only ever refers to ONE variable name, 'g' -- but every single one of the 300 reads calls visit_variable, which calls self.chunk.add_constant(node.name) independently, with no memory of whether that exact string was already added a moment ago. Without the dedup check, add_constant() blindly appends 'g' as a brand new constant-pool entry every time it's called, so after 256 reads the constant pool already holds 256 separate (but identical-valued) entries, and the 257th read's own OP_GET_GLOBAL operand byte can no longer be represented in a single byte. This is exactly the ceiling Chapter 2 documented for numeric literals, showing up again for exactly the same structural reason, but triggered by something that looks completely harmless at the Wisp source level -- reading the same variable repeatedly is about as ordinary as Wisp code gets. This is precisely why this chapter's own Compiler reuses Chapter 2 Exercise 2's dedup fix for add_constant() rather than treating it as optional polish: without it, ANY moderately long loop that reads a global more than 256 times would fail to compile at all, which would make this VM unusable for almost any real program with a loop in it.