haifenghuang / anko

Scriptable interpreter written in golang

Home Page:http://play-anko.appspot.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

anko

Build Status GoDoc

Anko is a scriptable interpreter written in Go.

(Picture licensed under CC BY-SA 3.0 by wikipedia)

Installation

Requires Go.

$ go get -u github.com/mattn/anko

Examples

# declare function
func plus(n){
  return n + 1
}

# declare variables
x = 1
y = x + 1

# print values 
println(x * (y + 2 * x + plus(x) / 2))

# if/else condition
if plus(y) > 1 {
  println("こんにちわ世界")
} else {
  println("Hello, World")
}

# array type
a = [1,2,3]
println(a[2])
println(len(a))

# map type
m = {"foo": "bar", "far": "boo"}
m.foo = "baz"
for k in keys(m) {
  println(m[k])
}

See _examples/scripts for more examples.

Usage

Embedding the interpreter into your own program:

var env = vm.NewEnv()

env.Define("foo", 1)
env.Define("bar", func() int {
	return 2
})

val, err := env.Execute(`foo + bar()`)
if err != nil {
	panic(err)
}

fmt.Println(val) 
// output:
// 3

To import all builtins, allowing the example scripts to work:

import anko_core "github.com/mattn/anko/builtins"

var env = vm.NewEnv()
anko_core.LoadAllBuiltins(env)

_, err := env.Execute(`println("test")`)
if err != nil {
	panic(err)
}
// output:
// "test"

Running scripts using anko command-line tool:

$ anko script.ank

License

MIT

Author

Yasuhiro Matsumoto (a.k.a mattn)

About

Scriptable interpreter written in golang

http://play-anko.appspot.com/


Languages

Language:Go 88.8%Language:Yacc 8.3%Language:Vim Script 2.6%Language:Batchfile 0.2%Language:Shell 0.1%Language:Makefile 0.0%