-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastBuilder.go
332 lines (290 loc) · 10 KB
/
astBuilder.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gojacego
import (
"errors"
"fmt"
"strings"
"github.com/mrxrsd/gojacego/stack"
)
var precedences = map[rune]int{
'(': 0,
'&': 1,
'|': 1,
'<': 2,
'>': 2,
'≤': 2,
'≥': 2,
'≠': 2,
'=': 2,
'+': 3,
'-': 3,
'*': 4,
'/': 4,
'%': 4,
'_': 5,
'^': 6,
}
type astBuilder struct {
caseSensitive bool
resultStack *stack.Stack
operatorStack *stack.Stack
parameterCount *stack.Stack
constantRegistry *constantRegistry
compiledConstantRegistry *constantRegistry
functionRegistry *functionRegistry
}
func newAstBuilder(caseSensitive bool, functionRegistry *functionRegistry, constantRegistry *constantRegistry, compiledConstantRegistry *constantRegistry) *astBuilder {
resultStack := stack.New()
operatorStack := stack.New()
parameterCount := stack.New()
return &astBuilder{
caseSensitive: caseSensitive,
resultStack: resultStack,
operatorStack: operatorStack,
parameterCount: parameterCount,
constantRegistry: constantRegistry,
functionRegistry: functionRegistry,
compiledConstantRegistry: compiledConstantRegistry,
}
}
func (this astBuilder) popOperations(untilLeftBracket bool, currentToken *token) error {
if untilLeftBracket && currentToken == nil {
return errors.New("If the parameter \"untillLeftBracket\" is set to true, " +
"the parameter \"currentToken\" cannot be null.")
}
for this.operatorStack.Len() > 0 && this.operatorStack.Peek().(token).Type != tt_LEFT_BRACKET {
token := this.operatorStack.Pop().(token)
switch token.Type {
case tt_OPERATION:
t, err := this.convertOperation(token)
if err == nil {
this.resultStack.Push(t)
}
break
case tt_TEXT:
f, err := this.convertFunction(token)
if err == nil {
this.resultStack.Push(f)
}
break
}
}
if untilLeftBracket {
if this.operatorStack.Len() > 0 && this.operatorStack.Peek().(token).Type == tt_LEFT_BRACKET {
this.operatorStack.Pop()
} else {
return errors.New(fmt.Sprintf("No matching left bracket found for the right "+
"bracket at position %d.", currentToken.StartPosition))
}
} else {
if this.operatorStack.Len() > 0 && this.operatorStack.Peek().(token).Type == tt_LEFT_BRACKET && !(currentToken != nil && currentToken.Type == tt_ARGUMENT_SEPARATOR) {
return errors.New(fmt.Sprintf("No matching right bracket found for the left "+
"bracket at position %d.", this.operatorStack.Peek().(token).StartPosition))
}
}
return nil
}
func (this astBuilder) verifyResult() error {
if this.resultStack.Len() > 1 {
return errors.New("The syntax of the provided formula is not valid.")
}
return nil
}
func (this astBuilder) convertFunction(operationToken token) (operation, error) {
functionName := operationToken.Value.(string)
if item, found := this.functionRegistry.get(functionName); found {
var numberOfParameters int
if true {
numberOfParameters = this.parameterCount.Pop().(int)
} else {
// fixed parameter
}
operations := make([]operation, numberOfParameters)
for i := 0; i < numberOfParameters; i++ {
operations[i] = this.resultStack.Pop().(operation)
}
// vscode reverse
for i, j := 0, len(operations)-1; i < j; i, j = i+1, j-1 {
operations[i], operations[j] = operations[j], operations[i]
}
return newFunctionOperation(floatingPoint, functionName, operations, item.isIdempotent), nil
}
return nil, nil
}
func (this astBuilder) convertOperation(operationToken token) (operation, error) {
var dataType operationDataType
var argument1 operation
var argument2 operation
var divisor operation
var divident operation
switch rune(operationToken.Value.(int32)) {
case '+':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newAddOperation(dataType, argument1, argument2), nil
case '-':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newSubtractionOperation(dataType, argument1, argument2), nil
case '*':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newMultiplicationOperation(dataType, argument1, argument2), nil
case '/':
divisor = this.resultStack.Pop().(operation)
divident = this.resultStack.Pop().(operation)
return newDivisorOperation(floatingPoint, divident, divisor), nil
case '%':
divisor = this.resultStack.Pop().(operation)
divident = this.resultStack.Pop().(operation)
return newModuloOperation(floatingPoint, divident, divisor), nil
case '_':
argument1 = this.resultStack.Pop().(operation)
return newUnaryMinusOperation(argument1.OperationMetadata().DataType, argument1), nil
case '^':
exponent := this.resultStack.Pop().(operation)
base := this.resultStack.Pop().(operation)
return newExponentiationOperation(floatingPoint, base, exponent), nil
case '&':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newAndOperation(dataType, argument1, argument2), nil
case '|':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newOrOperation(dataType, argument1, argument2), nil
case '<':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newLessThanOperation(dataType, argument1, argument2), nil
case '≤':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newLessOrEqualThanOperation(dataType, argument1, argument2), nil
case '>':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newGreaterThanOperation(dataType, argument1, argument2), nil
case '≥':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newGreaterOrEqualThanOperation(dataType, argument1, argument2), nil
case '=':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newEqualOperation(dataType, argument1, argument2), nil
case '≠':
argument2 = this.resultStack.Pop().(operation)
argument1 = this.resultStack.Pop().(operation)
dataType = requiredDataType(argument1, argument2)
return newNotEqualOperation(dataType, argument1, argument2), nil
default:
return nil, fmt.Errorf("unknown operation %s", operationToken.Value)
}
}
func (this astBuilder) build(tokens []token) (operation, error) {
for _, tokenItem := range tokens {
val := tokenItem.Value
switch tokenItem.Type {
case tt_INTEGER:
this.resultStack.Push(newConstantOperation(integer, val))
break
case tt_FLOATING_POINT:
this.resultStack.Push(newConstantOperation(floatingPoint, val))
break
case tt_TEXT:
tokenText := tokenItem.Value.(string)
if _, found := this.functionRegistry.get(tokenText); found {
this.operatorStack.Push(tokenItem)
this.parameterCount.Push(1)
} else {
if this.compiledConstantRegistry != nil {
if val, found := this.compiledConstantRegistry.get(tokenText); found {
// constant registry
this.resultStack.Push(newConstantOperation(floatingPoint, val))
break
}
}
if val, found := this.constantRegistry.get(tokenText); found {
// constant registry
this.resultStack.Push(newConstantOperation(floatingPoint, val))
} else {
if !this.caseSensitive {
tokenText = strings.ToLower(tokenText)
}
this.resultStack.Push(newVariableOperation(tokenText))
}
}
break
case tt_LEFT_BRACKET:
this.operatorStack.Push(tokenItem)
break
case tt_RIGHT_BRACKET:
this.popOperations(true, &tokenItem)
break
case tt_ARGUMENT_SEPARATOR:
this.popOperations(false, &tokenItem)
this.parameterCount.Push(this.parameterCount.Pop().(int) + 1)
break
case tt_OPERATION:
operation1Token := tokenItem
// operation1 := []rune(operation1Token.Value.(string))[0]
operation1 := rune(operation1Token.Value.(int32))
for this.operatorStack.Len() > 0 && (this.operatorStack.Peek().(token).Type == tt_OPERATION || this.operatorStack.Peek().(token).Type == tt_TEXT) {
var operation2Token token
operation2Token = this.operatorStack.Peek().(token)
isFunctionOnTopOfStack := false
if operation2Token.Type == tt_TEXT {
isFunctionOnTopOfStack = true
}
if !isFunctionOnTopOfStack {
operation2 := rune(operation2Token.Value.(int32))
// operation2 = []rune(operation2Token.Value.(string))[0]
if (isLeftAssociativeOperation(operation1) && precedences[operation1] <= precedences[operation2]) || (precedences[operation1] < precedences[operation2]) {
this.operatorStack.Pop()
t, err := this.convertOperation(operation2Token)
if err == nil {
this.resultStack.Push(t)
}
} else {
break
}
} else {
this.operatorStack.Pop()
t, err := this.convertFunction(operation2Token)
if err == nil {
this.resultStack.Push(t)
}
}
}
this.operatorStack.Push(operation1Token)
break
}
}
this.popOperations(false, nil)
err := this.verifyResult()
if err != nil {
return nil, err
} else {
resultOperation := this.resultStack.Pop().(operation)
return resultOperation, nil
}
}
func isLeftAssociativeOperation(character rune) bool {
return character == '*' || character == '+' || character == '-' || character == '/'
}
func requiredDataType(argument1 operation, argument2 operation) operationDataType {
if argument1.OperationMetadata().DataType == floatingPoint || argument2.OperationMetadata().DataType == floatingPoint {
return floatingPoint
}
return integer
}