aboutsummaryrefslogtreecommitdiff
path: root/src/sexp/display.rs
blob: dc5d6cbaa8cbcaaf6177ce3853c829396c4612dc (plain)
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
57

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(),
            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) =>
                write!(
                    f,
                    "({})",
                    scons(a.clone(), b.clone())
                        .parts()
                        .into_iter()
                        .map(|x| x.to_string())
                        .collect::<Vec<String>>()
                        .join(" ")
                )
        }
    }
}