Created
September 14, 2012 03:17
-
-
Save dbp/3719588 to your computer and use it in GitHub Desktop.
fmt datavisitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern mod std; // for tests | |
use reflect::*; | |
enum fmt_visitor = @{ | |
out: io::WriterUtil, | |
}; | |
// duplicated from syntax/ast.rs so as not to depend on it | |
enum mutability { m_mutbl, m_imm, m_const, } | |
impl fmt_visitor: DataVisitor { | |
fn visit_nil() { | |
self.out.write_str(~"()"); | |
} | |
fn visit_bool(val: bool) { | |
self.out.write_str(bool::to_str(va)); | |
} | |
fn visit_int(val: int) { | |
self.out.write_str(int::to_str(val, 10)); | |
} | |
fn visit_i8(val: i8) { | |
self.out.write_str(i8::to_str(val, 10)); | |
} | |
fn visit_i16(val: i16) { | |
self.out.write_str(i16::to_str(val, 10)); | |
} | |
fn visit_i32(val: i32) { | |
self.out.write_str(i32::to_str(val, 10)); | |
} | |
fn visit_i64(val: i64) { | |
self.out.write_str(i64::to_str(val, 10)); | |
} | |
fn visit_uint(val: uint) { | |
self.out.write_str(uint::to_str(val, 10)); | |
} | |
fn visit_u8(val: u8) { | |
self.out.write_str(u8::to_str(val, 10)); | |
} | |
fn visit_u16(val: u16) { | |
self.out.write_str(u16::to_str(val, 10)); | |
} | |
fn visit_u32(val: u32) { | |
self.out.write_str(u32::to_str(val, 10)); | |
} | |
fn visit_u64(val: u64) { | |
self.out.write_str(u64::to_str(val, 10)); | |
} | |
fn visit_float(val: float) { | |
self.out.write_str(float::to_str(val, 10)); | |
} | |
fn visit_f32(val: f32) { | |
} | |
fn visit_f64(val: f32) { | |
} | |
fn visit_char(val: char) { | |
self.out.write_char('\''); | |
self.out.write_char(val); | |
self.out.write_char('\''); | |
} | |
// is this type used? | |
// fn visit_str(val: str) | |
fn visit_str_box(val: @str) { | |
self.out.write_char('@'); | |
self.out.write_char('"'); | |
self.out.write_str(val); | |
self.out.write_char('"'); | |
} | |
fn visit_str_uniq(val: ~str) { | |
self.out.write_char('~'); | |
self.out.write_char('"'); | |
self.out.write_str(val); | |
self.out.write_char('"'); | |
} | |
fn visit_str_slice(val: &str) { | |
self.out.write_char('&'); | |
self.out.write_char('"'); | |
self.out.write_str(val); | |
self.out.write_char('"'); | |
} | |
fn visit_str_fixed(val: &str) { | |
self.out.write_char('"'); | |
self.out.write_str(val); | |
self.out.write_char('"'); | |
} // can't actually pass fixed size | |
fn visit_box() { | |
} // indicates next visit is in a box | |
fn visit_uniq() { | |
} | |
//fn visit_ptr // not sure what these are | |
//fn visit_rptr | |
// is this type used? | |
// fn visit_vec(val: vec) | |
fn visit_vec_box_start(size: uint) { | |
} | |
fn visit_vec_box_elem() { | |
} | |
fn visit_vec_box_end() { | |
} | |
fn visit_vec_uniq_start(size: uint) { | |
} | |
fn visit_vec_uniq_elem() { | |
} | |
fn visit_vec_uniq_end() { | |
} | |
fn visit_vec_slice_start(size: uint) { | |
} | |
fn visit_vec_slice_elem() { | |
} | |
fn visit_vec_slice_end() { | |
} | |
fn visit_vec_fixed_start(size: uint) { | |
} | |
fn visit_vec_fixed_elem() { | |
} | |
fn visit_vec_fixed_end() { | |
} | |
fn visit_rec_start(fields: uint) { | |
} | |
fn visit_rec_field(name: &str) { | |
} // next visit is value | |
fn visit_rec_end() { | |
} | |
// what is a class now? are the visit_class_* all deprecated | |
fn visit_tup_start(size: uint) { | |
} | |
fn visit_tup_elem() { | |
} | |
fn visit_tup_end() { | |
} | |
fn visit_enum_start(size: uint, disr_val: int, name: &str) { | |
} | |
fn visit_enum_field() { | |
} // next is value | |
fn visit_enum_field_end() { | |
} | |
} | |
fn conv_poly<T>(v: T) -> ~str { | |
return do io::with_str_writer |writer| { | |
let mut fmv = fmt_visitor(@{out: writer as io::WriterUtil}); | |
visit_data(fmv, v); | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
#[test] | |
fn int_types() { | |
assert conv_poly(-20) == ~"-20"; | |
assert conv_poly(-20i8) == ~"-20"; | |
assert conv_poly(-20i16) == ~"-20"; | |
assert conv_poly(-20i32) == ~"-20"; | |
assert conv_poly(-20i64) == ~"-20"; | |
assert conv_poly(20u) == ~"20"; | |
assert conv_poly(20u8) == ~"20"; | |
assert conv_poly(20u16) == ~"20"; | |
assert conv_poly(20u32) == ~"20"; | |
assert conv_poly(20u64) == ~"20"; | |
} | |
#[test] | |
fn float_types() { | |
// FIXME: do smart conversions of floating point | |
// assert(conv_poly(1.2) == ~"1.2"); | |
// assert(conv_poly(1.2e8) == ~"1.2e+08"); | |
} | |
#[test] | |
fn str_types() { | |
assert conv_poly(~"hello") == ~"~\"hello\""; | |
assert conv_poly(@"hello") == ~"@\"hello\""; | |
assert conv_poly("hello") == ~"&\"hello\""; | |
assert conv_poly("hello"/5) == ~"\"hello\""; | |
} | |
#[test] | |
fn char_types() { | |
assert conv_poly('A') == ~"'A'"; | |
} | |
// #[test] | |
// fn rec_types() { | |
// let r = {a: 1, b: 2}; | |
// let m = {mut a: 10}; | |
// assert conv_poly(r) == ~"{a: 1, b: 2}"; | |
// assert conv_poly(m) == ~"{mut a: 10}"; | |
// } | |
// #[test] | |
// fn tuple_types() { | |
// let t = (10, ~"hello"); | |
// assert conv_poly(t) == ~"(10, ~\"hello\")"; | |
// } | |
// #[test] | |
// fn enum_variant_type() { | |
// enum a { b, c }; | |
// assert conv_poly(b) == ~"b"; | |
// enum d { e(int), f(~str) }; | |
// assert conv_poly(e(10)) == ~"e(10)"; | |
// enum g { h(int,int,int), i(int) }; | |
// assert conv_poly(h(1,2,3)) == ~"h(1, 2, 3)"; | |
// } | |
// #[test] | |
// fn vec_types() { | |
// let v = ~[1,2]; | |
// let u = @[0,0]; | |
// assert conv_poly(v) == ~"~[1, 2]"; | |
// assert conv_poly(u) == ~"@[0, 0]"; | |
// assert conv_poly(&[1,2,3]) == ~"&[1, 2, 3]"; | |
// assert conv_poly([1,2,3]/3) == ~"[1, 2, 3]"; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment