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
use super::{
    build_nfa::{Action, Edge, Length, Vertex, NFA},
    expression::clone_filter,
    Expression, Irp, Options,
};
use log::info;
use std::{
    collections::{HashMap, HashSet},
    hash::Hash,
    rc::Rc,
};

/// Deterministic finite automation for decoding IR. Using this we can match IR.
#[derive(Debug, Default)]
#[allow(clippy::upper_case_acronyms)]
pub struct DFA {
    pub(crate) verts: Vec<Vertex>,
}

#[derive(Clone, Debug)]
struct Path {
    from: usize,
    to: usize,
    edge_no: usize,
}

#[derive(Hash, PartialEq, Eq)]

struct DfaEdge {
    from: usize,
    flash: bool,
    length: Length,
    actions: Vec<Action>,
}

impl NFA {
    /// Build the DFA from the NFA
    pub fn build_dfa(&self, options: &Options) -> DFA {
        let mut builder = Builder {
            options,
            verts: Vec::new(),
            nfa_to_dfa: HashMap::new(),
            edges: HashMap::new(),
            nfa: self,
            visited: HashSet::new(),
        };

        builder.build();

        let dfa = DFA {
            verts: builder.verts,
        };

        if options.nfa {
            let filename = options.filename("_nfa.dot");
            info!("saving NFA as {filename}");
            self.dotgraphviz(&filename);
        }

        if options.dfa {
            let filename = options.filename("_dfa.dot");
            info!("saving DFA as {filename}");
            dfa.dotgraphviz(&filename);
        }

        dfa
    }
}

impl DFA {
    /// Generate a GraphViz dot file and write to the given path
    pub fn dotgraphviz(&self, path: &str) {
        crate::graphviz::graphviz(&self.verts, "DFA", &[], path);
    }
}

impl<'a> Options<'a> {
    /// Create file name for saving of intermediates. The extension should include the dot,
    /// so we can have `_nfa.dot` as extension.
    pub(crate) fn filename(&self, ext: &str) -> String {
        // characters not allowed on Windows/Mac/Linux: https://stackoverflow.com/a/35352640

        self.name
            .chars()
            .filter(|c| !matches!(c, ':' | '/' | '\\' | '*' | '?' | '"' | '<' | '>' | '|'))
            .chain(ext.chars())
            .collect::<String>()
    }
}

impl Irp {
    /// Generate an DFA decoder state machine for this IRP
    pub fn compile(&self, options: &Options) -> Result<DFA, String> {
        let nfa = self.build_nfa()?;

        Ok(nfa.build_dfa(options))
    }
}

struct Builder<'a> {
    options: &'a Options<'a>,
    nfa: &'a NFA,
    nfa_to_dfa: HashMap<usize, usize>,
    edges: HashMap<DfaEdge, usize>,
    verts: Vec<Vertex>,
    visited: HashSet<usize>,
}

impl<'a> Builder<'a> {
    fn build(&mut self) {
        assert_eq!(self.add_vertex(), 0);

        self.verts[0].entry.clone_from(&self.nfa.verts[0].entry);

        self.nfa_to_dfa.insert(0, 0);

        self.recurse_nfa_path(true, vec![0]);
    }

    // Recursively add a new path
    fn recurse_nfa_path(&mut self, flash: bool, pos: Vec<usize>) {
        struct UniqueEdge {
            length: Length,
            actions: Vec<Action>,
            nfa_edges: Vec<(usize, usize)>,
        }

        let mut paths = Vec::new();

        for pos in pos {
            if !self.visited.contains(&pos) {
                self.input_closure(pos, flash, Vec::new(), &mut paths);
                self.visited.insert(pos);
            }
        }

        let mut edges: Vec<UniqueEdge> = Vec::new();

        for path in &paths {
            let length = self.path_length(flash, path);
            let actions = self.path_actions(path);
            let from = path[0].from;
            let to = path.last().unwrap().to;

            // If an edge overlaps with an existing edge, merge it
            if let Some(e) = edges
                .iter_mut()
                .find(|e| e.length.overlaps(&length) && e.actions == actions)
            {
                e.length = e.length.merge(&length);
                e.nfa_edges.push((from, to));
            } else {
                edges.push(UniqueEdge {
                    length,
                    actions,
                    nfa_edges: vec![(from, to)],
                });
            }
        }

        for UniqueEdge {
            length,
            actions,
            nfa_edges,
        } in edges
        {
            let pos = nfa_edges.iter().map(|(_, nfa_to)| *nfa_to).collect();
            self.add_path_to_dfa(flash, actions, length, nfa_edges);

            self.recurse_nfa_path(!flash, pos);
        }
    }

    fn add_path_to_dfa(
        &mut self,
        flash: bool,
        mut actions: Vec<Action>,
        length: Length,
        nfa_edges: Vec<(usize, usize)>,
    ) {
        let from = self.nfa_to_dfa[&nfa_edges[0].0];

        actions.insert(
            0,
            if flash {
                Action::Flash {
                    length: length.clone(),
                    complete: true,
                }
            } else {
                Action::Gap {
                    length: length.clone(),
                    complete: true,
                }
            },
        );

        let dfa_edge = DfaEdge {
            from,
            flash,
            length,
            actions: actions.clone(),
        };

        if let Some(to) = self.edges.get(&dfa_edge) {
            if self.verts[from]
                .edges
                .iter()
                .any(|edge| edge.dest == *to && edge.actions == actions)
            {
                for (nfa_from, nfa_to) in nfa_edges {
                    self.nfa_to_dfa.insert(nfa_from, from);
                    self.nfa_to_dfa.insert(nfa_to, *to);
                }
                return;
            }
        }

        let to = if let Some(vert_no) = self.nfa_to_dfa.get(&nfa_edges[0].1) {
            *vert_no
        } else {
            self.add_vertex()
        };

        for (nfa_from, nfa_to) in nfa_edges {
            self.nfa_to_dfa.insert(nfa_from, from);
            self.nfa_to_dfa.insert(nfa_to, to);
        }

        self.edges.insert(dfa_edge, to);

        self.verts[from].edges.push(Edge { dest: to, actions });
    }

    fn path_length(&self, flash: bool, path: &[Path]) -> Length {
        let mut len: Option<Rc<Expression>> = None;

        let mut vars: HashMap<&str, Rc<Expression>> = HashMap::new();

        for elem in path {
            for action in self.nfa.verts[elem.from].edges[elem.edge_no]
                .actions
                .iter()
                .chain(&self.nfa.verts[elem.to].entry)
            {
                match action {
                    Action::Gap { length, .. } | Action::Flash { length, .. } => {
                        let length = match length {
                            Length::Expression(expr) => expr,
                            Length::Range(..) => unreachable!(),
                        };

                        let length = replace_vars(length, &vars);

                        if let Some(prev) = len {
                            if let (Expression::Number(left), Expression::Number(right)) =
                                (length.as_ref(), prev.as_ref())
                            {
                                // TODO: proper const folding
                                len = Some(Rc::new(Expression::Number(left + right)));
                            } else {
                                len = Some(Rc::new(Expression::Add(length.clone(), prev)));
                            }
                        } else {
                            len = Some(length.clone());
                        }
                    }
                    Action::Set { var, expr } => {
                        let expr = replace_vars(expr, &vars);

                        vars.insert(var, expr);
                    }
                    _ => (),
                }
            }
        }

        let length = len.unwrap();

        if let Expression::Number(length) = length.as_ref() {
            let length = *length as u32;
            let min = std::cmp::min(
                length.saturating_sub(self.options.aeps),
                (length * (100 - self.options.eps)) / 100,
            );

            let max = std::cmp::max(
                length + self.options.aeps,
                (length * (100 + self.options.eps)) / 100,
            );

            if !flash && self.options.max_gap > 0 {
                if min > self.options.max_gap {
                    Length::Range(self.options.max_gap, None)
                } else if max > self.options.max_gap {
                    Length::Range(min, None)
                } else {
                    Length::Range(min, Some(max))
                }
            } else {
                Length::Range(min, Some(max))
            }
        } else {
            Length::Expression(length)
        }
    }

    fn path_actions(&self, path: &[Path]) -> Vec<Action> {
        let mut res: Vec<Action> = Vec::new();

        for elem in path {
            res.extend(self.nfa.verts[elem.to].entry.iter().cloned());
            res.extend(
                self.nfa.verts[elem.from].edges[elem.edge_no]
                    .actions
                    .iter()
                    .filter(|action| !matches!(action, Action::Flash { .. } | Action::Gap { .. }))
                    .cloned(),
            );
        }

        res
    }

    fn input_closure(
        &self,
        pos: usize,
        flash: bool,
        current_path: Vec<Path>,
        res: &mut Vec<Vec<Path>>,
    ) {
        for path in self.get_input_edges(pos, flash) {
            let mut p = current_path.clone();
            let pos = path.to;
            if current_path.iter().all(|p| p.from != pos) {
                p.push(path);
                res.push(p.clone());
                self.input_closure(pos, flash, p, res);
            }
        }

        for path in self.get_non_input_edges(pos) {
            let mut p = current_path.clone();
            let pos = path.to;
            if current_path.iter().all(|p| p.from != pos) {
                p.push(path);
                self.input_closure(pos, flash, p, res);
            }
        }
    }

    fn get_input_edges(&self, pos: usize, flash: bool) -> Vec<Path> {
        let mut res = Vec::new();
        for (edge_no, edge) in self.nfa.verts[pos].edges.iter().enumerate() {
            for action in &edge.actions {
                match action {
                    Action::Flash { .. } if flash => {
                        res.push(Path {
                            from: pos,
                            to: edge.dest,
                            edge_no,
                        });
                        break;
                    }
                    Action::Gap { .. } if !flash => {
                        res.push(Path {
                            from: pos,
                            to: edge.dest,
                            edge_no,
                        });
                        break;
                    }
                    _ => (),
                }
            }
        }
        res
    }

    fn get_non_input_edges(&self, pos: usize) -> Vec<Path> {
        let mut res = Vec::new();
        for (i, edge) in self.nfa.verts[pos].edges.iter().enumerate() {
            if !edge
                .actions
                .iter()
                .any(|action| matches!(action, Action::Flash { .. } | Action::Gap { .. }))
            {
                res.push(Path {
                    from: pos,
                    to: edge.dest,
                    edge_no: i,
                });
            }
        }

        res
    }

    fn add_vertex(&mut self) -> usize {
        let node = self.verts.len();

        self.verts.push(Vertex::default());

        node
    }
}

fn replace_vars(expr: &Rc<Expression>, vars: &HashMap<&str, Rc<Expression>>) -> Rc<Expression> {
    clone_filter(expr, &|e| {
        if let Expression::Identifier(id) = e.as_ref() {
            if let Some(expr) = vars.get(&id.as_str()) {
                return Some(expr.clone());
            }
        }
        None
    })
    .unwrap_or(expr.clone())
}

impl Length {
    fn overlaps(&self, other: &Self) -> bool {
        if let (Length::Range(min1, max1), Length::Range(min2, max2)) = (self, other) {
            let max1 = max1.unwrap_or(u32::MAX);
            let max2 = max2.unwrap_or(u32::MAX);

            !(max1 < *min2 || max2 < *min1)
        } else {
            false
        }
    }

    fn merge(&self, other: &Self) -> Length {
        debug_assert!(self.overlaps(other));

        if let (Length::Range(min1, max1), Length::Range(min2, max2)) = (self, other) {
            Length::Range(std::cmp::min(*min1, *min2), std::cmp::max(*max1, *max2))
        } else {
            unreachable!();
        }
    }
}

#[test]
fn overlaps() {
    assert!(!Length::Range(1, Some(10)).overlaps(&Length::Range(11, Some(20))));
    assert!(!Length::Range(11, Some(20)).overlaps(&Length::Range(1, Some(10))));

    assert!(Length::Range(1, Some(11)).overlaps(&Length::Range(11, Some(20))));
    assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(1, Some(11))));

    assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(11, Some(20))));
    assert!(Length::Range(5, Some(25)).overlaps(&Length::Range(11, Some(20))));
    assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(5, Some(25))));

    assert!(Length::Range(5, None).overlaps(&Length::Range(11, Some(20))));
    assert!(!Length::Range(21, None).overlaps(&Length::Range(11, Some(20))));

    assert!(Length::Range(5, Some(25)).overlaps(&Length::Range(11, None)));
    assert!(!Length::Range(11, Some(20)).overlaps(&Length::Range(21, None)));

    assert!(Length::Range(5, None).overlaps(&Length::Range(11, None)));
}