-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
49 lines (42 loc) · 900 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/spy16/slurp"
"github.com/spy16/slurp/builtin"
"github.com/spy16/slurp/core"
"github.com/spy16/slurp/repl"
)
var globals = map[string]core.Any{
"nil": builtin.Nil{},
"true": builtin.Bool(true),
"false": builtin.Bool(false),
"version": builtin.String("1.0"),
// custom Go functions.
"=": slurp.Func("=", core.Eq),
"+": slurp.Func("sum", func(a ...int) int {
sum := 0
for _, item := range a {
sum += item
}
return sum
}),
">": slurp.Func(">", func(a, b builtin.Int64) bool {
return a > b
}),
}
func main() {
env := slurp.New()
if err := env.Bind(globals); err != nil {
fmt.Printf("bind failed: %+v\n", err)
os.Exit(1)
}
r := repl.New(env,
repl.WithBanner("Welcome to slurp!"),
repl.WithPrompts(">>", " |"))
if err := r.Loop(context.Background()); err != nil {
log.Fatal(err)
}
}