use std::fmt; use super::types::Type; #[derive(PartialEq, Debug)] pub enum SExpr { Atom(Type), Sexpr(Vec) } impl fmt::Display for SExpr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { SExpr::Atom(ref t) => write!(f, "{}", t), SExpr::Sexpr(ref s) => write!(f, "{:?}", s) } } } #[test] fn construct() { let atom1 = SExpr::Atom(Type::Number(Number::Int(1))); let atom2 = SExpr::Atom(Type::Number(Number::Int(2))); let atom3 = SExpr::Atom(Type::Number(Number::Int(3))); let atom4 = SExpr::Atom(Type::Number(Number::Int(4))); let sexp = SExpr::Sexpr(vec!(atom1, atom2, atom3, atom4)); match sexp { SExpr::Sexpr(ref x) => { assert_eq!(x[0], SExpr::Atom(Type::Number(Number::Int(1)))); }, _ => panic!("What") } } #[test] fn mutability() { let atom1 = SExpr::Atom(Type::Number(Number::Int(1))); let atom2 = SExpr::Atom(Type::Number(Number::Int(2))); let atom3 = SExpr::Atom(Type::Number(Number::Int(3))); let atom4 = SExpr::Atom(Type::Number(Number::Int(4))); let mut sexp = SExpr::Sexpr(vec!(atom1, atom2, atom3, atom4)); match sexp { SExpr::Sexpr(ref mut x) => match x[0] { SExpr::Atom(Type::Number(Number::Int(ref mut x))) => *x += 7, _ => panic!("What") }, _ => panic!("What") } match sexp { SExpr::Sexpr(ref x) => { assert_eq!(x[0], SExpr::Atom(Type::Number(Number::Int(8)))); }, _ => panic!("What") } }