nloxvm is a Nim implementation of a bytecode virtual machine for interpreting the Lox programming language. This implementation is based on the clox interpreter, which is done in C.
Lox is a scripting language created by Robert Nystrom to teach in a practical way the implementation of a programming language throughout the book Crafting Interpreters. To learn more, visit craftinginterpreters.com.
I have always been interested in learning how a programming language is implemented and the book Crafting Interpreters brings this in a very didactic and practical way. So it's a perfect opportunity to learn something new and develop myself as a programmer.
Nim is currently the programming language I have the best aptitude for and I feel more comfortable exploring something new and unknown. I tried to keep this Nim implementation as faithful as possible with the base implementation made in C.
The big challenge was trying to keep the project organization (files and code) the same as the book, since refactoring/code reallocation was necessary to avoid cyclical imports. To do this, I had to create some *_helpers.nim
modules, as well as put all type declarations in types.nim
. I had to bail myself out to {.exportc.}
and {.importc.}
to avoid a major code change.
I. WELCOME
- 1. Introduction
- 2. A Map of the Territory
- 3. The Lox Language
III. A TREE-WALK INTERPRETER
- 14. Chunks of Bytecode
- 15. A Virtual Machine
- 16. Scanning on Demand
- 17. Compiling Expressions
- 18. Types of Values
- 19. Strings
- 20. Hash Tables
- 21. Global Variables
- 22. Local Variables
- 23. Jumping Back and Forth
- 24. Calls and Functions
- 25. Closures
- 26. Garbage Collection
- 27. Classes and Instances
- 28. Methods and Initializers
- 29. Superclasses
- 30. Optimization
Visit nlox to see the Nim implementation of the jlox-based Lox interpreter.
First you need to have a Nim 2.0.0 compiler or higher.
Then clone this repository:
git clone https://github.com/rockcavera/nim-nloxvm.git
Enter the cloned folder:
cd nim-nloxvm
Finally, compile the project:
nim c -d:release src/nloxvm
or install with Nimble:
nimble install
For best performance, I recommend compiling with:
nim c -d:danger -d:lto src/nloxvm
Assuming you have already cloned the repository, just be in the project folder and type:
nim r tests/tall
or:
nimble test
All files are under the MIT license, with the exception of the .lox files located in the tests/scripts folder, which are under this LICENSE because they are third-party code, which can be accessed here.