-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.go
71 lines (58 loc) · 1.16 KB
/
interpreter.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
package whitespace_go
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
)
var (
versionOpt = flag.Bool("v", false, "display version information")
)
const version = "v0.0.1"
type Interpreter struct {
args []string
stderr io.Writer
parser Parser
executor Executor
}
func New() *Interpreter {
return &Interpreter{
args: os.Args,
stderr: os.Stderr,
}
}
func (i *Interpreter) Run() int {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n ws [FILE]\n", os.Args[0])
flag.PrintDefaults()
}
if len(i.args) < 2 {
flag.Usage()
return 1
}
flag.Parse()
if *versionOpt {
fmt.Printf("ws version %s\n", version)
return 1
}
filename := i.args[1]
bytes, errReadFile := ioutil.ReadFile(filename)
if errReadFile != nil {
fmt.Fprintf(i.stderr, "%s can not read\n", filename)
return 1
}
i.parser = NewParser(filename, string(bytes))
errParse := i.parser.ParseAll()
if errParse != nil {
fmt.Fprintln(i.stderr, errParse.Error())
return 1
}
i.executor = Executor{instructions: i.parser.Instructions}
errRuntime := i.executor.Run()
if errRuntime != nil {
fmt.Fprintln(i.stderr, errRuntime.Error())
return 1
}
return 0
}