Skip to content

A tree walking interpreter and bytecode compiler & VM for monkey

Notifications You must be signed in to change notification settings

michaelbdev/ts-monkey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

145 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-monkey

ts-monkey is a typescript implementation of both a tree-walking interpreter and bytecode compiler & virtual machine for Monkey.

Extensions

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

Run

Clone the project

  git clone https://github.com/michaelbdev/ts-monkey

Go to the project directory

  cd ts-monkey

Install dependencies

  bun install

Run from monkey files

With compiler & vm

   bun monkey-run -c

With interpreter

   bun monkey-run 

Using the REPL

With compiler & vm

   bun monkey-repl -c

With interpreter

   bun monkey-repl

Options

  • print AST --ast
  • print bytecode (compiler only) --bytecode

Benchmarking

To run the benchmark run the following:

interpreter

bun benchmark --engine=eval

vm

bun benchmark --engine=vm

About

A tree walking interpreter and bytecode compiler & VM for monkey

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published