Skip to content

Commit 8e6aa65

Browse files
committed
---
yaml --- r: 870 b: refs/heads/master c: e5fdd7b h: refs/heads/master v: v3
1 parent 67d0b9d commit 8e6aa65

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: dc299c9a19cd8e643258f07a485eaeecb3e900b9
2+
refs/heads/master: e5fdd7b63a7365df015c9d08111a8f635b25e058

trunk/src/comp/front/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ tag expr_ {
9494
expr_lit(@lit, option[@ty]);
9595
expr_cast(@expr, @ty);
9696
expr_if(@expr, block, option[block], option[@ty]);
97+
expr_while(@expr, block, option[@ty]);
98+
expr_do_while(block, @expr, option[@ty]);
9799
expr_block(block, option[@ty]);
98100
expr_assign(@expr /* TODO: @expr|is_lval */, @expr, option[@ty]);
99101
expr_field(@expr, ident, option[@ty]);

trunk/src/comp/front/parser.rs

+43
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,33 @@ impure fn parse_if_expr(parser p) -> @ast.expr {
613613
ret @spanned(lo, hi, ast.expr_if(cond, thn, els, none[@ast.ty]));
614614
}
615615

616+
impure fn parse_while_expr(parser p) -> @ast.expr {
617+
auto lo = p.get_span();
618+
auto hi = lo;
619+
620+
expect(p, token.WHILE);
621+
expect (p, token.LPAREN);
622+
auto cond = parse_expr(p);
623+
expect(p, token.RPAREN);
624+
auto body = parse_block(p);
625+
hi = body.span;
626+
ret @spanned(lo, hi, ast.expr_while(cond, body, none[@ast.ty]));
627+
}
628+
629+
impure fn parse_do_while_expr(parser p) -> @ast.expr {
630+
auto lo = p.get_span();
631+
auto hi = lo;
632+
633+
expect(p, token.DO);
634+
auto body = parse_block(p);
635+
expect(p, token.WHILE);
636+
expect (p, token.LPAREN);
637+
auto cond = parse_expr(p);
638+
expect(p, token.RPAREN);
639+
hi = cond.span;
640+
ret @spanned(lo, hi, ast.expr_do_while(body, cond, none[@ast.ty]));
641+
}
642+
616643
impure fn parse_expr(parser p) -> @ast.expr {
617644
alt (p.peek()) {
618645
case (token.LBRACE) {
@@ -623,6 +650,12 @@ impure fn parse_expr(parser p) -> @ast.expr {
623650
case (token.IF) {
624651
ret parse_if_expr(p);
625652
}
653+
case (token.WHILE) {
654+
ret parse_while_expr(p);
655+
}
656+
case (token.DO) {
657+
ret parse_do_while_expr(p);
658+
}
626659
case (_) {
627660
ret parse_assign_expr(p);
628661
}
@@ -741,6 +774,16 @@ impure fn parse_stmt(parser p) -> @ast.stmt {
741774
ret @spanned(lo, e.span, ast.stmt_expr(e));
742775
}
743776

777+
case (token.WHILE) {
778+
auto e = parse_expr(p);
779+
ret @spanned(lo, e.span, ast.stmt_expr(e));
780+
}
781+
782+
case (token.DO) {
783+
auto e = parse_expr(p);
784+
ret @spanned(lo, e.span, ast.stmt_expr(e));
785+
}
786+
744787
case (token.LBRACE) {
745788
auto e = parse_expr(p);
746789
ret @spanned(lo, e.span, ast.stmt_expr(e));

trunk/src/comp/middle/fold.rs

+35
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ type ast_fold[ENV] =
8181
&option[block] els,
8282
option[@ty] ty) -> @expr) fold_expr_if,
8383

84+
(fn(&ENV e, &span sp,
85+
@expr cond, &block body,
86+
option[@ty] ty) -> @expr) fold_expr_while,
87+
88+
(fn(&ENV e, &span sp,
89+
&block body, @expr cond,
90+
option[@ty] ty) -> @expr) fold_expr_do_while,
91+
8492
(fn(&ENV e, &span sp,
8593
&block blk, option[@ty] ty) -> @expr) fold_expr_block,
8694

@@ -335,6 +343,18 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
335343
ret fld.fold_expr_if(env_, e.span, ccnd, tthn, eels, t);
336344
}
337345

346+
case (ast.expr_while(?cnd, ?body, ?t)) {
347+
auto ccnd = fold_expr(env_, fld, cnd);
348+
auto bbody = fold_block(env_, fld, body);
349+
ret fld.fold_expr_while(env_, e.span, ccnd, bbody, t);
350+
}
351+
352+
case (ast.expr_do_while(?body, ?cnd, ?t)) {
353+
auto bbody = fold_block(env_, fld, body);
354+
auto ccnd = fold_expr(env_, fld, cnd);
355+
ret fld.fold_expr_do_while(env_, e.span, bbody, ccnd, t);
356+
}
357+
338358
case (ast.expr_block(?b, ?t)) {
339359
auto bb = fold_block(env_, fld, b);
340360
ret fld.fold_expr_block(env_, e.span, bb, t);
@@ -598,6 +618,18 @@ fn identity_fold_expr_if[ENV](&ENV env, &span sp,
598618
ret @respan(sp, ast.expr_if(cond, thn, els, t));
599619
}
600620

621+
fn identity_fold_expr_while[ENV](&ENV env, &span sp,
622+
@expr cond, &block body,
623+
option[@ty] t) -> @expr {
624+
ret @respan(sp, ast.expr_while(cond, body, t));
625+
}
626+
627+
fn identity_fold_expr_do_while[ENV](&ENV env, &span sp,
628+
&block body, @expr cond,
629+
option[@ty] t) -> @expr {
630+
ret @respan(sp, ast.expr_do_while(body, cond, t));
631+
}
632+
601633
fn identity_fold_expr_block[ENV](&ENV env, &span sp, &block blk,
602634
option[@ty] t) -> @expr {
603635
ret @respan(sp, ast.expr_block(blk, t));
@@ -765,6 +797,9 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
765797
fold_expr_unary = bind identity_fold_expr_unary[ENV](_,_,_,_,_),
766798
fold_expr_lit = bind identity_fold_expr_lit[ENV](_,_,_,_),
767799
fold_expr_if = bind identity_fold_expr_if[ENV](_,_,_,_,_,_),
800+
fold_expr_while = bind identity_fold_expr_while[ENV](_,_,_,_,_),
801+
fold_expr_do_while
802+
= bind identity_fold_expr_do_while[ENV](_,_,_,_,_),
768803
fold_expr_block = bind identity_fold_expr_block[ENV](_,_,_,_),
769804
fold_expr_assign = bind identity_fold_expr_assign[ENV](_,_,_,_,_),
770805
fold_expr_field = bind identity_fold_expr_field[ENV](_,_,_,_,_),

trunk/src/comp/middle/trans.rs

+36
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,38 @@ impure fn trans_if(@block_ctxt cx, &ast.expr cond,
653653
ret res(next_cx, phi);
654654
}
655655

656+
impure fn trans_while(@block_ctxt cx, &ast.expr cond,
657+
&ast.block body) -> result {
658+
659+
auto cond_cx = new_empty_block_ctxt(cx.fcx);
660+
auto body_cx = new_empty_block_ctxt(cx.fcx);
661+
auto next_cx = new_extension_block_ctxt(cx);
662+
663+
cx.build.Br(cond_cx.llbb);
664+
auto cond_res = trans_expr(cond_cx, cond);
665+
cond_cx.build.CondBr(cond_res.val,
666+
body_cx.llbb,
667+
next_cx.llbb);
668+
auto body_res = trans_block(body_cx, body);
669+
body_cx.build.Br(cond_cx.llbb);
670+
ret res(next_cx, C_nil());
671+
}
672+
673+
impure fn trans_do_while(@block_ctxt cx, &ast.block body,
674+
&ast.expr cond) -> result {
675+
676+
auto body_cx = new_empty_block_ctxt(cx.fcx);
677+
auto next_cx = new_extension_block_ctxt(cx);
678+
679+
cx.build.Br(body_cx.llbb);
680+
auto body_res = trans_block(body_cx, body);
681+
auto cond_res = trans_expr(body_cx, cond);
682+
body_cx.build.CondBr(cond_res.val,
683+
body_cx.llbb,
684+
next_cx.llbb);
685+
ret res(next_cx, body_res.val);
686+
}
687+
656688
// The additional bool returned indicates whether it's a local
657689
// (that is represented as an alloca, hence needs a 'load' to be
658690
// used as an rval).
@@ -723,6 +755,10 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
723755
ret trans_if(cx, *cond, thn, els);
724756
}
725757

758+
case (ast.expr_while(?cond, ?body, _)) {
759+
ret trans_while(cx, *cond, body);
760+
}
761+
726762
case (ast.expr_block(?blk, _)) {
727763
auto sub_cx = new_empty_block_ctxt(cx.fcx);
728764
auto next_cx = new_extension_block_ctxt(cx);

0 commit comments

Comments
 (0)