From 8e2ca4bbc356d87ff5a37f20bea4f5b3cc561041 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Tue, 29 Jul 2025 14:37:06 +0300 Subject: 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 --- src/sexp/mod.rs | 1 + src/sexp/step.rs | 61 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs index dee672f..3dbcbb5 100644 --- a/src/sexp/mod.rs +++ b/src/sexp/mod.rs @@ -17,6 +17,7 @@ pub enum SLeaf { Div, Int(i32), Var(String), + Nil, } /// An S-Expression; the defining structure of the language. 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))); /// } /// /// ``` -- cgit v1.2.3