Week 2 of 125
Crafting Interpreters, chapter 4
Sign up or sign in to tick these off and give this week a due date.
-
You build the scanner for jlox: turning source text into a list of tokens, handling lexemes, literals, keywords and identifiers, line numbers for errors, and the first bits of error reporting.
Time About 6 hours this week
Why it matters
Every tool that reads text (config parsers, linters, syntax highlighters) starts with a scanner. It is the cleanest first step into the codebase.
How to approach it
Type the code yourself instead of copying; that is the whole point of the book. Run it on small inputs after each step. Set up the official test runner this week so every later chapter is checked automatically.
Challenges
4 challenges at the end of the chapter.
Challenge 4 (C-style block comments) is the one to actually implement; consider supporting nesting as the challenge suggests. Challenges 1 to 3 are discussion questions; think about them briefly. 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.
Traps
- Off-by-one errors in current/start indices when extracting lexemes.
- Forgetting to increment the line counter inside multi-line strings.
- Writing jlox in a language other than Java: totally fine, but you translate every snippet yourself and the Visitor-heavy code in later chapters is more work in some languages. Check the wiki of other implementations for your language.
- Using a Java version too old for the code. Any modern JDK (17 or 21) works.
-
NeetCode LeetCode: 2 NeetCode 150 problems
Two problems from the NeetCode 150, in order. One box for the pair.