-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric.go
370 lines (344 loc) · 9.05 KB
/
numeric.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package dLola
import (
"errors"
"fmt"
"strconv"
)
//
// NumComparison
//
type NumComparison interface {
Sprint() string
AcceptNumComp(NumComparisonVisitor)
GetPos() Position
InstantiateNumCompExpr(int, int) InstNumComparison
ConstantSubsNumCompExpr(spec *Spec) NumComparison
}
type NumComparisonVisitor interface {
VisitNumLess(NumLess)
VisitNumLessEq(NumLessEq)
VisitNumEq(NumEq)
VisitNumGreater(NumGreater)
VisitNumGreaterEq(NumGreaterEq)
VisitNumNotEq(NumNotEq)
}
type NumLess struct {
Left NumExpr
Right NumExpr
}
type NumLessEq struct {
Left NumExpr
Right NumExpr
}
type NumEq struct {
Left NumExpr
Right NumExpr
}
type NumGreater struct {
Left NumExpr
Right NumExpr
}
type NumGreaterEq struct {
Left NumExpr
Right NumExpr
}
type NumNotEq struct {
Left NumExpr
Right NumExpr
}
func NewNumLess(a, b interface{}) NumLess {
return NumLess{a.(NumExpr), b.(NumExpr)}
}
func NewNumLessEq(a, b interface{}) NumLessEq {
return NumLessEq{a.(NumExpr), b.(NumExpr)}
}
func NewNumGreater(a, b interface{}) NumGreater {
return NumGreater{a.(NumExpr), b.(NumExpr)}
}
func NewNumGreaterEq(a, b interface{}) NumGreaterEq {
return NumGreaterEq{a.(NumExpr), b.(NumExpr)}
}
func NewNumEq(a, b interface{}) NumEq {
return NumEq{a.(NumExpr), b.(NumExpr)}
}
func NewNumNotEq(a, b interface{}) NumNotEq {
return NumNotEq{a.(NumExpr), b.(NumExpr)}
}
func (this NumLess) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumLess(this)
}
func (this NumLessEq) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumLessEq(this)
}
func (this NumGreater) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumGreater(this)
}
func (this NumGreaterEq) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumGreaterEq(this)
}
func (this NumEq) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumEq(this)
}
func (this NumNotEq) AcceptNumComp(visitor NumComparisonVisitor) {
visitor.VisitNumNotEq(this)
}
func (this NumLess) GetPos() Position {
return this.Left.GetPos()
}
func (this NumLessEq) GetPos() Position {
return this.Left.GetPos()
}
func (this NumGreater) GetPos() Position {
return this.Left.GetPos()
}
func (this NumGreaterEq) GetPos() Position {
return this.Left.GetPos()
}
func (this NumEq) GetPos() Position {
return this.Left.GetPos()
}
func (this NumNotEq) GetPos() Position {
return this.Left.GetPos()
}
func (this NumLess) Sprint() string {
return fmt.Sprintf("%s < %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this NumLessEq) Sprint() string {
return fmt.Sprintf("%s <= %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this NumGreater) Sprint() string {
return fmt.Sprintf("%s > %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this NumGreaterEq) Sprint() string {
return fmt.Sprintf("%s >= %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this NumNotEq) Sprint() string {
return fmt.Sprintf("%s != %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this NumEq) Sprint() string {
return fmt.Sprintf("%s = %s", this.Left.Sprint(), this.Right.Sprint())
}
//
// Numeric Expressions
//
type NumExpr interface {
AcceptNum(NumExprVisitor)
Sprint() string
GetPos() Position
InstantiateNumExpr(int, int) InstNumExpr
ConstantSubsNumExpr(spec *Spec) NumExpr
}
type NumExprVisitor interface {
VisitIntLiteralExpr(IntLiteralExpr) //
VisitFloatLiteralExpr(FloatLiteralExpr) //
VisitStreamOffsetExpr(StreamOffsetExpr) //same method with same arguments as in ExprVisitor
VisitConstExpr(ConstExpr) //same method with same arguments as in ExprVisitor
VisitIfThenElseExpr(IfThenElseExpr) //same method with same arguments as in ExprVisitor
VisitNumMulExpr(NumMulExpr) //
VisitNumDivExpr(NumDivExpr) //
VisitNumPlusExpr(NumPlusExpr) //
VisitNumMinusExpr(NumMinusExpr) //
// VisitNumPathExpr(NumPathExpr)
}
type IntLiteralExpr struct {
Num int
Pos Position
}
type FloatLiteralExpr struct {
Num float32
Pos Position
}
type NumMulExpr struct {
Left NumExpr
Right NumExpr
}
type NumDivExpr struct {
Left NumExpr
Right NumExpr
}
type NumPlusExpr struct {
Left NumExpr
Right NumExpr
}
type NumMinusExpr struct {
Left NumExpr
Right NumExpr
}
func NewMulExpr(a, b interface{}) NumMulExpr {
return NumMulExpr{a.(NumExpr), b.(NumExpr)}
}
func NewDivExpr(a, b interface{}) NumDivExpr {
return NumDivExpr{a.(NumExpr), b.(NumExpr)}
}
func NewPlusExpr(a, b interface{}) NumPlusExpr {
return NumPlusExpr{a.(NumExpr), b.(NumExpr)}
}
func NewMinusExpr(a, b interface{}) NumMinusExpr {
return NumMinusExpr{a.(NumExpr), b.(NumExpr)}
}
func NewIntLiteralExpr(a, p interface{}) IntLiteralExpr {
return IntLiteralExpr{a.(int), NewPosition(p)}
}
func NewFloatLiteralExpr(a, p interface{}) FloatLiteralExpr {
// return FloatLiteralExpr{a.(float32)}
return FloatLiteralExpr{float32(a.(float64)), NewPosition(p)}
}
func (this IntLiteralExpr) GetPos() Position {
return this.Pos
}
func (this FloatLiteralExpr) GetPos() Position {
return this.Pos
}
func (this NumMulExpr) GetPos() Position {
return this.Left.GetPos()
}
func (this NumDivExpr) GetPos() Position {
return this.Left.GetPos()
}
func (this NumPlusExpr) GetPos() Position {
return this.Left.GetPos()
}
func (this NumMinusExpr) GetPos() Position {
return this.Left.GetPos()
}
func (e NumMulExpr) Sprint() string {
return fmt.Sprintf("(%s)%s(%s)", e.Left.Sprint(), "*", e.Right.Sprint())
}
func (e NumDivExpr) Sprint() string {
return fmt.Sprintf("(%s)%s(%s)", e.Left.Sprint(), "/", e.Right.Sprint())
}
func (e NumPlusExpr) Sprint() string {
return fmt.Sprintf("(%s)%s(%s)", e.Left.Sprint(), "+", e.Right.Sprint())
}
func (e NumMinusExpr) Sprint() string {
return fmt.Sprintf("(%s)%s(%s)", e.Left.Sprint(), "-", e.Right.Sprint())
}
func (e IntLiteralExpr) Sprint() string {
return strconv.Itoa(e.Num)
}
func (e FloatLiteralExpr) Sprint() string {
return strconv.FormatFloat(float64(e.Num), 'f', 4, 32)
}
func (e NumMulExpr) AcceptNum(v NumExprVisitor) {
v.VisitNumMulExpr(e)
}
func (e NumDivExpr) AcceptNum(v NumExprVisitor) {
v.VisitNumDivExpr(e)
}
func (e NumPlusExpr) AcceptNum(v NumExprVisitor) {
v.VisitNumPlusExpr(e)
}
func (e NumMinusExpr) AcceptNum(v NumExprVisitor) {
v.VisitNumMinusExpr(e)
}
func (e IntLiteralExpr) AcceptNum(v NumExprVisitor) {
v.VisitIntLiteralExpr(e)
}
func (e FloatLiteralExpr) AcceptNum(v NumExprVisitor) {
v.VisitFloatLiteralExpr(e)
}
func (e StreamOffsetExpr) AcceptNum(v NumExprVisitor) {
v.VisitStreamOffsetExpr(e)
}
func (e IfThenElseExpr) AcceptNum(v NumExprVisitor) {
v.VisitIfThenElseExpr(e)
}
func (e ConstExpr) AcceptNum(v NumExprVisitor) {
v.VisitConstExpr(e)
}
//
//
//
func getNumExpr(e interface{}) (NumExpr, error) {
switch v := e.(type) {
case NumericExpr:
return v.NExpr, nil
case StreamOffsetExpr:
return v, nil
case ConstExpr:
return v, nil
case NumExpr:
return v, nil
case FloatLiteralExpr:
return v, nil
case IntLiteralExpr:
return v, nil
default:
str := fmt.Sprintf("cannot convert to num \"%s\"\n", e.(Expr).Sprint())
return nil, errors.New(str)
}
}
func NumExprToExpr(expr NumExpr) Expr {
/*if s, ok := expr.(StreamOffsetExpr); ok {
return s
} else if k, ok := expr.(ConstExpr); ok {
return k
} else {
return NewNumericExpr(expr)
}*/
switch v := expr.(type) {
case StreamOffsetExpr:
return v
case ConstExpr:
return v
default:
return NewNumericExpr(expr) //note: here we use expr, not its type
}
}
// FLATTEN
type RightSubexpr interface {
buildBinaryExpr(left NumExpr, right NumExpr) NumExpr
getInner() NumExpr
}
type RightMultExpr struct{ E NumExpr }
type RightDivExpr struct{ E NumExpr }
type RightPlusExpr struct{ E NumExpr }
type RightMinusExpr struct{ E NumExpr }
func (r RightMultExpr) buildBinaryExpr(left NumExpr, right NumExpr) NumExpr {
return NumMulExpr{left, right}
}
func (r RightMultExpr) getInner() NumExpr { return r.E }
func (r RightDivExpr) buildBinaryExpr(left NumExpr, right NumExpr) NumExpr {
return NumDivExpr{left, right}
}
func (r RightDivExpr) getInner() NumExpr { return r.E }
func (r RightPlusExpr) buildBinaryExpr(left NumExpr, right NumExpr) NumExpr {
return NumPlusExpr{left, right}
}
func (r RightPlusExpr) getInner() NumExpr { return r.E }
func (r RightMinusExpr) buildBinaryExpr(left NumExpr, right NumExpr) NumExpr {
return NumMinusExpr{left, right}
}
func (r RightMinusExpr) getInner() NumExpr { return r.E }
func NewRightMultExpr(a interface{}) RightMultExpr {
n, _ := getNumExpr(a)
return RightMultExpr{n}
}
func NewRightDivExpr(a interface{}) RightDivExpr {
n, _ := getNumExpr(a)
return RightDivExpr{n}
}
func NewRightPlusExpr(a interface{}) RightPlusExpr {
n, _ := getNumExpr(a)
return RightPlusExpr{n}
}
func NewRightMinusExpr(a interface{}) RightMinusExpr {
n, _ := getNumExpr(a)
return RightMinusExpr{n}
}
func Flatten(a, b interface{}) Expr {
exprs := ToSlice(b)
first, _ := getNumExpr(a)
if len(exprs) == 0 {
return NumExprToExpr(first)
}
right := exprs[len(exprs)-1].(RightSubexpr)
curr := right.getInner()
for i := len(exprs) - 2; i >= 0; i-- {
left := exprs[i].(RightSubexpr)
curr = right.buildBinaryExpr(left.getInner(), curr)
right = left
}
ret := NumExprToExpr(right.buildBinaryExpr(first, curr))
return ret
}