From 1badf9316a174615e9e5c60e6cfd12e4f071d623 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 27 Feb 2011 18:22:54 -0500 Subject: Begin an AST pretty-printer --- src/comp/front/pretty.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/comp/front/pretty.rs (limited to 'src/comp/front/pretty.rs') 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 ""; +} + +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: +// -- cgit v1.2.3 From 7cef1b3a0f645a3cc420a4ae9583b1fd5463e833 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 27 Feb 2011 22:19:03 -0500 Subject: Add pretty printing for expr_call, expr_path, and more literals --- src/comp/front/pretty.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/comp/front/pretty.rs') 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 ""; @@ -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 -- cgit v1.2.3 From 0624f9db4aeaa5681941750c3a1a17ca5fbb7e72 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 4 Mar 2011 07:22:43 +0100 Subject: Add a pretty-printer Adds a -pp option to the compiler which will cause it to simply pretty-print the given file. --- src/comp/front/pretty.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/comp/front/pretty.rs') diff --git a/src/comp/front/pretty.rs b/src/comp/front/pretty.rs index 267763e3..2fd58126 100644 --- a/src/comp/front/pretty.rs +++ b/src/comp/front/pretty.rs @@ -5,6 +5,8 @@ import std._vec; export print_expr; +// FIXME this is superseded by ../pretty/pprust.rs. can it be dropped? + fn unknown() -> str { ret ""; } -- cgit v1.2.3