forked from crossoverJie/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_function.go
86 lines (78 loc) · 2.04 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
package gscript
import (
"fmt"
"github.com/crossoverJie/gscript/parser"
"hash/fnv"
)
func (v *Visitor) println(ctx *parser.FunctionCallContext) {
if ctx.ExpressionList() != nil {
ret := v.VisitExpressionList(ctx.ExpressionList().(*parser.ExpressionListContext))
switch ret.(type) {
case *LeftValue:
ret = ret.(*LeftValue).GetValue()
case *ArrayObject:
arrayObject := ret.(*ArrayObject)
ret = arrayObject.GetIndexValue()
}
fmt.Println(ret)
fmt.Print()
} else {
fmt.Println("")
}
}
func (v *Visitor) assertEqual(ctx *parser.FunctionCallContext) {
paramValues := v.buildParamValues(ctx)
if len(paramValues) != 2 {
// todo crossoverJie 编译器报错
panic("")
}
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))
}
}
func (v *Visitor) append(ctx *parser.FunctionCallContext) []interface{} {
paramValues := v.buildParamValues(ctx)
if len(paramValues) != 2 {
// todo crossoverJie 运行时报错
panic("")
}
switch paramValues[0].(type) {
case []interface{}:
array := paramValues[0].([]interface{})
array = append(array, paramValues[1])
return array
default:
// todo crossoverJie 运行时报错
}
return nil
}
func (v *Visitor) len(ctx *parser.FunctionCallContext) int {
paramValues := v.buildParamValues(ctx)
if len(paramValues) != 1 {
// todo crossoverJie 运行时报错
panic("")
}
switch paramValues[0].(type) {
case []interface{}:
return len(paramValues[0].([]interface{}))
}
return 0
}
func (v *Visitor) hash(ctx *parser.FunctionCallContext) int {
paramValues := v.buildParamValues(ctx)
if len(paramValues) != 1 {
// todo crossoverJie 运行时报错
panic("")
}
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))
}