aboutsummaryrefslogtreecommitdiff
path: root/src/display.rs
blob: f89f2840347774ec0fc4a5b87b8cfc2a49b5d8b8 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use super::ast::{BinaryOp, Expr, UnaryOp};
use std::fmt;

impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.print(f, 0)
    }
}

impl Expr {
    fn print(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result {
        match self {
            Expr::Binary { op, left, right, .. } => {
                let label = match op {
                    BinaryOp::And => "conjunction",
                    BinaryOp::Or => "disjunction",
                };
                writeln!(f, "{:indent$}{}", "", label, indent = indent)?;
                left.print(f, indent + 2)?;
                right.print(f, indent + 2)
            }

            Expr::Unary { op, expr, .. } => {
                let label = match op {
                    UnaryOp::Minus => "minus",
                    UnaryOp::Not => "not",
                };
                writeln!(f, "{:indent$}{}", "", label, indent = indent)?;
                expr.print(f, indent + 2)
            }

            Expr::Comparison { op, left, right, .. } => {
                writeln!(f, "{:indent$}compare {}", "", op.as_str(), indent = indent)?;
                left.print(f, indent + 2)?;
                right.print(f, indent + 2)
            }

            Expr::Name { name, is_string, .. } => {
                if *is_string {
                    writeln!(f, "{:indent$}{:?}", "", name, indent = indent)
                } else {
                    writeln!(f, "{:indent$}{}", "", name, indent = indent)
                }
            }

            Expr::Member { holder, member, .. } => {
                // Walk the member chain to collect all name segments.
                let mut parts = vec![member.as_str()];
                let mut cur = holder.as_ref();
                loop {
                    match cur {
                        Expr::Member { holder, member, .. } => {
                            parts.push(member.as_str());
                            cur = holder.as_ref();
                        }
                        Expr::Name { name, .. } => {
                            parts.push(name.as_str());
                            break;
                        }
                        _ => break,
                    }
                }
                parts.reverse();
                write!(f, "{:indent$}", "", indent = indent)?;
                for (i, part) in parts.iter().enumerate() {
                    if i > 0 {
                        write!(f, ".")?;
                    }
                    write!(f, "{}", part)?;
                }
                writeln!(f)
            }

            Expr::Function { func, args } => {
                // "call" runs directly into the function name (no newline between them).
                // The function name is indented at indent+2, which produces the
                // two-space gap: "call  funcname\n".
                write!(f, "{:indent$}call", "", indent = indent)?;
                func.print(f, indent + 2)?;
                for arg in args {
                    arg.print(f, indent + 2)?;
                }
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::parse;

    #[test]
    fn test_display_comparison() {
        let expr = parse("x = 42").unwrap().unwrap();
        assert_eq!(format!("{expr}"), "compare =\n  x\n  42\n");
    }

    #[test]
    fn test_display_conjunction() {
        let expr = parse("a : b c = 17").unwrap().unwrap();
        assert_eq!(
            format!("{expr}"),
            "conjunction\n  compare :\n    a\n    b\n  compare =\n    c\n    17\n"
        );
    }

    #[test]
    fn test_display_member() {
        let expr = parse("a.b > 17").unwrap().unwrap();
        assert_eq!(format!("{expr}"), "compare >\n  a.b\n  17\n");
    }

    #[test]
    fn test_display_function() {
        let expr = parse("func(val)").unwrap().unwrap();
        assert_eq!(format!("{expr}"), "call  func\n  val\n");
    }

    #[test]
    fn test_display_string_literal() {
        // String literals are displayed with Rust's {:?} quoting.
        let expr = parse(r#"x = "hello""#).unwrap().unwrap();
        assert_eq!(format!("{expr}"), "compare =\n  x\n  \"hello\"\n");
    }
}