Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/micahcantor/racket-lox
An implementation of the Lox language in Racket.
https://github.com/micahcantor/racket-lox
crafting-interpreters racket
Last synced: 21 days ago
JSON representation
An implementation of the Lox language in Racket.
- Host: GitHub
- URL: https://github.com/micahcantor/racket-lox
- Owner: micahcantor
- License: mit
- Created: 2021-10-22T05:41:47.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-28T15:53:05.000Z (over 1 year ago)
- Last Synced: 2023-10-28T16:33:32.943Z (over 1 year ago)
- Topics: crafting-interpreters, racket
- Language: Racket
- Homepage:
- Size: 15.7 MB
- Stars: 20
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# racket-lox
racket-lox is an implementation of [the Lox language](https://github.com/munificent/craftinginterpreters/) from Robert Nystrom's book [Crafting Interpreters](https://craftinginterpreters.com/) 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](https://craftinginterpreters.com/representing-code.html#the-visitor-pattern) in the evaluator and the resolver.
- As such, there is no need for [the metaprogramming described in chapter 5](https://craftinginterpreters.com/representing-code.html#metaprogramming-the-trees) 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](src/interpreter.rkt) rather than in a separate file.
- Error related functions are placed in a separate file, [error.rkt](src/error.rkt) rather than with the top-level program interface, which lives in [main.rkt](src/main.rkt).Despite the differences, racket-lox passes all of [the tests provided by Nystrom](https://github.com/munificent/craftinginterpreters#testing).
## 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`.