Skip to main content

tidepool_eval/
pass.rs

1use tidepool_repr::CoreExpr;
2
3/// Whether a pass changed the expression.
4pub type Changed = bool;
5
6/// An optimization pass over CoreExpr.
7pub trait Pass {
8    /// Run the pass, mutating the expression in place. Returns true if anything changed.
9    fn run(&self, expr: &mut CoreExpr) -> Changed;
10
11    /// Human-readable name for diagnostics.
12    fn name(&self) -> &str;
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18    use tidepool_repr::{CoreFrame, RecursiveTree, VarId};
19
20    struct NoOpPass;
21
22    impl Pass for NoOpPass {
23        fn run(&self, _expr: &mut CoreExpr) -> Changed {
24            false
25        }
26
27        fn name(&self) -> &str {
28            "NoOpPass"
29        }
30    }
31
32    #[test]
33    fn test_noop_pass() {
34        let pass = NoOpPass;
35        let mut expr = RecursiveTree {
36            nodes: vec![CoreFrame::Var(VarId(0))],
37        };
38        let changed = pass.run(&mut expr);
39        assert!(!changed);
40        assert_eq!(pass.name(), "NoOpPass");
41    }
42}