-
Notifications
You must be signed in to change notification settings - Fork 0
/
wspacego.go
62 lines (56 loc) · 1.16 KB
/
wspacego.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
package main
import (
"flag"
"fmt"
"github.com/135yshr/wspacego/lib"
"io/ioutil"
"os"
"strings"
)
var (
showHelp bool
)
func main() {
flag.Parse()
if showHelp {
flag.Usage()
os.Exit(0)
}
if flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "missing arguments\n")
flag.Usage()
os.Exit(-1)
}
filename := flag.Arg(1)
original, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(err)
flag.Usage()
os.Exit(-1)
}
interpreter := lib.NewInterpreter(original)
mode := flag.Arg(0)
switch strings.ToLower(mode) {
case "run":
interpreter.Run()
case "disasm":
interpreter.PrintCode()
case "char":
interpreter.PrintChar()
default:
fmt.Fprintf(os.Stderr, "not support subcommand\n")
flag.Usage()
os.Exit(-1)
}
os.Exit(0)
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: [run|disasm|char] <whitespace file>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\trun run the program\n")
fmt.Fprintf(os.Stderr, "\tdisasm disassemble the program\n")
fmt.Fprintf(os.Stderr, "\tchar convert the program (space -> S, Tab -> T)\n")
flag.PrintDefaults()
}
flag.BoolVar(&showHelp, "h", false, "display this help and exit")
}