From e35522189f5217987bc455d70c3b9056541ef8c6 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sat, 16 Aug 2025 02:04:09 +0300 Subject: feat: type conversion from (Vec/Quote X) -> X and add vec/quote to rest pattern substitutions --- src/sexp/case.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/sexp/case.rs') diff --git a/src/sexp/case.rs b/src/sexp/case.rs index 40310e4..0c3d82d 100644 --- a/src/sexp/case.rs +++ b/src/sexp/case.rs @@ -1,5 +1,5 @@ -use crate::sexp::{SExp, SExp::*, SLeaf::*}; +use crate::sexp::{SExp, SLeaf, SExp::*, SLeaf::*, util::*}; impl SExp { /** @@ -20,7 +20,7 @@ impl SExp { * scons(1, scons(scons(2, scons(3, Nil)), Nil)) * .matches_pat(&var("x")), * Some(vec![( - * "x".to_string(), + * Var("x".to_string()), * scons(1, scons(scons(2, scons(3, Nil)), Nil)) * )]) * ); @@ -34,8 +34,8 @@ impl SExp { * scons(1, scons(scons(2, scons(3, Nil)), Nil)) * .matches_pat(&scons(var("x"), scons(var("y"), Nil))), * Some(vec![ - * ("x".to_string(), Atom(Int(1))), - * ("y".to_string(), scons(2, scons(3, Nil))) + * (Var("x".to_string()), Atom(Int(1))), + * (Var("y".to_string()), scons(2, scons(3, Nil))) * ]) * ); * ``` @@ -47,24 +47,24 @@ impl SExp { * scons(1, scons(2, scons(3, Nil))) * .matches_pat(&scons(var("x"), scons(RestPat("y".to_string()), Nil))), * Some(vec![ - * ("x".to_string(), Atom(Int(1))), - * ("y".to_string(), scons(2, scons(3, Nil))) + * (Var("x".to_string()), Atom(Int(1))), + * (RestPat("y".to_string()), scons(2, scons(3, Nil))) * ]) * ); * ``` */ - pub fn matches_pat(&self, pat: &SExp) -> Option> { + pub fn matches_pat(&self, pat: &SExp) -> Option> { match (self, pat) { (a, b) if a == b => Some(vec![]), - (a, Atom(Var(name))) => Some(vec![(name.clone(), a.clone())]), + (a, Atom(Var(name))) => Some(vec![(Var(name.to_string()), a.clone())]), (SCons(a1, a2), SCons(b1, b2)) => { let lefthand = a1.matches_pat(b1); let checkres = (*b2).check_res_pat(); let righthand = match checkres { - Some(name) => Some(vec![(name, *a2.clone())]), + Some(name) => Some(vec![(RestPat(name.clone()), *a2.clone())]), None => a2.matches_pat(b2), }; lefthand.zip(righthand) -- cgit v1.2.3