forked from mattn/anko
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pako.go
240 lines (206 loc) · 4.19 KB
/
pako.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// +build !appengine
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/dgrr/pako/ast"
"github.com/dgrr/pako/core"
"github.com/dgrr/pako/env"
_ "github.com/dgrr/pako/packages"
"github.com/dgrr/pako/parser"
"github.com/dgrr/pako/vm"
)
const version = "0.2.3"
var (
flagExecute string
file string
args []string
e *env.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() {
e = env.NewEnv()
e.Define("args", args)
e.Import = doImport
core.Import(e)
}
func doImport(pkg string) (*env.Env, error) {
e := env.NewEnv()
pkg = strings.Replace(pkg, ".", string(os.PathSeparator), -1)
d, err := ioutil.ReadFile(pkg + ".pak")
if err != nil {
return nil, err
}
_, err = vm.Execute(e, nil, string(d))
if err != nil {
return nil, err
}
return e, nil
}
func printCode(file string, err error) {
var pos ast.Position
switch e := err.(type) {
case *vm.Error:
pos = e.Pos
case *parser.Error:
pos = e.Pos
default:
fmt.Fprintf(os.Stderr, e.Error())
return
}
printDefErr := func() {
fmt.Fprintf(os.Stderr, "%d:%d %s\n", pos.Line, pos.Column, err)
}
d, e := ioutil.ReadFile(file)
if e != nil {
printDefErr()
return
}
lines := strings.Split(string(d), "\n")
for i, line := range lines {
if i > pos.Line+5 {
break
}
if i > pos.Line-15 {
if i == pos.Line-1 {
fmt.Print("* ")
} else {
fmt.Print(" ")
}
n := fmt.Sprintf("%d", i)
r := 2 - len(n)
if r > 0 {
fmt.Print(strings.Repeat(" ", r))
}
fmt.Printf("%s: %s\n", n, line)
}
}
fmt.Printf("%s\n", err)
}
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 := vm.Execute(e, nil, source)
if err != nil {
if flagExecute == "" {
printCode(file, err)
} else {
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)
}
}
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(e, nil, stmts)
}
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
}