Skip to content

Commit

Permalink
Split f and r
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 23, 2024
1 parent f9ec679 commit 2336abe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
21 changes: 15 additions & 6 deletions crates/rue-typing/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub enum Check {
And(Vec<Check>),
Or(Vec<Check>),
If(Box<Check>, Box<Check>, Box<Check>),
Pair(Box<Check>, Box<Check>),
First(Box<Check>),
Rest(Box<Check>),
}

impl fmt::Display for Check {
Expand Down Expand Up @@ -164,7 +165,10 @@ where
let (rhs_first, rhs_rest) = (*rhs_first, *rhs_rest);
let first = check_type(types, lhs_first, rhs_first, visited)?;
let rest = check_type(types, lhs_rest, rhs_rest, visited)?;
Check::Pair(Box::new(first), Box::new(rest))
Check::And(vec![
Check::First(Box::new(first)),
Check::Rest(Box::new(rest)),
])
}
};

Expand Down Expand Up @@ -310,12 +314,17 @@ where
let rest =
check_union_against_rhs(types, original_type_id, &rest_items, rest, visited)?;

let pair_check = Check::Pair(Box::new(first), Box::new(rest));

if pairs.len() == length {
pair_check
Check::And(vec![
Check::First(Box::new(first)),
Check::Rest(Box::new(rest)),
])
} else {
Check::And(vec![Check::IsPair, pair_check])
Check::And(vec![
Check::IsPair,
Check::First(Box::new(first)),
Check::Rest(Box::new(rest)),
])
}
}
})
Expand Down
14 changes: 11 additions & 3 deletions crates/rue-typing/src/check/simplify_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ pub(crate) fn simplify_check(check: Check) -> Check {
let else_ = simplify_check(*else_);
Check::If(Box::new(cond), Box::new(then), Box::new(else_))
}
Check::Pair(first, rest) => {
Check::First(first) => {
let first = simplify_check(*first);

if first == Check::None {
Check::None
} else {
Check::First(Box::new(first))
}
}
Check::Rest(rest) => {
let rest = simplify_check(*rest);

if first == Check::None && rest == Check::None {
if rest == Check::None {
Check::None
} else {
Check::Pair(Box::new(first), Box::new(rest))
Check::Rest(Box::new(rest))
}
}
}
Expand Down
38 changes: 11 additions & 27 deletions crates/rue-typing/src/check/stringify_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,17 @@ pub(crate) fn stringify_check(
stringify_check(else_, f, path)?;
write!(f, ")")
}
Check::Pair(first, rest) => {
let has_first = first.as_ref() != &Check::None;
let has_rest = rest.as_ref() != &Check::None;

if has_first && has_rest {
write!(f, "(all ")?;
path.push(TypePath::First);
stringify_check(first, f, path)?;
path.pop().unwrap();
write!(f, " ")?;
path.push(TypePath::Rest);
stringify_check(rest, f, path)?;
path.pop().unwrap();
write!(f, ")")
} else if has_first {
path.push(TypePath::First);
stringify_check(first, f, path)?;
path.pop().unwrap();
Ok(())
} else if has_rest {
path.push(TypePath::Rest);
stringify_check(rest, f, path)?;
path.pop().unwrap();
Ok(())
} else {
write!(f, "1")
}
Check::First(first) => {
path.push(TypePath::First);
stringify_check(first, f, path)?;
path.pop().unwrap();
Ok(())
}
Check::Rest(rest) => {
path.push(TypePath::Rest);
stringify_check(rest, f, path)?;
path.pop().unwrap();
Ok(())
}
}
}

0 comments on commit 2336abe

Please sign in to comment.