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::taketo handleself.parser.previous = self.parser.current;and deriveDefaultfor theTokentype. - 17.6 A Pratt Parser - Impl a
nextassociated function for thePrecedencestruct to get the next enum item. - 18.1 Tagged Unions - Use
enuminstead ofunion, as theenumtype is quite powerful in Rust. - 24.4 Function declaration
- Set a
CompilerStatefield for theCompilerstruct, which contains local variables, scope depth, function, enclosing, and function_type - Before compiling the function declaration, use
std::mem::taketo remember the old state and store it in theenclosingfield
- Set a