A TypeScript implementation of C-like language based on excellent Writing An Interpreter In Go book.
- Download and install Node
- Open Terminal
$ git clone https://github.com/michalholasek/monkey-lang.git
$ npm i && npm run build
- Open Terminal
$ cd <monkey-lang directory>
$ node ./dist/monkey.js
Welcome to monkey-lang REPL!
Hit RETURN once to enter multiline editing, twice to enter your command.
> 5
5
> let let = 5;
invalid token(6, 1): expected Identifier, got Let instead
> quit
Exiting monkey-lang REPL...
- C-like syntax
- Integer, boolean, and string primitive data types
- Array and hashtable support
- Variable bindings
- Let and Return statements
- If-Else conditionals
- Basic arithmetic for integer expressions
- First class and higher-order functions, closures
- Built-in utility functions
Type | Examples |
---|---|
int |
0 , 123 , -29 |
boolean |
true , false |
string |
"Astralis" |
array |
[] , [1, 2, 3] , [fn (x) { return x; }, [], ""] |
hash |
{} , { "key": "value" } , { false: true, 1: "Yes!" } |
> let five = 5;
> five
5
> let ten = 5 + 10 - 5;
> let eleven = (5 * 2) + 1;
> ten
10
> eleven
11
> let identity = fn(value) { return value };
> identity(42);
42
> let add = fn(x) {
return fn(y) {
return x + y;
}
};
> let addTwo = add(2);
> addTwo(5);
7
> let elsy = fn(value) {
if (value) {
return value;
} else {
return "Alternative.";
}
};
> elsy(false);
Alternative.
> len("abc")
3
> len([1])
1
> push([1], 2, 3)
[1, 2, 3]
> first([1, 2, 3])
1
> last([1, 2, 3])
3
> rest([1, 2, 3])
[2, 3]
> puts(1, 2, 3)
1
2
3
> puts("Hello!")
"Hello!"
MIT