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::>() .join(" ") ), Err(_) => write!(f, "({} {})", *a, *b), } } } } }