aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sexp/mod.rs5
-rw-r--r--src/sexp/subst.rs21
-rw-r--r--src/sexp/util.rs5
3 files changed, 29 insertions, 2 deletions
diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs
index 087cb67..d866be5 100644
--- a/src/sexp/mod.rs
+++ b/src/sexp/mod.rs
@@ -1,12 +1,12 @@
pub mod step;
pub mod util;
+pub mod subst;
/// A leaf node for S-Expressions.
///
/// May represent built-in operators,
-/// variables (to be added), functions
-/// (to be added) or values.
+/// variables, functions (to be added) or values.
#[derive(Debug)]
#[derive(PartialEq)]
pub enum SLeaf {
@@ -15,6 +15,7 @@ pub enum SLeaf {
Mul,
Div,
Int(i32),
+ Var(String),
}
/// An S-Expression; the defining structure of the language.
diff --git a/src/sexp/subst.rs b/src/sexp/subst.rs
new file mode 100644
index 0000000..7cfa180
--- /dev/null
+++ b/src/sexp/subst.rs
@@ -0,0 +1,21 @@
+
+use crate::sexp::{SExp, SExp::*, SLeaf::*, util::*};
+
+impl SExp {
+ /// Performs substitution on the s-expression.
+ ///
+ /// Replaces all free occurrences of variables
+ /// named `name` with the given `value`
+ /// in this s-expression.
+ /// ```rust
+ /// 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"), scons(Sub, scons(2, 1))))
+ /// );
+ /// ```
+ pub fn subst(self, name: &str, value: SExp) -> SExp {
+ todo!();
+ }
+}
diff --git a/src/sexp/util.rs b/src/sexp/util.rs
index c31358f..ce6de5d 100644
--- a/src/sexp/util.rs
+++ b/src/sexp/util.rs
@@ -25,3 +25,8 @@ impl From<i32> for SLeaf {
pub fn scons(x: impl Into<Box<SExp>>, y: impl Into<Box<SExp>>) -> SExp {
SCons(x.into(), y.into())
}
+
+pub fn var(name: &str) -> SExp {
+ Atom(Var(name.to_string()))
+}
+