Skip to content

jonatanwestling/interpreter-lox

Repository files navigation

Lox Interpreter in Haskell

This project implements an interpreter for the Lox programming language, as described in Crafting Interpreters, using the Haskell programming language instead of Java. The interpreter is composed of three main modules: a scanner (lexer), a parser, and an interpreter, which together process and execute Lox source code.

Installation and Running

To install and run the project, follow these steps:

  1. Clone the Repository:

    git clone https://github.com/jonatanwestling/interpreter-lox.git
    cd interpreter-lox
  2. Compile the Interpreter: Ensure you have GHC (Glasgow Haskell Compiler) installed. Compile the interpreter by running:

    ghc lox.hs

    This generates an executable named lox.

  3. Run Lox Programs: Run the interpreter with a Lox source file (.lox or .txt file adhering to Lox syntax) as a command-line argument:

    ./lox path/to/your/file.lox

    The program reads the file, processes it through the scanner, parser, and interpreter, and outputs the results to the command line as specified by Lox's print statements.

Modules

  • Scanner:

    • File: Scanner.hs, Tokens.hs
    • Module: Scanner
    • Exported Function: scanTokens :: [Char] -> [Token]
    • Description: Converts Lox source code into a list of tokens using the Token, TokenType, and Literal data types defined in Tokens.hs. Handles invalid characters by throwing an exception with Haskell's error function and supports accurate line number tracking for multi-line strings.
  • Parser:

    • File: Parser.hs, ParserTypes.hs
    • Module: Parser
    • Exported Function: parse :: [Token] -> A
    • Description: Takes a list of tokens and constructs a parse tree using a custom data type A (e.g., Program containing a list of declarations). The data type is an instance of Show, formatting output for literals (e.g., 5.0), expressions (e.g., (5.0*2.0)), and statements (e.g., if(a)(5.0*2.0);). Handles invalid tokens with descriptive error messages.
  • Interpreter:

    • File: lox.hs
    • Description: Executes Lox code by processing the parse tree generated by the parser. Outputs results of print statements with newlines (e.g., print "hello"; -> hello). Uses custom data types like Value for different value types and Environment for variable bindings and scope. Handles runtime errors with informative error messages.

Limitations

This Lox interpreter supports fundamental language features such as loops, functions, and lexical scoping. However, several advanced capabilities were excluded due to time constraints and to keep the project focused on core interpreter functionality:

  • Object-Oriented Features: Classes, inheritance, method calls, and field access (e.g., object.field or object.method()) are not implemented.

  • Desugaring: Syntactic sugar transformations, such as converting certain constructs into simpler forms, are not performed.

  • Static Analysis: The interpreter does not perform type checking or verify that variables are declared before use.

Notes

  • The interpreter supports core Lox language features as defined in Crafting Interpreters, excluding object-oriented features unless implemented as extensions.
  • The Tokens.hs file from the course repository is used without modification for the scanner and parser.
  • I/O is handled in the main function of the interpreter, with output strings collected and printed at the end of execution.
  • The code is designed to be modular, well-documented, and aligned with Haskell best practices.

About

A lox interpreter in Haskell

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors