This project follows Bob Nystrom's excellent book, Crafting Interpreters which takes you through the process of writing an interpreter for a language called Lox.
The book describes two implementations. The first in Java and is a tree-walking interpreter. I have ported that version to Swift in my other project, slox. The final part of the book describes a bytecode interpreter in C. This is my Swift port of the bytecode interpreter. I also ported this bytecode interpreter to C++ in cloxpp.
The book is being released as chapters are completed, one chapter at a time.
Code from the following chapters is implemented in this port:
- Chunks of Bytecode.
- A Virtual Machine.
- Scanning on Demand.
- Compiling Expressions.
- Types of Values.
- Strings.
- Hash Tables. (no code required, will use
Dictionary
) - Global Variables.
- Local Variables.
- Jumping Back and Forth.
The test suite is from the reference C implementation. To run the tests:
dart tool/bin/test.dart chap23_jumping --interpreter .build/release/bslox
The command specifies .build/release/bslox
as the binary, which is where it ends up after running this command to compile the code:'
swift build -c release
For the test suite to run, you need to have the Dart programming language SDK installed. After that, you need to get the test runners dependencies by going to the tool
directory and running:
pub get
The goal is to get as close as possible to the C reference implementation in performance, while taking advantage of Swift's features.
MIT