diff options
Diffstat (limited to 'src/sexp')
-rw-r--r-- | src/sexp/mod.rs | 27 | ||||
-rw-r--r-- | src/sexp/step.rs | 49 | ||||
-rw-r--r-- | src/sexp/util.rs | 27 |
3 files changed, 103 insertions, 0 deletions
diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs new file mode 100644 index 0000000..c920e41 --- /dev/null +++ b/src/sexp/mod.rs @@ -0,0 +1,27 @@ + +pub mod step; +pub mod util; + +/// A leaf node for S-Expressions. +/// +/// May represent built-in operators, +/// variables (to be added), functions +/// (to be added) or values. +#[derive(Debug)] +#[derive(PartialEq)] +pub enum SLeaf { + Add, + Sub, + Mul, + Div, + Int(i32), +} + +/// An S-Expression; the defining structure of the language. +#[derive(Debug)] +#[derive(PartialEq)] +pub enum SExp { + SCons(Box<SExp>, Box<SExp>), + Atom(SLeaf), +} + diff --git a/src/sexp/step.rs b/src/sexp/step.rs new file mode 100644 index 0000000..86ad9ea --- /dev/null +++ b/src/sexp/step.rs @@ -0,0 +1,49 @@ + +use crate::sexp::SExp; + +impl SExp { + + /// Evaluates the s-expression one step. + /// + /// **Integer operations** + /// + /// Addition, subtraction, multiplication and division + /// are supported. + /// ```rust + /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; + /// + /// assert_eq!( + /// sexp(Add, sexp(1, 1)).step().unwrap(), + /// Atom(Int(2)) + /// ); + /// + /// assert_eq!( + /// sexp(Sub, sexp(1, 2).step().unwrap()), + /// Atom(Int(-1)) + /// ); + /// + /// assert_eq!( + /// sexp(Mul, sexp(2, 3).step().unwrap()), + /// Atom(Int(6)) + /// ); + /// + /// assert_eq!( + /// sexp(Div, sexp(6, 2).step().unwrap()), + /// Atom(Int(3)) + /// ); + /// ``` + /// + /// Division truncates the decimals. + /// ```rust + /// use melisp::sexp::{SExp::*, SLeaf::*, util::*}; + /// + /// assert_eq!( + /// sexp(Div, sexp(5, 2).step().unwrap()), + /// Atom(Int(2)) + /// ); + /// + /// ``` + pub fn step(self) -> Result<Self, String> { + todo!(); + } +} diff --git a/src/sexp/util.rs b/src/sexp/util.rs new file mode 100644 index 0000000..a4e3ab4 --- /dev/null +++ b/src/sexp/util.rs @@ -0,0 +1,27 @@ + +use crate::sexp::SExp; +use crate::sexp::SLeaf; +use crate::sexp::SExp::*; +use crate::sexp::SLeaf::*; + +impl From<i32> for Box<SExp> { + fn from(int: i32) -> Self { + Box::new(Atom(Int(int))) + } +} + +impl From<SLeaf> for Box<SExp> { + fn from(leaf: SLeaf) -> Self { + Box::new(Atom(leaf)) + } +} + +impl From<i32> for SLeaf { + fn from(int: i32) -> Self { + Int(int) + } +} + +pub fn sexp(x: impl Into<Box<SExp>>, y: impl Into<Box<SExp>>) -> SExp { + SCons(x.into(), y.into()) +} |