-
Notifications
You must be signed in to change notification settings - Fork 1
/
expr_binop.go
62 lines (56 loc) · 1.2 KB
/
expr_binop.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
package lure
type binOpType int
const (
binOpTypeEQ binOpType = iota
binOpTypeNE
binOpTypeGT
binOpTypeLT
binOpTypeGE
binOpTypeLE
binOpTypeAND
binOpTypeOR
)
// ExprBinOp represents an identity node in the AST
type ExprBinOp struct {
opTk binOpType
left Expr
right Expr
}
// evaluate returns:
// - FALSE if left or right or context is nil
// - a boolData that tells if right matches left
func (me ExprBinOp) evaluate(context map[string]IData) IData {
if me.left == nil || me.right == nil || context == nil {
return boolDataFalse
}
// return true for "<left> like <right>" if
leftData := me.left.evaluate(context)
rightData := me.right.evaluate(context)
cmp := leftData.compareTo(rightData)
var ret bool
switch me.opTk {
case binOpTypeEQ:
ret = cmp == 0
case binOpTypeNE:
ret = cmp != 0
case binOpTypeGT:
ret = cmp > 0
case binOpTypeLT:
ret = cmp < 0
case binOpTypeGE:
ret = cmp >= 0
case binOpTypeLE:
ret = cmp <= 0
case binOpTypeAND:
ret = leftData.toBoolean() && rightData.toBoolean()
case binOpTypeOR:
ret = leftData.toBoolean() || rightData.toBoolean()
}
if ret {
return boolDataTrue
}
return boolDataFalse
}
func (me ExprBinOp) isResolvable() bool {
return true
}