Week 10 of 125
Crafting Interpreters, chapters 18 and 19
Sign up or sign in to tick these off and give this week a due date.
-
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.
-
Heap-allocated objects in clox, starting with strings: an Obj header, string objects, concatenation, and a linked list of all objects so they can be freed at exit.
Time About 5 hours this week
Why it matters
This is where manual memory management starts in earnest. The object header plus linked list is the foundation for the garbage collector later.
How to approach it
Run every test under AddressSanitizer and LeakSanitizer. Understand the struct inheritance trick (an Obj as the first field) before moving on.
Challenges
3 challenges at the end of the chapter.
Challenge 1 (flexible array members to store characters inline) is a classic C optimization worth doing. Challenge 2 (strings that do not own their characters) is a good follow-up.
Traps
- Forgetting the null terminator or the +1 in allocations.
- Leaking the intermediate string in concatenation.
- Casting between Obj* and ObjString* incorrectly.
-
NeetCode LeetCode: 2 NeetCode 150 problems
Two problems from the NeetCode 150, in order. One box for the pair.