Skip to content

Commit 7cef1b3

Browse files
brsongraydon
authored andcommitted
Add pretty printing for expr_call, expr_path, and more literals
1 parent 7350b7f commit 7cef1b3

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/comp/front/pretty.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std;
1+
import std._int;
2+
import std._str;
3+
import std._uint;
4+
import std._vec;
5+
6+
export print_expr;
27

38
fn unknown() -> str {
49
ret "<unknown ast node>";
@@ -7,22 +12,34 @@ fn unknown() -> str {
712
fn print_expr(@ast.expr expr) -> str {
813
alt (expr.node) {
914
case (ast.expr_lit(?lit, _)) {
10-
ret print_expr_lit(lit);
15+
ret print_lit(lit);
1116
}
1217
case (ast.expr_binary(?op, ?lhs, ?rhs, _)) {
1318
ret print_expr_binary(op, lhs, rhs);
1419
}
20+
case (ast.expr_call(?path, ?args, _)) {
21+
ret print_expr_call(path, args);
22+
}
23+
case (ast.expr_path(?path, _, _)) {
24+
ret print_path(path);
25+
}
1526
case (_) {
1627
ret unknown();
1728
}
1829
}
1930
}
2031

21-
fn print_expr_lit(@ast.lit lit) -> str {
32+
fn print_lit(@ast.lit lit) -> str {
2233
alt (lit.node) {
2334
case (ast.lit_str(?s)) {
2435
ret "\"" + s + "\"";
2536
}
37+
case (ast.lit_int(?i)) {
38+
ret _int.to_str(i, 10u);
39+
}
40+
case (ast.lit_uint(?u)) {
41+
ret _uint.to_str(u, 10u);
42+
}
2643
case (_) {
2744
ret unknown();
2845
}
@@ -39,6 +56,23 @@ fn print_expr_binary(ast.binop op, @ast.expr lhs, @ast.expr rhs) -> str {
3956
}
4057
}
4158

59+
fn print_expr_call(@ast.expr path_expr, vec[@ast.expr] args) -> str {
60+
auto s = print_expr(path_expr);
61+
62+
s += "(";
63+
fn print_expr_ref(&@ast.expr e) -> str { ret print_expr(e); }
64+
auto mapfn = print_expr_ref;
65+
auto argstrs = _vec.map[@ast.expr, str](mapfn, args);
66+
s += _str.connect(argstrs, ", ");
67+
s += ")";
68+
69+
ret s;
70+
}
71+
72+
fn print_path(ast.path path) -> str {
73+
ret _str.connect(path.node.idents, ".");
74+
}
75+
4276
//
4377
// Local Variables:
4478
// mode: rust

0 commit comments

Comments
 (0)