aboutsummaryrefslogtreecommitdiff
path: root/src/sexp/subst.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-07-26 18:49:56 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-07-26 18:49:56 +0300
commit794df40494e8c83532aed39153088697aca2f57b (patch)
tree8192c1f4518d9cbf84a28a38eccfb97793606b92 /src/sexp/subst.rs
parent34074287861b3ef6c9ee89195056d20ae1603cfc (diff)
downloadmyslip-794df40494e8c83532aed39153088697aca2f57b.tar.gz
myslip-794df40494e8c83532aed39153088697aca2f57b.zip
Implemented substitution
Diffstat (limited to 'src/sexp/subst.rs')
-rw-r--r--src/sexp/subst.rs10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/sexp/subst.rs b/src/sexp/subst.rs
index 7cfa180..abab614 100644
--- a/src/sexp/subst.rs
+++ b/src/sexp/subst.rs
@@ -11,11 +11,15 @@ impl SExp {
/// use melisp::sexp::{SExp::*, SLeaf::*, util::*};
///
/// assert_eq!(
- /// scons(Add, scons(var("a"), var("b"))).subst("b", scons(Sub, scons(2, 1))),
+ /// scons(Add, scons(var("a"), var("b"))).subst("b", &scons(Sub, scons(2, 1))),
/// scons(Add, scons(var("a"), scons(Sub, scons(2, 1))))
/// );
/// ```
- pub fn subst(self, name: &str, value: SExp) -> SExp {
- todo!();
+ pub fn subst(self, name: &str, value: &SExp) -> SExp {
+ match self {
+ SCons(a, b) => scons((*a).subst(name, value), (*b).subst(name, value)),
+ Atom(Var(x)) if x == name => value.clone(),
+ t => t,
+ }
}
}