Lox interpreter written in Rust (Lox is a scripting language created by Robert Nystrom for this book)
This repository contains two packages that corresponds to the following in the book
loxi
→jlox
: a tree-walk interpreter (implementation complete)loxii
→clox
: a bytecode interpreter (not implemented yet)
cargo build --bin loxi # or loxii
cargo run --bin loxi -- --help # opens help
The tests are performed using a python script
./test.py <interpreter> -c <chapter>
Use the -h
flag to see how to use it.
You can run the benchmarks yourself using the python script
./benchmark.py <interpreter> <repeat>
Use the -h
flag to see how to use it.
You can add other interpreters to the script by adding more enumeration to
Interpreter
class yourself. That is what I do to add and benchmarkjlox
below.
The benchmarks are performed on an Intel Core i5-10500H machine with the frequency locked at 2.5GHz. Each benchmark are repeated 5 times and then averaged to get better result.
loxi | jlox | diff | |
---|---|---|---|
binary_trees.lox |
14.443941 s | 9.880800 s | +46.18 % |
equality.lox |
9.941284 s | 5.494600 s | +80.93 % |
fib.lox |
12.876985 s | 6.060200 s | +112.48 % |
instantiation.lox |
3.923048 s | 2.008800 s | +95.29 % |
invocation.lox |
4.071392 s | 1.759000 s | +131.46 % |
method_call.lox |
2.020660 s | 2.427200 s | -16.75 % |
properties.lox |
5.592753 s | 6.464400 s | -13.48 % |
string_equality.lox |
3.659577 s | 6.170000 s | -40.69 % |
trees.lox |
27.242201 s | 32.009400 s | -14.89 % |
zoo.lox |
3.839772 s | 6.017600 s | -36.20 % |
zoo_batch.lox |
10.007977 s | 10.014200 s | -0.06 % |
Not implemented yet
enable using cargo
--features
flag
unicode
: allows non-whitespace unicode as identifier.debug
: adds a new statement, debug statement, that is similar to print but instead of printing tostdout
, it prints tostderr
.loxlox
: provides the following functions that facilitate LoxLox.getc()
: gets char fromstdin
as number (currently not handlingutf8
properly).chr(ch)
: turns char code number intostring
(currently not handlingutf8
properly, if they are encounteredchr
will returnnil
instead).exit(status)
: exits with given status code.print_error(msg)
: prints message tostderr
(I modify it so it can print any kind of LoxValue
).
These are from the challenges, I guess I'll add it as extension for the language and using cargo feature flag to enable it
- Add support for C-style block comments (
/* ... */
) - Implement bitwise, shift, modulo, and conditional operators
- Add support for comma expressions (like in C)
- Add support for C-style conditional or "ternary" operator
?:
- Add option to raise a runtime error on division by zero
- Make it a runtime error to access a variable that has not been initialized or been assigned
- Add support for
break
andcontinue
statements in a loop - Add anonymous function support
- Extend the resolver to raise an error if a local variable is never used
- Implement static methods (use
class
to mark a method as static) - Support "getter" and "setter" for class members (see the book on challenges section on classes chapter for the syntax)
- Add support for string interpolation
Other challenges but lower priority
Reasons for lower priority including but not limited to
- Implementing these as features will make programming more error-prone
- Allow ordering with different type (e.g.
3 < "pancake"
) - For
+
operator, if either of the operand is a string, convert the other operand to string.