An open API service indexing awesome lists of open source software.

https://github.com/bpetermann/node-interpreter

Typescript interpreter for monkey
https://github.com/bpetermann/node-interpreter

interpreter jest lexer nodejs parser typescript

Last synced: about 2 months ago
JSON representation

Typescript interpreter for monkey

Awesome Lists containing this project

README

          

# Node Interpreter

A Node/Typescript interpreter for the [monkey](https://monkeylang.org/) programming language, based on the excellent book ["Writing An Interpreter In Go"](https://interpreterbook.com/).

## โš™๏ธ Installation

To get started, clone the repository:

```bash
git clone https://github.com/bpetermann/node-interpreter.git
cd node-interpreter
```

Then, install dependencies and build the project:

```js
npm run build:fresh // Installs dependencies and builds the project
```

## ๐Ÿš€ Start

Finally, start the REPL (Read-Eval-Print Loop):

```js
npm run start // Starts the REPL
```

## ๐Ÿ“‹ Usage Examples

```js
>> let a = 2; // Declare a variable
>> let baz = ["foo", "bar"]; // Declare an array
>> baz[0]; // Acesss array
>> let person = {"name": "Alice"}; // Declare a hash map
>> person["name"]; // Access map
```

Here's a basic example illustrating the declaration and invocation of a function:

```js
>> let a = 2; // Declare a variable
>> let multiply = fn(x, y) { x * y }; // Declare a function
>> multiply(50 / 2, a); // Call the function
50
```

Example of closures:

```js
>> let newAdd = fn(x) {
fn(y) { x + y };
};
>> let addTwo = newAdd(2);
>> addTwo(2);
4
```

Close the REPL:

```js
eof;
```

## Interpreter Workflow

1. Lexical Analysis (Lexer):

- The input code undergoes tokenization by the Lexer, breaking it into smaller units known as tokens (keywords, identifiers, operators).

2. Parsing:

- Tokens are organized into an abstract syntax tree (AST) by the Parser, following the grammar rules specific to the language.
- The AST represents the hierarchical structure of the code, arranging statements and expressions.

3. Evaluation:

- The interpreter traverses the AST, evaluating nodes and executing code based on the languages syntax rules.

This interpreter employs a process of lexical analysis, parsing, and evaluation to systematically analyze and execute code, creating an AST to interpret programs according to the specific syntax and rules of the language.

## Syntax Overview

The syntax embodies a rich spectrum of functionalities, managing mathematical expressions, variable assignments, function definitions, calls, conditionals, and returns. It adeptly handles concepts like higher-order functions and closures.

Additionally, the interpreter accommodates diverse data types โ€” integers, booleans, strings, arrays, and hashes.

It also features a set of built-in functions tailored to expedite string/array operations and console output logging.

## ๐Ÿงช Tests

The following command will run all jest test suites:

```js
npm run test
```