Skip to content

Commit 916d26d

Browse files
brsongraydon
authored andcommitted
---
yaml --- r: 1471 b: refs/heads/master c: fe5de6b h: refs/heads/master i: 1469: 974383e 1467: 9e95bca 1463: 1192503 1455: 6021dd2 1439: 379173f 1407: bb08404 v: v3
1 parent 16d2c45 commit 916d26d

File tree

5 files changed

+127
-40
lines changed

5 files changed

+127
-40
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: f1500e5872d03e3ec3b140060641136a2ff5a15a
2+
refs/heads/master: fe5de6bfffee3c277f540517b5766547f34e81c7

trunk/src/comp/front/extfmt.rs

Lines changed: 120 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import front.parser;
1818
import std._str;
1919
import std._vec;
2020
import std.option;
21+
import std.option.none;
22+
import std.option.some;
2123

2224
tag signedness {
2325
signed;
@@ -55,13 +57,11 @@ tag count {
5557
}
5658

5759
// A formatted conversion from an expression to a string
58-
tag conv {
59-
conv_param(option.t[int]);
60-
conv_flags(vec[flag]);
61-
conv_width(count);
62-
conv_precision(count);
63-
conv_ty(ty);
64-
}
60+
type conv = rec(option.t[int] param,
61+
vec[flag] flags,
62+
count width,
63+
count precision,
64+
ty typ);
6565

6666
// A fragment of the output sequence
6767
tag piece {
@@ -84,6 +84,18 @@ fn expand_syntax_ext(vec[@ast.expr] args,
8484
auto fmt = expr_to_str(args.(0));
8585
log fmt;
8686
auto pieces = parse_fmt_string(fmt);
87+
log "printing all pieces";
88+
for (piece p in pieces) {
89+
alt (p) {
90+
case (piece_string(?s)) {
91+
log s;
92+
}
93+
case (piece_conv(_)) {
94+
log "conv";
95+
}
96+
}
97+
}
98+
log "done printing all pieces";
8799
ret pieces_to_expr(pieces, args);
88100
}
89101

@@ -107,33 +119,16 @@ fn parse_fmt_string(str s) -> vec[piece] {
107119
auto lim = _str.byte_len(s);
108120
auto buf = "";
109121

110-
// TODO: This is super ugly
111-
fn flush_buf(str buf, vec[piece] pieces) -> str {
112-
log "flushing";
122+
fn flush_buf(str buf, &vec[piece] pieces) -> str {
113123
if (_str.byte_len(buf) > 0u) {
114124
auto piece = piece_string(buf);
115125
pieces += piece;
116126
}
117-
log "buf:";
118-
log buf;
119-
log "pieces:";
120-
for (piece p in pieces) {
121-
alt (p) {
122-
case (piece_string(?s)) {
123-
log s;
124-
}
125-
case (piece_conv(_)) {
126-
log "conv";
127-
}
128-
}
129-
}
130127
ret "";
131128
}
132129

133130
auto i = 0u;
134131
while (i < lim) {
135-
log "step:";
136-
log i;
137132
auto curr = _str.substr(s, i, 1u);
138133
if (_str.eq(curr, "%")) {
139134
i += 1u;
@@ -146,18 +141,115 @@ fn parse_fmt_string(str s) -> vec[piece] {
146141
i += 1u;
147142
} else {
148143
buf = flush_buf(buf, pieces);
144+
auto res = parse_conversion(s, i, lim);
145+
pieces += res._0;
146+
i = res._1;
149147
}
150148
} else {
151149
buf += curr;
152-
log "buf:";
153-
log buf;
154150
i += 1u;
155151
}
156152
}
157-
153+
buf = flush_buf(buf, pieces);
158154
ret pieces;
159155
}
160156

157+
fn peek_num(str s, uint i, uint lim) -> option.t[tup(int, int)] {
158+
if (i >= lim) {
159+
ret none[tup(int, int)];
160+
} else {
161+
ret none[tup(int, int)];
162+
/*if ('0' <= c && c <= '9') {
163+
log c;
164+
fail;
165+
} else {
166+
ret option.none[tup(int, int)];
167+
}
168+
*/
169+
}
170+
}
171+
172+
fn parse_conversion(str s, uint i, uint lim) -> tup(piece, uint) {
173+
auto parm = parse_parameter(s, i, lim);
174+
auto flags = parse_flags(s, parm._1, lim);
175+
auto width = parse_width(s, flags._1, lim);
176+
auto prec = parse_precision(s, width._1, lim);
177+
auto ty = parse_type(s, prec._1, lim);
178+
ret tup(piece_conv(rec(param = parm._0,
179+
flags = flags._0,
180+
width = width._0,
181+
precision = prec._0,
182+
typ = ty._0)),
183+
ty._1);
184+
}
185+
186+
fn parse_parameter(str s, uint i, uint lim) -> tup(option.t[int], uint) {
187+
if (i >= lim) {
188+
ret tup(none[int], i);
189+
}
190+
191+
auto num = peek_num(s, i, lim);
192+
alt (num) {
193+
case (none[tup(int, int)]) {
194+
ret tup(none[int], i);
195+
}
196+
case (some[tup(int, int)](?t)) {
197+
fail;
198+
}
199+
}
200+
}
201+
202+
fn parse_flags(str s, uint i, uint lim) -> tup(vec[flag], uint) {
203+
let vec[flag] flags = vec();
204+
ret tup(flags, i);
205+
}
206+
207+
fn parse_width(str s, uint i, uint lim) -> tup(count, uint) {
208+
ret tup(count_implied, i);
209+
}
210+
211+
fn parse_precision(str s, uint i, uint lim) -> tup(count, uint) {
212+
ret tup(count_implied, i);
213+
}
214+
215+
fn parse_type(str s, uint i, uint lim) -> tup(ty, uint) {
216+
if (i >= lim) {
217+
log "missing type in conversion";
218+
fail;
219+
}
220+
221+
auto t;
222+
auto tstr = _str.substr(s, i, 1u);
223+
if (_str.eq(tstr, "b")) {
224+
t = ty_bool;
225+
} else if (_str.eq(tstr, "s")) {
226+
t = ty_str;
227+
} else if (_str.eq(tstr, "c")) {
228+
t = ty_char;
229+
} else if (_str.eq(tstr, "d")
230+
|| _str.eq(tstr, "i")) {
231+
// TODO: Do we really want two signed types here?
232+
// How important is it to be printf compatible?
233+
t = ty_int(signed);
234+
} else if (_str.eq(tstr, "u")) {
235+
t = ty_int(unsigned);
236+
} else if (_str.eq(tstr, "x")) {
237+
t = ty_hex(case_lower);
238+
} else if (_str.eq(tstr, "X")) {
239+
t = ty_hex(case_upper);
240+
} else if (_str.eq(tstr, "t")) {
241+
t = ty_bits;
242+
} else {
243+
// FIXME: This is a hack to avoid 'unsatisfied precondition
244+
// constraint' on uninitialized variable t below
245+
t = ty_bool;
246+
log "unknown type in conversion";
247+
fail;
248+
}
249+
250+
ret tup(t, i + 1u);
251+
}
252+
161253
fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
162254
auto lo = args.(0).span;
163255
auto hi = args.(0).span;

trunk/src/comp/middle/trans.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,8 +1957,6 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
19571957
ret C_nil();
19581958
}
19591959
case (ast.lit_str(?s)) {
1960-
log "translating literal:";
1961-
log s;
19621960
ret C_str(cx, s);
19631961
}
19641962
}
@@ -3601,7 +3599,6 @@ fn trans_rec(@block_ctxt cx, vec[ast.field] fields,
36013599
fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
36023600
alt (e.node) {
36033601
case (ast.expr_lit(?lit, ?ann)) {
3604-
log "translating literal";
36053602
ret res(cx, trans_lit(cx.fcx.ccx, *lit, ann));
36063603
}
36073604

@@ -3697,7 +3694,6 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
36973694
}
36983695

36993696
case (ast.expr_ext(_, _, _, ?expanded, _)) {
3700-
log "translating extension";
37013697
ret trans_expr(cx, option.get[@ast.expr](expanded));
37023698
}
37033699

trunk/src/rt/memory_region.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "rust_internal.h"
22
#include "memory_region.h"
33

4-
// #define TRACK_ALLOCATIONS
4+
#define TRACK_ALLOCATIONS
55

66
memory_region::memory_region(rust_srv *srv, bool synchronized) :
77
_srv(srv), _parent(NULL), _live_allocations(0),
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
//use std;
2-
//import std._str;
1+
use std;
2+
import std._str;
33

44
fn test(str actual, str expected) {
55
log actual;
66
log expected;
7-
//check (_str.eq(actual, expected));
7+
check (_str.eq(actual, expected));
88
}
99

1010
fn main() {
11-
/*test(#fmt("hello %d friends and %s things", 10, "formatted"),
12-
"hello 10 friends and formatted things");*/
13-
log #fmt("test");
11+
test(#fmt("hello %d friends and %s things", 10, "formatted"),
12+
"hello 10 friends and formatted things");
1413
}

0 commit comments

Comments
 (0)