Skip to content

Monkey language interpreter written in TypeScript

License

Notifications You must be signed in to change notification settings

AnsonH/monkey-language-ts

Repository files navigation

monkey-language-ts

Yet another TypeScript implementation of the Monkey language, based on the book Writing An Interpreter In Go.

hero

How to Run?

Prerequisites:

Steps:

  1. Clone the repository

  2. Install dependencies

    pnpm install
  3. Build the project

    pnpm build
  4. 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

Comparison with Prior Art

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:
  • 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

Example programs and their descriptions can be found under the examples directory.

Big Picture

The interpreter has 3 main components:

  1. Lexer: Converts the source code into tokens.
  2. Parser: Converts the tokens into an Abstract Syntax Tree (AST) that represents the program.
  3. Evaluator: Walks the AST and executes the program.

big picture

Related Work

Other TypeScript implementations:

Other implementations:

Future Work

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)

Special Thanks