/
binary_expr.go
190 lines (147 loc) 路 4.49 KB
/
binary_expr.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
package q
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// BinaryExpr evaluates a binary operator expression.
type BinaryExpr struct {
Left, Right Expression
Operator string
}
// Operators contains the tokens and functions for all operators.
//
// It is important that the operators are ordered so that the operators with
// most tokens are read first. This prevents it from consuming operators that
// are subsets of others.
var Operators = []struct {
Name string
Tokens []TokenKind
Function func(left, right interface{}) (bool, error)
}{
{"!=", []TokenKind{TokenNot, TokenEqual}, notEqual},
{">=", []TokenKind{TokenGreaterThan, TokenEqual}, greaterThanEqual},
{"<=", []TokenKind{TokenLessThan, TokenEqual}, lessThanEqual},
{"=", []TokenKind{TokenEqual}, equal},
{">", []TokenKind{TokenGreaterThan}, greaterThan},
{"<", []TokenKind{TokenLessThan}, lessThan},
}
func (e *BinaryExpr) Evaluate(engine *Engine, input interface{}, args []*Statement) (interface{}, error) {
in := reflect.ValueOf(input)
// If it is a slice we need to Evaluate each one.
if in.Kind() == reflect.Slice {
results := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(true)), 0, in.Len())
for i := 0; i < in.Len(); i++ {
result, err := e.Evaluate(engine, in.Index(i).Interface(), args)
if err != nil {
return nil, err
}
results = reflect.Append(results, reflect.ValueOf(result))
}
return results.Interface(), nil
}
left, err := e.Left.Evaluate(engine, input, args)
if err != nil {
return nil, err
}
right, err := e.Right.Evaluate(engine, input, args)
if err != nil {
return nil, err
}
for _, operator := range Operators {
if operator.Name == e.Operator {
return operator.Function(left, right)
}
}
return nil, fmt.Errorf("no such operator: %s", e.Operator)
}
func binaryFloats(left, right string) (float64, float64, bool) {
floatLeft, errLeft := strconv.ParseFloat(left, 64)
floatRight, errRight := strconv.ParseFloat(right, 64)
// Compare as numbers.
if errLeft == nil && errRight == nil {
return floatLeft, floatRight, true
}
return 0, 0, false
}
func binaryStrings(left, right interface{}) (sLeft string, sRight string) {
if s, ok := left.(string); ok {
sLeft = s
} else {
sLeft = fmt.Sprintf("%v", left)
}
if s, ok := right.(string); ok {
sRight = s
} else {
sRight = fmt.Sprintf("%v", right)
}
return
}
func compareStrings(s, t string, op func(string, string) bool) bool {
// This is not the most ideal way to do comparisons because it doesn't
// handle more complex characters as nicely as strings.EqualFold.
s = strings.TrimSpace(strings.ToLower(s))
t = strings.TrimSpace(strings.ToLower(t))
return op(s, t)
}
func equal(left, right interface{}) (bool, error) {
sLeft, sRight := binaryStrings(left, right)
// Compare as numbers.
if floatLeft, floatRight, ok := binaryFloats(sLeft, sRight); ok {
return floatLeft == floatRight, nil
}
compare := func(s, t string) bool {
return s == t
}
// At least one side could not be converted to a float, so compare them as
// strings.
return compareStrings(sLeft, sRight, compare), nil
}
func notEqual(left, right interface{}) (bool, error) {
result, err := equal(left, right)
if err != nil {
return false, err
}
return !result, nil
}
func greaterThan(left, right interface{}) (bool, error) {
sLeft, sRight := binaryStrings(left, right)
if floatLeft, floatRight, ok := binaryFloats(sLeft, sRight); ok {
return floatLeft > floatRight, nil
}
compare := func(s, t string) bool {
return s > t
}
return compareStrings(sLeft, sRight, compare), nil
}
func greaterThanEqual(left, right interface{}) (bool, error) {
sLeft, sRight := binaryStrings(left, right)
if floatLeft, floatRight, ok := binaryFloats(sLeft, sRight); ok {
return floatLeft >= floatRight, nil
}
compare := func(s, t string) bool {
return s >= t
}
return compareStrings(sLeft, sRight, compare), nil
}
func lessThan(left, right interface{}) (bool, error) {
sLeft, sRight := binaryStrings(left, right)
if floatLeft, floatRight, ok := binaryFloats(sLeft, sRight); ok {
return floatLeft < floatRight, nil
}
compare := func(s, t string) bool {
return s < t
}
return compareStrings(sLeft, sRight, compare), nil
}
func lessThanEqual(left, right interface{}) (bool, error) {
sLeft, sRight := binaryStrings(left, right)
if floatLeft, floatRight, ok := binaryFloats(sLeft, sRight); ok {
return floatLeft <= floatRight, nil
}
compare := func(s, t string) bool {
return s <= t
}
return compareStrings(sLeft, sRight, compare), nil
}