fix up matches

there was a bug i ran into when matching on the empty list
This commit is contained in:
mehbark 2023-06-15 00:07:27 -04:00
parent 798245aa0f
commit 17463c839f

View file

@ -502,6 +502,7 @@ type Matches<'src> = HashMap<Symbol, &'src Sexp>;
// DONE?: there. can. be. at most. one. match.
// i'm happy that this is faster, but it might be worth going back to the per-variable thing
#[must_use]
pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option<Matches<'src>> {
match (lhs, expr) {
(Sexp::Atom(a), Sexp::Atom(b)) => {
@ -526,13 +527,14 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option<Ma
}
(Sexp::List(_), Sexp::Atom(_)) => None,
(Sexp::List(xs), Sexp::List(ys)) => {
if xs.len() == ys.len() {
if xs == ys {
Some(HashMap::with_capacity(0))
} else if xs.len() == ys.len() {
xs.iter()
.zip(ys)
.map(|(lhs, expr)| matches(vars, lhs, expr))
.reduce(|a, b| match (a, b) {
(None, _) => None,
(_, None) => None,
(None, _) | (_, None) => None,
(Some(a), Some(b)) => merge_matches(vars, a, b),
})
.unwrap_or(None)