diff options
Diffstat (limited to 'src/sexp/display.rs')
-rw-r--r-- | src/sexp/display.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/sexp/display.rs b/src/sexp/display.rs new file mode 100644 index 0000000..e6dbb7c --- /dev/null +++ b/src/sexp/display.rs @@ -0,0 +1,45 @@ + +use std::fmt; +use crate::sexp::{SLeaf, SExp, SLeaf::*, SExp::*, util::*}; + + +impl fmt::Display for SLeaf { + + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", match self { + Add => "+".to_string(), + Sub => "-".to_string(), + Mul => "*".to_string(), + Div => "/".to_string(), + Int(x) => x.to_string(), + Var(s) => s.to_string(), + Quote => "quote".to_string(), + Nil => "()".to_string(), + }) + } + +} + + +impl fmt::Display for SExp { + + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Atom(leaf) => write!(f, "{}", leaf.to_string()), + SCons(a, b) => { + match scons(a.clone(), b.clone()).into_vec() { + Ok(l) => write!( + f, + "({})", + l.into_iter() + .map(|x| x.to_string()) + .collect::<Vec<String>>() + .join(" ") + ), + Err(_) => write!(f, "({} {})", *a, *b), + } + } + } + } + +} |