| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package operators |
| 19 |
|
| 20 |
|
| 21 |
const ( |
| 22 |
|
| 23 |
Conditional = "_?_:_" |
| 24 |
LogicalAnd = "_&&_" |
| 25 |
LogicalOr = "_||_" |
| 26 |
LogicalNot = "!_" |
| 27 |
Equals = "_==_" |
| 28 |
NotEquals = "_!=_" |
| 29 |
Less = "_<_" |
| 30 |
LessEquals = "_<=_" |
| 31 |
Greater = "_>_" |
| 32 |
GreaterEquals = "_>=_" |
| 33 |
Add = "_+_" |
| 34 |
Subtract = "_-_" |
| 35 |
Multiply = "_*_" |
| 36 |
Divide = "_/_" |
| 37 |
Modulo = "_%_" |
| 38 |
Negate = "-_" |
| 39 |
Index = "_[_]" |
| 40 |
OptIndex = "_[?_]" |
| 41 |
OptSelect = "_?._" |
| 42 |
|
| 43 |
|
| 44 |
Has = "has" |
| 45 |
All = "all" |
| 46 |
Exists = "exists" |
| 47 |
ExistsOne = "exists_one" |
| 48 |
Map = "map" |
| 49 |
Filter = "filter" |
| 50 |
|
| 51 |
|
| 52 |
NotStrictlyFalse = "@not_strictly_false" |
| 53 |
In = "@in" |
| 54 |
|
| 55 |
|
| 56 |
OldNotStrictlyFalse = "__not_strictly_false__" |
| 57 |
OldIn = "_in_" |
| 58 |
) |
| 59 |
|
| 60 |
var ( |
| 61 |
operators = map[string]string{ |
| 62 |
"+": Add, |
| 63 |
"/": Divide, |
| 64 |
"==": Equals, |
| 65 |
">": Greater, |
| 66 |
">=": GreaterEquals, |
| 67 |
"in": In, |
| 68 |
"<": Less, |
| 69 |
"<=": LessEquals, |
| 70 |
"%": Modulo, |
| 71 |
"*": Multiply, |
| 72 |
"!=": NotEquals, |
| 73 |
"-": Subtract, |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
operatorMap = map[string]struct { |
| 81 |
displayName string |
| 82 |
precedence int |
| 83 |
arity int |
| 84 |
}{ |
| 85 |
Conditional: {displayName: "", precedence: 8, arity: 3}, |
| 86 |
LogicalOr: {displayName: "||", precedence: 7, arity: 2}, |
| 87 |
LogicalAnd: {displayName: "&&", precedence: 6, arity: 2}, |
| 88 |
Equals: {displayName: "==", precedence: 5, arity: 2}, |
| 89 |
Greater: {displayName: ">", precedence: 5, arity: 2}, |
| 90 |
GreaterEquals: {displayName: ">=", precedence: 5, arity: 2}, |
| 91 |
In: {displayName: "in", precedence: 5, arity: 2}, |
| 92 |
Less: {displayName: "<", precedence: 5, arity: 2}, |
| 93 |
LessEquals: {displayName: "<=", precedence: 5, arity: 2}, |
| 94 |
NotEquals: {displayName: "!=", precedence: 5, arity: 2}, |
| 95 |
OldIn: {displayName: "in", precedence: 5, arity: 2}, |
| 96 |
Add: {displayName: "+", precedence: 4, arity: 2}, |
| 97 |
Subtract: {displayName: "-", precedence: 4, arity: 2}, |
| 98 |
Divide: {displayName: "/", precedence: 3, arity: 2}, |
| 99 |
Modulo: {displayName: "%", precedence: 3, arity: 2}, |
| 100 |
Multiply: {displayName: "*", precedence: 3, arity: 2}, |
| 101 |
LogicalNot: {displayName: "!", precedence: 2, arity: 1}, |
| 102 |
Negate: {displayName: "-", precedence: 2, arity: 1}, |
| 103 |
Index: {displayName: "", precedence: 1, arity: 2}, |
| 104 |
OptIndex: {displayName: "", precedence: 1, arity: 2}, |
| 105 |
OptSelect: {displayName: "", precedence: 1, arity: 2}, |
| 106 |
} |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
func Find(text string) (string, bool) { |
| 111 |
op, found := operators[text] |
| 112 |
return op, found |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
func FindReverse(symbol string) (string, bool) { |
| 117 |
op, found := operatorMap[symbol] |
| 118 |
if !found { |
| 119 |
return "", false |
| 120 |
} |
| 121 |
return op.displayName, true |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func FindReverseBinaryOperator(symbol string) (string, bool) { |
| 129 |
op, found := operatorMap[symbol] |
| 130 |
if !found || op.arity != 2 { |
| 131 |
return "", false |
| 132 |
} |
| 133 |
if op.displayName == "" { |
| 134 |
return "", false |
| 135 |
} |
| 136 |
return op.displayName, true |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
func Precedence(symbol string) int { |
| 142 |
op, found := operatorMap[symbol] |
| 143 |
if !found { |
| 144 |
return 0 |
| 145 |
} |
| 146 |
return op.precedence |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
func Arity(symbol string) int { |
| 152 |
op, found := operatorMap[symbol] |
| 153 |
if !found { |
| 154 |
return -1 |
| 155 |
} |
| 156 |
return op.arity |
| 157 |
} |