- Supports integer, boolean, and string datatypes.
- Supports function declarations.
- supports recursion.
- Supports the List data structure.
- Arithmetic operations: +, -, *, /, %.
- Conditional statements with if-else blocks.
- Includes a REPL for interactive evaluation.
- Error handling for invalid syntax and type errors.
- The interpreter consists of mainly three components.
- The Lexer.
- The Parser.
- The Evaluator.
- The lexer is supported with a module
Tokenthat has defined the various tokens of this toy language. The main purpose of the lexer is to Tokenize the input code into valid tokens. - The parser then takes these valid tokens and constructs an Abstract Syntax Tree (AST) from them. The structure of the AST is defined in the
Astmodule. The parser that is built for this language uses the pratt-parser design, which is a top down recursive approach. - The Evaluator then finally traverses the AST and interprets and executes the commands.
graph LR
A(Lexer) -->B(Parser)
B --> C(Evaluator)
C --> D(Result)
M[Token] --> A
N[AST, Object] --> B
F[Env] --> C
-
Unit tests cover:
- Lexer tokenization
- Parser structure
- Evaluator correctness (including recursive functions like Fibonacci)
-
Tests are written using Alcotest.
Run tests with:
cd desert_monkey
dune testNote : You must have OCAML and OPAM installed in your device to build this project, for obvious reasons.
cd desert_monkey
dune buildcd desert_monkey
dune exec ./_build/default/bin/main.exe>> let add = fn(a, b) { return a + b; };
>> add(2, 3); // Int(5).
-
This project is based on the book "Writing an Interpreter in Go" by Thorsten Ball.
-
Built independently with some reference to public open-source implementations.
- MIT
- support for floating point integers.
- complete the line and col no in error handling.
- Self healing code capabilities using LLM suggestions for errors? must make it different from a glorified auto-complete.