-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (199 loc) · 4.19 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
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
package main
import (
"os"
"io"
"fmt"
"io/ioutil"
"runtime"
logpkg "log"
)
import (
"github.com/timtadh/getopt"
)
import (
"github.com/cwru-compilers/type-check-example/frontend"
"github.com/cwru-compilers/type-check-example/checker"
"github.com/cwru-compilers/type-check-example/evaluator"
)
var log *logpkg.Logger
func init() {
log = logpkg.New(os.Stderr, "", 0)
runtime.GOMAXPROCS(4)
}
var UsageMessage string = "type-check-example -o <path> <input>+ "
var ExtendedMessage string = `
Options
-h, --help print this message
-o, output=<path> output path
-L, lex stop at lexing
-A, ast stop at AST generation
-T, typed-ast stop at type checked AST
Specs
<path>
A file system path to an existing file
`
func Usage(code int) {
fmt.Fprintln(os.Stderr, UsageMessage)
if code == 0 {
fmt.Fprintln(os.Stderr, ExtendedMessage)
code = 1
} else {
fmt.Fprintln(os.Stderr, "Try -h or --help for help")
}
os.Exit(code)
}
func write(X interface{String() string}, f io.Writer) {
f.Write([]byte(X.String()))
f.Write([]byte("\n"))
return
}
type FileTokens struct {
Filename string
Tokens []*frontend.Token
}
func (self *FileTokens) String() string {
return fmt.Sprintf("%v %v", self.Filename, self.Tokens)
}
type FilesTokens []*FileTokens
func (self FilesTokens) String() string {
return fmt.Sprintf("%v", []*FileTokens(self))
}
func lex(paths ... string) FilesTokens {
/*
defer func() {
if e := recover(); e != nil {
log.Fatal(e)
}
}()*/
var files []*FileTokens
for _, path := range paths {
log.Print("> lexing ", path)
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
program, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
scanner, err := frontend.Lexer(string(program), path)
if err != nil {
log.Fatal(err)
}
var tokens []*frontend.Token
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
if err != nil {
panic(err)
}
tokens = append(tokens, tok.(*frontend.Token))
}
files = append(files, &FileTokens{path, tokens})
}
return files
}
func parse(files FilesTokens) *frontend.Node {
/*
defer func() {
if e := recover(); e != nil {
log.Fatal(e)
}
}()*/
var A *frontend.Node = nil
for _, file := range files {
log.Println("> parsing", file.Filename)
n, err := frontend.Parse(file.Tokens)
if err != nil {
log.Fatal(err)
}
if A == nil {
A = n
} else {
for _, kid := range n.Children {
A.AddKid(kid)
}
}
}
if A == nil {
log.Fatal("You must supply input paths")
}
return A
}
func typecheck(node *frontend.Node) *frontend.Node {
log.Print("> type checking")
err := checker.Check(node)
if err != nil {
log.Fatal(err)
}
return node
}
func eval(node *frontend.Node) []interface{} {
log.Print("> evaluating")
values, err := evaluator.Evaluate(node)
if err != nil {
log.Fatal(err)
}
return values
}
func main() {
short := "ho:LAT"
long := []string{
"help",
"output=",
"lex", "ast", "typed-ast",
}
args, optargs, err := getopt.GetOpt(os.Args[1:], short, long)
if err != nil {
log.Print(os.Stderr, err)
Usage(1)
}
output := ""
stop_at := "run"
for _, oa := range optargs {
switch oa.Opt() {
case "-h", "--help": Usage(0)
case "-o", "--output":
output = oa.Arg()
case "-L", "--lex":
stop_at = "lex"
case "-A", "--ast":
stop_at = "ast"
case "-T", "--typed-ast":
stop_at = "typed-ast"
}
}
var ouf io.Writer
if output == "" {
ouf = os.Stdout
} else {
f, err := os.Create(output)
if err != nil {
log.Print(os.Stderr, err)
Usage(1)
}
defer f.Close()
ouf = f
}
if len(args) <= 0 {
log.Print("Must supply some input paths")
Usage(1)
}
L := lex(args...)
if stop_at == "lex" {
write(L, ouf)
return
}
A := parse(L)
if stop_at == "ast" {
ouf.Write([]byte(fmt.Sprintf("%v\n", A.Serialize(false))))
return
}
T := typecheck(A)
if stop_at == "typed-ast" {
ouf.Write([]byte(fmt.Sprintf("%v\n", T.Serialize(false))))
return
}
values := eval(T)
for _, value := range values {
ouf.Write([]byte(fmt.Sprintf("%v\n", value)))
}
}