Skip to content

Commit 1badf93

Browse files
brsongraydon
authored andcommitted
Begin an AST pretty-printer
1 parent 5e06ec9 commit 1badf93

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/comp/front/pretty.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std;
2+
3+
fn unknown() -> str {
4+
ret "<unknown ast node>";
5+
}
6+
7+
fn print_expr(@ast.expr expr) -> str {
8+
alt (expr.node) {
9+
case (ast.expr_lit(?lit, _)) {
10+
ret print_expr_lit(lit);
11+
}
12+
case (ast.expr_binary(?op, ?lhs, ?rhs, _)) {
13+
ret print_expr_binary(op, lhs, rhs);
14+
}
15+
case (_) {
16+
ret unknown();
17+
}
18+
}
19+
}
20+
21+
fn print_expr_lit(@ast.lit lit) -> str {
22+
alt (lit.node) {
23+
case (ast.lit_str(?s)) {
24+
ret "\"" + s + "\"";
25+
}
26+
case (_) {
27+
ret unknown();
28+
}
29+
}
30+
}
31+
32+
fn print_expr_binary(ast.binop op, @ast.expr lhs, @ast.expr rhs) -> str {
33+
alt (op) {
34+
case (ast.add) {
35+
auto l = print_expr(lhs);
36+
auto r = print_expr(rhs);
37+
ret l + " + " + r;
38+
}
39+
}
40+
}
41+
42+
//
43+
// Local Variables:
44+
// mode: rust
45+
// fill-column: 78;
46+
// indent-tabs-mode: nil
47+
// c-basic-offset: 4
48+
// buffer-file-coding-system: utf-8-unix
49+
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
50+
// End:
51+
//

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod front {
88
mod extfmt;
99
mod lexer;
1010
mod parser;
11+
mod pretty;
1112
mod token;
1213
mod eval;
1314
}

0 commit comments

Comments
 (0)