diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-07-29 14:37:06 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-07-29 14:37:06 +0300 |
commit | 8e2ca4bbc356d87ff5a37f20bea4f5b3cc561041 (patch) | |
tree | 4835ec5c872feb98e0dccd14517516c33895d5d2 /src/sexp/step.rs | |
parent | 69bcc7d4d306d1c8a7596ae13641ffe479d98dcc (diff) | |
download | myslip-8e2ca4bbc356d87ff5a37f20bea4f5b3cc561041.tar.gz myslip-8e2ca4bbc356d87ff5a37f20bea4f5b3cc561041.zip |
Added Nil and modified tests to make this more of a list interpreter rather than just a do-what-you-want-with-s-expressions thing
Diffstat (limited to 'src/sexp/step.rs')
-rw-r--r-- | src/sexp/step.rs | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/src/sexp/step.rs b/src/sexp/step.rs index a6ce664..4391bb9 100644 --- a/src/sexp/step.rs +++ b/src/sexp/step.rs @@ -13,23 +13,23 @@ impl SExp { /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; /// /// assert_eq!( - /// scons(Add, scons(1, 1)).step().unwrap(), - /// Atom(Int(2)) + /// scons(Add, scons(1, scons(1, Nil))).step(), + /// Ok(Atom(Int(2))) /// ); /// /// assert_eq!( - /// scons(Sub, scons(1, 2)).step().unwrap(), - /// Atom(Int(-1)) + /// scons(Sub, scons(1, scons(2, Nil))).step(), + /// Ok(Atom(Int(-1))) /// ); /// /// assert_eq!( - /// scons(Mul, scons(2, 3)).step().unwrap(), - /// Atom(Int(6)) + /// scons(Mul, scons(2, scons(3, Nil))).step(), + /// Ok(Atom(Int(6))) /// ); /// /// assert_eq!( - /// scons(Div, scons(6, 2)).step().unwrap(), - /// Atom(Int(3)) + /// scons(Div, scons(6, scons(2, Nil))).step(), + /// Ok(Atom(Int(3))) /// ); /// ``` /// @@ -38,33 +38,48 @@ impl SExp { /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; /// /// assert_eq!( - /// scons(Div, scons(5, 2)).step().unwrap(), - /// Atom(Int(2)) + /// scons(Div, scons(5, scons(2, Nil))).step(), + /// Ok(Atom(Int(2))) /// ); + /// ``` + /// + /// Also, addition and multiplication can take more than two arguments: + /// ```rust + /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; /// + /// assert_eq!( + /// scons(Add, scons(1, scons(2, scons(3, Nil)))).step(), + /// Ok(Atom(Int(6))) + /// ); + /// + /// assert_eq!( + /// scons(Mul, scons(1, scons(2, scons(3, Nil)))).step(), + /// Ok(Atom(Int(6))) + /// ); /// ``` /// - /// Here's an example of a bit more complicated expression: + /// Here's an example of a bit more complicated expression + /// from wikipedias article on s-expressions. /// ```rust /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; /// /// fn main() { - /// let exp = scons(Mul, scons( - /// 2, - /// scons(Add, scons( - /// scons(Div, scons(5, 2)), - /// 4 - /// )) - /// )); - /// - /// let exp = exp.step().unwrap(); - /// assert_eq!(exp, scons(Mul, scons(2, scons(Add, scons(2, 4))))); + /// let exp = scons( + /// Mul, + /// scons( + /// 2, + /// scons( + /// scons(Add, scons(3, scons(4, Nil))), + /// Nil + /// ) + /// ) + /// ); /// /// let exp = exp.step().unwrap(); - /// assert_eq!(exp, scons(Mul, scons(2, 6))); + /// assert_eq!(exp, scons(Mul, scons(2, scons(7, Nil)))); /// /// let exp = exp.step().unwrap(); - /// assert_eq!(exp, Atom(Int(12))); + /// assert_eq!(exp, Atom(Int(14))); /// } /// /// ``` |