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(), Gt => ">".to_string(), Lt => "<".to_string(), Ge => ">=".to_string(), Le => "<=".to_string(), Eq => "=".to_string(), Neq => "!=".to_string(), And => "and".to_string(), Or => "or".to_string(), Not => "not".to_string(), Xor => "xor".to_string(), True => "true".to_string(), False => "false".to_string(), Int(x) => x.to_string(), Var(s) => s.to_string(), Quote => "quote".to_string(), Vector => "vector".to_string(), Print => "print".to_string(), Let => "let".to_string(), Fun => "fn".to_string(), Ty(t) => t.to_string(), Arr => "->".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) if **b == Atom(Nil) => (*a).fmt(f), SCons(a, b) => write!( f, "({})", scons(a.clone(), b.clone()) .parts() .into_iter() .map(|x| x.to_string()) .collect::>() .join(" ") ) } } }