/
lines.go
329 lines (317 loc) · 7.52 KB
/
lines.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
package igtest
import (
"fmt"
"github.com/Centny/Cny4go/util"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type Line struct {
T string
L string
C *Compiler
Pre *Line
Args []string
Lines []*Line
Num int
OnExeced OnExecedFunc
}
func (l *Line) addl(line *Line) {
line.Pre = l
l.Lines = append(l.Lines, line)
}
func (l *Line) Exec(ctx *Ctx, left bool) (interface{}, error) {
v, err := l.exec(ctx, left)
if l.OnExeced == nil {
return v, err
} else {
return l.OnExeced(l, left, v, err)
}
}
func (l *Line) exec(ctx *Ctx, left bool) (interface{}, error) {
switch l.T {
case "BC":
if len(l.Args) < 1 {
return nil, Err("Usage:BC $a*$b,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return ctx.BC(l.Args[0])
case "SET":
if len(l.Args) < 2 {
return nil, Err("Usage:SET path value,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return true, ctx.SET(l.Args[0], l.Args[1])
case "GET":
if len(l.Args) < 1 {
return nil, Err("Usage:GET path,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return ctx.GET(l.Args[0]), nil
case "HR":
if len(l.Args) < 2 {
return nil, Err("Usage:HR method url key=val ... ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
_, v, err := ctx.HR(l.Args...)
return v, err
case "HP":
if len(l.Args) < 1 {
return nil, Err("Usage:HP url key=val ... ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
_, v, err := ctx.HP(l.Args...)
return v, err
case "HG":
if len(l.Args) < 1 {
return nil, Err("Usage:HG url key=val ... ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
_, v, err := ctx.HG(l.Args...)
return v, err
case "EX":
if len(l.Args) < 1 {
return nil, Err("Usage:EX cmd args ... ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return ctx.EX(l.Args...)
case "P":
ctx.P(l.Args...)
return true, nil
case "R":
if len(l.Args) < 1 {
return nil, Err("Usage:R filepath ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return ctx.R(l.Args...)
case "W":
if len(l.Args) < 2 {
return nil, Err("Usage:W filepath $val ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return true, ctx.W(l.Args...)
case "D":
if len(l.Args) < 1 {
return nil, Err("Usage:D filepath ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
return true, ctx.D(l.Args...)
case "M":
if len(l.Args) < 1 {
return nil, Err("Usage:M msg ,but:%v in %v:%v", l.L, l.C.F, l.Num)
}
ctx.M(l)
return true, nil
case "SUB":
return l.Sub(ctx, left)
case "FOR":
return l.For(ctx, left)
case "EXP":
return l.Exp(ctx, left)
case "Y":
return l.Y(ctx, left)
case "N":
return l.N(ctx, left)
case "ASSIGN":
return l.Assign(ctx, left)
default:
return nil, Err("invalid command in %v:%v", l.L, l.C.F, l.Num)
}
}
func (l *Line) for_r(ctx *Ctx, left bool) (interface{}, error) {
rgs := strings.Split(l.Args[2], "~")
if len(rgs) < 2 {
return nil, Err("invalid for(%v) range:%v", l.L, l.Args[2])
}
beg, err := strconv.ParseInt(ctx.Compile(rgs[0]), 10, 32)
if err != nil {
return nil, Err("for(%v) range(%v) error:%v", l.L, l.Args[2], err.Error())
}
end, err := strconv.ParseInt(ctx.Compile(rgs[1]), 10, 32)
if err != nil {
return nil, Err("for(%v) range(%v) error:%v", l.L, l.Args[2], err.Error())
}
for i := beg; i < end; i++ {
ctx.SET(l.Args[0], i)
for _, line := range l.Lines {
_, err := line.Exec(ctx, true)
if err != nil {
return nil, err
}
}
}
return true, nil
}
func (l *Line) for_in(ctx *Ctx, left bool) (interface{}, error) {
ins := strings.Split(l.Args[2], "~")
for _, in := range ins {
ctx.SET(l.Args[0], ctx.Compile(in))
for _, line := range l.Lines {
_, err := line.Exec(ctx, true)
if err != nil {
return nil, err
}
}
}
return true, nil
}
func (l *Line) For(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 3 {
return nil, Err("Usage:FOR k R|IN 1~5|A~b~c,but:%v", l.L)
}
if l.Args[1] == "R" {
return l.for_r(ctx, left)
} else if l.Args[1] == "IN" {
return l.for_in(ctx, left)
} else {
return nil, Err("invalid for(%v) option:%v in %v:%v", l.L, l.Args[1], l.C.F, l.Num)
}
}
func (l *Line) Sub(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 1 {
return nil, Err("Usage:SUB path k=v... ,but:%v", l.L)
}
var ignore_err bool = false
nctx := NewCtx(ctx)
for _, arg := range l.Args {
if arg == "-CTX" { //setting the same context to sub
nctx = ctx
continue
}
if arg == "-cookie" { //setting cookie to sub
nctx.H = ctx.H
continue
}
if arg == "-ig" {
ignore_err = true
continue
}
kvs := strings.SplitN(arg, "=", 2)
if len(kvs) < 2 {
continue
}
nctx.Kvs[ctx.Compile(kvs[0])] = ctx.Compile(kvs[1])
}
if ctx.Mark != nil {
if nctx == ctx {
om := ctx.Mark
defer func() {
ctx.Mark = om
}()
}
nctx.Mark = ctx.Mark.Sub(l, ctx)
}
c := Compiler{}
tpath := ctx.Compile(l.Args[0])
if reg_http.MatchString(tpath) {
spath := fmt.Sprintf("/tmp/%v.ig", util.Timestamp(time.Now()))
defer os.RemoveAll(spath)
err := util.DLoad(spath, tpath)
if err != nil {
return nil, err
}
tpath = spath //set the tmp script to subscript path
} else if !filepath.IsAbs(tpath) {
tpath = filepath.Join(l.C.Cwd, tpath)
}
err := c.Load(tpath)
if err != nil {
return nil, err
}
err = c.CompileAndExec(nctx, l.OnExeced)
if ignore_err {
return true, nil
} else {
return true, err
}
}
func (l *Line) Assign(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 2 {
return nil, Err("Invalid assign:%v", l.L)
}
var v interface{} = nil
var err error = nil
if c_reg_EXP.MatchString(l.Args[1]) {
nl, _ := l.C.NewLine(l.Args[1], l.Num, l.OnExeced)
v, err = nl.Exec(ctx, false)
if err != nil {
return nil, err
}
} else if c_reg_BC2.MatchString(l.Args[1]) {
v, err = ctx.BC(l.Args[1])
if err != nil {
return nil, err
}
} else {
sv := ctx.Compile(l.Args[1])
vs := strings.SplitN(sv, "==", 2)
if len(vs) == 2 && vs[0] == vs[1] {
v = 1
} else {
v = sv
}
}
err = ctx.SET(strings.Trim(l.Args[0], " \t$()"), v)
return true, err
}
func (l *Line) Exp(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 1 {
return nil, Err("Invalid expression:%v", l.L)
}
exp := l.Args[0]
if e_reg_c.MatchString(exp) {
nl, err := l.C.NewLine(strings.Trim(exp, " \t @[]"), l.Num, l.OnExeced)
if err != nil {
return nil, err
}
return nl.Exec(ctx, left)
} else if e_reg_m.MatchString(exp) {
return ctx.BC(strings.Trim(exp, " \t@{}"))
} else if e_reg_s.MatchString(exp) {
return ctx.Join(strings.Split(strings.Trim(exp, " \t @()"), "+")...), nil
} else {
return nil, Err("invalid express(%v)", exp)
}
}
func (l *Line) Y(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 1 {
return nil, Err("Usage:Y $a,but:%v", l.L)
}
var v interface{} = nil
var err error = nil
if c_reg_BC2.MatchString(l.Args[0]) {
v, err = ctx.BC(l.Args[0])
if err != nil {
return nil, err
}
} else {
sv := ctx.Compile(l.Args[0])
vs := strings.SplitN(sv, "==", 2)
if len(vs) == 2 && vs[0] == vs[1] {
v = 1
} else {
v = sv
}
}
if !ValY(v) {
return false, Err("line(%v) expected YES but NO in %v:%v", l.L, l.C.F, l.Num)
}
return true, nil
}
func (l *Line) N(ctx *Ctx, left bool) (interface{}, error) {
if len(l.Args) < 1 {
return nil, Err("Usage:N $a,but:%v", l.L)
}
var v interface{} = nil
var err error = nil
if c_reg_BC2.MatchString(l.Args[0]) {
v, err = ctx.BC(l.Args[0])
if err != nil {
return nil, err
}
} else {
sv := ctx.Compile(l.Args[0])
vs := strings.SplitN(sv, "==", 2)
if len(vs) == 2 {
v = vs[0] == vs[1]
} else {
v = sv
}
}
if ValY(v) {
return false, Err("line(%v) expected NO but YES in %v:%v", l.L, l.C.F, l.Num)
}
return true, nil
}