Skip to content

Commit bb57e0d

Browse files
brsongraydon
authored andcommitted
---
yaml --- r: 1468 b: refs/heads/master c: 9528c34 h: refs/heads/master v: v3
1 parent 9e95bca commit bb57e0d

File tree

6 files changed

+142
-5
lines changed

6 files changed

+142
-5
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: e2d36e00ce2b58272b52a5c1f999b0bcb61d066b
2+
refs/heads/master: 9528c34774ff27b112c9e66afff6e10fa7021635

trunk/src/comp/front/ast.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ tag expr_ {
185185
expr_field(@expr, ident, ann);
186186
expr_index(@expr, @expr, ann);
187187
expr_path(path, option.t[def], ann);
188-
expr_ext(path, vec[@expr], option.t[@expr], ann);
188+
expr_ext(path, vec[@expr], option.t[@expr], option.t[@expr], ann);
189189
expr_fail;
190190
expr_ret(option.t[@expr]);
191191
expr_put(option.t[@expr]);
@@ -363,6 +363,17 @@ fn is_call_expr(@expr e) -> bool {
363363
}
364364
}
365365

366+
fn is_ext_expr(@expr e) -> bool {
367+
alt (e.node) {
368+
case (expr_ext(_, _, _, _, _)) {
369+
ret true;
370+
}
371+
case (_) {
372+
ret false;
373+
}
374+
}
375+
}
376+
366377
//
367378
// Local Variables:
368379
// mode: rust

trunk/src/comp/front/extfmt.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* The 'fmt' extension is modeled on the posix printf system.
2+
*
3+
* A posix conversion ostensibly looks like this:
4+
*
5+
* %[parameter][flags][width][.precision][length]type
6+
*
7+
* Given the different numeric type bestiary we have, we omit the 'length'
8+
* parameter and support slightly different conversions for 'type':
9+
*
10+
* %[parameter][flags][width][.precision]type
11+
*
12+
* we also only support translating-to-rust a tiny subset of the possible
13+
* combinations at the moment.
14+
*/
15+
16+
use std;
17+
18+
import std.option;
19+
20+
tag signedness {
21+
signed;
22+
unsigned;
23+
}
24+
25+
tag caseness {
26+
case_upper;
27+
case_lower;
28+
}
29+
30+
tag ty {
31+
ty_bool;
32+
ty_str;
33+
ty_char;
34+
ty_int(signedness);
35+
ty_bits;
36+
ty_hex(caseness);
37+
// FIXME: More types
38+
}
39+
40+
tag flag {
41+
flag_left_justify;
42+
flag_left_zero_pad;
43+
flag_left_space_pad;
44+
flag_plus_if_positive;
45+
flag_alternate;
46+
}
47+
48+
tag count {
49+
count_is(int);
50+
count_is_param(int);
51+
count_is_next_param;
52+
count_implied;
53+
}
54+
55+
// A formatted conversion from an expression to a string
56+
tag conv {
57+
conv_param(option.t[int]);
58+
conv_flags(vec[flag]);
59+
conv_width(count);
60+
conv_precision(count);
61+
conv_ty(ty);
62+
}
63+
64+
// A fragment of the output sequence
65+
tag piece {
66+
piece_string(str);
67+
piece_conv(str);
68+
}
69+
70+
fn expand_syntax_ext(vec[@ast.expr] args,
71+
option.t[@ast.expr] body) -> @ast.expr {
72+
fail;
73+
}
74+
75+
//
76+
// Local Variables:
77+
// mode: rust
78+
// fill-column: 78;
79+
// indent-tabs-mode: nil
80+
// c-basic-offset: 4
81+
// buffer-file-coding-system: utf-8-unix
82+
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
83+
// End:
84+
//

trunk/src/comp/front/parser.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,10 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
656656
some(token.COMMA),
657657
pf, p);
658658
hi = es.span;
659-
ex = ast.expr_ext(pth, es.node, none[@ast.expr], ast.ann_none);
659+
ex = ast.expr_ext(pth, es.node, none[@ast.expr],
660+
none[@ast.expr], ast.ann_none);
661+
// FIXME: Here is probably not the right place for this
662+
ex = expand_syntax_ext(p, @spanned(lo, hi, ex)).node;
660663
}
661664

662665
case (token.FAIL) {
@@ -736,6 +739,36 @@ impure fn parse_bottom_expr(parser p) -> @ast.expr {
736739
ret @spanned(lo, hi, ex);
737740
}
738741

742+
/*
743+
* FIXME: This is a crude approximation of the syntax-extension system,
744+
* for purposes of prototyping and/or hard-wiring any extensions we
745+
* wish to use while bootstrapping. The eventual aim is to permit
746+
* loading rust crates to process extensions, but this will likely
747+
* require a rust-based frontend, or an ocaml-FFI-based connection to
748+
* rust crates. At the moment we have neither.
749+
*/
750+
751+
impure fn expand_syntax_ext(parser p, @ast.expr ext) -> @ast.expr {
752+
check (ast.is_ext_expr(ext));
753+
alt (ext.node) {
754+
case (ast.expr_ext(?path, ?args, ?body, _, ?ann)) {
755+
check (_vec.len[ast.ident](path.node.idents) > 0u);
756+
auto extname = path.node.idents.(0);
757+
if (_str.eq(extname, "fmt")) {
758+
auto expanded = extfmt.expand_syntax_ext(args, body);
759+
check (ast.is_ext_expr(expanded));
760+
auto newexpr = ast.expr_ext(path, args, body,
761+
some[@ast.expr](expanded), ann);
762+
763+
ret @spanned(ext.span, ext.span, newexpr);
764+
} else {
765+
p.err("unknown syntax extension");
766+
}
767+
}
768+
}
769+
fail;
770+
}
771+
739772
impure fn extend_expr_by_ident(parser p, span lo, span hi,
740773
@ast.expr e, ast.ident i) -> @ast.expr {
741774
auto e_ = e.node;

trunk/src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std;
55

66
mod front {
77
mod ast;
8+
mod extfmt;
89
mod lexer;
910
mod parser;
1011
mod token;
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
use std;
2+
import std._str;
3+
4+
fn test(str actual, str expected) {
5+
log actual;
6+
log expected;
7+
check (_str.eq(actual, expected));
8+
}
9+
210
fn main() {
3-
auto s = #fmt("hello %d friends and %s things", 10, "formatted");
4-
log s;
11+
test(#fmt("hello %d friends and %s things", 10, "formatted"),
12+
"hello 10 friends and formatted things");
513
}

0 commit comments

Comments
 (0)