Crafting Interpreters · Week 10
Crafting Interpreters 18: Types of Values: reading guide
Read the chapterVerified by tests
Tagged unions for Lox values in C (numbers, booleans, nil), type checks in the VM, runtime errors, and new instructions for comparison and logical not.
Time About 4 hours this week
Why it matters
Tagged unions are how dynamic languages represent values in a statically typed host. This design shows up in Lua, JavaScript engines and Ruby.
How to approach it
Draw the memory layout of the Value struct. Check sizeof(Value) on your machine.
Challenges
2 challenges at the end of the chapter.
Both challenges are design discussions about instruction set size versus speed; think about them and maybe benchmark one idea.
Traps
- Accessing the wrong union member without checking the tag first.
- Forgetting to reset the stack after a runtime error.
Verified by the book's chap18_types tests when you push your repo.