forked from mattn/anko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anko.go
168 lines (141 loc) · 2.79 KB
/
anko.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// +build !appengine
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/mattn/anko/core"
"github.com/mattn/anko/packages"
"github.com/mattn/anko/parser"
"github.com/mattn/anko/vm"
)
const version = "0.0.5"
var (
flagExecute string
file string
args []string
env *vm.Env
)
func main() {
var exitCode int
parseFlags()
setupEnv()
if flagExecute != "" || flag.NArg() > 0 {
exitCode = runNonInteractive()
} else {
exitCode = runInteractive()
}
os.Exit(exitCode)
}
func parseFlags() {
flagVersion := flag.Bool("v", false, "prints out the version and then exits")
flag.StringVar(&flagExecute, "e", "", "execute the Anko code")
flag.Parse()
if *flagVersion {
fmt.Println(version)
os.Exit(0)
}
if flagExecute != "" || flag.NArg() < 1 {
args = flag.Args()
return
}
file = flag.Arg(0)
args = flag.Args()[1:]
}
func setupEnv() {
env = vm.NewEnv()
env.Define("args", args)
core.Import(env)
packages.DefineImport(env)
}
func runNonInteractive() int {
var source string
if flagExecute != "" {
source = flagExecute
} else {
sourceBytes, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("ReadFile error:", err)
return 2
}
source = string(sourceBytes)
}
_, err := env.Execute(source)
if err != nil {
fmt.Println("Execute error:", err)
return 4
}
return 0
}
func runInteractive() int {
var following bool
var source string
scanner := bufio.NewScanner(os.Stdin)
parser.EnableErrorVerbose()
for {
if following {
source += "\n"
fmt.Print(" ")
} else {
fmt.Print("> ")
}
if !scanner.Scan() {
break
}
source += scanner.Text()
if source == "" {
continue
}
if source == "quit()" {
break
}
stmts, err := parser.ParseSrc(source)
if e, ok := err.(*parser.Error); ok {
es := e.Error()
if strings.HasPrefix(es, "syntax error: unexpected") {
if strings.HasPrefix(es, "syntax error: unexpected $end,") {
following = true
continue
}
} else {
if e.Pos.Column == len(source) && !e.Fatal {
fmt.Fprintln(os.Stderr, e)
following = true
continue
}
if e.Error() == "unexpected EOF" {
following = true
continue
}
}
}
following = false
source = ""
var v interface{}
if err == nil {
v, err = vm.Run(stmts, env)
}
if err != nil {
if e, ok := err.(*vm.Error); ok {
fmt.Fprintf(os.Stderr, "%d:%d %s\n", e.Pos.Line, e.Pos.Column, err)
} else if e, ok := err.(*parser.Error); ok {
fmt.Fprintf(os.Stderr, "%d:%d %s\n", e.Pos.Line, e.Pos.Column, err)
} else {
fmt.Fprintln(os.Stderr, err)
}
continue
}
fmt.Printf("%#v\n", v)
}
if err := scanner.Err(); err != nil {
if err != io.EOF {
fmt.Fprintln(os.Stderr, "ReadString error:", err)
return 12
}
}
return 0
}