-
Notifications
You must be signed in to change notification settings - Fork 24
/
internal_function.go
521 lines (480 loc) · 13.4 KB
/
internal_function.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package gscript
import (
"encoding/json"
"fmt"
"github.com/crossoverJie/gscript/log"
"github.com/crossoverJie/gscript/parser"
"github.com/crossoverJie/gscript/stack"
"github.com/crossoverJie/xjson"
"hash/fnv"
"io/fs"
"os"
"os/exec"
"reflect"
"time"
)
var internalFunction map[string]func(v *Visitor, ctx *parser.FunctionCallContext) interface{}
func GetInternalFunction(name string) func(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
if internalFunction == nil {
internalFunction = map[string]func(v *Visitor, ctx *parser.FunctionCallContext) interface{}{
"println": gPrintln,
"assertEqual": assertEqual,
"append": gAppend,
"len": gLen,
"cap": gCap,
"copy": gCopy,
"hash": gHash,
"JSON": JSON,
"JSONGet": JSONGet,
"httpHandle": httpHandle,
"httpRun": httpRun,
"FprintfJSON": fprintfJSON,
"FprintfHTML": fprintfHTML,
"GetCurrentTime": getCurrentTime,
"Unix": unix,
"QueryPath": queryPath,
"FormValue": formValue,
"PostFormValue": postFormValue,
"GetOSArgs": getOSArgs,
"Command": command,
"WriteFile": writeFile,
"Remove": remove,
"printf": printf,
"sprintf": sprintf,
"print": gPrint,
"dumpAST": dumpAST,
"dumpSymbol": dumpSymbol,
"RequestBody": requestBody,
"Getwd": getWd,
"toByteArray": toByteArray,
"toString": toString,
}
}
return internalFunction[name]
}
func gPrintln(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
if ctx.ExpressionList() != nil {
ret := v.VisitExpressionList(ctx.ExpressionList().(*parser.ExpressionListContext))
switch ret.(type) {
case *LeftValue:
ret = ret.(*LeftValue).GetValue()
switch ret.(type) {
case []interface{}:
// 对象数组 Person[] list = {p1,p2}; println(list);
i := ret.([]interface{})
ret = v.printArray(i)
}
case *ArrayObject:
arrayObject := ret.(*ArrayObject)
ret = arrayObject.GetIndexValue()
}
fmt.Println(ret)
fmt.Print()
} else {
fmt.Println("")
}
return nil
}
// 打印数组对象 Person[] p
func (v *Visitor) printArray(value []interface{}) []interface{} {
if len(value) == 0 {
return nil
}
var retList []interface{}
for _, val := range value {
switch val.(type) {
case *LeftValue:
leftValue := val.(*LeftValue).GetValue()
switch leftValue.(type) {
case *stack.ClassObject:
classObject := leftValue.(*stack.ClassObject)
var data []interface{}
for _, val := range classObject.AllField() {
data = append(data, val)
}
// [[1 a] [1 b]] Person[] list = {p1,p2};
retList = append(retList, data)
}
}
}
if len(retList) > 0 {
return retList
}
return value
}
func assertEqual(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
// todo crossoverJie 参数是个变量,需要取左值,也可以是个数组取值 a[0]
if paramValues[0] != paramValues[1] {
line := ctx.GetStart().GetLine()
column := ctx.GetStart().GetColumn()
panic(fmt.Sprintf("assertEqual fail [%v,%v] in line:%d and column:%d", paramValues[0], paramValues[1], line, column))
}
return nil
}
func gAppend(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues, left := v.buildAppendParamValuesReturnLeft(ctx)
switch paramValues[0].(type) {
case []interface{}:
array := paramValues[0].([]interface{})
array = append(array, paramValues[1])
left.SetValue(array)
return array
case []byte:
array := paramValues[0].([]byte)
value := paramValues[1].([]byte)
array = append(array, value...)
left.SetValue(array)
default:
log.RuntimePanic(ctx, fmt.Sprintf("first argument to append must be array"))
}
return nil
}
func gLen(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
switch p0.(type) {
case []interface{}:
return len(p0.([]interface{}))
case []string:
return len(p0.([]string))
case []float64:
return len(p0.([]float64))
case []bool:
return len(p0.([]bool))
case []int:
return len(p0.([]int))
case []byte:
return len(p0.([]byte))
}
return 0
}
func gCap(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
switch p0.(type) {
case []interface{}:
return cap(p0.([]interface{}))
case []string:
return cap(p0.([]string))
case []float64:
return cap(p0.([]float64))
case []bool:
return cap(p0.([]bool))
case []int:
return cap(p0.([]int))
case []byte:
return cap(p0.([]byte))
}
return 0
}
func gCopy(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
return copy(p0.([]interface{}), p1.([]interface{}))
}
func gHash(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
return hash(paramValues[0])
}
func hash(v interface{}) int {
//hash := sha256.New()
//hash.Write([]byte(fmt.Sprintf("%v", v)))
h := fnv.New32a()
h.Write([]byte(fmt.Sprintf("%v", v)))
return int(h.Sum32())
//return fmt.Sprintf("%x", hash.Sum(nil))
}
func JSON(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
value := paramValues[0]
switch value.(type) {
case *stack.ClassObject:
classObject := value.(*stack.ClassObject)
data := v.classObject2Map(classObject)
marshal, err := json.Marshal(data)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
}
return string(marshal)
case []interface{}:
dataList := value.([]interface{})
var dataClass []interface{}
for _, data := range dataList {
switch data.(type) {
case *LeftValue:
// Person[] list = {p1,p2}; string j = JSON(list);
leftValue := data.(*LeftValue)
getValue := leftValue.GetValue()
classObject, ok := getValue.(*stack.ClassObject)
if ok {
object2Map := v.classObject2Map(classObject)
dataClass = append(dataClass, object2Map)
}
}
}
if dataClass != nil {
marshal, err := json.Marshal(dataClass)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
}
return string(marshal)
}
// int[] a = {1,2,3}; json = JSON(a)
marshal, err := json.Marshal(dataList)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
}
// {1,2,3}
return string(marshal)
default:
marshal, err := json.Marshal(value)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
}
return string(marshal)
}
return ""
}
func (v *Visitor) classObject2Map(classObject *stack.ClassObject) map[string]interface{} {
data := make(map[string]interface{})
for variable, val := range classObject.AllField() {
switch val.(type) {
case *stack.ClassObject:
// class 包含 class 的情况
classObject := val.(*stack.ClassObject)
classObject2Map := v.classObject2Map(classObject)
data[variable.GetName()] = classObject2Map
default:
data[variable.GetName()] = val
}
}
return data
}
func JSONGet(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
var (
v1, v2 string
)
switch p0.(type) {
case *LeftValue:
value := p0.(*LeftValue).GetValue()
switch value.(type) {
case string:
v1 = fmt.Sprintf("%s", value)
default:
log.RuntimePanic(ctx, fmt.Sprintf("JSONGet JSON parameter are not a string: %v", reflect.TypeOf(value)))
}
case string:
v1 = fmt.Sprintf("%s", p0)
}
switch p1.(type) {
case *LeftValue:
value := p1.(*LeftValue).GetValue()
switch value.(type) {
case string:
v2 = fmt.Sprintf("%s", value)
default:
log.RuntimePanic(ctx, fmt.Sprintf("JSONGet path parameter are not a string: %v", reflect.TypeOf(value)))
}
case string:
v2 = fmt.Sprintf("%s", p1)
}
return xjson.Get(v1, v2).Raw()
}
func getCurrentTime(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
tz, layout := v.getTzAndLayout(ctx)
location, err := time.LoadLocation(tz)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("getCurrentTime function error occurred,error:%s", err))
}
local := time.Now().In(location)
return local.Format(layout)
}
func unix(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var (
tz string
)
switch p0.(type) {
case string:
tz = fmt.Sprintf("%s", p0)
}
location, err := time.LoadLocation(tz)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("unix function error occurred,error:%s", err))
}
local := time.Now().In(location)
return local.Unix()
}
func (v *Visitor) getTzAndLayout(ctx *parser.FunctionCallContext) (string, string) {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
var (
tz, layout string
)
switch p0.(type) {
case string:
tz = fmt.Sprintf("%s", p0)
}
switch p1.(type) {
case string:
layout = fmt.Sprintf("%s", p1)
}
return tz, layout
}
func getOSArgs(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
return Args
}
// 执行 command
func command(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
command, variableParams := v.getPrintfParams(ctx)
var args []string
if len(variableParams) > 0 {
params := variableParams[0]
strings, ok := params.([]interface{})
if ok {
for _, s := range strings {
args = append(args, s.(string))
}
}
}
cmd := exec.Command(command, args...)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("system.command function error occurred,error:%s, msg:%s", err, stdoutStderr))
}
return string(stdoutStderr)
}
func writeFile(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
p1 := paramValues[1]
p2 := paramValues[2]
fileName := p0.(string)
value := p1.(string)
perm := p2.(int)
err := os.WriteFile(fileName, []byte(value), fs.FileMode(perm))
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("system.writeFile function error occurred,error:%s", err))
}
return nil
}
func remove(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
fileName := p0.(string)
err := os.Remove(fileName)
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("system.remove function error occurred,error:%s", err))
}
return nil
}
func (v *Visitor) buildAppendParamValuesReturnLeft(ctx *parser.FunctionCallContext) ([]interface{}, *LeftValue) {
ret := make([]interface{}, 0)
if ctx.ExpressionList() == nil {
return ret, nil
}
var left *LeftValue
for _, context := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpr() {
value := v.Visit(context)
switch value.(type) {
case *LeftValue:
leftValue := value.(*LeftValue)
if left == nil {
// 只需要赋值一次左值即可,避免 append(buf, temp); temp 也是变量时,第二次遍历时会把 temp 赋值给 left
left = leftValue
}
ret = append(ret, leftValue.GetValue())
default:
ret = append(ret, value)
}
}
return ret, left
}
func printf(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
format, variableParams := v.getPrintfParams(ctx)
fmt.Printf(format, variableParams...)
return nil
}
// 获取格式化字符串参数
func (v *Visitor) getPrintfParams(ctx *parser.FunctionCallContext) (string, []interface{}) {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var format string
switch p0.(type) {
case string:
format = p0.(string)
case *LeftValue:
value := p0.(*LeftValue).GetValue()
switch value.(type) {
case string:
format = value.(string)
}
}
variableParams := paramValues[1:]
return format, variableParams
}
func sprintf(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
format, variableParams := v.getPrintfParams(ctx)
return fmt.Sprintf(format, variableParams...)
}
func gPrint(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
fmt.Print(paramValues...)
return nil
}
func dumpAST(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
code := v.getCode(ctx)
return NewCompiler().GetCompileInfo(code, true)
}
func dumpSymbol(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
code := v.getCode(ctx)
return NewCompiler().GetCompileInfo(code, false)
}
func (v *Visitor) getCode(ctx *parser.FunctionCallContext) string {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
var format string
switch p0.(type) {
case string:
format = p0.(string)
}
return format
}
func toByteArray(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
code := v.getCode(ctx)
return []byte(code)
}
func toString(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
paramValues := v.buildParamValues(ctx)
p0 := paramValues[0]
switch p0.(type) {
case []byte:
b := p0.([]byte)
return string(b)
case []interface{}:
b, ok := p0.([]interface{})
if !ok {
return ""
}
var byteArray []byte
for _, a := range b {
byteArray = append(byteArray, a.([]byte)...)
}
return string(byteArray)
}
return ""
}
func getWd(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
str, err := os.Getwd()
if err != nil {
log.RuntimePanic(ctx, fmt.Sprintf("system.getwd function error occurred,error:%s", err))
}
return str
}