(A C# version of Monkey is also available)
A Python port of the Monkey programming language from the Writing an interpreter in Go book. It's written in idiomatic Python targeting at least CPython 3.6.
From the book:
It supports mathematical expressions, variable bindings, functions and the application of those functions, conditionals, return statements and even advanced concepts like higher-order functions and closures. And then there are the different data types: integers, booleans, strings, arrays and hashes.
The Monkey parser consists of a hand-written LL(1) traditional recursive descent parser for statements combined with a Pratt parser for expressions. The hybrid parser ensures efficient parsing while elegantly supporting operator precedence. Its outputs is an abstract syntax tree walked by the evaluator as part of program execution.
The complete implementation of the lexer, parser, and evaluator consists of 1,150 lines of code with an additional 775 lines of tests. Not a lot for such a capable interpreter, implemented entirely without third party libraries.
See "The Monkey Programming Language" section on the official homepage and have a look at the unit tests and Examples folder in this repository.
$ git clone https://github.com/ronnieholm/Monkey-Python.git
$ cd Monkey-Python
$ python3 -m venv venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt
$ mypy *.py
$ python3 -m unittest *test.py
$ python3 main.py
$ python3 main.py examples/fibonacci.monkey
$ deactivate
- Top Down Operator Precedence, Vaughan R. Pratt.
- Some problems of recursive descent parsers, Eli Bendersky.
- Top-Down operator precedence parsing, Eli Bendersky.
- A recursive descent parser with an infix expression evaluator, Eli Bendersky.
- Parsing expressions by precedence climbing, Eli Bendersky.
- Practical explanation and example of Pratt parser, Bob Nystrom.
- GoRuby by Michael Wagner extends the concepts, structures, and code of Monkey to Ruby.
- GoAwk implements the relatively simple Awk language.
- Get Productive with Python in Visual Studio Code (including Docker integration) from VSCode, Dan Taylor.
- Visual Studio Code (Windows) - Setting up a Python Development Environment and Complete Overview, Corey Schafer.
- Python Tutorial: VENV (Windows) - How to Use Virtual Environments with the Built-In venv Module, Corey Schafer.