This is an interpreter for the Lox programming language specified in the book Crafting Interpreters by Robert Nystrom. It's implemented using OCaml.
This project uses the Dune build system. To build the interpreter, run
dune build --profile release
Then you are expected to find the executable file at ./_build/default/bin/main.exe
.
To scan, parse, and interpret a lox script (e.g., ./lox-scripts/factorial.lox
), run
./_build/default/bin/main.exe ./lox-scripts/factorial.lox
You can find some simple Lox scripts in ./lox-scripts
.
The meat of this interpreter is in ./lib/interpreter.ml
.
Starting with the source code (type string
), we
- scan (function
scan()
) the source code into tokens (typetoken_t list
) - parse (function
parse()
) the tokens into statements (typestatement_t list
) - interpret (function
execute_statements()
) the statements
This implementation follows most of the implementation in Crafting Interpreters, except
- It doesn't use a resolver. Instead, it creates a new environment when a new variable is declared.
- Classes are not yet implemented.