This is (an attempt at) a faithful, idiomatic re-implementation of Lox (which was built in Java) in Rust. More than anything, it's an exercise.
- Clone and build
git clone https://github.com/cachebag/rlox.git
cd rlox
cargo build --release
- Run REPL (currently stateless)
cargo run
> print "Hello, world!";
Hello, World!
> var msg = 10 != 100 ? "true" : "false"; print msg;
true
- Run a
lox
file
// examples/recursive_fib.lox
fn fib(n) {
return n <= 1 ? n : fib(n - 2) + fib(n - 1);
}
for (var i = 0; i < 20; i = i + 1) {
print fib(i);
}
cargo run examples/recursive_fib.lox
0
1
1
2
3
5
8
13
21
...
You can print the tokens of a given file or stdin
input.
cargo run show-tokens <file> # Scans the given file and prints its tokens.
cargo run show-tokens - # Reads from stdin and prints tokens.
You can also print the parsed AST for a given file or input.
cargo run show-ast <file> [output.txt] # Prints the parsed AST for the given file. (defaults to ast_output.txt if no name is given
cargo run show-ast - [output.txt] - # Stdin