From 90734a0d3378b26f7800fc0ffdaa3a6897839514 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 24 Jan 2013 10:33:20 -0800 Subject: [PATCH] Convert extfmt::rt::Conv into a struct --- src/libcore/extfmt.rs | 29 +++++++++++++++++++++++++---- src/libsyntax/ext/build.rs | 9 +++++++++ src/libsyntax/ext/fmt.rs | 27 ++++++++++++++++----------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs index c048fa085a0..854f99be1e2 100644 --- a/src/libcore/extfmt.rs +++ b/src/libcore/extfmt.rs @@ -376,13 +376,23 @@ fn test(s: &str, flags: &[Flag], next: uint) { fn test_parse_fmt_string() { assert parse_fmt_string("foo %s bar", die) == ~[ PieceString(~"foo "), - PieceConv(Conv {param: None, flags: ~[], width: CountImplied, - precision: CountImplied, ty: TyStr}), + PieceConv(Conv { + param: None, + flags: ~[], + width: CountImplied, + precision: CountImplied, + ty: TyStr, + }), PieceString(~" bar")]; assert parse_fmt_string("%s", die) == ~[ - PieceConv(Conv {param: None, flags: ~[], width: CountImplied, - precision: CountImplied, ty: TyStr })]; + PieceConv(Conv { + param: None, + flags: ~[], + width: CountImplied, + precision: CountImplied, + ty: TyStr, + })]; assert parse_fmt_string("%%%%", die) == ~[ PieceString(~"%"), PieceString(~"%")]; @@ -486,8 +496,19 @@ pub enum Count { CountIs(uint), CountImplied, } pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } + #[cfg(stage0)] pub type Conv = {flags: u32, width: Count, precision: Count, ty: Ty}; + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + pub struct Conv { + flags: u32, + width: Count, + precision: Count, + ty: Ty, + } + pub pure fn conv_int(cv: Conv, i: int) -> ~str { let radix = 10; let prec = get_int_precision(cv); diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 5e3d88b8b7f..5a8a1d8753d 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -172,6 +172,15 @@ fn mk_struct_e(cx: ext_ctxt, sp: span, mk_fields(sp, fields), option::None::<@ast::expr>)) } +fn mk_global_struct_e(cx: ext_ctxt, sp: span, + ctor_path: ~[ast::ident], + fields: ~[{ident: ast::ident, ex: @ast::expr}]) -> + @ast::expr { + mk_expr(cx, sp, + ast::expr_struct(mk_raw_path_global(sp, ctor_path), + mk_fields(sp, fields), + option::None::<@ast::expr>)) +} fn mk_glob_use(cx: ext_ctxt, sp: span, path: ~[ast::ident]) -> @ast::view_item { let glob = @ast::spanned { diff --git a/src/libsyntax/ext/fmt.rs b/src/libsyntax/ext/fmt.rs index 2b7bd9ddf32..65890351e39 100644 --- a/src/libsyntax/ext/fmt.rs +++ b/src/libsyntax/ext/fmt.rs @@ -56,8 +56,8 @@ fn parse_fmt_err_(cx: ext_ctxt, sp: span, msg: &str) -> ! { fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: ~[Piece], args: ~[@ast::expr]) -> @ast::expr { - fn make_path_vec(_cx: ext_ctxt, ident: @~str) -> ~[ast::ident] { - let intr = _cx.parse_sess().interner; + fn make_path_vec(cx: ext_ctxt, ident: @~str) -> ~[ast::ident] { + let intr = cx.parse_sess().interner; return ~[intr.intern(@~"extfmt"), intr.intern(@~"rt"), intr.intern(ident)]; } @@ -111,23 +111,28 @@ fn make_ty(cx: ext_ctxt, sp: span, t: Ty) -> @ast::expr { } return make_rt_path_expr(cx, sp, @rt_type); } - fn make_conv_rec(cx: ext_ctxt, sp: span, flags_expr: @ast::expr, + fn make_conv_struct(cx: ext_ctxt, sp: span, flags_expr: @ast::expr, width_expr: @ast::expr, precision_expr: @ast::expr, ty_expr: @ast::expr) -> @ast::expr { let intr = cx.parse_sess().interner; - return mk_rec_e(cx, sp, - ~[{ident: intr.intern(@~"flags"), ex: flags_expr}, - {ident: intr.intern(@~"width"), ex: width_expr}, - {ident: intr.intern(@~"precision"), - ex: precision_expr}, - {ident: intr.intern(@~"ty"), ex: ty_expr}]); + mk_global_struct_e( + cx, + sp, + make_path_vec(cx, @~"Conv"), + ~[ + {ident: intr.intern(@~"flags"), ex: flags_expr}, + {ident: intr.intern(@~"width"), ex: width_expr}, + {ident: intr.intern(@~"precision"), ex: precision_expr}, + {ident: intr.intern(@~"ty"), ex: ty_expr}, + ] + ) } let rt_conv_flags = make_flags(cx, sp, cnv.flags); let rt_conv_width = make_count(cx, sp, cnv.width); let rt_conv_precision = make_count(cx, sp, cnv.precision); let rt_conv_ty = make_ty(cx, sp, cnv.ty); - return make_conv_rec(cx, sp, rt_conv_flags, rt_conv_width, - rt_conv_precision, rt_conv_ty); + make_conv_struct(cx, sp, rt_conv_flags, rt_conv_width, + rt_conv_precision, rt_conv_ty) } fn make_conv_call(cx: ext_ctxt, sp: span, conv_type: ~str, cnv: Conv, arg: @ast::expr) -> @ast::expr {