-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
269 lines (239 loc) · 6.51 KB
/
types.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package xpression
import (
"fmt"
"regexp"
"strconv"
)
type Operator byte // list of operators: + - * / < > == !=
type TokenCategory byte // operator, literal (operand), parentheses
type OperandType byte // string, number, boolean, null, undefined
type Associativity byte // left, right
const (
opNone Operator = '\x00'
opLogicalOR Operator = 'O'
opLogicalAND Operator = 'A'
opBitwiseOR Operator = '|'
opBitwiseXOR Operator = '^'
opBitwiseAND Operator = '&'
opEqual Operator = 'E'
opStrictEqual Operator = 'e'
opNotEqual Operator = 'N'
opStrictNotEqual Operator = 'n'
opGE Operator = 'G'
opG Operator = 'g'
opLE Operator = 'L'
opL Operator = 'l'
opRegexMatch Operator = 'R'
opNotRegexMatch Operator = 'r'
opShiftRight Operator = '>'
opShiftLeft Operator = '<'
opPlus Operator = '+'
opMinus Operator = '-'
opMultiply Operator = '*'
opDivide Operator = '/'
opRemainder Operator = '%'
opExponentiation Operator = 'P'
opLogicalNOT Operator = '!'
opBitwiseNOT Operator = '~'
opUnaryMinus Operator = '_'
opLeftParenthesis Operator = '('
opRightParenthesis Operator = ')'
)
const (
tcIntermediateResult TokenCategory = 0 // intermediate result placeholder
tcLiteral TokenCategory = 1 << iota // string, number, bool
tcOperator // +-*/^!<=>
tcLeftParenthesis //
tcRightParenthesis //
tcVariable // @.key etc
)
const (
otString OperandType = 1 << iota
otNumber
otBoolean
otNull
otUndefined
otRegexp
otVariable
)
const (
// public aliases
StringOperand = otString
NumberOperand = otNumber
BooleanOperand = otBoolean
NullOperand = otNull
UndefinedOperand = otUndefined
RegexpOperand = otRegexp
VariableOperand = otVariable
)
const (
aLeft Associativity = iota
aRight
)
type OperatorDetail struct {
Associativity Associativity
Precedence int
Arguments int
}
var operatorSpelling = []struct {
Spelling []byte
Code Operator
}{ // IMPORTANT: first longest string, then substring(s)! Ex: "!=~", "!=", "!"
{[]byte("||"), opLogicalOR},
{[]byte("&&"), opLogicalAND},
{[]byte("|"), opBitwiseOR},
{[]byte("&"), opBitwiseAND},
{[]byte("^"), opBitwiseXOR},
{[]byte("!=~"), opNotRegexMatch},
{[]byte("!~"), opNotRegexMatch},
{[]byte("==="), opStrictEqual},
{[]byte("=="), opEqual},
{[]byte("!=="), opStrictNotEqual},
{[]byte("!="), opNotEqual},
{[]byte(">>"), opShiftRight},
{[]byte("<<"), opShiftLeft},
{[]byte(">="), opGE},
{[]byte(">"), opG},
{[]byte("<="), opLE},
{[]byte("<"), opL},
{[]byte("=~"), opRegexMatch},
{[]byte("**"), opExponentiation},
{[]byte("+"), opPlus},
{[]byte("-"), opMinus},
{[]byte("*"), opMultiply},
{[]byte("/"), opDivide},
{[]byte("%"), opRemainder},
{[]byte("!"), opLogicalNOT},
{[]byte("~"), opBitwiseNOT},
{[]byte("-"), opUnaryMinus},
}
var operatorDetails = map[Operator]OperatorDetail{
opLogicalOR: {aLeft, 1, 2}, // logical OR
opLogicalAND: {aLeft, 2, 2}, // logical AND
opBitwiseOR: {aLeft, 3, 2}, // bitwise OR
opBitwiseXOR: {aLeft, 4, 2}, // bitwise XOR
opBitwiseAND: {aLeft, 5, 2}, // bitwise AND
opEqual: {aLeft, 6, 2}, // ==
opStrictEqual: {aLeft, 6, 2}, // ===
opNotEqual: {aLeft, 6, 2}, // !=
opStrictNotEqual: {aLeft, 6, 2}, // !==
opGE: {aLeft, 7, 2}, // >=
opG: {aLeft, 7, 2}, // >
opLE: {aLeft, 7, 2}, // <=
opL: {aLeft, 7, 2}, // <
opRegexMatch: {aLeft, 7, 2}, // =~
opNotRegexMatch: {aLeft, 7, 2}, // !=~
opShiftRight: {aLeft, 8, 2}, // >>
opShiftLeft: {aLeft, 8, 2}, // <<
opPlus: {aLeft, 9, 2}, // +
opMinus: {aLeft, 9, 2}, // -
opMultiply: {aLeft, 10, 2}, // *
opDivide: {aLeft, 10, 2}, // /
opRemainder: {aLeft, 10, 2}, // %
opExponentiation: {aRight, 11, 2}, // **
opLogicalNOT: {aLeft, 12, 1}, // logical NOT (!)
opBitwiseNOT: {aLeft, 12, 1}, // bitwise NOT (~)
opUnaryMinus: {aLeft, 12, 1}, // unary -
opLeftParenthesis: {aLeft, 13, 2}, // (
opRightParenthesis: {aLeft, 13, 2}, // )
}
type Operand struct {
Type OperandType
Str []byte
Number float64
Bool bool
Regexp *regexp.Regexp
// + node reference
}
var operatorBound []byte
func init() {
operatorBound = []byte{' ', '['}
for _, operator := range operatorSpelling {
if !bytein(operator.Spelling[0], operatorBound) {
operatorBound = append(operatorBound, operator.Spelling[0])
}
}
}
func (op *Operand) String() string {
switch op.Type {
case otNull:
return "null"
case otUndefined:
return "undefined"
case otString:
return fmt.Sprintf("\"%s\"", string(op.Str))
case otNumber:
return strconv.FormatFloat(op.Number, 'f', -1, 64)
case otBoolean:
return fmt.Sprintf("%v", op.Bool)
case otRegexp:
return fmt.Sprintf("/%s/", op.Regexp.String())
}
return "???"
}
type Token struct {
Category TokenCategory
Operator Operator
Operand
}
func (tok *Token) String() string {
opCode := func(op Operator) string {
for _, rec := range operatorSpelling {
if rec.Code == op {
return string(rec.Spelling)
}
}
return "???"
}
switch tok.Category {
case tcIntermediateResult:
return "IR"
case tcLiteral:
return tok.Operand.String()
case tcOperator:
return opCode(tok.Operator)
case tcVariable:
return string(tok.Str)
}
return "unknown"
}
func (op *Operand) SetString(s string) {
op.Type = otString
op.Str = []byte(s)
}
func (op *Operand) SetNumber(f float64) {
op.Type = otNumber
op.Number = f
}
func (op *Operand) SetBoolean(b bool) {
op.Type = otBoolean
op.Bool = b
}
func (op *Operand) SetNull() {
op.Type = otNull
}
func (op *Operand) SetUndefined() {
op.Type = otUndefined
}
func (op *Operand) SetRegexp(r *regexp.Regexp) {
op.Type = otRegexp
op.Regexp = r
}
func String(s string) *Operand {
return &Operand{Type: otString, Str: []byte(s)}
}
func Number(f float64) *Operand {
return &Operand{Type: otNumber, Number: f}
}
func Boolean(b bool) *Operand {
return &Operand{Type: otBoolean, Bool: b}
}
func Null() *Operand {
return &Operand{Type: otNull}
}
func Undefined() *Operand {
return &Operand{Type: otUndefined}
}
func Regexp(r *regexp.Regexp) *Operand {
return &Operand{Type: otRegexp, Regexp: r}
}