micahcantor / racket-lox

An implementation of the Lox language in Racket.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

racket-lox

racket-lox is an implementation of the Lox language from Robert Nystrom's book Crafting Interpreters in Typed Racket.

This interpreter is based on the tree-walking Java interpreter described in part II of the book, meaning it largely follows the book's imperative style, rather than the functional programming that is more common in Racket. However, there are a few differences in the implementation:

  • Rather than using classes and inheritance to model the AST nodes, we instead model the tree as a union type of structs. This means that we use pattern matching rather than the Visitor pattern in the evaluator and the resolver.
  • As such, there is no need for the metaprogramming described in chapter 5 to create classes for the AST nodes.
  • Callable is implemented as a union type of Function, NativeFunction and Class. Because of annoyances surrounding cyclic imports, the Callable-related code lives in interpreter.rkt rather than in a separate file.
  • Error related functions are placed in a separate file, error.rkt rather than with the top-level program interface, which lives in main.rkt.

Despite the differences, racket-lox passes all of the tests provided by Nystrom.

Running the interpreter

To run the interpreter, simply use racket src/main.rkt, passing a .lox file as an argument to run a script. Note that because of the use of typed racket, the interpreter is very slow when run uncompiled. Therefore it's best to use raco make src/main.rkt before executing.

To build an executable (e.g. to use with the test runner), use raco exe -o dist/main src/main.rkt.

About

An implementation of the Lox language in Racket.

License:MIT License


Languages

Language:Racket 100.0%