aboutsummaryrefslogtreecommitdiff
path: root/src/comp/front/pretty.rs
blob: 267763e39972cf3df89ff7a85aec7f9608a7568e (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
import std._int;
import std._str;
import std._uint;
import std._vec;

export print_expr;

fn unknown() -> str {
    ret "<unknown ast node>";
}

fn print_expr(@ast.expr expr) -> str {
    alt (expr.node) {
        case (ast.expr_lit(?lit, _)) {
            ret print_lit(lit);
        }
        case (ast.expr_binary(?op, ?lhs, ?rhs, _)) {
            ret print_expr_binary(op, lhs, rhs);
        }
        case (ast.expr_call(?path, ?args, _)) {
            ret print_expr_call(path, args);
        }
        case (ast.expr_path(?path, _, _)) {
            ret print_path(path);
        }
        case (_) {
            ret unknown();
        }
    }
}

fn print_lit(@ast.lit lit) -> str {
    alt (lit.node) {
        case (ast.lit_str(?s)) {
            ret "\"" + s + "\"";
        }
        case (ast.lit_int(?i)) {
            ret _int.to_str(i, 10u);
        }
        case (ast.lit_uint(?u)) {
            ret _uint.to_str(u, 10u);
        }
        case (_) {
            ret unknown();
        }
    }
}

fn print_expr_binary(ast.binop op, @ast.expr lhs, @ast.expr rhs) -> str {
    alt (op) {
        case (ast.add) {
            auto l = print_expr(lhs);
            auto r = print_expr(rhs);
            ret l + " + " + r;
        }
    }
}

fn print_expr_call(@ast.expr path_expr, vec[@ast.expr] args) -> str {
    auto s = print_expr(path_expr);

    s += "(";
    fn print_expr_ref(&@ast.expr e) -> str { ret print_expr(e); }
    auto mapfn = print_expr_ref;
    auto argstrs = _vec.map[@ast.expr, str](mapfn, args);
    s += _str.connect(argstrs, ", ");
    s += ")";

    ret s;
}

fn print_path(ast.path path) -> str {
    ret _str.connect(path.node.idents, ".");
}

//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//