Skip to content

Commit

Permalink
add error checking file oops
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Jan 29, 2024
1 parent 6d2c4aa commit f825b88
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tree_shared/src/error_checking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Rules that perform sanity checks,
//! such as checking that switch children
//! are all `Branch`es.

use strum::IntoEnumIterator;

use crate::ir::{Constructor, ESort};

pub(crate) fn error_checking_rules() -> Vec<String> {
let mut res = vec![format!(
"
(relation IsBranchList (ListExpr))
(rule ((Switch pred outputs))
((IsBranchList outputs))
:ruleset error-checking)
(rule ((IsBranchList (Cons a rest)))
((IsBranchList rest))
:ruleset error-checking)
"
)];

for ctor in Constructor::iter() {
if ctor.sort() == ESort::ListExpr {
continue;
}
if ctor == Constructor::Branch {
continue;
}

let pat = ctor.construct(|field| field.var());
res.push(format!(
"
(rule ((IsBranchList (Cons {pat} rest)))
((panic \"Expected Branch, got {ctor}\"))
:ruleset error-checking)
"
));
}

res
}

#[test]
#[should_panic(expected = "Expected Branch, got Num")]
fn test_switch_with_num_child() {
let build = "
(Switch
(Num (Shared) 0)
(Cons
(Num (Shared) 1)
(Cons
(Branch (Shared) (Num (Shared) 2))
(Nil))))
";
crate::run_test(build, "").unwrap()
}

0 comments on commit f825b88

Please sign in to comment.