A fully-featured interpreter for the Monkey programming language, built in Go following Thorsten Ball's "Writing an Interpreter in Go" book.
Monkey is a programming language designed for learning interpreter and compiler construction. It supports:
- Variables and integers
- Arithmetic expressions
- Built-in functions
- First-class functions and closures
- Strings and string manipulation
- Arrays and hash maps
- Control flow (if/else statements)
- Interactive REPL (Read-Eval-Print Loop)
- File execution mode
- Comprehensive error reporting
- Full test coverage with Go testing
- Clean, modular architecture
- Go 1.16 or later
- Clone the repository:
git clone https://github.com/Devashish08/InterPreter-Compiler.git
cd InterPreter-Compiler
- Build the interpreter:
make build
./interpreter repl
./interpreter run examples/fibonacci.monkey
./interpreter help
let name = "Monkey";
let age = 1;
let inspirations = ["Scheme", "Lisp", "JavaScript", "Clojure"];
let greetPeople = fn(name, age) {
return "Hello " + name + ", you are " + age + " years old!";
};
greetPeople("Anna", 24);
let fibonacci = fn(x) {
if (x < 2) {
return x;
}
return fibonacci(x - 1) + fibonacci(x - 2);
};
fibonacci(10); // Returns: 55
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
accumulated
} else {
iter(rest(arr), push(accumulated, f(first(arr))));
}
};
iter(arr, []);
};
let double = fn(x) { x * 2 };
map([1, 2, 3, 4], double); // Returns: [2, 4, 6, 8]
The interpreter is built with a clean, modular architecture:
βββ lexer/ # Tokenizes source code
βββ parser/ # Builds Abstract Syntax Tree (AST)
βββ ast/ # AST node definitions
βββ object/ # Object system and environment
βββ evaluator/ # Tree-walking evaluator
βββ repl/ # Interactive REPL implementation
βββ token/ # Token definitions
βββ examples/ # Sample Monkey programs
- Lexer (
lexer/
): Converts source code into tokens - Parser (
parser/
): Builds an Abstract Syntax Tree (AST) from tokens - AST (
ast/
): Defines the structure of the syntax tree - Evaluator (
evaluator/
): Tree-walking interpreter that executes the AST - Object System (
object/
): Represents values and manages scope/environment - REPL (
repl/
): Interactive shell for testing and exploration
make test
make coverage
make clean
Monkey comes with several built-in functions:
len(object)
- Returns length of strings, arrays, or hash mapsfirst(array)
- Returns first element of an arraylast(array)
- Returns last element of an arrayrest(array)
- Returns new array with all elements except the firstpush(array, element)
- Returns new array with element appendedputs(object)
- Prints object to stdout
- Integers:
42
,-5
- Booleans:
true
,false
- Strings:
"Hello World"
- Arrays:
[1, 2, 3]
- Hash Maps:
{"key": "value", "number": 42}
- Functions:
fn(x, y) { x + y }
- Arithmetic:
+
,-
,*
,/
- Comparison:
==
,!=
,<
,>
- Logical:
!
(bang operator)
if (condition) {
// do something
} else {
// do something else
}
This interpreter was built following:
- "Writing an Interpreter in Go" by Thorsten Ball
- Official Book Website
This is a learning project, but contributions are welcome! Please feel free to:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is open source and available under the MIT License.
- Thorsten Ball for the excellent "Writing an Interpreter in Go" book
- The Go community for creating such a great language for systems programming
- Robert Nystrom for "Crafting Interpreters" which inspired the field
Happy coding! πβ¨