Skip to content

Commit 064435a

Browse files
committed
---
yaml --- r: 2099 b: refs/heads/master c: 4844e1c h: refs/heads/master i: 2097: f0b7687 2095: 2b1f055 v: v3
1 parent 9367c25 commit 064435a

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: aebdef0cd6dff87322f51850f72c42ccb54fbd53
2+
refs/heads/master: 4844e1c08a0f87f8c2bf4ba752630e1af0794a63

trunk/src/comp/front/extfmt.rs

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,86 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
111111
ret @rec(node=binexpr, span=sp);
112112
}
113113

114-
fn make_call(common.span sp, vec[ast.ident] fn_path,
115-
vec[@ast.expr] args) -> @ast.expr {
116-
let vec[ast.ident] path_idents = fn_path;
117-
let vec[@ast.ty] path_types = vec();
118-
auto path = rec(idents = path_idents, types = path_types);
114+
fn make_path_expr(common.span sp, vec[ast.ident] idents) -> @ast.expr {
115+
let vec[@ast.ty] types = vec();
116+
auto path = rec(idents=idents, types=types);
119117
auto sp_path = rec(node=path, span=sp);
120118
auto pathexpr = ast.expr_path(sp_path, none[ast.def], ast.ann_none);
121119
auto sp_pathexpr = @rec(node=pathexpr, span=sp);
122-
auto callexpr = ast.expr_call(sp_pathexpr, args, ast.ann_none);
120+
ret sp_pathexpr;
121+
}
122+
123+
fn make_call(common.span sp, vec[ast.ident] fn_path,
124+
vec[@ast.expr] args) -> @ast.expr {
125+
auto pathexpr = make_path_expr(sp, fn_path);
126+
auto callexpr = ast.expr_call(pathexpr, args, ast.ann_none);
123127
auto sp_callexpr = @rec(node=callexpr, span=sp);
124128
ret sp_callexpr;
125129
}
126130

131+
fn make_rec_expr(common.span sp,
132+
vec[tup(ast.ident, @ast.expr)] fields) -> @ast.expr {
133+
let vec[ast.field] astfields = vec();
134+
for (tup(ast.ident, @ast.expr) field in fields) {
135+
auto ident = field._0;
136+
auto val = field._1;
137+
auto astfield = rec(mut = ast.imm,
138+
ident = ident,
139+
expr = val);
140+
astfields += vec(astfield);
141+
}
142+
143+
auto recexpr = ast.expr_rec(astfields,
144+
option.none[@ast.expr],
145+
ast.ann_none);
146+
auto sp_recexpr = @rec(node=recexpr, span=sp);
147+
ret sp_recexpr;
148+
}
149+
150+
fn make_path_vec(str ident) -> vec[str] {
151+
ret vec("std", "ExtFmt", "RT", ident);
152+
}
153+
154+
fn make_rt_conv_expr(common.span sp, &conv cnv) -> @ast.expr {
155+
fn make_ty(common.span sp, &ty t) -> @ast.expr {
156+
auto rt_type;
157+
alt (t) {
158+
case (ty_hex(?c)) {
159+
alt (c) {
160+
case (case_upper) {
161+
rt_type = "ty_hex_upper";
162+
}
163+
case (case_lower) {
164+
rt_type = "ty_hex_lower";
165+
}
166+
}
167+
}
168+
case (ty_bits) {
169+
rt_type = "ty_bits";
170+
}
171+
case (_) {
172+
rt_type = "ty_default";
173+
}
174+
}
175+
176+
auto idents = make_path_vec(rt_type);
177+
ret make_path_expr(sp, idents);
178+
}
179+
180+
fn make_conv_rec(common.span sp, &@ast.expr ty_expr) -> @ast.expr {
181+
ret make_rec_expr(sp, vec(tup("ty", ty_expr)));
182+
}
183+
184+
auto rt_conv_ty = make_ty(sp, cnv.ty);
185+
ret make_conv_rec(sp, rt_conv_ty);
186+
}
187+
127188
fn make_conv_call(common.span sp, str conv_type,
128-
@ast.expr arg) -> @ast.expr {
189+
&conv cnv, @ast.expr arg) -> @ast.expr {
129190
auto fname = "conv_" + conv_type;
130-
let vec[str] path = vec("std", "ExtFmt", "RT", fname);
131-
let vec[@ast.expr] args = vec(arg);
191+
auto path = make_path_vec(fname);
192+
auto cnv_expr = make_rt_conv_expr(sp, cnv);
193+
auto args = vec(cnv_expr, arg);
132194
ret make_call(arg.span, path, args);
133195
}
134196

@@ -175,18 +237,21 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
175237
case (ty_int(?sign)) {
176238
alt (sign) {
177239
case (signed) {
178-
ret make_conv_call(arg.span, "int", arg);
240+
ret make_conv_call(arg.span, "int", cnv, arg);
179241
}
180242
case (unsigned) {
181-
ret make_conv_call(arg.span, "uint", arg);
243+
ret make_conv_call(arg.span, "uint", cnv, arg);
182244
}
183245
}
184246
}
185247
case (ty_bool) {
186-
ret make_conv_call(arg.span, "bool", arg);
248+
ret make_conv_call(arg.span, "bool", cnv, arg);
187249
}
188250
case (ty_char) {
189-
ret make_conv_call(arg.span, "char", arg);
251+
ret make_conv_call(arg.span, "char", cnv, arg);
252+
}
253+
case (ty_hex(_)) {
254+
ret make_conv_call(arg.span, "uint", cnv, arg);
190255
}
191256
case (_) {
192257
log unsupported;

trunk/src/lib/ExtFmt.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,23 +264,40 @@ mod CT {
264264

265265
// Functions used by the fmt extension at runtime
266266
mod RT {
267-
fn conv_int(int i) -> str {
267+
268+
tag ty {
269+
ty_default;
270+
ty_bits;
271+
ty_hex_upper;
272+
ty_hex_lower;
273+
}
274+
275+
type conv = rec(ty ty);
276+
277+
fn conv_int(&conv cv, int i) -> str {
268278
ret _int.to_str(i, 10u);
269279
}
270280

271-
fn conv_uint(uint u) -> str {
272-
ret _uint.to_str(u, 10u);
281+
fn conv_uint(&conv cv, uint u) -> str {
282+
alt (cv.ty) {
283+
case (ty_default) {
284+
ret _uint.to_str(u, 10u);
285+
}
286+
case (ty_hex_lower) {
287+
ret _uint.to_str(u, 16u);
288+
}
289+
}
273290
}
274291

275-
fn conv_bool(bool b) -> str {
292+
fn conv_bool(&conv cv, bool b) -> str {
276293
if (b) {
277294
ret "true";
278295
} else {
279296
ret "false";
280297
}
281298
}
282299

283-
fn conv_char(char c) -> str {
300+
fn conv_char(&conv cv, char c) -> str {
284301
ret _str.from_char(c);
285302
}
286303
}

trunk/src/test/run-pass/syntax-extension-fmt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ fn main() {
2222
test(#fmt("%b", true), "true");
2323
test(#fmt("%b", false), "false");
2424
test(#fmt("%c", 'A'), "A");
25+
test(#fmt("%x", 0xff_u), "ff");
2526
}

0 commit comments

Comments
 (0)