1use crate::lexer::Op;
2
3pub enum Node {
4 Char(char),
5 BinaryExpr(BinaryExprNode),
6 UnaryExpr(UnaryExprNode),
7}
8
9pub struct BinaryExprNode {
10 pub left: Box<Node>,
11 pub right: Box<Node>,
12 pub op: Op,
13}
14
15pub struct UnaryExprNode {
16 pub child: Box<Node>,
17 pub op: Op,
18}
19
20pub struct Match {
21 pub root: Node,
22 pub name: String
23}
24impl Node {
25 pub fn char(&self) -> char {
26 if let Node::Char(c) = *self { return c; }
27 panic!("Not A Letter!");
28 }
29 pub fn print(&self) {
30 print!("{}", self.to_string());
31 }
32
33 pub fn to_string(&self) -> String {
34 let mut out = String::new();
35 self._print(&self, 0, &mut out);
36 return out;
37 }
38
39 fn _print(&self, node: &Node, depth: u32, out: &mut String) {
41 let mut tabs = String::new();
42 for _ in 0..depth { tabs.push_str(" "); }
43 match node {
44 Node::BinaryExpr(n) => {
45 out.push_str(&format!("{tabs}<{:?}>\n", n.op));
46 self._print(&n.left, depth+1, out);
47 self._print(&n.right, depth+1, out);
48 out.push_str(&format!("{tabs}</{:?}>\n", n.op));
49 },
50 Node::UnaryExpr(n) => {
51 out.push_str(&format!("{tabs}<{:?}>\n", n.op));
52 self._print(&n.child, depth+1, out);
53 out.push_str(&format!("{tabs}</{:?}>\n", n.op));
54 },
55 Node::Char(c) => {
56 out.push_str(&format!(
57 "{tabs}<\"{}\"> </\"{}\">\n",
58 c.escape_debug(), c.escape_debug()
59 ));
60 }
61 }
62 }
63}