-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperators.go
76 lines (68 loc) · 1.34 KB
/
operators.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
package charlatan
// operatorType is the type of an operator
type operatorType int
// operators can be either logical or comparison-al
const (
operatorInvalid operatorType = iota
operatorAnd
operatorOr
operatorEq
operatorNeq
operatorLt
operatorLte
operatorGt
operatorGte
)
// operatorTypeFromTokenType converts a TokenType to an operatorType
func operatorTypeFromTokenType(ty tokenType) operatorType {
switch ty {
case tokAnd:
return operatorAnd
case tokOr:
return operatorOr
case tokEq:
return operatorEq
case tokNeq:
return operatorNeq
case tokLt:
return operatorLt
case tokLte:
return operatorLte
case tokGt:
return operatorGt
case tokGte:
return operatorGte
default:
return operatorInvalid
}
}
// IsLogical tests if an operator is logical
func (o operatorType) IsLogical() bool {
return o == operatorAnd || o == operatorOr
}
// isComparison tests if an operator is a comparison
func (o operatorType) isComparison() bool {
return !o.IsLogical() && o != operatorInvalid
}
func (o operatorType) String() string {
switch o {
case operatorAnd:
return "&&"
case operatorOr:
return "||"
case operatorEq:
return "="
case operatorNeq:
return "!="
case operatorLt:
return "<"
case operatorLte:
return "<="
case operatorGt:
return ">"
case operatorGte:
return ">="
default:
return "<unknown operator>"
}
}