Lox is a dynamically typed, interpreted script language, which is designed by Robert Nystrom for his book Crafting interpreters.
A virtual machine(VM) for the Lox programming language implemented in Rust. The original book utilizes C to build the VM, referred to as clox
. To maintain consistency, I chose the name rustlox
for the Rust implementation.
# REPL
# debug build with debug info
$ cargo run
# execute a lox file
$ cargo run -- <file>
A naive benchmark in my MBP Intel i5-8257U @1.40GHz:
operations | Rust(-O ) |
Python 3.10.9 | rustlox | PyLox |
---|---|---|---|---|
fib(35) |
~ 0.03s | ~ 3s | ~ 8s | ~ 600s |
- 17.2 Parsing Tokens - Use
std::mem::take
to handleself.parser.previous = self.parser.current;
and deriveDefault
for theToken
type. - 17.6 A Pratt Parser - Impl a
next
associated function for thePrecedence
struct to get the next enum item. - 18.1 Tagged Unions - Use
enum
instead ofunion
, as theenum
type is quite powerful in Rust. - 24.4 Function declaration
- Set a
CompilerState
field for theCompiler
struct, which contains local variables, scope depth, function, enclosing, and function_type - Before compiling the function declaration, use
std::mem::take
to remember the old state and store it in theenclosing
field
- Set a