This is a bytecode runtime for Lox made in Zig. This mostly conforms to the language outlined in the book. The implementation passes all the tests provided in the repository for the original book. This implementation also has some additional features:
- const variables, prefixed with
const
, - bytecode support for 2^24 locals per scope (compilation disabled to make tests pass),
- switch statement (identical to C, but doesn't require
break
keyword), - supports
continue
statement, - native functions have arity checking,
- native functions can return a runtime error,
in
keyword to check if a property exists on an instance"property" in instance
,setField
native function to set an arbitrary string field on an instance,getField
native function to get an arbitrary string field on an instance,deleteField
native function to delete a field from an instance,gc
native function to force a garbage collection cycle to run.
This implementation doesn't include the NaN boxing optimization, as I didn't reuse the macro approach outlined in the book. In order to implement NaN boxing, I'm required to refactor some value-handling code, which could take some time. I already got all I wanted from the book, so I decided not to invest any more time into this.
The runtime could be installed and used in the following way:
- install zig,
- clone the repository:
git@github.com:InfiniteRain/zig-lox.git
, - cd into the cloned repository:
cd zig-lox
, - use the runtime in one of the following ways:
- via REPL:
zig build run
, - via running a file:
zig build run -- my_lox_program.lox
.
- via REPL:
Zig build accepts the following flags:
-Dprint-code
- prints the generated VM instructions,-Dtrace-execution
- prints the contents of the stack between each instruction,-Dstress-gc
- forces GC to run before every new allocation,-Dlog-gc
- enables GC logging.
MIT