@@ -15,7 +15,48 @@ import (
15
15
"time"
16
16
)
17
17
18
- func (v * Visitor ) println (ctx * parser.FunctionCallContext ) {
18
+ var internalFunction map [string ]func (v * Visitor , ctx * parser.FunctionCallContext ) interface {}
19
+
20
+ func GetInternalFunction (name string ) func (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
21
+ if internalFunction == nil {
22
+ internalFunction = map [string ]func (v * Visitor , ctx * parser.FunctionCallContext ) interface {}{
23
+ "println" : gPrintln ,
24
+ "assertEqual" : assertEqual ,
25
+ "append" : gAppend ,
26
+ "len" : gLen ,
27
+ "cap" : gCap ,
28
+ "copy" : gCopy ,
29
+ "hash" : gHash ,
30
+ "JSON" : JSON ,
31
+ "JSONGet" : JSONGet ,
32
+ "httpHandle" : httpHandle ,
33
+ "httpRun" : httpRun ,
34
+ "FprintfJSON" : fprintfJSON ,
35
+ "FprintfHTML" : fprintfHTML ,
36
+ "GetCurrentTime" : getCurrentTime ,
37
+ "Unix" : unix ,
38
+ "QueryPath" : queryPath ,
39
+ "FormValue" : formValue ,
40
+ "PostFormValue" : postFormValue ,
41
+ "GetOSArgs" : getOSArgs ,
42
+ "Command" : command ,
43
+ "WriteFile" : writeFile ,
44
+ "Remove" : remove ,
45
+ "printf" : printf ,
46
+ "sprintf" : sprintf ,
47
+ "print" : gPrint ,
48
+ "dumpAST" : dumpAST ,
49
+ "dumpSymbol" : dumpSymbol ,
50
+ "RequestBody" : requestBody ,
51
+ "Getwd" : getWd ,
52
+ "toByteArray" : toByteArray ,
53
+ "toString" : toString ,
54
+ }
55
+ }
56
+ return internalFunction [name ]
57
+ }
58
+
59
+ func gPrintln (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
19
60
if ctx .ExpressionList () != nil {
20
61
ret := v .VisitExpressionList (ctx .ExpressionList ().(* parser.ExpressionListContext ))
21
62
switch ret .(type ) {
@@ -36,6 +77,7 @@ func (v *Visitor) println(ctx *parser.FunctionCallContext) {
36
77
} else {
37
78
fmt .Println ("" )
38
79
}
80
+ return nil
39
81
}
40
82
41
83
// 打印数组对象 Person[] p
@@ -66,17 +108,18 @@ func (v *Visitor) printArray(value []interface{}) []interface{} {
66
108
return value
67
109
}
68
110
69
- func (v * Visitor ) assertEqual ( ctx * parser.FunctionCallContext ) {
111
+ func assertEqual (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
70
112
paramValues := v .buildParamValues (ctx )
71
113
// todo crossoverJie 参数是个变量,需要取左值,也可以是个数组取值 a[0]
72
114
if paramValues [0 ] != paramValues [1 ] {
73
115
line := ctx .GetStart ().GetLine ()
74
116
column := ctx .GetStart ().GetColumn ()
75
117
panic (fmt .Sprintf ("assertEqual fail [%v,%v] in line:%d and column:%d" , paramValues [0 ], paramValues [1 ], line , column ))
76
118
}
119
+ return nil
77
120
}
78
121
79
- func (v * Visitor ) append ( ctx * parser.FunctionCallContext ) [] interface {} {
122
+ func gAppend (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
80
123
paramValues , left := v .buildAppendParamValuesReturnLeft (ctx )
81
124
switch paramValues [0 ].(type ) {
82
125
case []interface {}:
@@ -96,7 +139,7 @@ func (v *Visitor) append(ctx *parser.FunctionCallContext) []interface{} {
96
139
return nil
97
140
}
98
141
99
- func (v * Visitor ) len ( ctx * parser.FunctionCallContext ) int {
142
+ func gLen (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
100
143
paramValues := v .buildParamValues (ctx )
101
144
p0 := paramValues [0 ]
102
145
switch p0 .(type ) {
@@ -115,7 +158,7 @@ func (v *Visitor) len(ctx *parser.FunctionCallContext) int {
115
158
}
116
159
return 0
117
160
}
118
- func (v * Visitor ) cap ( ctx * parser.FunctionCallContext ) int {
161
+ func gCap (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
119
162
paramValues := v .buildParamValues (ctx )
120
163
p0 := paramValues [0 ]
121
164
switch p0 .(type ) {
@@ -135,13 +178,13 @@ func (v *Visitor) cap(ctx *parser.FunctionCallContext) int {
135
178
return 0
136
179
}
137
180
138
- func (v * Visitor ) copy ( ctx * parser.FunctionCallContext ) int {
181
+ func gCopy (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
139
182
paramValues := v .buildParamValues (ctx )
140
183
p0 := paramValues [0 ]
141
184
p1 := paramValues [1 ]
142
185
return copy (p0 .([]interface {}), p1 .([]interface {}))
143
186
}
144
- func (v * Visitor ) hash ( ctx * parser.FunctionCallContext ) int {
187
+ func gHash (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
145
188
paramValues := v .buildParamValues (ctx )
146
189
return hash (paramValues [0 ])
147
190
}
@@ -157,7 +200,7 @@ func hash(v interface{}) int {
157
200
//return fmt.Sprintf("%x", hash.Sum(nil))
158
201
}
159
202
160
- func (v * Visitor ) JSON ( ctx * parser.FunctionCallContext ) string {
203
+ func JSON (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
161
204
paramValues := v .buildParamValues (ctx )
162
205
value := paramValues [0 ]
163
206
switch value .(type ) {
@@ -230,7 +273,7 @@ func (v *Visitor) classObject2Map(classObject *stack.ClassObject) map[string]int
230
273
return data
231
274
}
232
275
233
- func (v * Visitor ) JSONGet ( ctx * parser.FunctionCallContext ) interface {} {
276
+ func JSONGet (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
234
277
paramValues := v .buildParamValues (ctx )
235
278
p0 := paramValues [0 ]
236
279
p1 := paramValues [1 ]
@@ -265,7 +308,7 @@ func (v *Visitor) JSONGet(ctx *parser.FunctionCallContext) interface{} {
265
308
266
309
}
267
310
268
- func (v * Visitor ) getCurrentTime ( ctx * parser.FunctionCallContext ) string {
311
+ func getCurrentTime (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
269
312
tz , layout := v .getTzAndLayout (ctx )
270
313
271
314
location , err := time .LoadLocation (tz )
@@ -276,7 +319,7 @@ func (v *Visitor) getCurrentTime(ctx *parser.FunctionCallContext) string {
276
319
return local .Format (layout )
277
320
278
321
}
279
- func (v * Visitor ) unix ( ctx * parser.FunctionCallContext ) int64 {
322
+ func unix (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
280
323
paramValues := v .buildParamValues (ctx )
281
324
p0 := paramValues [0 ]
282
325
var (
@@ -313,12 +356,12 @@ func (v *Visitor) getTzAndLayout(ctx *parser.FunctionCallContext) (string, strin
313
356
return tz , layout
314
357
}
315
358
316
- func (v * Visitor ) getOSArgs ( ctx * parser.FunctionCallContext ) [] string {
359
+ func getOSArgs (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
317
360
return Args
318
361
}
319
362
320
363
// 执行 command
321
- func (v * Visitor ) command ( ctx * parser.FunctionCallContext ) string {
364
+ func command (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
322
365
command , variableParams := v .getPrintfParams (ctx )
323
366
var args []string
324
367
if len (variableParams ) > 0 {
@@ -339,7 +382,7 @@ func (v *Visitor) command(ctx *parser.FunctionCallContext) string {
339
382
return string (stdoutStderr )
340
383
}
341
384
342
- func (v * Visitor ) writeFile ( ctx * parser.FunctionCallContext ) {
385
+ func writeFile (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
343
386
paramValues := v .buildParamValues (ctx )
344
387
p0 := paramValues [0 ]
345
388
p1 := paramValues [1 ]
@@ -351,15 +394,17 @@ func (v *Visitor) writeFile(ctx *parser.FunctionCallContext) {
351
394
if err != nil {
352
395
log .RuntimePanic (ctx , fmt .Sprintf ("system.writeFile function error occurred,error:%s" , err ))
353
396
}
397
+ return nil
354
398
}
355
- func (v * Visitor ) remove ( ctx * parser.FunctionCallContext ) {
399
+ func remove (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
356
400
paramValues := v .buildParamValues (ctx )
357
401
p0 := paramValues [0 ]
358
402
fileName := p0 .(string )
359
403
err := os .Remove (fileName )
360
404
if err != nil {
361
405
log .RuntimePanic (ctx , fmt .Sprintf ("system.remove function error occurred,error:%s" , err ))
362
406
}
407
+ return nil
363
408
}
364
409
365
410
func (v * Visitor ) buildAppendParamValuesReturnLeft (ctx * parser.FunctionCallContext ) ([]interface {}, * LeftValue ) {
@@ -385,10 +430,11 @@ func (v *Visitor) buildAppendParamValuesReturnLeft(ctx *parser.FunctionCallConte
385
430
return ret , left
386
431
}
387
432
388
- func (v * Visitor ) printf ( ctx * parser.FunctionCallContext ) {
433
+ func printf (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
389
434
format , variableParams := v .getPrintfParams (ctx )
390
435
391
436
fmt .Printf (format , variableParams ... )
437
+ return nil
392
438
}
393
439
394
440
// 获取格式化字符串参数
@@ -411,19 +457,20 @@ func (v *Visitor) getPrintfParams(ctx *parser.FunctionCallContext) (string, []in
411
457
return format , variableParams
412
458
}
413
459
414
- func (v * Visitor ) sprintf ( ctx * parser.FunctionCallContext ) string {
460
+ func sprintf (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
415
461
format , variableParams := v .getPrintfParams (ctx )
416
462
return fmt .Sprintf (format , variableParams ... )
417
463
}
418
- func (v * Visitor ) print ( ctx * parser.FunctionCallContext ) {
464
+ func gPrint (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
419
465
paramValues := v .buildParamValues (ctx )
420
466
fmt .Print (paramValues ... )
467
+ return nil
421
468
}
422
- func (v * Visitor ) dumpAST ( ctx * parser.FunctionCallContext ) string {
469
+ func dumpAST (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
423
470
code := v .getCode (ctx )
424
471
return NewCompiler ().GetCompileInfo (code , true )
425
472
}
426
- func (v * Visitor ) dumpSymbol ( ctx * parser.FunctionCallContext ) string {
473
+ func dumpSymbol (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
427
474
code := v .getCode (ctx )
428
475
return NewCompiler ().GetCompileInfo (code , false )
429
476
}
@@ -439,12 +486,12 @@ func (v *Visitor) getCode(ctx *parser.FunctionCallContext) string {
439
486
return format
440
487
}
441
488
442
- func (v * Visitor ) toByteArray ( ctx * parser.FunctionCallContext ) [] byte {
489
+ func toByteArray (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
443
490
code := v .getCode (ctx )
444
491
return []byte (code )
445
492
}
446
493
447
- func (v * Visitor ) toString ( ctx * parser.FunctionCallContext ) string {
494
+ func toString (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
448
495
paramValues := v .buildParamValues (ctx )
449
496
p0 := paramValues [0 ]
450
497
switch p0 .(type ) {
@@ -465,7 +512,7 @@ func (v *Visitor) toString(ctx *parser.FunctionCallContext) string {
465
512
return ""
466
513
}
467
514
468
- func (v * Visitor ) getWd ( ctx * parser.FunctionCallContext ) string {
515
+ func getWd (v * Visitor , ctx * parser.FunctionCallContext ) interface {} {
469
516
str , err := os .Getwd ()
470
517
if err != nil {
471
518
log .RuntimePanic (ctx , fmt .Sprintf ("system.getwd function error occurred,error:%s" , err ))
0 commit comments