-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
493 lines (455 loc) · 11.5 KB
/
expression.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package dLola
import (
"fmt"
)
//
// Every expression that can be parsed is an Expr
// including Predicates, Numeric Expressions, Constants and Streams
//
//
// 1. There are two possibilities for NumExpr and BoolExpr to be Expr
// One is to force every type that implements NumExpr to implement Expr too
// The second is to Embed a NumExpr into a struct that implements Expr
// This second option facilitates the extension of NumExpr to new numeric expressions
// 2. Additionally, ConstExpr and StreamExpr must implement Expr, NumExpr
// and BoolExpr because these are untyped. Also If_Then_Else must also
// implement Expr, NumExpr and BoolExpr as these can appear everywhere.
//
// We use duck typing for this
//
type HasPos interface { //should be implemented by all structs that implement Expr or subStructs
GetPos() Position
}
type Expr interface {
Sprint() string
Accept(ExprVisitor)
GetPos() Position
InstantiateExpr(int, int) InstExpr // implemented in InstantiateExpr.go
ConstantSubs(spec *Spec) Expr
}
type ExprVisitor interface {
VisitConstExpr(ConstExpr)
VisitLetExpr(LetExpr)
VisitIfThenElseExpr(IfThenElseExpr)
VisitStringExpr(StringExpr)
VisitStreamOffsetExpr(StreamOffsetExpr)
VisitBooleanExpr(BooleanExpr)
VisitNumericExpr(NumericExpr)
/*striver
VisitTimeExpr(TimeExpr)
VisitOutsideExpr(OutsideExpr)
VisitNoTickExpr(NoTickExpr)
*/
}
type ConstExpr struct { // implements Expr,NumExpr,BooleanExpr
Name StreamName
Pos Position
}
type LetExpr struct {
Name StreamName
Params []StreamName
Result *StreamType
Bind Expr
Body Expr
}
type IfThenElseExpr struct { // implements Expr,NumExpr,BoolExpr
If Expr
Then Expr
Else Expr
}
type StreamOffsetExpr struct { // StreamOffsetExpr implements Expr,NumExpr,BoolExpr, StrExpr
SExpr StreamExpr
}
type BooleanExpr struct {
BExpr BoolExpr
}
type NumericExpr struct {
NExpr NumExpr
}
type StringExpr struct {
StExpr StrExpr
}
/*striver
type TimeExpr struct {
TExpr Time
}
type OutsideExpr struct{}
type NoTickExpr struct{}
*/
// Accept
func (this ConstExpr) Accept(visitor ExprVisitor) {
visitor.VisitConstExpr(this)
}
func (this LetExpr) Accept(visitor ExprVisitor) {
visitor.VisitLetExpr(this)
}
func (this IfThenElseExpr) Accept(visitor ExprVisitor) {
visitor.VisitIfThenElseExpr(this)
}
func (this StreamOffsetExpr) Accept(visitor ExprVisitor) {
visitor.VisitStreamOffsetExpr(this)
}
func (this BooleanExpr) Accept(visitor ExprVisitor) {
visitor.VisitBooleanExpr(this)
}
func (this NumericExpr) Accept(visitor ExprVisitor) {
visitor.VisitNumericExpr(this)
}
func (this StringExpr) Accept(visitor ExprVisitor) {
visitor.VisitStringExpr(this)
}
func (this ConstExpr) GetPos() Position {
return this.Pos
}
func (this LetExpr) GetPos() Position {
return this.Bind.GetPos()
}
func (this IfThenElseExpr) GetPos() Position {
return this.If.GetPos()
}
func (this StreamOffsetExpr) GetPos() Position {
return this.SExpr.GetPos()
}
func (this BooleanExpr) GetPos() Position {
return this.BExpr.GetPos()
}
func (this NumericExpr) GetPos() Position {
return this.NExpr.GetPos()
}
func (this StringExpr) GetPos() Position {
return this.StExpr.GetPos()
}
/*striver
func (this TimeExpr) Accept(visitor ExprVisitor) {
visitor.VisitTimeExpr(this)
}
func (this OutsideExpr) Accept(visitor ExprVisitor) {
visitor.VisitOutsideExpr(this)
}
func (this NoTickExpr) Accept(visitor ExprVisitor) {
visitor.VisitNoTickExpr(this)
}
*/
// Sprint()
func (this ConstExpr) Sprint() string {
return string(this.Name)
}
func (this LetExpr) Sprint() string {
params := fmt.Sprintf("%v", this.Params)
result := this.Result.Sprint()
bind := this.Bind.Sprint()
body := this.Body.Sprint()
return fmt.Sprintf("let %s %s = %s :: %s in %s", this.Name, params, bind, result, body)
}
func (this IfThenElseExpr) Sprint() string {
if_part := this.If.Sprint()
then_part := this.Then.Sprint()
else_part := this.Else.Sprint()
return fmt.Sprintf("if %s then %s else %s", if_part, then_part, else_part)
}
func (this NumericExpr) Sprint() string {
return this.NExpr.Sprint()
}
func (this StreamOffsetExpr) Sprint() string {
return this.SExpr.Sprint()
}
func (this BooleanExpr) Sprint() string {
return this.BExpr.Sprint()
}
func (this StringExpr) Sprint() string {
return this.StExpr.Sprint()
}
/*striver
func (this TimeExpr) Sprint() string {
return this.TExpr.Sprint()
}
func (this NoTickExpr) Sprint() string {
return "notick"
}
func (this OutsideExpr) Sprint() string {
return "outside"
}
var (
TheOutsideExpr OutsideExpr
TheNoTickExpr NoTickExpr
)
*/
func NewConstExpr(a, p interface{}) ConstExpr {
return ConstExpr{getStreamName(a), NewPosition(p)}
}
func NewNumericExpr(a interface{}) NumericExpr {
return NumericExpr{a.(NumExpr)}
}
func NewStreamOffsetExpr(a interface{}) StreamOffsetExpr {
return StreamOffsetExpr{a.(StreamExpr)}
}
func NewBooleanExpr(b interface{}) BooleanExpr {
return BooleanExpr{b.(BoolExpr)}
}
func NewIfThenElseExpr(p, a, b interface{}) IfThenElseExpr {
return IfThenElseExpr{p.(Expr), a.(Expr), b.(Expr)}
}
func NewLetExpr(n, p, e, b interface{}) LetExpr {
name := getStreamName(n)
p2 := ToSlice(p)
params := make([]StreamName, 0)
for _, param := range p2 {
params = append(params, getStreamName(param))
}
return LetExpr{name, params, nil, e.(Expr), b.(Expr)} //Result type will be assigned in TypeVisitor
}
func NewStringExpr(s interface{}) StringExpr {
return StringExpr{s.(StrExpr)}
}
/*striver
func NewTimeExpr(a interface{}) TimeExpr {
return TimeExpr{a.(Time)}
}
*/
//
// StreamExpr : s(~t) s(t~) s(<t) s(t>) s(s<t) ...
//
type StreamExpr interface {
AcceptStream(StreamExprVisitor)
Sprint() string
GetPos() Position
InstantiateStreamExpr(int, int) InstExpr //note it is generic
InstantiateBoolStreamExpr(int, int) InstBoolExpr
InstantiateNumStreamExpr(int, int) InstNumExpr
InstantiateStrStreamExpr(int, int) InstStrExpr
ConstantSubsStreamExpr(spec *Spec) Expr
ConstantSubsBoolStreamExpr(spec *Spec) BoolExpr
ConstantSubsNumStreamExpr(spec *Spec) NumExpr
ConstantSubsStrStreamExpr(spec *Spec) StrExpr
}
type StreamExprVisitor interface {
/* VisitPrevEqValExpr(PrevEqValExpr)
VisitPrevValExpr(PrevValExpr)
VisitSuccEqValExpr(SuccEqValExpr)
VisitSuccValExpr(SuccValExpr)
*/
VisitStreamFetchExpr(StreamFetchExpr)
}
/*
type PrevEqValExpr struct { //implements StreamExpr
Name StreamName
Expr Time
}
type PrevValExpr struct { //implements StreamExpr
Name StreamName
Expr Time
}
type SuccEqValExpr struct { //implements StreamExpr
Name StreamName
Expr Time
}
type SuccValExpr struct { //implements StreamExpr
Name StreamName
Expr Time
}*/
type StreamFetchExpr struct { //implements StreamExpr
Name StreamName
Offset OffsetExpr
Default DefaultExpr //default value for the instantiated stream that gets out of the trace
Pos Position
}
/*
func NewPrevEqValExpr(s, t interface{}) PrevEqValExpr {
name := getStreamName(s)
return PrevEqValExpr{name, t.(Time)}
}
func NewPrevValExpr(s, t interface{}) PrevValExpr {
name := getStreamName(s)
return PrevValExpr{name, t.(Time)}
}
func NewSuccEqValExpr(s, t interface{}) SuccEqValExpr {
name := getStreamName(s)
return SuccEqValExpr{name, t.(Time)}
}
func NewSuccValExpr(s, t interface{}) SuccValExpr {
name := getStreamName(s)
return SuccValExpr{name, t.(Time)}
}
*/
func NewStreamFetchExpr(s, t, co, p interface{}) StreamFetchExpr {
pos := NewPosition(p)
offset := NewOffsetExpr(t)
name := getStreamName(s)
def := NewDefaultExpr(co)
return StreamFetchExpr{name, offset, def, pos}
}
/*
func (e PrevEqValExpr) Sprint() string {
return fmt.Sprintf("%s(~ %s )", e.Name.Sprint(), e.Expr.Sprint())
}
func (e PrevValExpr) Sprint() string {
return fmt.Sprintf("%s(< %s )", e.Name.Sprint(), e.Expr.Sprint())
}
func (e SuccEqValExpr) Sprint() string {
return fmt.Sprintf("%s( %s ~)", e.Name.Sprint(), e.Expr.Sprint())
}
func (e SuccValExpr) Sprint() string {
return fmt.Sprintf("%s( %s >)", e.Name.Sprint(), e.Expr.Sprint())
}
*/
func (e StreamFetchExpr) Sprint() string {
if e.Default.typp == Unknown {
return fmt.Sprintf("%s[%s]", e.Name.Sprint(), e.Offset.Sprint())
} else {
return fmt.Sprintf("%s[%s|%s]", e.Name.Sprint(), e.Offset.Sprint(), e.Default.Sprint())
}
}
func (this StreamFetchExpr) GetPos() Position {
return this.Pos
}
/*
func (this PrevEqValExpr) AcceptStream(v StreamExprVisitor) {
v.VisitPrevEqValExpr(this)
}
func (this PrevValExpr) AcceptStream(v StreamExprVisitor) {
v.VisitPrevValExpr(this)
}
func (this SuccEqValExpr) AcceptStream(v StreamExprVisitor) {
v.VisitSuccEqValExpr(this)
}
func (this SuccValExpr) AcceptStream(v StreamExprVisitor) {
v.VisitSuccValExpr(this)
}
*/
func (this StreamFetchExpr) AcceptStream(v StreamExprVisitor) {
v.VisitStreamFetchExpr(this)
}
//
// Time
//
/*striver
type TimeBasic interface {
//Accept(...)
Sprint() string
}
type TimeLiteral struct { // Implement TimeBasic
Val float32
}
type TimeConstant struct { // Implement TimeBasic
Name StreamName // this is for an symbolic time contant
}
func NewTimeLiteral(n interface{}) TimeLiteral {
num := n.(FloatLiteralExpr).Num
return TimeLiteral{num}
}
func NewTimeConstant(s interface{}) TimeConstant {
k := getStreamName(s)
return TimeConstant{k}
}
func (l TimeLiteral) Sprint() string {
return fmt.Sprintf("%f", l.Val)
}
func (c TimeConstant) Sprint() string {
return string(c.Name)
}
type Time interface {
Sprint() string
}
type Time_t struct{} // implements Time
var (
T Time_t // "t" as in "define foo t := ..."
)
func (t Time_t) Sprint() string {
return "t"
}
*/
//
// Offset
//
type OffsetExpr struct { // OffsetExpr "implements" Time
err bool
val int
}
func NewOffsetExpr(n interface{}) OffsetExpr {
num, ok := n.(IntLiteralExpr)
return OffsetExpr{!ok, num.Num}
}
func (o OffsetExpr) Sprint() string {
return fmt.Sprintf("%d", o.val)
}
type Printable interface {
Sprint() string
}
type DefaultExpr struct { //implements DefaultExpr
typp StreamType //as in TypeVisitor
val Printable
}
var InvalidPos Position = Position{-1, -1, -1}
var InvalidVal BooleanExpr = BooleanExpr{TruePredicate{InvalidPos}} //nil is not allowed
var InvalidDef = DefaultExpr{Unknown, InvalidVal}
/*co must not be nil*/
func NewDefaultExpr(co interface{}) DefaultExpr {
var t StreamType
if co != nil {
switch co.(type) {
case TruePredicate:
t = BoolT
case FalsePredicate:
t = BoolT
case NumExpr:
t = NumT
case StrExpr:
t = StringT
default:
t = Unknown
}
return DefaultExpr{t, co.(Printable)}
} else {
return InvalidDef
}
}
func (d DefaultExpr) Sprint() string {
return d.val.Sprint()
}
/*striver
type PrevEqExpr struct { // implements OffsetExpr
Name StreamName
Time Time
}
type PrevExpr struct { // implements OffsetExpr
Name StreamName
Time Time
}
type SuccEqExpr struct { // implements OffsetExpr
Name StreamName
Time Time
}
type SuccExpr struct { // implements OffsetExpr
Name StreamName
Time Time
}
// offsets
func NewPrevEqExpr(n, t interface{}) PrevEqExpr {
name := getStreamName(n)
return PrevEqExpr{name, t.(Time)}
}
func NewPrevExpr(n, t interface{}) PrevExpr {
name := getStreamName(n)
return PrevExpr{name, t.(Time)}
}
func NewSuccEqExpr(n, t interface{}) SuccEqExpr {
name := getStreamName(n)
return SuccEqExpr{name, t.(Time)}
}
func NewSuccExpr(n, t interface{}) SuccExpr {
name := getStreamName(n)
return SuccExpr{name, t.(Time)}
}
func (e PrevEqExpr) Sprint() string {
return fmt.Sprintf("%s<~%s", e.Name.Sprint(), e.Time.Sprint())
}
func (e PrevExpr) Sprint() string {
return fmt.Sprintf("%s<<%s", e.Name.Sprint(), e.Time.Sprint())
}
func (e SuccEqExpr) Sprint() string {
return fmt.Sprintf("%s~>%s", e.Name.Sprint(), e.Time.Sprint())
}
func (e SuccExpr) Sprint() string {
return fmt.Sprintf("%s>>%s", e.Name.Sprint(), e.Time.Sprint())
}
*/