A high-performance bytecode virtual machine for the Lox programming language, implemented in C. This project follows the implementation described in the second half of Crafting Interpreters by Robert Nystrom.
- Bytecode VM: Stack-based execution with a custom instruction set.
- Pratt Parser: A single-pass compiler that translates source code directly into bytecode.
- Garbage Collection: An incremental mark-and-sweep collector.
- Dynamic Typing: Support for numbers, Booleans, strings, and
nil. - First-Class Functions: Support for closures and upvalues.
- Object-Oriented Programming: Classes, instances, dynamic fields, methods, and inheritance.
- Optimized Method Calls: Direct method invocation and
thisbinding.
src/: C source code and headers.test/: Lox test scripts split by functional area.obj/: Compiled object files (generated during build).
The project includes a Makefile for easy compilation. Ensure you have gcc (or another C compiler) installed.
makeThis will produce the clox executable in the root directory.
To execute a Lox source file:
./clox path/to/script.loxTo start the interactive Read-Eval-Print Loop:
./cloxYou can run all tests using the Makefile:
make testOr run individual tests manually:
./clox test/arithmetic.lox
./clox test/logic.lox
# ... etcmain.c: Entry point and REPL/file loading logic.vm.c / vm.h: Core virtual machine execution loop and call stack management.compiler.c / compiler.h: Pratt parser and bytecode generation.scanner.c / scanner.h: Tokenization logic.object.c / object.h: Heap-allocated object representations (strings, functions, classes).table.c / table.h: Hash table implementation for globals, fields, and string interning.memory.c / memory.h: Garbage collector and memory management.chunk.c / chunk.h: Bytecode array management.value.c / value.h: Lox value representation and tagged unions.debug.c / debug.h: Disassembler for debugging bytecode.