1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
use std::fmt;
use super::types::Type;
#[derive(PartialEq, Debug)]
pub enum SExpr {
Atom(Type),
Sexpr(Vec<SExpr>)
}
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")
}
}
|