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.
To install and run the project, follow these steps:
-
Clone the Repository:
git clone https://github.com/jonatanwestling/interpreter-lox.git cd interpreter-lox -
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. -
Run Lox Programs: Run the interpreter with a Lox source file (
.loxor.txtfile 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
printstatements.
-
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, andLiteraldata types defined inTokens.hs. Handles invalid characters by throwing an exception with Haskell'serrorfunction and supports accurate line number tracking for multi-line strings.
- File:
-
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.,Programcontaining a list of declarations). The data type is an instance ofShow, 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.
- File:
-
Interpreter:
- File:
lox.hs - Description: Executes Lox code by processing the parse tree generated by the parser. Outputs results of
printstatements with newlines (e.g.,print "hello"; -> hello). Uses custom data types likeValuefor different value types andEnvironmentfor variable bindings and scope. Handles runtime errors with informative error messages.
- File:
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.fieldorobject.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.
- The interpreter supports core Lox language features as defined in Crafting Interpreters, excluding object-oriented features unless implemented as extensions.
- The
Tokens.hsfile from the course repository is used without modification for the scanner and parser. - I/O is handled in the
mainfunction 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.