Glossary

What is clox?

clox is the second Lox implementation in Crafting Interpreters: a single-pass compiler and a stack-based bytecode virtual machine written in C across chapters 14 to 30. It is many times faster than jlox because it builds everything itself.

Taught in Crafting Interpreters 14: Chunks of Bytecode, Crafting Interpreters 17: Compiling Expressions, Crafting Interpreters 26: Garbage Collection

What you build

A dynamic array of bytecode with a constant pool (14), the VM loop (15), an on-demand scanner (16), a Pratt parser that emits bytecode with no syntax tree (17), value types and strings (18, 19), a hash table with string interning (20), globals and locals (21, 22), jumps (23), calls (24), closures with upvalues (25), a mark-sweep garbage collector (26), classes and methods (27 to 29), and optimizations including NaN boxing (30).

Why it is harder than jlox

C gives you nothing for free. You manage every allocation, and a bug in the garbage collector shows up far from its cause. Compile with -Wall -Wextra -fsanitize=address,undefined from the first C chapter; it turns mysterious crashes into a line number. On the cracked.guide path clox is weeks 12 to 28.

Questions

Do I need to finish jlox before clox?

The book assumes it, because clox explains its design by contrast with jlox. You could start at chapter 14, but you would miss the reasons.

How fast is clox compared with jlox?

Much faster on the book's benchmarks, because it runs compact bytecode in a tight loop instead of walking Java objects. The exact factor depends on the program.