Have the pretty-printer take a writer stream as argument

It now uses a string writer to also fill in for
middle.ty.ast_ty_to_str
This commit is contained in:
Marijn Haverbeke 2011-03-09 11:41:50 +01:00 committed by unknown
parent dddd7d8f44
commit aed40fbcd8
6 changed files with 61 additions and 119 deletions

View file

@ -81,87 +81,10 @@ tag unify_result {
// Stringification
fn ast_ty_to_str(&@ast.ty ty) -> str {
fn ast_fn_input_to_str(&rec(ast.mode mode, @ast.ty ty) input) -> str {
auto s;
if (mode_is_alias(input.mode)) {
s = "&";
} else {
s = "";
}
ret s + ast_ty_to_str(input.ty);
}
fn ast_ty_field_to_str(&ast.ty_field f) -> str {
ret ast_ty_to_str(f.ty) + " " + f.ident;
}
auto s;
alt (ty.node) {
case (ast.ty_nil) { s = "()"; }
case (ast.ty_bool) { s = "bool"; }
case (ast.ty_int) { s = "int"; }
case (ast.ty_uint) { s = "uint"; }
case (ast.ty_machine(?tm)) { s = common.ty_mach_to_str(tm); }
case (ast.ty_char) { s = "char"; }
case (ast.ty_str) { s = "str"; }
case (ast.ty_box(?t)) { s = "@" + ast_ty_to_str(t); }
case (ast.ty_vec(?t)) { s = "vec[" + ast_ty_to_str(t) + "]"; }
case (ast.ty_type) { s = "type"; }
case (ast.ty_tup(?elts)) {
auto f = ast_ty_to_str;
s = "tup(";
s += _str.connect(_vec.map[@ast.ty,str](f, elts), ",");
s += ")";
}
case (ast.ty_rec(?fields)) {
auto f = ast_ty_field_to_str;
s = "rec(";
s += _str.connect(_vec.map[ast.ty_field,str](f, fields), ",");
s += ")";
}
case (ast.ty_fn(?proto, ?inputs, ?output)) {
auto f = ast_fn_input_to_str;
if (proto == ast.proto_fn) {
s = "fn(";
} else {
s = "iter(";
}
auto is = _vec.map[rec(ast.mode mode, @ast.ty ty),str](f, inputs);
s += _str.connect(is, ", ");
s += ")";
if (output.node != ast.ty_nil) {
s += " -> " + ast_ty_to_str(output);
}
}
case (ast.ty_path(?path, _)) {
s = path_to_str(path);
}
case (ast.ty_mutable(?t)) {
s = "mutable " + ast_ty_to_str(t);
}
case (_) {
fail; // FIXME: typestate bug
}
}
ret s;
}
fn path_to_str(&ast.path pth) -> str {
auto result = _str.connect(pth.node.idents, ".");
if (_vec.len[@ast.ty](pth.node.types) > 0u) {
auto f = ast_ty_to_str;
auto f = pretty.pprust.ty_to_str;
result += "[";
result += _str.connect(_vec.map[@ast.ty,str](f, pth.node.types), ",");
result += "]";
@ -169,8 +92,6 @@ fn path_to_str(&ast.path pth) -> str {
ret result;
}
// FIXME use the pretty-printer for this once it has a concept of an
// abstract stream
fn ty_to_str(&@t typ) -> str {
fn fn_input_to_str(&rec(ast.mode mode, @t ty) input) -> str {