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
58
59
60
61
62
63
use crate::lexer::Op;

pub enum Node {
    Char(char),
    BinaryExpr(BinaryExprNode),
    UnaryExpr(UnaryExprNode),
}

pub struct BinaryExprNode {
    pub left:  Box<Node>,
    pub right: Box<Node>,
    pub op: Op,
}

pub struct UnaryExprNode {
    pub child: Box<Node>,
    pub op: Op,
}

pub struct Match {
    pub root: Node,
    pub name: String
}
impl Node {
    pub fn char(&self) -> char {
        if let Node::Char(c) = *self { return c; }
        panic!("Not A Letter!");
    }
    pub fn print(&self) {
        print!("{}", self.to_string());
    }

    pub fn to_string(&self) -> String {
        let mut out = String::new();
        self._print(&self, 0, &mut out);
        return out;
    } 

    /* OUTPUT TO XML? */
    fn _print(&self, node: &Node, depth: u32, out: &mut String) {
        let mut tabs = String::new();
        for _ in 0..depth { tabs.push_str("  "); }
        match node {
            Node::BinaryExpr(n) => {
                out.push_str(&format!("{tabs}<{:?}>\n", n.op));
                self._print(&n.left, depth+1, out);
                self._print(&n.right, depth+1, out);
                out.push_str(&format!("{tabs}</{:?}>\n", n.op));
            },
            Node::UnaryExpr(n) => {
                out.push_str(&format!("{tabs}<{:?}>\n", n.op));
                self._print(&n.child, depth+1, out);
                out.push_str(&format!("{tabs}</{:?}>\n", n.op));
            },
            Node::Char(c) => {
                out.push_str(&format!(
                    "{tabs}<\"{}\"> </\"{}\">\n",
                    c.escape_debug(), c.escape_debug()
                ));
            }
        }
    }
}