/
compiler.go
330 lines (318 loc) · 7.8 KB
/
compiler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package igtest
import (
"bufio"
"bytes"
"fmt"
"github.com/Centny/Cny4go/util"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Compiler struct {
cidx int
Lines []string
Lnum []int
Cwd string
F string
}
func (c *Compiler) addl(line string, num int) {
c.Lines = append(c.Lines, line)
c.Lnum = append(c.Lnum, num)
}
func (c *Compiler) Load(spath string) error {
c.F = spath
cwd, _ := os.Getwd()
c.Cwd = filepath.Join(cwd, filepath.Dir(spath))
return c.load(spath)
}
func (c *Compiler) load(spath string) error {
f, err := os.Open(spath)
if err != nil {
return err
}
sbys, err := ioutil.ReadAll(f)
if err != nil {
return err
}
f.Close()
return c.PreCompile(sbys)
}
//precompile will load all script and execute the INC cmd.
func (c *Compiler) PreCompile(sbys []byte) error {
c.Lines = []string{}
// sbys = b_reg_cmt.ReplaceAll(sbys, []byte(""))
// sbys = l_reg_cmt.ReplaceAll(sbys, []byte("\n"))
// sbys = Escape(sbys)
var iscmt bool = false
R := bufio.NewReader(bytes.NewReader(sbys))
for i := 1; true; i++ {
bys, err := util.ReadLine(R, 1024000, false)
if err != nil {
break
}
if b_reg_cmt_beg.Match(bys) {
iscmt = true
}
if iscmt && b_reg_cmt_end.Match(bys) {
iscmt = false
continue
}
if iscmt {
continue
}
bys = l_reg_cmt.ReplaceAll(bys, []byte(""))
line := string(Escape(bys))
line = strings.Trim(line, "\t \n")
if len(line) < 1 {
continue
}
// if c_reg_INC.MatchString(line) {
// args := empty.Split(line, -1)
// if len(args) < 2 {
// return errors.New(fmt.Sprintf(
// "INC(%v) error:%v", line, "argument is empty"))
// }
// tpath := ""
// if filepath.IsAbs(args[1]) {
// tpath = args[1]
// } else {
// tpath = filepath.Join(c.Cwd, args[1])
// }
// err := c.load(tpath)
// if err != nil {
// return errors.New(fmt.Sprintf("INC(%v) load error:%v", line, err.Error()))
// }
// continue
// }
c.addl(line, i)
}
return nil
}
func (c *Compiler) CompileAndExec(ctx *Ctx, onexeced OnExecedFunc) error {
ls, err := c.Compile(onexeced)
if err != nil {
return err
}
for _, l := range ls {
_, err := l.Exec(ctx, true)
if err != nil {
return err
}
}
return nil
}
func (c *Compiler) Compile(onexeced OnExecedFunc) ([]*Line, error) {
lcount := len(c.Lines)
clines := []*Line{}
for_l := []*Line{}
for_i := -1
for cidx := 0; cidx < lcount; cidx++ {
line := c.Lines[cidx]
num := c.Lnum[cidx]
if c_reg_FOR.MatchString(line) {
args := empty.Split(line, -1)
if len(args) < 4 {
return nil, Err("invalid FOR in %v:%v %v", c.F, num, line)
}
if c_reg_ROF.MatchString(line) { //if FOR ROF in the same line
args := empty.Split(line, -1)
if len(args) < 6 {
return nil, Err("invalid FOR in %v:%v %v", c.F, num, line)
}
l, err := c.NewLine(strings.Join(args[4:len(args)-1], " "), num, onexeced)
if err != nil {
return nil, err
}
nl := c.cline("FOR", line, num, args[1:4], onexeced)
nl.addl(l)
clines = append(clines, nl)
} else {
l := c.cline("FOR", line, num, args[1:4], onexeced)
for_l = append(for_l, l)
for_i++
if for_i == 0 {
clines = append(clines, l)
} else {
for_l[for_i-1].addl(l)
}
}
} else if c_reg_ROF.MatchString(line) {
cline := c_reg_e_ROF.ReplaceAllString(line, "")
cline = strings.Trim(cline, "\t ")
if len(cline) > 0 { //check if have command in ROF
l, err := c.NewLine(cline, num, onexeced)
if err != nil {
return nil, err
}
for_l[for_i].addl(l)
}
for_l = for_l[:len(for_l)-1]
for_i--
} else if for_i > -1 {
l, err := c.NewLine(line, num, onexeced)
if err != nil {
return nil, err
}
for_l[for_i].addl(l)
} else {
l, err := c.NewLine(line, num, onexeced)
if err != nil {
return nil, err
}
clines = append(clines, l)
}
}
return clines, nil
}
func (c *Compiler) NewLine(line string, num int, onexeced OnExecedFunc) (*Line, error) {
line = strings.Trim(line, " \t")
var T = ""
var args []string = []string{}
if c_reg_Assign1.MatchString(line) {
args = strings.SplitN(line, "=", 2)
T = "ASSIGN"
} else if c_reg_Assign2.MatchString(line) {
args = strings.SplitN(line, "=", 2)
T = "ASSIGN"
} else if c_reg_BC.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("BC error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "BC"
args = args[1:]
} else if c_reg_SET.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 3 {
return nil, Err("SET error:%v in %v:%v", "argument is less 2", c.F, num)
}
T = "SET"
args = args[1:]
} else if c_reg_GET.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("GET error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "GET"
args = args[1:]
} else if c_reg_HR.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 3 {
return nil, Err("HR error:%v in %v:%v", "argument is less 2", c.F, num)
}
T = "HR"
args = args[1:]
} else if c_reg_HG.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("HG error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "HG"
args = args[1:]
} else if c_reg_HP.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("HP error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "HP"
args = args[1:]
} else if c_reg_EX.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("EX error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "EX"
args = args[1:]
} else if c_reg_P.MatchString(line) {
args = empty.Split(line, -1)
T = "P"
args = args[1:]
} else if c_reg_SUB.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("SUB error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "SUB"
args = args[1:]
} else if c_reg_EXP.MatchString(line) {
T = "EXP"
args = []string{line}
} else if c_reg_Y.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("Y error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "Y"
args = args[1:]
} else if c_reg_N.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("N error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "N"
args = args[1:]
} else if c_reg_R.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("R error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "R"
args = args[1:]
} else if c_reg_W.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 3 {
return nil, Err("W error:%v in %v:%v", "argument is less 2", c.F, num)
}
T = "W"
args = args[1:]
} else if c_reg_D.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("D error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "D"
args = args[1:]
} else if c_reg_M.MatchString(line) {
args = empty.Split(line, -1)
if len(args) < 2 {
return nil, Err("M error:%v in %v:%v", "argument is empty", c.F, num)
}
T = "M"
args = args[1:]
} else {
return nil, Err("invalid line(%v) in %v:%v", line, c.F, num)
}
return c.cline(T, line, num, args, onexeced), nil
}
func (c *Compiler) cline(t, l string, num int, args []string, onexeced OnExecedFunc) *Line {
return &Line{
T: t,
L: l,
Num: num,
Args: args,
Lines: []*Line{},
OnExeced: onexeced,
C: c,
}
}
func (c *Compiler) ShowLine() {
for _, l := range c.Lines {
fmt.Println(l)
}
}
func NewCompiler() *Compiler {
return &Compiler{}
}
func ShowLine(ls []*Line, inner int) {
pre := ""
for i := 0; i < inner; i++ {
pre = fmt.Sprintf("%v ", pre)
}
for idx, l := range ls {
fmt.Println(fmt.Sprintf("%v%v\t\t%v%v", pre, l.T, pre, idx))
if len(l.Lines) > 0 {
ShowLine(l.Lines, inner+1)
}
}
}