-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (60 loc) · 1.04 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
)
var in string
var userIn string
var isDev bool
func parseArgs() {
devPtr := flag.Bool("dev", false, "Output logs for development.")
inPtr := flag.String("in", "", "Input string directly.")
buildPtr := flag.String("build", "", "Input file name.")
flag.Parse()
isDev = *devPtr
if len(*buildPtr) > 0 {
if len(os.Args) < 1 {
panic("invalid number of arguments")
}
b, err := ioutil.ReadFile(*buildPtr)
if err != nil {
panic(err)
}
in = string(b)
userIn = string(b)
} else {
in = *inPtr
userIn = *inPtr
}
}
func main() {
parseArgs()
// tokenize
tokens = tokenize()
if isDev {
fmt.Println(tokens)
}
// parse
prog, pkg := program()
// type
for _, gv := range prog.globals {
addType(gv)
}
for _, fn := range prog.funcs {
resetOffset()
for _, s := range fn.stmts {
addType(s)
}
fn.stackSize = stackSize(fn.locals)
}
// debug
if isDev {
fmt.Println("package name:", pkg)
printNodes(prog.funcs)
}
// codegen
codegen(prog)
return
}