提交 e5fdd7b6 编写于 作者: G Graydon Hoare

Support while and do-while loops in rustc.

上级 dc299c9a
......@@ -94,6 +94,8 @@
expr_lit(@lit, option[@ty]);
expr_cast(@expr, @ty);
expr_if(@expr, block, option[block], option[@ty]);
expr_while(@expr, block, option[@ty]);
expr_do_while(block, @expr, option[@ty]);
expr_block(block, option[@ty]);
expr_assign(@expr /* TODO: @expr|is_lval */, @expr, option[@ty]);
expr_field(@expr, ident, option[@ty]);
......
......@@ -613,6 +613,33 @@ fn spanned[T](&span lo, &span hi, &T node) -> ast.spanned[T] {
ret @spanned(lo, hi, ast.expr_if(cond, thn, els, none[@ast.ty]));
}
impure fn parse_while_expr(parser p) -> @ast.expr {
auto lo = p.get_span();
auto hi = lo;
expect(p, token.WHILE);
expect (p, token.LPAREN);
auto cond = parse_expr(p);
expect(p, token.RPAREN);
auto body = parse_block(p);
hi = body.span;
ret @spanned(lo, hi, ast.expr_while(cond, body, none[@ast.ty]));
}
impure fn parse_do_while_expr(parser p) -> @ast.expr {
auto lo = p.get_span();
auto hi = lo;
expect(p, token.DO);
auto body = parse_block(p);
expect(p, token.WHILE);
expect (p, token.LPAREN);
auto cond = parse_expr(p);
expect(p, token.RPAREN);
hi = cond.span;
ret @spanned(lo, hi, ast.expr_do_while(body, cond, none[@ast.ty]));
}
impure fn parse_expr(parser p) -> @ast.expr {
alt (p.peek()) {
case (token.LBRACE) {
......@@ -623,6 +650,12 @@ fn spanned[T](&span lo, &span hi, &T node) -> ast.spanned[T] {
case (token.IF) {
ret parse_if_expr(p);
}
case (token.WHILE) {
ret parse_while_expr(p);
}
case (token.DO) {
ret parse_do_while_expr(p);
}
case (_) {
ret parse_assign_expr(p);
}
......@@ -741,6 +774,16 @@ fn spanned[T](&span lo, &span hi, &T node) -> ast.spanned[T] {
ret @spanned(lo, e.span, ast.stmt_expr(e));
}
case (token.WHILE) {
auto e = parse_expr(p);
ret @spanned(lo, e.span, ast.stmt_expr(e));
}
case (token.DO) {
auto e = parse_expr(p);
ret @spanned(lo, e.span, ast.stmt_expr(e));
}
case (token.LBRACE) {
auto e = parse_expr(p);
ret @spanned(lo, e.span, ast.stmt_expr(e));
......
......@@ -81,6 +81,14 @@
&option[block] els,
option[@ty] ty) -> @expr) fold_expr_if,
(fn(&ENV e, &span sp,
@expr cond, &block body,
option[@ty] ty) -> @expr) fold_expr_while,
(fn(&ENV e, &span sp,
&block body, @expr cond,
option[@ty] ty) -> @expr) fold_expr_do_while,
(fn(&ENV e, &span sp,
&block blk, option[@ty] ty) -> @expr) fold_expr_block,
......@@ -335,6 +343,18 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
ret fld.fold_expr_if(env_, e.span, ccnd, tthn, eels, t);
}
case (ast.expr_while(?cnd, ?body, ?t)) {
auto ccnd = fold_expr(env_, fld, cnd);
auto bbody = fold_block(env_, fld, body);
ret fld.fold_expr_while(env_, e.span, ccnd, bbody, t);
}
case (ast.expr_do_while(?body, ?cnd, ?t)) {
auto bbody = fold_block(env_, fld, body);
auto ccnd = fold_expr(env_, fld, cnd);
ret fld.fold_expr_do_while(env_, e.span, bbody, ccnd, t);
}
case (ast.expr_block(?b, ?t)) {
auto bb = fold_block(env_, fld, b);
ret fld.fold_expr_block(env_, e.span, bb, t);
......@@ -598,6 +618,18 @@ fn identity_fold_expr_if[ENV](&ENV env, &span sp,
ret @respan(sp, ast.expr_if(cond, thn, els, t));
}
fn identity_fold_expr_while[ENV](&ENV env, &span sp,
@expr cond, &block body,
option[@ty] t) -> @expr {
ret @respan(sp, ast.expr_while(cond, body, t));
}
fn identity_fold_expr_do_while[ENV](&ENV env, &span sp,
&block body, @expr cond,
option[@ty] t) -> @expr {
ret @respan(sp, ast.expr_do_while(body, cond, t));
}
fn identity_fold_expr_block[ENV](&ENV env, &span sp, &block blk,
option[@ty] t) -> @expr {
ret @respan(sp, ast.expr_block(blk, t));
......@@ -765,6 +797,9 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_expr_unary = bind identity_fold_expr_unary[ENV](_,_,_,_,_),
fold_expr_lit = bind identity_fold_expr_lit[ENV](_,_,_,_),
fold_expr_if = bind identity_fold_expr_if[ENV](_,_,_,_,_,_),
fold_expr_while = bind identity_fold_expr_while[ENV](_,_,_,_,_),
fold_expr_do_while
= bind identity_fold_expr_do_while[ENV](_,_,_,_,_),
fold_expr_block = bind identity_fold_expr_block[ENV](_,_,_,_),
fold_expr_assign = bind identity_fold_expr_assign[ENV](_,_,_,_,_),
fold_expr_field = bind identity_fold_expr_field[ENV](_,_,_,_,_),
......
......@@ -653,6 +653,38 @@ fn trans_drop_str(@block_ctxt cx, ValueRef v) -> result {
ret res(next_cx, phi);
}
impure fn trans_while(@block_ctxt cx, &ast.expr cond,
&ast.block body) -> result {
auto cond_cx = new_empty_block_ctxt(cx.fcx);
auto body_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);
cx.build.Br(cond_cx.llbb);
auto cond_res = trans_expr(cond_cx, cond);
cond_cx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
auto body_res = trans_block(body_cx, body);
body_cx.build.Br(cond_cx.llbb);
ret res(next_cx, C_nil());
}
impure fn trans_do_while(@block_ctxt cx, &ast.block body,
&ast.expr cond) -> result {
auto body_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);
cx.build.Br(body_cx.llbb);
auto body_res = trans_block(body_cx, body);
auto cond_res = trans_expr(body_cx, cond);
body_cx.build.CondBr(cond_res.val,
body_cx.llbb,
next_cx.llbb);
ret res(next_cx, body_res.val);
}
// The additional bool returned indicates whether it's a local
// (that is represented as an alloca, hence needs a 'load' to be
// used as an rval).
......@@ -723,6 +755,10 @@ fn trans_lval(@block_ctxt cx, &ast.expr e)
ret trans_if(cx, *cond, thn, els);
}
case (ast.expr_while(?cond, ?body, _)) {
ret trans_while(cx, *cond, body);
}
case (ast.expr_block(?blk, _)) {
auto sub_cx = new_empty_block_ctxt(cx.fcx);
auto next_cx = new_extension_block_ctxt(cx);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册