From c7354d3c56ed388bfbbad469f1112916caa123ec Mon Sep 17 00:00:00 2001 From: mehbark Date: Thu, 15 Jun 2023 00:43:56 -0400 Subject: [PATCH] fix match bug i hope so. put equality checking after variable checking when matching symbols --- src/main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index e467b2f..720c13a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -506,12 +506,12 @@ type Matches<'src> = HashMap; pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option> { match (lhs, expr) { (Sexp::Atom(a), Sexp::Atom(b)) => { - if a == b { - Some(HashMap::with_capacity(0)) - } else if vars.contains(a) { + if vars.contains(a) { let mut out = HashMap::with_capacity(1); out.insert(*a, expr); Some(out) + } else if a == b { + Some(HashMap::with_capacity(0)) } else { None } @@ -527,7 +527,7 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option None, (Sexp::List(xs), Sexp::List(ys)) => { - if xs == ys { + if xs.is_empty() && ys.is_empty() { Some(HashMap::with_capacity(0)) } else if xs.len() == ys.len() { xs.iter() @@ -535,7 +535,7 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option None, - (Some(a), Some(b)) => merge_matches(vars, a, b), + (Some(a), Some(b)) => merge_matches(vars, &a, &b), }) .unwrap_or(None) } else { @@ -545,10 +545,11 @@ pub fn matches<'src>(vars: &[Symbol], lhs: &Sexp, expr: &'src Sexp) -> Option( vars: &[Symbol], - a: Matches<'src>, - b: Matches<'src>, + a: &Matches<'src>, + b: &Matches<'src>, ) -> Option> { let mut out = HashMap::with_capacity(vars.len()); @@ -579,6 +580,7 @@ pub fn merge_matches<'src>( // } // } +#[must_use] pub fn rw(rule: &Rule, sexp: &Sexp) -> Sexp { if rule.lhs() == rule.rhs() { return sexp.clone();