LTWI is a C++ implementation of the Lox programming language, based on the interpreter described in Bob Nystrom's book Crafting Interpreters. This implementation extends the original jlox interpreter with several optimizations and enhancements.
Lox is a dynamically-typed scripting language with C-like syntax that supports:
- Variables and expressions
- Control flow (if/else, while loops)
- Functions and closures
- Classes with inheritance
- First-class functions
This implementation, named LTWI (Lox Tree-Walk Interpreter), follows the same tree-walk interpretation approach as the original jlox but is written in modern C++ (C++20) with several performance optimizations and enhancements.
LTWI extends the original jlox implementation with the following features:
- Optional variant-based Number class that can store either int64_t or double
- Automatic optimization to use integer representation when possible
- Overflow checking for arithmetic operations
- Configurable via the
USE_INT64_NUMBERCMake option
- Environment object pooling to reduce memory allocation/deallocation overhead
- Smart pointer management for memory safety
- Memory usage tracking and statistics
- Uses C++20 standard
- Leverages std::variant, std::unique_ptr, and std::shared_ptr for type safety and memory management
- Visitor pattern implementation for AST traversal
- Execution time measurement for scripts
- Optimized environment lookups
- Static method support in classes
- CMake 3.31 or higher
- C++20 compatible compiler
-
Clone the repository
-
Create a build directory:
mkdir build cd build -
Configure with CMake:
cmake ..To enable the int64_t+double variant for Number:
cmake -DUSE_INT64_NUMBER=ON .. -
Build the project:
cmake --build .
./ltwi path/to/script.lox
./ltwi
--version: Display version information--help: Show usage information
- Scanner: Lexical analysis (tokenization)
- Parser: Syntactic analysis (parsing)
- Resolver: Variable resolution and scope analysis
- Interpreter: Execution of the AST
- Environment: Variable environment management
- EnvironmentPool: Object pooling for environments
- GenerateAst: Tool for generating AST classes
The following features could be added to enhance LTWI:
-
Standard Library: Implement a more comprehensive standard library with additional functions for string manipulation, math operations, file I/O, etc.
-
Error Recovery: Improve error handling and recovery to continue parsing/execution after errors.
-
Optimization Passes: Add optimization passes to the AST before interpretation.
-
JIT Compilation: Implement a simple JIT compiler for hot functions.
-
Garbage Collection: Replace shared pointers with a custom garbage collector for better performance.
-
Modules/Imports: Add support for modules and imports to better organize code.
-
Debugging Tools: Implement debugging features like breakpoints, step execution, and variable inspection.
-
REPL Improvements: Enhance the interactive mode with features like history, auto-completion, and syntax highlighting.
-
Native Function Interface: Create a more robust interface for defining native functions in C++.
-
Concurrency Support: Add primitives for concurrent programming.
This project is open source and available under the MIT License.
- Based on the Lox language designed by Bob Nystrom for his book "Crafting Interpreters"
- Inspired by the original jlox implementation but rewritten and enhanced in C++