/
main.go
89 lines (78 loc) · 1.64 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"io"
"strings"
"github.com/chzyer/readline"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
)
const ps1 = "> "
const ps2 = ": "
var completer = readline.NewPrefixCompleter(
readline.PcItem("print("),
)
func main() {
resolve.AllowFloat = true
resolve.AllowSet = true
l, err := readline.NewEx(&readline.Config{
AutoComplete: completer,
DisableAutoSaveHistory: true,
EOFPrompt: "exit",
HistoryFile: "/tmp/readline.tmp",
InterruptPrompt: "^C",
Prompt: ps1,
})
if err != nil {
panic(err)
}
defer l.Close()
predeclared := starlark.StringDict{}
thread := &starlark.Thread{}
var lines []string
LOOP:
for {
line, err := l.Readline()
switch {
case err == readline.ErrInterrupt:
if len(line) == 0 {
break LOOP
} else {
continue
}
case err == io.EOF:
break LOOP
case line == "exit":
break LOOP
}
lines = append(lines, line)
switch {
case strings.HasPrefix(line, " "):
continue
case strings.HasSuffix(line, ":"):
l.SetPrompt(ps2)
continue
}
buffer := strings.Join(lines, "\n")
lines = lines[:0]
l.SetPrompt(ps1)
l.SaveHistory(buffer)
_, err = syntax.ParseExpr("<stdin>", line, 0)
if err != nil {
if globals, err := starlark.ExecFile(thread, "<stdin>", buffer, predeclared); err != nil {
fmt.Println(err)
} else {
for k, v := range globals {
predeclared[k] = v
}
}
} else {
if v, err := starlark.Eval(thread, "<stdin>", buffer, predeclared); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(v.String())
}
}
}
}