Crafting Interpreters · Week 13
Crafting Interpreters 24: Calls and Functions: reading guide
Read the chapterVerified by tests
Function objects, call frames, a compiler per function, the call instruction, return, stack traces for errors, and native functions.
Time About 8 hours this week
Why it matters
Call frames are exactly how your CPU and every runtime manage function calls. After this chapter, stack traces make complete sense.
How to approach it
One of the biggest chapters; plan two sessions. Draw the value stack and frames during a recursive call. Compare your Fibonacci benchmark against jlox from chapter 10.
Challenges
4 challenges at the end of the chapter.
Challenge 1 (cache ip in a local or register) is a real speedup worth measuring. Challenges 2 and 3 (arity and error handling for natives) make the VM safer. Challenge 4 (add your own natives) is fun.
Traps
- Wrong slot offset for the callee and arguments in a frame.
- Not checking frame count for stack overflow on deep recursion.
- Forgetting to update the cached frame pointer after a call or return.
Verified by the book's chap24_calls tests when you push your repo.