This repository contains a C# port of the Monkey programming language from the Writing an interpreter in Go book. Its code closely resembles that presented in the book, but written in idiomatic C# for the .NET Core runtime. Using the instructions below, the interpreter runs on both Windows, Mac, and Linux.
The Monkey parser is an hand-written LL(1) hybrid of a traditional recursive descent parser for statements and a Pratt, also known as Top Down Operator Precedence, parser for expressions. The hybrid parser ensures efficient parsing while supporting operator precedence and associativity. Its outputs is an abstract syntax tree which the tree walking evaluator traverses as part of executing a Monkey program.
The complete implementation of the lexer, parser, and evaluator consists of 2,200 lines of code with an additional 1,150 lines of tests. Not a lot of code for such a capable interpreter, implemented entirely without third party libraries.
The Monkey language supports mathematical expressions, variable bindings, functions and the application of those functions, conditionals, return statements, and advanced concepts such as higher-order functions and closures. The data types supported are integers, booleans, strings, arrays, and hashes.
See the official homepage. Navigate to "The Monkey Programming Language" section for examples and browse through the tests in this repository.
$ git clone https://github.com/ronnieholm/Monkey-CSharp.git $ cd Monkey-CSharp $ dotnet build $ dotnet test Monkey.Tests $ dotnet run -p Monkey.Cli
- 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.