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

https://github.com/bpetermann/ts-compiler

A typescript compiler
https://github.com/bpetermann/ts-compiler

compiler jest lexer nodejs parser typescript virtual-machine

Last synced: about 2 months ago
JSON representation

A typescript compiler

Awesome Lists containing this project

README

          

# TypeScript Compiler

A TypeScript compiler for the monkey programming language, based on the excellent book ["Writing An Compiler In Go"](https://compilerbook.com/)

## โš™๏ธ Installation

To get started, clone the repository:

```bash
git clone https://github.com/bpetermann/ts-compiler.git
cd ts-compiler
```

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 fibonacci = fn(x) {
if (x == 0) {
0
} else {
if (x == 1) {
return 1;
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
fibonacci(5);
```

Example of closures:

```js
let newClosure = fn(a) {
fn() { a; };
};
let closure = newClosure(99);
closure();
```

Close the REPL:

```js
eof;
```

## 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 compiler 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
```