-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
122 lines (113 loc) · 2.53 KB
/
token.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
package gorules
import (
"errors"
"go/token"
"reflect"
)
func operate(x, y interface{}, tk token.Token) (interface{}, error) {
xv := reflect.ValueOf(x)
yv := reflect.ValueOf(y)
switch tk {
case token.ADD, token.SUB, token.MUL, token.QUO:
return mathOp(xv, yv, tk)
case token.LSS, token.GTR, token.LEQ, token.GEQ, token.EQL, token.NEQ:
return compare(xv, yv, tk)
case token.LAND, token.LOR:
return boolOp(xv, yv, tk)
default:
return nil, ErrUnsupportToken
}
}
// 数字计算操作
func mathOp(x, y reflect.Value, tk token.Token) (float64, error) {
numx, err := number(x)
if err != nil {
return 0, err
}
numy, err := number(y)
if err != nil {
return 0, err
}
switch tk {
case token.ADD:
return numx + numy, nil
case token.SUB:
return numx - numy, nil
case token.MUL:
return numx * numy, nil
case token.QUO:
if numy == 0 {
return 0, errors.New("x/0 error")
}
return numx / numy, nil
default:
return 0, ErrUnsupportToken
}
}
// 数值比较,暂时支持6种 >, <, >=,<=, ==, !=
func compare(x, y reflect.Value, tk token.Token) (bool, error) {
if x.Kind() == reflect.String && y.Kind() == reflect.String {
return compareString(x.String(), y.String(), tk)
}
numx, err := number(x)
if err != nil {
return false, err
}
numy, err := number(y)
if err != nil {
return false, err
}
switch tk {
case token.LSS:
return numx < numy, nil
case token.GTR:
return numx > numy, nil
case token.LEQ:
return numx <= numy, nil
case token.GEQ:
return numx >= numy, nil
case token.EQL:
return numx == numy, nil
case token.NEQ:
return numx != numy, nil
default:
return false, ErrUnsupportToken
}
}
// 布尔操作 && || 两种
func boolOp(x, y reflect.Value, tk token.Token) (bool, error) {
if x.Kind() != reflect.Bool || y.Kind() != reflect.Bool {
return false, ErrNotBool
}
switch tk {
case token.LAND:
return x.Bool() && y.Bool(), nil
case token.LOR:
return x.Bool() || y.Bool(), nil
default:
return false, ErrUnsupportToken
}
}
func number(x reflect.Value) (float64, error) {
switch x.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(x.Int()), nil
case reflect.Float32, reflect.Float64:
return x.Float(), nil
default:
return 0, ErrNotNumber
}
}
func compareString(x, y string, tk token.Token) (bool, error) {
switch tk {
case token.EQL:
return x == y, nil
case token.NEQ:
return x != y, nil
default:
return false, ErrUnsupportToken
}
}
func stringPair(x, y reflect.Value) (string, string, error) {
return "", "", nil
}