Skip to content

mrizaln/loxx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loxx

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)
  • loxiiclox: a bytecode interpreter (not implemented yet)

Building

cargo build --bin loxi              # or loxii
cargo run --bin loxi -- --help      # opens help

Testing

The tests are performed using a python script

./test.py <interpreter> -c <chapter>

Use the -h flag to see how to use it.

Benchmarks

Running

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 benchmark jlox below.

Result

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.

Tree-walk interpreter

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 %

Bytecode interpreter

Not implemented yet

Extension

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 to stdout, it prints to stderr.
  • loxlox: provides the following functions that facilitate LoxLox.
    • getc(): gets char from stdin as number (currently not handling utf8 properly).
    • chr(ch): turns char code number into string (currently not handling utf8 properly, if they are encountered chr will return nil instead).
    • exit(status): exits with given status code.
    • print_error(msg): prints message to stderr (I modify it so it can print any kind of Lox Value).

TODO

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 and continue 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.