aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-02-27 18:22:54 -0500
committerGraydon Hoare <[email protected]>2011-03-02 10:28:14 -0800
commit1badf9316a174615e9e5c60e6cfd12e4f071d623 (patch)
tree775188e773e1611149293f76864a658ff5a9eafe /src
parentRewrite expand_syntax_ext to avoid a mysterious memory leak (diff)
downloadrust-1badf9316a174615e9e5c60e6cfd12e4f071d623.tar.xz
rust-1badf9316a174615e9e5c60e6cfd12e4f071d623.zip
Begin an AST pretty-printer
Diffstat (limited to 'src')
-rw-r--r--src/comp/front/pretty.rs51
-rw-r--r--src/comp/rustc.rc1
2 files changed, 52 insertions, 0 deletions
diff --git a/src/comp/front/pretty.rs b/src/comp/front/pretty.rs
new file mode 100644
index 00000000..8e5414ee
--- /dev/null
+++ b/src/comp/front/pretty.rs
@@ -0,0 +1,51 @@
+use std;
+
+fn unknown() -> str {
+ ret "<unknown ast node>";
+}
+
+fn print_expr(@ast.expr expr) -> str {
+ alt (expr.node) {
+ case (ast.expr_lit(?lit, _)) {
+ ret print_expr_lit(lit);
+ }
+ case (ast.expr_binary(?op, ?lhs, ?rhs, _)) {
+ ret print_expr_binary(op, lhs, rhs);
+ }
+ case (_) {
+ ret unknown();
+ }
+ }
+}
+
+fn print_expr_lit(@ast.lit lit) -> str {
+ alt (lit.node) {
+ case (ast.lit_str(?s)) {
+ ret "\"" + s + "\"";
+ }
+ 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;
+ }
+ }
+}
+
+//
+// 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:
+//
diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc
index 16d24e9b..43a04117 100644
--- a/src/comp/rustc.rc
+++ b/src/comp/rustc.rc
@@ -8,6 +8,7 @@ mod front {
mod extfmt;
mod lexer;
mod parser;
+ mod pretty;
mod token;
mod eval;
}