Skip to content

ttheghost/loxpp-interpreter

Repository files navigation

LTWI - Lox Tree-Walk Interpreter

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.

Overview

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.

Features and Enhancements

LTWI extends the original jlox implementation with the following features:

1. Optimized Number Representation

  • 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_NUMBER CMake option

2. Memory Optimizations

  • Environment object pooling to reduce memory allocation/deallocation overhead
  • Smart pointer management for memory safety
  • Memory usage tracking and statistics

3. Modern C++ Features

  • 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

4. Performance Improvements

  • Execution time measurement for scripts
  • Optimized environment lookups
  • Static method support in classes

Installation

Prerequisites

  • CMake 3.31 or higher
  • C++20 compatible compiler

Build Instructions

  1. Clone the repository

  2. Create a build directory:

    mkdir build
    cd build
    
  3. Configure with CMake:

    cmake ..
    

    To enable the int64_t+double variant for Number:

    cmake -DUSE_INT64_NUMBER=ON ..
    
  4. Build the project:

    cmake --build .
    

Usage

Running Scripts

./ltwi path/to/script.lox

Interactive Mode

./ltwi

Command Line Options

  • --version: Display version information
  • --help: Show usage information

Project Structure

  • 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

Future Improvements

The following features could be added to enhance LTWI:

  1. Standard Library: Implement a more comprehensive standard library with additional functions for string manipulation, math operations, file I/O, etc.

  2. Error Recovery: Improve error handling and recovery to continue parsing/execution after errors.

  3. Optimization Passes: Add optimization passes to the AST before interpretation.

  4. JIT Compilation: Implement a simple JIT compiler for hot functions.

  5. Garbage Collection: Replace shared pointers with a custom garbage collector for better performance.

  6. Modules/Imports: Add support for modules and imports to better organize code.

  7. Debugging Tools: Implement debugging features like breakpoints, step execution, and variable inspection.

  8. REPL Improvements: Enhance the interactive mode with features like history, auto-completion, and syntax highlighting.

  9. Native Function Interface: Create a more robust interface for defining native functions in C++.

  10. Concurrency Support: Add primitives for concurrent programming.

License

This project is open source and available under the MIT License.

Acknowledgements

  • 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++

About

A C++ implementation of the Lox language from Crafting Interpreters. This project features a full tree-walker interpreter built from scratch in about 3000 lines of modern C++. It supports expressions, variables, functions, control flow, class inheritance, and static methods. Faithfully recreates jlox’s behavior and design.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors