Skip to content

Commit 59e551f

Browse files
authored
fix: [And-Or] fixed And and Or operation for const (#34)
1 parent 79f48bd commit 59e551f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

binary_op.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ func binaryOpEvalNumber(op string, left, right float64, isBool bool) float64 {
191191
case "^":
192192
left = binaryop.Pow(left, right)
193193
case "and":
194-
// Nothing to do
194+
left = binaryop.And(left, right)
195195
case "or":
196-
// Nothing to do
196+
left = binaryop.Or(left, right)
197197
case "unless":
198198
left = nan
199199
case "default":

binaryop/funcs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,21 @@ func Ifnot(left, right float64) float64 {
107107
}
108108
return nan
109109
}
110+
111+
// And return left if left and right is not NaN. Otherwise, NaN is returned.
112+
func And(left, right float64) float64 {
113+
if math.IsNaN(left) || math.IsNaN(right) {
114+
return nan
115+
}
116+
return left
117+
}
118+
119+
// Or return the first non-NaN item. If both left and right are NaN, it returns NaN.
120+
func Or(left, right float64) float64 {
121+
if !math.IsNaN(left) {
122+
return left
123+
} else if !math.IsNaN(right) {
124+
return right
125+
}
126+
return nan
127+
}

parser_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,16 @@ func TestParseSuccess(t *testing.T) {
251251
another(`m + ON (Foo) n[5m]`, `m + on(Foo) n[5m]`)
252252
same(`m + ignoring(a,b) n[5m]`)
253253
another(`1 or 2`, `1`)
254+
another(`1 or NaN`, `1`)
255+
another(`NaN or 1`, `1`)
256+
another(`(1 > 0) or 2`, `1`)
257+
another(`(1 < 0) or 2`, `2`)
258+
another(`(1 < 0) or (2 < 0)`, `NaN`)
259+
another(`NaN or NaN`, `NaN`)
254260
another(`1 and 2`, `1`)
261+
another(`1 and (1 > 0)`, `1`)
262+
another(`1 and (1 < 0)`, `NaN`)
263+
another(`1 and NaN`, `NaN`)
255264
another(`1 unless 2`, `NaN`)
256265
another(`1 default 2`, `1`)
257266
another(`1 default NaN`, `1`)

0 commit comments

Comments
 (0)