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
//! Contains the internal representation of stream expressions inside the StreamIR
use super::{StreamReference, Type, WindowReference};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Represents a stream expression
pub struct Expr {
/// The type of the expression
pub ty: Type,
/// The kind of the expression
pub kind: ExprKind,
}
/// Represents the different kinds a expression can have
#[derive(Clone, Debug)]
pub enum ExprKind {
/// A Constant
Constant(Constant),
/// An binary operation
BinaryOperation(Operator, Box<Expr>, Box<Expr>),
/// An unary operation
UnaryOperation(Operator, Box<Expr>),
/// An if-then-else operation
Ite(Box<Expr>, Box<Expr>, Box<Expr>),
/// An synchronous access to a stream
SyncStreamAccess {
/// with that stream reference
target: StreamReference,
/// with these parameters
parameters: Vec<Expr>,
},
/// An synchronous access to a stream with an offset
OffsetStreamAccess {
/// with that stream reference
target: StreamReference,
/// the offset
offset: u32,
/// the default value of the access
default: Box<Expr>,
/// and these parameters
parameters: Vec<Expr>,
},
/// An asynchronous access to a stream
HoldStreamAccess {
/// with that stream reference
target: StreamReference,
/// the default value of the access
default: Box<Expr>,
/// and these parameters
parameters: Vec<Expr>,
},
/// An is fresh acess to a stream
IsFresh {
/// The target of the access
target: StreamReference,
/// and these parameters
parameters: Vec<Expr>,
},
/// An get access to a stream
GetAccess {
/// the target of the access
target: StreamReference,
/// the default value of the access
default: Box<Expr>,
/// and these parameters
parameters: Vec<Expr>,
},
/// The access to a window
WindowAccess {
/// the window aggregates of this stream
target: StreamReference,
/// the reference of the accessed sliding window
window: WindowReference,
/// the parameters
parameters: Vec<Expr>,
/// the default value (for e.g. average, min max window operations)
default: Option<Box<Expr>>,
},
/// The cast to another type
Cast(Type, Box<Expr>),
/// The access of a parameter of a stream
ParameterAccess(StreamReference, usize),
/// A function call (with the given arguments)
FunctionCall(Function, Vec<Expr>),
/// The construction of a tuple expression
Tuple(Vec<Expr>),
/// The access of an element in a tuple
TupleAccess(Box<Expr>, usize),
/// The access to a parameter of a lambda expression
LambdaParameterAccess(WindowReference, usize),
}
impl std::hash::Hash for ExprKind {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
/// Represent the different functions usable in expressions
pub enum Function {
/// The square root function
Sqrt,
/// The absolute value function
Abs,
/// The sinus function
Sin,
/// The arcsin function
Arcsin,
/// The cosinus function
Cos,
/// The arccos function
Arccos,
/// The tan function
Tan,
/// The arctan function
Arctan,
/// The minimum function
Min,
/// The maximum function
Max,
}
/// Represents a constant of a stream expression
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Constant {
/// A string
Str(String),
/// A boolean
Bool(bool),
/// An unsigned integer
UInt(u64, u16),
/// A signed integer
Int(i64, u16),
/// A 32-bit floating point number
Float32(f64),
/// A 64-bit floating point number
Float64(f64),
/// A constant tuple
Tuple(Vec<Constant>),
}
/// Represents a binary or unary operation in a stream expression
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Operator {
/// Binary negation
Not,
/// Numeric negation
Neg,
/// Addition
Add,
/// Subtraction
Sub,
/// Multiplication
Mul,
/// Division
Div,
/// Modulo
Rem,
/// Power
Pow,
/// Binary and
And,
/// Binary or
Or,
/// Bitwise xor
BitXor,
/// Bitwise and
BitAnd,
/// Bitwise or
BitOr,
/// Bitwise not
BitNot,
/// left-shift
Shl,
/// right-shift
Shr,
/// equality test
Eq,
/// less-than test
Lt,
/// less-than or equal test
Le,
/// not-equal test
Ne,
/// greater or equal test
Ge,
/// greater-than test
Gt,
}
impl PartialEq for ExprKind {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Constant(l0), Self::Constant(r0)) => l0 == r0,
(Self::BinaryOperation(l0, l1, l2), Self::BinaryOperation(r0, r1, r2)) => {
l0 == r0 && l1 == r1 && l2 == r2
}
(Self::UnaryOperation(l0, l1), Self::UnaryOperation(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Ite(l0, l1, l2), Self::Ite(r0, r1, r2)) => l0 == r0 && l1 == r1 && l2 == r2,
(
Self::SyncStreamAccess {
target: l_target,
parameters: l_parameters,
},
Self::SyncStreamAccess {
target: r_target,
parameters: r_parameters,
},
) => l_target == r_target && l_parameters == r_parameters,
(
Self::OffsetStreamAccess {
target: l_target,
offset: l_offset,
default: l_default,
parameters: l_parameters,
},
Self::OffsetStreamAccess {
target: r_target,
offset: r_offset,
default: r_default,
parameters: r_parameters,
},
) => {
l_target == r_target
&& l_offset == r_offset
&& l_default == r_default
&& l_parameters == r_parameters
}
(
Self::HoldStreamAccess {
target: l_target,
default: l_default,
parameters: l_parameters,
},
Self::HoldStreamAccess {
target: r_target,
default: r_default,
parameters: r_parameters,
},
) => l_target == r_target && l_default == r_default && l_parameters == r_parameters,
(
Self::IsFresh {
target: l_target,
parameters: l_parameters,
},
Self::IsFresh {
target: r_target,
parameters: r_parameters,
},
) => l_target == r_target && l_parameters == r_parameters,
(
Self::GetAccess {
target: l_target,
default: l_default,
parameters: l_parameters,
},
Self::GetAccess {
target: r_target,
default: r_default,
parameters: r_parameters,
},
) => l_target == r_target && l_default == r_default && l_parameters == r_parameters,
(
Self::WindowAccess {
target: l_target,
window: l_window,
parameters: l_parameters,
default: l_default,
},
Self::WindowAccess {
target: r_target,
window: r_window,
parameters: r_parameters,
default: r_default,
},
) => {
l_target == r_target
&& l_window == r_window
&& l_parameters == r_parameters
&& l_default == r_default
}
(Self::Cast(l0, l1), Self::Cast(r0, r1)) => l0 == r0 && l1 == r1,
(Self::ParameterAccess(_, l1), Self::ParameterAccess(_, r1)) => l1 == r1, // ignore the stream reference
(Self::FunctionCall(l0, l1), Self::FunctionCall(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Tuple(l0), Self::Tuple(r0)) => l0 == r0,
(Self::TupleAccess(l0, l1), Self::TupleAccess(r0, r1)) => l0 == r0 && l1 == r1,
(Self::LambdaParameterAccess(l0, l1), Self::LambdaParameterAccess(r0, r1)) => {
l0 == r0 && l1 == r1
}
_ => false,
}
}
}
impl Eq for ExprKind {}
impl Expr {
/// Returns whether the expression contains an expression to the streams own parameter
pub fn contains_parameter_access(&self) -> Option<StreamReference> {
match &self.kind {
ExprKind::ParameterAccess(s, _) => Some(*s),
ExprKind::Constant(_) => None,
ExprKind::BinaryOperation(_, lhs, rhs) => lhs
.contains_parameter_access()
.or_else(|| rhs.contains_parameter_access()),
ExprKind::Cast(_, expr) | ExprKind::UnaryOperation(_, expr) => {
expr.contains_parameter_access()
}
ExprKind::Ite(cond, cons, alt) => cond
.contains_parameter_access()
.or_else(|| cons.contains_parameter_access())
.or_else(|| alt.contains_parameter_access()),
ExprKind::IsFresh {
target: _,
parameters,
}
| ExprKind::WindowAccess {
target: _,
window: _,
parameters,
default: None,
}
| ExprKind::SyncStreamAccess {
target: _,
parameters,
} => parameters.iter().find_map(Self::contains_parameter_access),
ExprKind::HoldStreamAccess {
target: _,
default,
parameters,
}
| ExprKind::GetAccess {
target: _,
default,
parameters,
}
| ExprKind::WindowAccess {
target: _,
window: _,
parameters,
default: Some(default),
}
| ExprKind::OffsetStreamAccess {
target: _,
offset: _,
default,
parameters,
} => parameters
.iter()
.find_map(Self::contains_parameter_access)
.or_else(|| default.contains_parameter_access()),
ExprKind::FunctionCall(_, exprs) | ExprKind::Tuple(exprs) => {
exprs.iter().find_map(Self::contains_parameter_access)
}
ExprKind::TupleAccess(expr, _) => expr.contains_parameter_access(),
ExprKind::LambdaParameterAccess(_, _) => None,
}
}
}