diff options
| author | Brian Anderson <[email protected]> | 2011-02-27 22:19:03 -0500 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-03-02 10:28:14 -0800 |
| commit | 7cef1b3a0f645a3cc420a4ae9583b1fd5463e833 (patch) | |
| tree | a76f4302345e63d7fc18f1f9b0b3403b1880610d /src/comp | |
| parent | Add more #fmt tests (diff) | |
| download | rust-7cef1b3a0f645a3cc420a4ae9583b1fd5463e833.tar.xz rust-7cef1b3a0f645a3cc420a4ae9583b1fd5463e833.zip | |
Add pretty printing for expr_call, expr_path, and more literals
Diffstat (limited to 'src/comp')
| -rw-r--r-- | src/comp/front/pretty.rs | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/src/comp/front/pretty.rs b/src/comp/front/pretty.rs index 8e5414ee..267763e3 100644 --- a/src/comp/front/pretty.rs +++ b/src/comp/front/pretty.rs @@ -1,4 +1,9 @@ -use std; +import std._int; +import std._str; +import std._uint; +import std._vec; + +export print_expr; fn unknown() -> str { ret "<unknown ast node>"; @@ -7,22 +12,34 @@ fn unknown() -> str { fn print_expr(@ast.expr expr) -> str { alt (expr.node) { case (ast.expr_lit(?lit, _)) { - ret print_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_expr_lit(@ast.lit lit) -> str { +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(); } @@ -39,6 +56,23 @@ fn print_expr_binary(ast.binop op, @ast.expr lhs, @ast.expr rhs) -> str { } } +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 |