Home

Awesome

Loxido

A Rust implementation of the Lox programming language from the amazing book Crafting Interpreters by Bob Nystrom.

This is based on the clox reference implementation in C. It implements a byte-code compiler and VM.

You can read a detailed blog post about this here:

https://ceronman.com/2021/07/22/my-experience-crafting-an-interpreter-with-rust/

This implementation passes the 243 integration tests from clox and jlox.

Two branches

This repository contains two main branches:

Differences between loxido and clox

The safe implementation follows the design of clox, but trying to be Rust idiomatic. Some of the differences are:

Changes in the unsafe branch

The unsafe branch introduces some unsafe code to improve performance. The changes are:

How to run

To compile and run this project you need Cargo.

To run the REPL, just go for:

cargo run

To run a specific file:

cargo run program.lox

Running the tests

To run the tests just run:

cargo test

Performance

I measured the running times of the different benchmarks available in the lox repository. I also ported the benchmarks to Python and Perl. Here are the compared running times of different implementations of Lox, including mind.

These were run on my Thinkpad X1 laptop with Intel Core i7-8565U CPU

note: The horizontal axis is capped at 15 seconds to improve visibility. Performance of Lox implementation

cloxjloxloxidoloxido unsafePython 3.9.2Perl 5.32.1
arithmetic0.6304s3.3200s5.4746s0.9706s1.6815s1.0798s
binary_tree2.6283s7.0670s8.4780s3.4534s4.1730s6.3510s
equality1.3740s3.6209s11.337s1.9433s3.4657s2.2632s
fib0.9067s4.2789s5.0318s1.2042s2.1110s4.2134s
instantiation0.5264s1.5280s7.2826s0.9068s1.4895s3.8319s
invocation0.2916s1.3600s1.6865s0.3566s0.7789s0.9535s
method_call0.1574s1.9440s1.1231s0.2255s0.5696s0.9560s
properties0.3716s3.9539s2.2317s0.5132s1.4074s1.4339s
trees1.9714s21.677s14.478s2.9840s6.4791s13.019s
zoo0.3132s2.8380s1.6914s0.3836s0.9739s1.1222s

Tree walker implementation

I also wrote another implementation of the tree walk interpreter in Clojure called cloxure.

Copyright note

Most of the test and benchmark files are copied from Crafting Interpreters repository. The copyright for these belongs to Bob Nystrom and are copied here because their license allows it (MIT).

The source code for this implementation is copyright of Manuel Cerón. And it's also licensed as MIT (see LICENCE for details.)