-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (128 loc) · 2.56 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
package main
import (
"bufio"
"flag"
"github.com/milochristiansen/lua/ast"
"log"
"os"
"path/filepath"
"strings"
)
var input = flag.String("input", "input.lua", "Input file")
var inputpath = flag.String("inputpath", "", "Input path")
var output = flag.String("output", "output.lua", "Output file")
var opt_table_access = flag.Bool("opt_table_access", false, "Optimize table access")
var opt_table_constructor = flag.Bool("opt_table_constructor", false, "Optimize table constructor")
var has_opt bool
var gfilename string
var goptcount int
func main() {
flag.Parse()
log.SetFlags(log.Lshortfile)
if *inputpath != "" {
opt_path(*inputpath)
} else {
opt(*input, *output)
}
}
func opt_path(inputpath string) {
filepath.Walk(inputpath, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".lua") {
return nil
}
log.Println("start opt_path:", path)
opt(path, "./tmp.lua")
if goptcount > 0 {
os.Remove(path)
os.Rename("./tmp.lua", path)
} else {
os.Remove("./tmp.lua")
}
return nil
})
}
func opt(input string, output string) {
gfilename = input
goptcount = 0
read_file(input)
write_file(output)
has_opt = true
for has_opt {
has_opt = false
read_file(output)
parse_lua()
opt_lua()
write_file(output)
}
}
var gfilecontent []string
func read_file(filename string) {
var filecontent []string
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
filecontent = append(filecontent, scanner.Text())
}
gfilecontent = filecontent
}
var gblock []ast.Stmt
func parse_lua() {
source := ""
for _, line := range gfilecontent {
source += line + "\n"
}
block, err := ast.Parse(source, 1)
if err != nil {
log.Fatalf("%v %v", gfilename, err)
}
gblock = block
}
func opt_lua() {
f := lua_visitor{f: func(n ast.Node, ok *bool) {
if has_opt {
*ok = false
return
}
if n != nil {
switch n.(type) {
case *ast.FuncDecl:
func_decl := n.(*ast.FuncDecl)
opt_func(func_decl)
}
}
}}
for _, stmt := range gblock {
ast.Walk(&f, stmt)
}
}
func opt_func(func_decl *ast.FuncDecl) {
if *opt_table_constructor {
opt_func_table_constructor(func_decl)
if has_opt {
return
}
}
if *opt_table_access {
opt_func_table_access(func_decl)
if has_opt {
return
}
}
}
func write_file(filename string) {
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
for _, line := range gfilecontent {
file.WriteString(line + "\n")
}
}