Skip to content

Commit 988ec75

Browse files
committed
---
yaml --- r: 1386 b: refs/heads/master c: 4a72a23 h: refs/heads/master v: v3
1 parent dba9399 commit 988ec75

File tree

8 files changed

+70
-2
lines changed

8 files changed

+70
-2
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: 15a01f5c3691a152793d8933a7be9d16a0fc7030
2+
refs/heads/master: 4a72a23171d87fb5a0f9b7ad039944856b93bf0f

trunk/src/comp/front/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ tag expr_ {
151151
expr_if(@expr, block, vec[tup(@expr, block)], option.t[block], ann);
152152
expr_while(@expr, block, ann);
153153
expr_for(@decl, @expr, block, ann);
154+
expr_for_each(@decl, @expr, block, ann);
154155
expr_do_while(block, @expr, ann);
155156
expr_alt(@expr, vec[arm], ann);
156157
expr_block(block, ann);

trunk/src/comp/front/parser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,14 @@ impure fn parse_head_local(parser p) -> @ast.decl {
10491049
impure fn parse_for_expr(parser p) -> @ast.expr {
10501050
auto lo = p.get_span();
10511051
auto hi = lo;
1052+
auto is_each = false;
10521053

10531054
expect(p, token.FOR);
1055+
if (p.peek() == token.EACH) {
1056+
is_each = true;
1057+
p.bump();
1058+
}
1059+
10541060
expect (p, token.LPAREN);
10551061

10561062
auto decl = parse_head_local(p);
@@ -1060,9 +1066,16 @@ impure fn parse_for_expr(parser p) -> @ast.expr {
10601066
expect(p, token.RPAREN);
10611067
auto body = parse_block(p);
10621068
hi = body.span;
1063-
ret @spanned(lo, hi, ast.expr_for(decl, seq, body, ast.ann_none));
1069+
if (is_each) {
1070+
ret @spanned(lo, hi, ast.expr_for_each(decl, seq, body,
1071+
ast.ann_none));
1072+
} else {
1073+
ret @spanned(lo, hi, ast.expr_for(decl, seq, body,
1074+
ast.ann_none));
1075+
}
10641076
}
10651077

1078+
10661079
impure fn parse_while_expr(parser p) -> @ast.expr {
10671080
auto lo = p.get_span();
10681081
auto hi = lo;
@@ -1422,6 +1435,8 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
14221435
case (ast.expr_cast(_,_,_)) { ret true; }
14231436
case (ast.expr_if(_,_,_,_,_)) { ret false; }
14241437
case (ast.expr_for(_,_,_,_)) { ret false; }
1438+
case (ast.expr_for_each(_,_,_,_))
1439+
{ ret false; }
14251440
case (ast.expr_while(_,_,_)) { ret false; }
14261441
case (ast.expr_do_while(_,_,_)) { ret false; }
14271442
case (ast.expr_alt(_,_,_)) { ret false; }

trunk/src/comp/middle/fold.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ type ast_fold[ENV] =
112112
@decl decl, @expr seq, &block body,
113113
ann a) -> @expr) fold_expr_for,
114114

115+
(fn(&ENV e, &span sp,
116+
@decl decl, @expr seq, &block body,
117+
ann a) -> @expr) fold_expr_for_each,
118+
115119
(fn(&ENV e, &span sp,
116120
@expr cond, &block body,
117121
ann a) -> @expr) fold_expr_while,
@@ -574,6 +578,13 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
574578
ret fld.fold_expr_for(env_, e.span, ddecl, sseq, bbody, t);
575579
}
576580

581+
case (ast.expr_for_each(?decl, ?seq, ?body, ?t)) {
582+
auto ddecl = fold_decl(env_, fld, decl);
583+
auto sseq = fold_expr(env_, fld, seq);
584+
auto bbody = fold_block(env_, fld, body);
585+
ret fld.fold_expr_for_each(env_, e.span, ddecl, sseq, bbody, t);
586+
}
587+
577588
case (ast.expr_while(?cnd, ?body, ?t)) {
578589
auto ccnd = fold_expr(env_, fld, cnd);
579590
auto bbody = fold_block(env_, fld, body);
@@ -1087,6 +1098,12 @@ fn identity_fold_expr_for[ENV](&ENV env, &span sp,
10871098
ret @respan(sp, ast.expr_for(d, seq, body, a));
10881099
}
10891100

1101+
fn identity_fold_expr_for_each[ENV](&ENV env, &span sp,
1102+
@decl d, @expr seq,
1103+
&block body, ann a) -> @expr {
1104+
ret @respan(sp, ast.expr_for_each(d, seq, body, a));
1105+
}
1106+
10901107
fn identity_fold_expr_while[ENV](&ENV env, &span sp,
10911108
@expr cond, &block body, ann a) -> @expr {
10921109
ret @respan(sp, ast.expr_while(cond, body, a));
@@ -1402,6 +1419,8 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
14021419
fold_expr_cast = bind identity_fold_expr_cast[ENV](_,_,_,_,_),
14031420
fold_expr_if = bind identity_fold_expr_if[ENV](_,_,_,_,_,_,_),
14041421
fold_expr_for = bind identity_fold_expr_for[ENV](_,_,_,_,_,_),
1422+
fold_expr_for_each
1423+
= bind identity_fold_expr_for_each[ENV](_,_,_,_,_,_),
14051424
fold_expr_while = bind identity_fold_expr_while[ENV](_,_,_,_,_),
14061425
fold_expr_do_while
14071426
= bind identity_fold_expr_do_while[ENV](_,_,_,_,_),

trunk/src/comp/middle/resolve.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ fn update_env_for_expr(&env e, @ast.expr x) -> env {
565565
case (ast.expr_for(?d, _, _, _)) {
566566
ret rec(scopes = cons[scope](scope_loop(d), @e.scopes) with e);
567567
}
568+
case (ast.expr_for_each(?d, _, _, _)) {
569+
ret rec(scopes = cons[scope](scope_loop(d), @e.scopes) with e);
570+
}
568571
case (_) { }
569572
}
570573
ret e;

trunk/src/comp/middle/trans.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,15 @@ fn trans_for(@block_ctxt cx,
21162116
bind inner(_, local, _, _, body));
21172117
}
21182118

2119+
fn trans_for_each(@block_ctxt cx,
2120+
@ast.decl decl,
2121+
@ast.expr seq,
2122+
&ast.block body) -> result {
2123+
cx.fcx.ccx.sess.unimpl("for each loop");
2124+
fail;
2125+
}
2126+
2127+
21192128
fn trans_while(@block_ctxt cx, @ast.expr cond,
21202129
&ast.block body) -> result {
21212130

@@ -3035,6 +3044,10 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
30353044
ret trans_for(cx, decl, seq, body);
30363045
}
30373046

3047+
case (ast.expr_for_each(?decl, ?seq, ?body, _)) {
3048+
ret trans_for_each(cx, decl, seq, body);
3049+
}
3050+
30383051
case (ast.expr_while(?cond, ?body, _)) {
30393052
ret trans_while(cx, cond, body);
30403053
}

trunk/src/comp/middle/ty.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ fn expr_ty(@ast.expr expr) -> @t {
676676
case (ast.expr_cast(_, _, ?ann)) { ret ann_to_type(ann); }
677677
case (ast.expr_if(_, _, _, _, ?ann)) { ret ann_to_type(ann); }
678678
case (ast.expr_for(_, _, _, ?ann)) { ret ann_to_type(ann); }
679+
case (ast.expr_for_each(_, _, _, ?ann))
680+
{ ret ann_to_type(ann); }
679681
case (ast.expr_while(_, _, ?ann)) { ret ann_to_type(ann); }
680682
case (ast.expr_do_while(_, _, ?ann)) { ret ann_to_type(ann); }
681683
case (ast.expr_alt(_, _, ?ann)) { ret ann_to_type(ann); }

trunk/src/comp/middle/typeck.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,10 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e,
995995
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
996996
e_1 = ast.expr_for(decl, seq, bloc, ast.ann_type(t));
997997
}
998+
case (ast.expr_for_each(?decl, ?seq, ?bloc, ?ann)) {
999+
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
1000+
e_1 = ast.expr_for_each(decl, seq, bloc, ast.ann_type(t));
1001+
}
9981002
case (ast.expr_while(?cond, ?bloc, ?ann)) {
9991003
auto t = demand(fcx, e.span, expected, ann_to_type(ann));
10001004
e_1 = ast.expr_while(cond, bloc, ast.ann_type(t));
@@ -1448,6 +1452,17 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
14481452
body_1, ann));
14491453
}
14501454

1455+
case (ast.expr_for_each(?decl, ?seq, ?body, _)) {
1456+
auto decl_1 = check_decl_local(fcx, decl);
1457+
auto seq_1 = check_expr(fcx, seq);
1458+
auto body_1 = check_block(fcx, body);
1459+
1460+
auto ann = ast.ann_type(plain_ty(ty.ty_nil));
1461+
ret @fold.respan[ast.expr_](expr.span,
1462+
ast.expr_for_each(decl_1, seq_1,
1463+
body_1, ann));
1464+
}
1465+
14511466
case (ast.expr_while(?cond, ?body, _)) {
14521467
auto cond_0 = check_expr(fcx, cond);
14531468
auto cond_1 = demand_expr(fcx, plain_ty(ty.ty_bool), cond_0);

0 commit comments

Comments
 (0)