Pako is a scriptable interpreter written in Go.
- Import directives with local imports. Controlled by Env.Import
- Load function controlled by Env.LoadFrom
?
operator to handle return values easily.as
assignments.- Strict lvalue and rvalue assignment.
- Value unpacking/ignoring. Using ()
- Operator override
- Struct declaration
- Safe and unsafe division of the packages.
- Struct methods
- Struct constructors.
- Runs struct declarations before executing. See this example.
Source -> Parser -> VM
package main
import (
"errors"
"fmt"
"log"
"reflect"
"strconv"
"github.com/dgrr/pako/env"
"github.com/dgrr/pako/vm"
)
func main() {
env.Packages["my_pkg"] = map[string]reflect.Value{
"print": reflect.ValueOf(fmt.Println),
"getStr": reflect.ValueOf(func(n int64) (string, error) {
if n < 0 {
return "", errors.New("Cannot represent negative numbers")
}
return strconv.FormatInt(n, 10), nil
}),
}
e := env.NewEnv()
script := `import my_pkg as pkg
str = pkg.getStr(20)?
pkg.print("My string representation:", str)
pkg.print("Now should panic!!!")
pkg.getStr(-1)?`
_, err := vm.Execute(e, nil, script)
if err != nil {
log.Fatalf("Execute error: %v\n", err)
}
// output:
// My string representation: 20
// Now should panic!!!
}
More examples are located in the GoDoc:
https://godoc.org/github.com/dgrr/pako/vm
go get github.com/dgrr/pako
go install github.com/dgrr/pako
pako script.pak
// declare variables
x = 1
y = x + 1
// print using outside the script defined println function
println(x + y) // 3
// if else statement
if x < 1 || y < 1 {
println(x)
} else if x < 1 && y < 1 {
println(y)
} else {
println(x + y)
}
// slice
a = []interface{1, 2, 3}
println(a) // [1 2 3]
println(a[0]) // 1
// map
a = map[interface]interface{"x": 1}
println(a) // map[x:1]
a.b = 2
a["c"] = 3
println(a["b"]) // 2
println(a.c) // 3
// struct
a = make(struct {
A int64,
B float64
})
a.A = 4
a.B = 5.5
println(a.A) // 4
println(a.B) // 5.5
// function
fn a (x) {
println(x + 1)
}
a(5) // 6
The master branch language and API may change at any time.
To mitigate breaking changes, please use tagged branches. New tagged branches will be created for breaking changes.
Yasuhiro Matsumoto (a.k.a mattn)
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]