Skip to content

Commit e6d7bc5

Browse files
committed
semantic_resolver
1 parent 20bb94f commit e6d7bc5

File tree

8 files changed

+165
-103
lines changed

8 files changed

+165
-103
lines changed

compiler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func (c *Compiler) compile(script string) interface{} {
7070
// 消解变量、函数的引用
7171
walker.Walk(resolver.NewRefResolver(at), tree)
7272

73+
// 语义检查
74+
walker.Walk(NewSemanticResolver(at), tree)
75+
7376
// 闭包分析
7477
resolver.NewClosureResolver(at).Analyze()
7578

internal/internal.go

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

internal/internal.gs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class Map{
262262
}
263263

264264
any get(any key){
265-
if(key==""){
265+
if(key == ""){
266266
return;
267267
}
268268
int hashcode = hash(key);
@@ -280,6 +280,7 @@ class Map{
280280
}else {
281281
return nil;
282282
}
283+
return nil;
283284
}
284285

285286
int getSize(){

internal_function.go

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,48 @@ import (
1515
"time"
1616
)
1717

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{} {
1960
if ctx.ExpressionList() != nil {
2061
ret := v.VisitExpressionList(ctx.ExpressionList().(*parser.ExpressionListContext))
2162
switch ret.(type) {
@@ -36,6 +77,7 @@ func (v *Visitor) println(ctx *parser.FunctionCallContext) {
3677
} else {
3778
fmt.Println("")
3879
}
80+
return nil
3981
}
4082

4183
// 打印数组对象 Person[] p
@@ -66,17 +108,18 @@ func (v *Visitor) printArray(value []interface{}) []interface{} {
66108
return value
67109
}
68110

69-
func (v *Visitor) assertEqual(ctx *parser.FunctionCallContext) {
111+
func assertEqual(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
70112
paramValues := v.buildParamValues(ctx)
71113
// todo crossoverJie 参数是个变量,需要取左值,也可以是个数组取值 a[0]
72114
if paramValues[0] != paramValues[1] {
73115
line := ctx.GetStart().GetLine()
74116
column := ctx.GetStart().GetColumn()
75117
panic(fmt.Sprintf("assertEqual fail [%v,%v] in line:%d and column:%d", paramValues[0], paramValues[1], line, column))
76118
}
119+
return nil
77120
}
78121

79-
func (v *Visitor) append(ctx *parser.FunctionCallContext) []interface{} {
122+
func gAppend(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
80123
paramValues, left := v.buildAppendParamValuesReturnLeft(ctx)
81124
switch paramValues[0].(type) {
82125
case []interface{}:
@@ -96,7 +139,7 @@ func (v *Visitor) append(ctx *parser.FunctionCallContext) []interface{} {
96139
return nil
97140
}
98141

99-
func (v *Visitor) len(ctx *parser.FunctionCallContext) int {
142+
func gLen(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
100143
paramValues := v.buildParamValues(ctx)
101144
p0 := paramValues[0]
102145
switch p0.(type) {
@@ -115,7 +158,7 @@ func (v *Visitor) len(ctx *parser.FunctionCallContext) int {
115158
}
116159
return 0
117160
}
118-
func (v *Visitor) cap(ctx *parser.FunctionCallContext) int {
161+
func gCap(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
119162
paramValues := v.buildParamValues(ctx)
120163
p0 := paramValues[0]
121164
switch p0.(type) {
@@ -135,13 +178,13 @@ func (v *Visitor) cap(ctx *parser.FunctionCallContext) int {
135178
return 0
136179
}
137180

138-
func (v *Visitor) copy(ctx *parser.FunctionCallContext) int {
181+
func gCopy(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
139182
paramValues := v.buildParamValues(ctx)
140183
p0 := paramValues[0]
141184
p1 := paramValues[1]
142185
return copy(p0.([]interface{}), p1.([]interface{}))
143186
}
144-
func (v *Visitor) hash(ctx *parser.FunctionCallContext) int {
187+
func gHash(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
145188
paramValues := v.buildParamValues(ctx)
146189
return hash(paramValues[0])
147190
}
@@ -157,7 +200,7 @@ func hash(v interface{}) int {
157200
//return fmt.Sprintf("%x", hash.Sum(nil))
158201
}
159202

160-
func (v *Visitor) JSON(ctx *parser.FunctionCallContext) string {
203+
func JSON(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
161204
paramValues := v.buildParamValues(ctx)
162205
value := paramValues[0]
163206
switch value.(type) {
@@ -230,7 +273,7 @@ func (v *Visitor) classObject2Map(classObject *stack.ClassObject) map[string]int
230273
return data
231274
}
232275

233-
func (v *Visitor) JSONGet(ctx *parser.FunctionCallContext) interface{} {
276+
func JSONGet(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
234277
paramValues := v.buildParamValues(ctx)
235278
p0 := paramValues[0]
236279
p1 := paramValues[1]
@@ -265,7 +308,7 @@ func (v *Visitor) JSONGet(ctx *parser.FunctionCallContext) interface{} {
265308

266309
}
267310

268-
func (v *Visitor) getCurrentTime(ctx *parser.FunctionCallContext) string {
311+
func getCurrentTime(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
269312
tz, layout := v.getTzAndLayout(ctx)
270313

271314
location, err := time.LoadLocation(tz)
@@ -276,7 +319,7 @@ func (v *Visitor) getCurrentTime(ctx *parser.FunctionCallContext) string {
276319
return local.Format(layout)
277320

278321
}
279-
func (v *Visitor) unix(ctx *parser.FunctionCallContext) int64 {
322+
func unix(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
280323
paramValues := v.buildParamValues(ctx)
281324
p0 := paramValues[0]
282325
var (
@@ -313,12 +356,12 @@ func (v *Visitor) getTzAndLayout(ctx *parser.FunctionCallContext) (string, strin
313356
return tz, layout
314357
}
315358

316-
func (v *Visitor) getOSArgs(ctx *parser.FunctionCallContext) []string {
359+
func getOSArgs(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
317360
return Args
318361
}
319362

320363
// 执行 command
321-
func (v *Visitor) command(ctx *parser.FunctionCallContext) string {
364+
func command(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
322365
command, variableParams := v.getPrintfParams(ctx)
323366
var args []string
324367
if len(variableParams) > 0 {
@@ -339,7 +382,7 @@ func (v *Visitor) command(ctx *parser.FunctionCallContext) string {
339382
return string(stdoutStderr)
340383
}
341384

342-
func (v *Visitor) writeFile(ctx *parser.FunctionCallContext) {
385+
func writeFile(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
343386
paramValues := v.buildParamValues(ctx)
344387
p0 := paramValues[0]
345388
p1 := paramValues[1]
@@ -351,15 +394,17 @@ func (v *Visitor) writeFile(ctx *parser.FunctionCallContext) {
351394
if err != nil {
352395
log.RuntimePanic(ctx, fmt.Sprintf("system.writeFile function error occurred,error:%s", err))
353396
}
397+
return nil
354398
}
355-
func (v *Visitor) remove(ctx *parser.FunctionCallContext) {
399+
func remove(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
356400
paramValues := v.buildParamValues(ctx)
357401
p0 := paramValues[0]
358402
fileName := p0.(string)
359403
err := os.Remove(fileName)
360404
if err != nil {
361405
log.RuntimePanic(ctx, fmt.Sprintf("system.remove function error occurred,error:%s", err))
362406
}
407+
return nil
363408
}
364409

365410
func (v *Visitor) buildAppendParamValuesReturnLeft(ctx *parser.FunctionCallContext) ([]interface{}, *LeftValue) {
@@ -385,10 +430,11 @@ func (v *Visitor) buildAppendParamValuesReturnLeft(ctx *parser.FunctionCallConte
385430
return ret, left
386431
}
387432

388-
func (v *Visitor) printf(ctx *parser.FunctionCallContext) {
433+
func printf(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
389434
format, variableParams := v.getPrintfParams(ctx)
390435

391436
fmt.Printf(format, variableParams...)
437+
return nil
392438
}
393439

394440
// 获取格式化字符串参数
@@ -411,19 +457,20 @@ func (v *Visitor) getPrintfParams(ctx *parser.FunctionCallContext) (string, []in
411457
return format, variableParams
412458
}
413459

414-
func (v *Visitor) sprintf(ctx *parser.FunctionCallContext) string {
460+
func sprintf(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
415461
format, variableParams := v.getPrintfParams(ctx)
416462
return fmt.Sprintf(format, variableParams...)
417463
}
418-
func (v *Visitor) print(ctx *parser.FunctionCallContext) {
464+
func gPrint(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
419465
paramValues := v.buildParamValues(ctx)
420466
fmt.Print(paramValues...)
467+
return nil
421468
}
422-
func (v *Visitor) dumpAST(ctx *parser.FunctionCallContext) string {
469+
func dumpAST(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
423470
code := v.getCode(ctx)
424471
return NewCompiler().GetCompileInfo(code, true)
425472
}
426-
func (v *Visitor) dumpSymbol(ctx *parser.FunctionCallContext) string {
473+
func dumpSymbol(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
427474
code := v.getCode(ctx)
428475
return NewCompiler().GetCompileInfo(code, false)
429476
}
@@ -439,12 +486,12 @@ func (v *Visitor) getCode(ctx *parser.FunctionCallContext) string {
439486
return format
440487
}
441488

442-
func (v *Visitor) toByteArray(ctx *parser.FunctionCallContext) []byte {
489+
func toByteArray(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
443490
code := v.getCode(ctx)
444491
return []byte(code)
445492
}
446493

447-
func (v *Visitor) toString(ctx *parser.FunctionCallContext) string {
494+
func toString(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
448495
paramValues := v.buildParamValues(ctx)
449496
p0 := paramValues[0]
450497
switch p0.(type) {
@@ -465,7 +512,7 @@ func (v *Visitor) toString(ctx *parser.FunctionCallContext) string {
465512
return ""
466513
}
467514

468-
func (v *Visitor) getWd(ctx *parser.FunctionCallContext) string {
515+
func getWd(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
469516
str, err := os.Getwd()
470517
if err != nil {
471518
log.RuntimePanic(ctx, fmt.Sprintf("system.getwd function error occurred,error:%s", err))

internal_http.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type httpTool struct {
2020
// path 与 http 工具的映射关系
2121
var path2HttpTool map[string]*httpTool
2222

23-
func (v *Visitor) httpHandle(ctx *parser.FunctionCallContext) interface{} {
23+
func httpHandle(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
2424
paramValues := v.buildParamValues(ctx)
2525
p := paramValues[0]
2626
p0 := paramValues[1]
@@ -77,7 +77,7 @@ func (v *Visitor) httpHandle(ctx *parser.FunctionCallContext) interface{} {
7777

7878
}
7979

80-
func (v *Visitor) httpRun(ctx *parser.FunctionCallContext) interface{} {
80+
func httpRun(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
8181
paramValues := v.buildParamValues(ctx)
8282
p0 := paramValues[0]
8383
var addr string
@@ -100,7 +100,7 @@ func (v *Visitor) httpRun(ctx *parser.FunctionCallContext) interface{} {
100100
return nil
101101
}
102102

103-
func (v *Visitor) fprintfJSON(ctx *parser.FunctionCallContext) {
103+
func fprintfJSON(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
104104
paramValues := v.buildParamValues(ctx)
105105
code, path, json := v.getCodePathValue(paramValues)
106106
tool, ok := path2HttpTool[path]
@@ -112,10 +112,10 @@ func (v *Visitor) fprintfJSON(ctx *parser.FunctionCallContext) {
112112
tool.w.WriteHeader(code)
113113
}
114114
fmt.Fprintf(tool.w, json)
115-
115+
return nil
116116
}
117117

118-
func (v *Visitor) fprintfHTML(ctx *parser.FunctionCallContext) {
118+
func fprintfHTML(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
119119
paramValues := v.buildParamValues(ctx)
120120
code, path, html := v.getCodePathValue(paramValues)
121121
tool, ok := path2HttpTool[path]
@@ -127,9 +127,10 @@ func (v *Visitor) fprintfHTML(ctx *parser.FunctionCallContext) {
127127
tool.w.WriteHeader(code)
128128
}
129129
fmt.Fprintf(tool.w, html)
130+
return nil
130131
}
131132

132-
func (v *Visitor) requestBody(ctx *parser.FunctionCallContext) string {
133+
func requestBody(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
133134
paramValues := v.buildParamValues(ctx)
134135
p0 := paramValues[0]
135136
var path string
@@ -176,7 +177,7 @@ func (v *Visitor) getCodePathValue(paramValues []interface{}) (int, string, stri
176177
return code, path, value
177178
}
178179

179-
func (v *Visitor) queryPath(ctx *parser.FunctionCallContext) string {
180+
func queryPath(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
180181
paramValues := v.buildParamValues(ctx)
181182
p0 := paramValues[0]
182183
var path string
@@ -193,7 +194,7 @@ func (v *Visitor) queryPath(ctx *parser.FunctionCallContext) string {
193194

194195
}
195196

196-
func (v *Visitor) formValue(ctx *parser.FunctionCallContext) string {
197+
func formValue(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
197198
paramValues := v.buildParamValues(ctx)
198199
p0 := paramValues[0]
199200
p1 := paramValues[1]
@@ -219,7 +220,7 @@ func (v *Visitor) formValue(ctx *parser.FunctionCallContext) string {
219220
return tool.r.FormValue(key)
220221

221222
}
222-
func (v *Visitor) postFormValue(ctx *parser.FunctionCallContext) string {
223+
func postFormValue(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
223224
paramValues := v.buildParamValues(ctx)
224225
p0 := paramValues[0]
225226
p1 := paramValues[1]

0 commit comments

Comments
 (0)