/monkey.rs

šŸµ Monkey interpreter and compiler-vm in Rust. šŸš€

Primary LanguageRustMIT LicenseMIT

monkey.rs

Summury

We can learn how to make an interpreter and a compiler-vm in this book.
====> ā˜†ā˜†ā˜† "Writing An Interpreter in Go" ā˜†ā˜†ā˜†
====> ā˜†ā˜†ā˜† "Writing A Compiler In Go" ā˜†ā˜†ā˜†
That interpreter and compiler-VM is called Monkey in the book.
The Monkey is written in Go in the book.
But in this repository it is written in Rust.

Supports

  • Lexer
  • Parser
  • Evaluator
  • Compiler
  • VM
  • REPL
  • Test case
  • Evaluator and VM benchmarks
  • Not use unsafe

Example

REPL

$ cargo run
>> let a = 5
5
>> a + 10
15
>> let new_closure = fn(a) { fn() { a; }; };
Closure[CompiledFunction[0000 GetLocal 0Ā„n0002 Closure 3 1Ā„n0006 ReturnValueĀ„n] ]
>> let closure = new_closure(99);
Closure[CompiledFunction[0000 GetFree 0Ā„n0002 ReturnValueĀ„n] 99]
>> closure();
99

Fibonacchi

let fibonacci = fn(x) {
  if (x == 0) {
    return 0;
  } else {
    if (x == 1) {
      return 1;
    } else {
      fibonacci(x - 1) + fibonacci(x - 2);
    } 
  }
};
fibonacci(15); #=> 610

TODO

  • Improve print output
  • Improve REPL
  • Refactoring
  • Optimize data structure
  • Support LLVM

Contributors

License

MIT