Yet another TypeScript implementation of the Monkey language, based on the book Writing An Interpreter In Go.
Prerequisites:
- NodeJS >=18.3.0
- Install pnpm package manager
Steps:
-
Clone the repository
-
Install dependencies
pnpm install
-
Build the project
pnpm build
-
Run the CLI
# REPL Mode node ./lib/cli/index.js # Run a Monkey script (*.monkey) node ./lib/cli/index.js ./examples/1-hello-world.monkey
This project aims to differentiate itself from many existing implementations (see Related Work):
- Uses idiomatic TypeScript instead of a direct 1:1 translation from the official Go implementation, for example:
- Uses TypeScript naming conventions
- Uses JavaScript
Error
object to represent parsing or evaluation errors
- The Abstract Syntax Tree (AST) types are inspired by Babel's AST.
- Plenty of JSDoc and comments to make the codebase easier to read.
Example programs and their descriptions can be found under the examples
directory.
The interpreter has 3 main components:
- Lexer: Converts the source code into tokens.
- Parser: Converts the tokens into an Abstract Syntax Tree (AST) that represents the program.
- Evaluator: Walks the AST and executes the program.
Other TypeScript implementations:
- CDThomas/ts-monkey - Highly inspired by this project, also with in-browser editor
- MichaelB-99/ts-monkey - With language extensions, bytecode compiler, and virtual machine
- MarioAriasC/TSMonkey - TypeScript implementation running on Bun
Other implementations:
- Monkey - Official Monkey language website. Contains links to various implementations.
- ThePrimeagen/ts-rust-zig-deez - Monkey lexer implementation in various languages.
If you're looking to extend the Monkey language, you can consider:
- UTF-8 character support
- Allow variable reassignment
- Code comments (e.g.
//
or/* */
) - More loops (e.g.
while
,for
) - More built-in functions
- Type system and type annotations
- Better error reporting (e.g. line and column numbers, better error message)
- Thorsten Ball for writing the book Writing An Interpreter In Go.
- create-typescript-app for the project template.