From 322f541d6922422e5a4aa29f50c6534517ee85be Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 15 Aug 2025 11:27:34 +0300 Subject: fix: subst not respecting fn and case binds --- src/sexp/subst.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/sexp') diff --git a/src/sexp/subst.rs b/src/sexp/subst.rs index 4cfa48d..d09732f 100644 --- a/src/sexp/subst.rs +++ b/src/sexp/subst.rs @@ -23,6 +23,35 @@ impl SExp { return SCons(a, b); } } + if scons(a.clone(), b.clone()).is_fun() { + let arglist = b.clone().parts()[0].clone(); + let arglist = arglist.parts().into_iter().map(|exp| match exp { + Atom(Var(s)) => s, + _ => todo!(), + }).collect::>(); + if arglist.contains(&name.to_string()) { + return SCons(a, b); + } + } + if let Some((scrut, pavec)) = scons(a.clone(), b.clone()) + .get_case_scrut_pats_and_arms() + { + let scrut = scrut.subst(name, value); + let mut res = vec![]; + for (pat, arm) in pavec { + if pat + .get_vars_bound_by_pattern() + .contains(&name.to_string()) + { + res.push(scons(pat, arm)); + } + else + { + res.push(scons(pat, arm.subst(name, value))); + } + } + return SExp::back_to_case(scrut, res); + } scons((*a).subst(name, value), (*b).subst(name, value)) }, Atom(Var(x)) if x == name => value.clone(), -- cgit v1.2.3