GithubHelp home page GithubHelp logo

monkey's Introduction

Writing an interpreter/compiler in Go

This is an implementation of the Monkey from Thorsten Ball's books

monkey > let hello = monkey
let hello = monkey;

Syntax

As of now, these syntaxes are available.

  • literal

    • integer: "1" -> 1
    • true: "true" -> true
    • false: "false" -> false
    • function: "fn(x) {return x}" -> fn(x) return x;
  • statement

    • let: "let x = 1" -> let x = 1;
    • return: "return x" -> return x;
  • expression

    • if: "if(x) {return x}" -> if x return x;
    • if-else: "if(x) {return x} else{return 0}" -> if x return x; else return 0;
    • call: "add(1,2)" -> add(1, 2)
  • operator

    • "!": "!x" -> (!x)
    • "+": "x + y" -> (x + y)
    • "-": "x - y" -> (x - y)
    • "*": "x * y" -> (x * y)
    • "/": "x / y" -> (x / y)
    • "==": "x == y" -> (x == y)
    • "!=": "x != y" -> (x != y)
    • ">": "x > y" -> (x > y)
    • "<": "x < y" -> (x < y)

Examples

  • Let statement
let x = 200;
let y = 100;
x + y; // -> 300
  • Return statement
return 200;
let x = 100;
3 * x; // -> 200
  • If-else expression
if(1 > 0) {
    return 100;
} else {
    return 200;
} // -> 100
  • Function literal & calling function
let f = fn(x) {
    return x * 100;
};
f(10); // -> 1000
  • Closure
let f = fn(x){
    let z = 3 * x;
    return fn(y){
        z + y;
    }
};
f(1)(2); // -> 5
  • Recursive function
let fib = fn(x){
    if(x < 2) {
        return x;
    }
    return fib(x-1) + fib(x-2);
};
fib(6); // -> 8

monkey's People

Contributors

masaxsuzu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

monkey's Issues

Panic when running "return 1;" on virtual machine

VM実行において、Function文以外でreturn命令を実行するとPanicが発生する。

~/go/src/github.com/masa-suzu/monkey
$ go run main.go -vm
Hello! This is the Monkey programming language!
>> return 1;
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/masa-suzu/monkey/vm.(*VirtualMachine).push(0xc0001be080, 0x515f00, 0xc00005e120, 0x0, 0x0)
        C:/Users/szk11/go/src/github.com/masa-suzu/monkey/vm/vm.go:260 +0xdf
github.com/masa-suzu/monkey/vm.(*VirtualMachine).Run(0xc0001be080, 0xc0000a4000, 0x10000)
        C:/Users/szk11/go/src/github.com/masa-suzu/monkey/vm/vm.go:188 +0xbde
github.com/masa-suzu/monkey/repl.Rep_VM(0xc00005e104, 0x9, 0x515600, 0xc000086008, 0xc00005e100, 0x5da6b8, 0x0, 0x0, 0xc0000a4000, 0x10000, ...)
        C:/Users/szk11/go/src/github.com/masa-suzu/monkey/repl/repl.go:87 +0x22e
github.com/masa-suzu/monkey/repl.Start(0x5155e0, 0xc000086000, 0x515600, 0xc000086008, 0x4fad30, 0x3, 0x1)
        C:/Users/szk11/go/src/github.com/masa-suzu/monkey/repl/repl.go:33 +0x458
main.main()
        C:/Users/szk11/go/src/github.com/masa-suzu/monkey/main.go:21 +0xbc
exit status 2

インタプリタ実行では発生しない。

~/go/src/github.com/masa-suzu/monkey
$ go run main.go
Hello! This is the Monkey programming language!
>> return 1;
1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.