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
use rtlola_reporting::Span;

use super::{ChangeSet, SynSugar};
use crate::ast::{BinOp, Expression, ExpressionKind, Offset, RtLolaAst, StreamAccessKind};

/// Allows for using a delta(x,dft: 0)  function to compute the difference between the current and last value of x; defaults to 0.
///
/// Transforms:
/// delta(x,dft:0.0) → x - x.last(or: 0.0)
#[derive(Debug, Clone)]
pub(crate) struct Delta {}

impl Delta {
    fn apply<'a>(&self, expr: &Expression, ast: &'a RtLolaAst) -> ChangeSet {
        match &expr.kind {
            // Function(FunctionName, Vec<Type>, Vec<Expression>),
            ExpressionKind::Function(name, _types, args) => {
                let f_name = name.name.name.clone();
                /* currently not parsable but intended: , "δ", "Δ" */
                if !["delta"].contains(&f_name.as_str()) {
                    return ChangeSet::empty();
                }
                let arg_names = name.arg_names.clone();
                if arg_names.len() != 2 {
                    return ChangeSet::empty();
                }
                if arg_names[0].is_some() || arg_names[1].is_none() {
                    return ChangeSet::empty();
                }
                if !["dft", "default", "or"].contains(&arg_names[1].as_ref().unwrap().name.as_str()) {
                    return ChangeSet::empty();
                }
                let target_stream = args[0].clone();
                let new_id = expr.id.primed();

                let indirect_span = Span::Indirect(Box::new(expr.span.clone()));

                let sync = Expression {
                    kind: ExpressionKind::StreamAccess(Box::new(target_stream.clone()), StreamAccessKind::Sync),
                    id: ast.next_id(),
                    span: expr.span.clone(),
                };
                let offset = Expression {
                    kind: ExpressionKind::Offset(Box::new(target_stream), Offset::Discrete(-1)),
                    id: new_id,
                    span: expr.span.clone(),
                };
                let default_expr = args[1].clone();
                let default = Expression {
                    kind: ExpressionKind::Default(Box::new(offset), Box::new(default_expr)),
                    id: ast.next_id(),
                    span: indirect_span.clone(),
                };
                let res = Expression {
                    kind: ExpressionKind::Binary(BinOp::Sub, Box::new(sync), Box::new(default)),
                    id: new_id,
                    span: indirect_span,
                };
                ChangeSet::replace_current_expression(res)
            },
            _ => ChangeSet::empty(),
        }
    }
}

impl SynSugar for Delta {
    fn desugarize_expr<'a>(&self, exp: &'a Expression, ast: &'a RtLolaAst) -> ChangeSet {
        self.apply(exp, ast)
    }
}