ts-monkey is a typescript implementation of both a tree-walking interpreter and bytecode compiler & virtual machine for Monkey.
My monkey implementation has been extended to support the following new features:
-
Friendly error messages that display the offending line of code and underline the exact location of the error.
-
arrow functions e.g
let adder = fn x => fn y => x+y; adder(1)(2)let adder = fn(x,y)=> x+y -
Default parameters
let add = fn (x,y=1) => x+y; add(1); //2 add(10,90); //100 -
for in loops
for(item,index in [1,2,3,4]){ puts(item+index)} -
Ability to run monkey files (.mo extension)
-
comments (both single and multi line)
// a comment!/* a multiline comment */ -
Logical OR and logical AND operators
-
<= and >= operators
-
% operator
-
String comparison
-
String indexing
-
more builtin functions
- map
map([1,2,3,4], fn x => x*2) - find
find(["hello", "world"], fn w => w=="hello") - reduce
reduce([1,2,3,4], fn(acc,curr) => acc+curr,100) - filter
filter([1,2,3,4], fn x => x%2==0) - set
let map = { "a": 1 }; set(map,"b",2); map["b"] //2
- map
Clone the project
git clone https://github.com/michaelbdev/ts-monkeyGo to the project directory
cd ts-monkeyInstall dependencies
bun installWith compiler & vm
bun monkey-run -cWith interpreter
bun monkey-run With compiler & vm
bun monkey-repl -cWith interpreter
bun monkey-repl- print AST
--ast - print bytecode (compiler only)
--bytecode
To run the benchmark run the following:
interpreter
bun benchmark --engine=eval
vm
bun benchmark --engine=vm