add a log_str() function and allow '%?' in fmt strings to use it

This commit is contained in:
Niko Matsakis 2012-01-11 09:09:46 -08:00
parent f3b867fd04
commit c68345e57e
7 changed files with 38 additions and 1 deletions

View File

@ -247,6 +247,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
ty_bits. { ret make_conv_call(cx, arg.span, "uint", cnv, arg); }
ty_octal. { ret make_conv_call(cx, arg.span, "uint", cnv, arg); }
ty_float. { ret make_conv_call(cx, arg.span, "float", cnv, arg); }
ty_poly. { ret make_conv_call(cx, arg.span, "poly", cnv, arg); }
_ { cx.span_unimpl(sp, unsupported); }
}
}
@ -303,6 +304,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
}
ty_octal. { #debug("type: octal"); }
ty_float. { #debug("type: float"); }
ty_poly. { #debug("type: poly"); }
}
}
let fmt_sp = args[0].span;

View File

@ -50,6 +50,7 @@ mod ct {
ty_hex(caseness);
ty_octal;
ty_float;
ty_poly;
// FIXME: More types
}
tag flag {
@ -249,6 +250,8 @@ mod ct {
ty_octal
} else if str::eq(tstr, "f") {
ty_float
} else if str::eq(tstr, "?") {
ty_poly
} else { error("unknown type in conversion: " + tstr) };
ret {ty: t, next: i + 1u};
}
@ -346,6 +349,10 @@ mod rt {
}
ret pad(cv, s, pad_signed);
}
fn conv_poly<T>(cv: conv, v: T) -> str {
let s = sys::log_str(v);
ret conv_str(cv, s);
}
// Convert an int to string with minimum number of digits. If precision is
// 0 and num is 0 then the result is the empty string.

View File

@ -19,6 +19,7 @@ native mod rustrt {
fn refcount<T>(t: @T) -> uint;
fn do_gc();
fn unsupervise();
fn shape_log_str<T>(t: *sys::type_desc, data: T) -> str;
}
#[abi = "rust-intrinsic"]
@ -87,6 +88,10 @@ fn unsupervise() -> () {
ret rustrt::unsupervise();
}
fn log_str<T>(t: T) -> str {
rustrt::shape_log_str(get_type_desc::<T>(), t)
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -548,6 +548,24 @@ shape_cmp_type(int8_t *result, const type_desc *tydesc,
}
}
extern "C" rust_str *
shape_log_str(const type_desc *tydesc, uint8_t *data) {
rust_task *task = rust_scheduler::get_task();
shape::arena arena;
shape::type_param *params =
shape::type_param::from_tydesc_and_data(tydesc, data, arena);
std::stringstream ss;
shape::log log(task, true, tydesc->shape, params, tydesc->shape_tables,
data, ss);
log.walk();
int len = ss.str().length();
return make_str(task->kernel, ss.str().c_str(), len, "log_str");
}
extern "C" void
shape_log_type(const type_desc *tydesc, uint8_t *data, uint32_t level) {
rust_task *task = rust_scheduler::get_task();

View File

@ -191,7 +191,7 @@ inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
typedef rust_vec rust_str;
inline rust_str *
make_str(rust_kernel* kernel, char* c, size_t strlen, const char* name) {
make_str(rust_kernel* kernel, const char* c, size_t strlen, const char* name) {
size_t str_fill = strlen + 1;
size_t str_alloc = str_fill;
rust_str *str = (rust_str *)

View File

@ -48,6 +48,7 @@ rust_task_sleep
rust_get_task
set_min_stack
sched_threads
shape_log_str
squareroot
start_task
vec_reserve_shared

View File

@ -0,0 +1,4 @@
fn main() {
assert "[1, 2, 3]" == sys::log_str([1, 2, 3]);
assert #fmt["%?/%5?", [1, 2, 3], "hi"] == "[1, 2, 3]/ \"hi\"";
}