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
494
495
496
497
498
499
500
use std::fmt::{Display, Formatter, Result};

use itertools::Itertools;

use super::{FloatTy, InputStream, IntTy, Mir, OutputStream, PacingType, Stream, Trigger, UIntTy, WindowOperation};
use crate::mir::{
    ActivationCondition, ArithLogOp, Constant, Expression, ExpressionKind, Offset, StreamAccessKind, Type,
};

impl Display for Constant {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Constant::Bool(b) => write!(f, "{b}"),
            Constant::UInt(u) => write!(f, "{u}"),
            Constant::Int(i) => write!(f, "{i}"),
            Constant::Float(fl) => write!(f, "{fl}"),
            Constant::Str(s) => write!(f, "{s}"),
        }
    }
}

impl Display for ArithLogOp {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        use ArithLogOp::*;
        match self {
            Not => write!(f, "!"),
            Neg => write!(f, "~"),
            Add => write!(f, "+"),
            Sub => write!(f, "-"),
            Mul => write!(f, "*"),
            Div => write!(f, "/"),
            Rem => write!(f, "%"),
            Pow => write!(f, "^"),
            And => write!(f, "∧"),
            Or => write!(f, "∨"),
            Eq => write!(f, "="),
            Lt => write!(f, "<"),
            Le => write!(f, "≤"),
            Ne => write!(f, "≠"),
            Ge => write!(f, "≥"),
            Gt => write!(f, ">"),
            BitNot => write!(f, "~"),
            BitAnd => write!(f, "&"),
            BitOr => write!(f, "|"),
            BitXor => write!(f, "^"),
            Shl => write!(f, "<<"),
            Shr => write!(f, ">>"),
        }
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Type::Float(_) => write!(f, "Float{}", self.size().expect("Floats are sized.").0 * 8),
            Type::UInt(_) => write!(f, "UInt{}", self.size().expect("UInts are sized.").0 * 8),
            Type::Int(_) => write!(f, "Int{}", self.size().expect("Ints are sized.").0 * 8),
            Type::Function { args, ret } => write_delim_list(f, args, "(", &format!(") -> {ret}"), ","),
            Type::Tuple(elems) => write_delim_list(f, elems, "(", ")", ","),
            Type::String => write!(f, "String"),
            Type::Bytes => write!(f, "Bytes"),
            Type::Option(inner) => write!(f, "Option<{inner}>"),
            Type::Bool => write!(f, "Bool"),
        }
    }
}

impl Display for IntTy {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            IntTy::Int8 => write!(f, "8"),
            IntTy::Int16 => write!(f, "16"),
            IntTy::Int32 => write!(f, "32"),
            IntTy::Int64 => write!(f, "64"),
        }
    }
}

impl Display for UIntTy {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            UIntTy::UInt8 => write!(f, "8"),
            UIntTy::UInt16 => write!(f, "16"),
            UIntTy::UInt32 => write!(f, "32"),
            UIntTy::UInt64 => write!(f, "64"),
        }
    }
}

impl Display for FloatTy {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            FloatTy::Float32 => write!(f, "32"),
            FloatTy::Float64 => write!(f, "64"),
        }
    }
}

/// Writes out the joined vector `v`, enclosed by the given strings `pref` and `suff`.
/// Uses the formatter.
pub(crate) fn write_delim_list<T: Display>(
    f: &mut Formatter<'_>,
    v: &[T],
    pref: &str,
    suff: &str,
    join: &str,
) -> Result {
    write!(f, "{pref}")?;
    if let Some(e) = v.first() {
        write!(f, "{e}")?;
        for b in &v[1..] {
            write!(f, "{join}{b}")?;
        }
    }
    write!(f, "{suff}")?;
    Ok(())
}

impl Display for Offset {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Offset::Past(u) => write!(f, "{u}"),
            _ => unimplemented!(),
        }
    }
}

impl Display for WindowOperation {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(
            f,
            "{}",
            match self {
                WindowOperation::Count => "count",
                WindowOperation::Min => "min",
                WindowOperation::Max => "max",
                WindowOperation::Sum => "sum",
                WindowOperation::Product => "product",
                WindowOperation::Average => "average",
                WindowOperation::Integral => "integral",
                WindowOperation::Conjunction => "conjunction",
                WindowOperation::Disjunction => "disjunction",
                WindowOperation::Last => "last",
                WindowOperation::Variance => "variance",
                WindowOperation::Covariance => "covariance",
                WindowOperation::StandardDeviation => "standard deviation",
                WindowOperation::NthPercentile(_) => todo!(),
            }
        )
    }
}

/// A lightweight wrapper around the Mir to provide a [Display] implementation for Mir struct `T`.
#[derive(Debug, Clone, Copy)]
pub struct RtLolaMirPrinter<'a, T> {
    mir: &'a Mir,
    inner: &'a T,
}

impl<'a, T> RtLolaMirPrinter<'a, T> {
    pub(crate) fn new(mir: &'a Mir, target: &'a T) -> Self {
        RtLolaMirPrinter { mir, inner: target }
    }
}

impl<'a, T: Display> Display for RtLolaMirPrinter<'a, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        self.inner.fmt(f)
    }
}

impl<'a> Display for RtLolaMirPrinter<'a, ActivationCondition> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self.inner {
            ActivationCondition::Conjunction(s) => {
                let rs = s
                    .iter()
                    .map(|ac| RtLolaMirPrinter::new(self.mir, ac).to_string())
                    .join(&ArithLogOp::And.to_string());
                write!(f, "{rs}")
            },
            ActivationCondition::Disjunction(s) => {
                let rs = s
                    .iter()
                    .map(|ac| RtLolaMirPrinter::new(self.mir, ac).to_string())
                    .join(&ArithLogOp::Or.to_string());
                write!(f, "{rs}")
            },
            ActivationCondition::Stream(s) => write!(f, "{}", self.mir.stream(*s).name()),
            ActivationCondition::True => write!(f, "true"),
        }
    }
}

impl<'a> Display for RtLolaMirPrinter<'a, PacingType> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self.inner {
            super::PacingType::Periodic(freq) => {
                let s = freq
                    .into_format_args(uom::si::frequency::hertz, uom::fmt::DisplayStyle::Abbreviation)
                    .to_string();
                write!(f, "{}Hz", &s[..s.len() - 3])
            },
            super::PacingType::Event(ac) => RtLolaMirPrinter::new(self.mir, ac).fmt(f),
            super::PacingType::Constant => write!(f, "true"),
        }
    }
}

type Associativity = bool;

fn precedence_level(op: &ArithLogOp) -> (u32, Associativity) {
    // https://en.cppreference.com/w/c/language/operator_precedence
    let precedence = match op {
        ArithLogOp::Not | ArithLogOp::BitNot | ArithLogOp::Neg => 2,

        ArithLogOp::Mul | ArithLogOp::Rem | ArithLogOp::Pow | ArithLogOp::Div => 3,

        ArithLogOp::Add | ArithLogOp::Sub => 4,

        ArithLogOp::Shl | ArithLogOp::Shr => 5,

        ArithLogOp::Lt | ArithLogOp::Le | ArithLogOp::Ge | ArithLogOp::Gt => 6,

        ArithLogOp::Eq | ArithLogOp::Ne => 7,

        ArithLogOp::BitAnd => 8,
        ArithLogOp::BitXor => 9,
        ArithLogOp::BitOr => 10,
        ArithLogOp::And => 11,
        ArithLogOp::Or => 12,
    };

    let associativity = !matches!(op, ArithLogOp::Div | ArithLogOp::Sub);

    (precedence, associativity)
}

pub(crate) fn display_expression(mir: &Mir, expr: &Expression, current_level: u32) -> String {
    match &expr.kind {
        ExpressionKind::LoadConstant(c) => c.to_string(),
        ExpressionKind::ArithLog(op, exprs) => {
            let (op_level, associative) = precedence_level(op);
            let display_exprs = exprs
                .iter()
                .map(|expr| display_expression(mir, expr, op_level))
                .collect::<Vec<_>>();
            let display = match display_exprs.len() {
                1 => format!("{}{}", op, display_exprs[0]),
                2 => format!("{} {} {}", display_exprs[0], op, display_exprs[1]),
                _ => unreachable!(),
            };
            if (associative && current_level < op_level || !associative && current_level <= op_level)
                && current_level != 0
            {
                format!("({display})")
            } else {
                display
            }
        },
        ExpressionKind::StreamAccess {
            target,
            parameters,
            access_kind,
        } => {
            let stream_name = mir.stream(*target).name();
            let target_name = if !parameters.is_empty() {
                let parameter_list = parameters
                    .iter()
                    .map(|parameter| display_expression(mir, parameter, 0))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{stream_name}({parameter_list})")
            } else {
                stream_name.into()
            };

            match access_kind {
                StreamAccessKind::Sync => target_name,
                StreamAccessKind::DiscreteWindow(_) => todo!(),
                StreamAccessKind::SlidingWindow(w) => {
                    let window = mir.sliding_window(*w);
                    let target_name = mir.stream(window.target).name();
                    let duration = window.duration.as_secs_f64().to_string();
                    let op = &window.op;
                    format!("{target_name}.aggregate(over: {duration}s, using: {op})")
                },
                StreamAccessKind::Hold => format!("{target_name}.hold()"),
                StreamAccessKind::Offset(o) => format!("{target_name}.offset(by:-{o})"),
                StreamAccessKind::Get => format!("{target_name}.get()"),
                StreamAccessKind::Fresh => format!("{target_name}.fresh()"),
            }
        },
        ExpressionKind::ParameterAccess(sref, parameter) => mir.output(*sref).params[*parameter].name.to_string(),
        ExpressionKind::Ite {
            condition,
            consequence,
            alternative,
        } => {
            let display_condition = display_expression(mir, condition, 0);
            let display_consequence = display_expression(mir, consequence, 0);
            let display_alternative = display_expression(mir, alternative, 0);
            format!("if {display_condition} then {display_consequence} else {display_alternative}")
        },
        ExpressionKind::Tuple(exprs) => {
            let display_exprs = exprs
                .iter()
                .map(|expr| display_expression(mir, expr, 0))
                .collect::<Vec<_>>()
                .join(", ");
            format!("({display_exprs})")
        },
        ExpressionKind::TupleAccess(expr, i) => {
            let display_expr = display_expression(mir, expr, 20);
            format!("{display_expr}({i})")
        },
        ExpressionKind::Function(name, args) => {
            let display_args = args
                .iter()
                .map(|arg| display_expression(mir, arg, 0))
                .collect::<Vec<_>>()
                .join(", ");
            format!("{name}({display_args})")
        },
        ExpressionKind::Convert { expr: inner_expr } => {
            let inner_display = display_expression(mir, inner_expr, 0);
            format!("Cast<{},{}>({inner_display})", expr.ty, inner_expr.ty)
        },
        ExpressionKind::Default { expr, default } => {
            let display_expr = display_expression(mir, expr, 0);
            let display_default = display_expression(mir, default, 0);
            format!("{display_expr}.defaults(to: {display_default})")
        },
    }
}

impl<'a> Display for RtLolaMirPrinter<'a, Expression> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "{}", display_expression(self.mir, self.inner, 0))
    }
}

impl Display for InputStream {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let name = &self.name;
        let ty = &self.ty;
        write!(f, "input {name} : {ty}")
    }
}

impl<'a> Display for RtLolaMirPrinter<'a, OutputStream> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let OutputStream {
            name,
            ty,
            spawn,
            eval,
            close,
            params,
            ..
        } = self.inner;

        let display_pacing = RtLolaMirPrinter::new(self.mir, &eval.eval_pacing).to_string();
        let display_parameters = if !params.is_empty() {
            let parameter_list = params
                .iter()
                .map(|parameter| format!("{} : {}", parameter.name, parameter.ty))
                .join(", ");
            format!("({parameter_list})")
        } else {
            "".into()
        };

        writeln!(f, "output {name}{display_parameters} : {ty}")?;

        if let Some(spawn_expr) = &spawn.expression {
            let display_spawn_expr = display_expression(self.mir, spawn_expr, 0);
            write!(f, "  spawn with {display_spawn_expr}")?;
            if let Some(spawn_condition) = &spawn.condition {
                let display_spawn_condition = display_expression(self.mir, spawn_condition, 0);
                write!(f, " when {display_spawn_condition}")?;
            }
            writeln!(f)?;
        }

        write!(f, "  eval @{display_pacing} ")?;
        if let Some(eval_condition) = &eval.condition {
            let display_eval_condition = display_expression(self.mir, eval_condition, 0);
            write!(f, "when {display_eval_condition} ")?;
        }
        let display_eval_expr = display_expression(self.mir, &eval.expression, 0);
        write!(f, "with {display_eval_expr}")?;

        if let Some(close_condition) = &close.condition {
            let display_close_condition = display_expression(self.mir, close_condition, 0);
            write!(f, "\n  close when {display_close_condition}")?;
        }

        Ok(())
    }
}

impl<'a> Display for RtLolaMirPrinter<'a, Trigger> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let output = self.mir.output(self.inner.reference);
        let output_name = output.name();
        let message = &self.inner.message;

        write!(f, "trigger {output_name} \"{message}\"")
    }
}

impl Display for Mir {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        self.inputs.iter().try_for_each(|input| {
            RtLolaMirPrinter::new(self, input).fmt(f)?;
            write!(f, "\n\n")
        })?;

        self.outputs.iter().try_for_each(|output| {
            RtLolaMirPrinter::new(self, output).fmt(f)?;
            write!(f, "\n\n")
        })?;

        self.triggers.iter().try_for_each(|trigger| {
            RtLolaMirPrinter::new(self, trigger).fmt(f)?;
            write!(f, "\n\n")
        })
    }
}

#[cfg(test)]
mod tests {
    use rtlola_parser::ParserConfig;

    use super::display_expression;
    use crate::parse;

    macro_rules! test_display_expression {
        ( $( $name:ident: $test:expr => $expected:literal, )+) => {
            $(
            #[test]
            fn $name() {
                let spec = format!("input a : UInt64\noutput b@a := {}", $test);
                let config = ParserConfig::for_string(spec);
                let mir = parse(config).expect("should parse");
                let expr = &mir.outputs[0].eval.expression;
                let display_expr = display_expression(&mir, expr, 0);
                assert_eq!(display_expr, $expected);
            }
            )+
        }
    }

    test_display_expression! {
        constant:
        "1" => "1",
        add:
        "1+2" => "1 + 2",
        mul:
        "1*2" => "1 * 2",
        add_after_mul:
        "1+2*3" => "1 + 2 * 3",
        mul_after_add:
        "1*(2+3)" => "1 * (2 + 3)",
        comparison1:
        "(1 > 2) && !false || (2 != 2)" => "1 > 2 ∧ !false ∨ 2 ≠ 2",
        comparison2:
        "(true == (1 > 2 || false)" => "true = (1 > 2 ∨ false)",
        associativity:
        "1 - (2 - 3)" => "1 - (2 - 3)",
        associativity2:
        "1 + (2 + 3)" => "1 + 2 + 3",
        sync_access:
        "a + 5" => "a + 5",
        hold_access:
        "a.hold().defaults(to: 0)" => "a.hold().defaults(to: 0)",
        offset_access:
        "a.offset(by:-2).defaults(to: 2+2)" => "a.offset(by:-2).defaults(to: 2 + 2)",
    }

    #[test]
    fn test() {
        let example = "input a : UInt64
        input b : UInt64
        output c := a + b.hold().defaults(to:0)
        output d@10Hz := b.aggregate(over: 2s, using: sum) + c.hold().defaults(to:0)
        output e(x)
            spawn with a when b == 0
            eval when x == a with e(x).offset(by:-1).defaults(to:0) + 1
            close when x == a && e(x) == 0
        trigger c > 5 \"message\"
        ";

        let config = ParserConfig::for_string(example.into());
        let mir = parse(config).expect("should parse");
        let config = ParserConfig::for_string(mir.to_string());
        parse(config).expect("should also parse");
    }
}