-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (94 loc) · 2.15 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var version = "0.1.2"
var (
trace bool
bigint bool
stat bool
check bool
ver bool
pluginDir string
)
func init() {
flag.BoolVar(&trace, "trace", false, "trace function calls")
flag.BoolVar(&trace, "t", false, "trace function calls (shorthand)")
flag.BoolVar(&bigint, "big", false, "use big math")
flag.BoolVar(&bigint, "b", false, "use big math (shorthand)")
flag.BoolVar(&stat, "stat", false, "dump statistics after program exit")
flag.BoolVar(&stat, "s", false, "dump statistics after program exit (shorthand)")
flag.BoolVar(&check, "check", false, "make parsing and typechecking only")
flag.BoolVar(&check, "c", false, "make parsing and typechecking only (shorthand)")
flag.StringVar(&pluginDir, "plugin-dir", "", "plugins directory")
flag.StringVar(&pluginDir, "p", "", "plugins directory (shorthand)")
flag.BoolVar(&ver, "version", false, "show version")
flag.BoolVar(&ver, "v", false, "show version")
}
func doMain() int {
flag.Parse()
if ver {
showVersion()
return 0
}
if !trace {
log.SetOutput(ioutil.Discard)
}
in := NewInterpreter(os.Stdout)
in.UseBigInt(bigint)
in.PluginDir = pluginDir
in.IncludeDirs = []string{in.PluginDir}
var file string
var input io.Reader
if len(flag.Args()) >= 1 {
fname := flag.Arg(0)
f, err := os.Open(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
defer f.Close()
input = f
file, err = filepath.Abs(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot determine absolute path for %q: %e", file, err)
file = fname
}
} else {
input = os.Stdin
file = "__stdin__"
}
if err := in.Parse(file, input); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
if errs := in.Check(); len(errs) > 0 {
for _, err := range errs {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
return 1
}
if check {
return 0
}
if err := in.Run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
if stat {
in.Stat()
}
return 0
}
func main() {
os.Exit(doMain())
}
func showVersion() {
fmt.Printf("SPIL version %v\n", version)
}