diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-05-10 14:45:19 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-05-10 14:45:19 -0400 |
commit | 10aea299fe118d852962e71f1a0e6a23b6105562 (patch) | |
tree | 1c6b5621ba7399e337fc4c3a548c0c20245c4271 | |
parent | 2302b1049ddedb4b8c2c1d29ed2e53f0cd56de62 (diff) |
remove while loops with no condition
-rw-r--r-- | cgen.c | 9 | ||||
-rw-r--r-- | copy.c | 3 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 29 | ||||
-rw-r--r-- | types.c | 2 |
5 files changed, 16 insertions, 28 deletions
@@ -100,8 +100,7 @@ static void cgen_defs_decl(CGenerator *g, Declaration *d); } break; \ case EXPR_WHILE: { \ WhileExpr *w = e->while_; \ - if (w->cond) \ - f(g, w->cond); \ + f(g, w->cond); \ block_f(g, &w->body); \ } break; \ case EXPR_FOR: { \ @@ -1478,11 +1477,7 @@ static void cgen_expr(CGenerator *g, Expression *e) { case EXPR_WHILE: { WhileExpr *w = e->while_; cgen_write(g, "while ("); - if (w->cond) { - cgen_expr(g, w->cond); - } else { - cgen_write(g, "true"); - } + cgen_expr(g, w->cond); cgen_write(g, ") "); cgen_block(g, &w->body, NULL, 0); } break; @@ -262,8 +262,7 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) { WhileExpr *win = in->while_; WhileExpr *wout = out->while_ = allocr_malloc(a, sizeof *wout); *wout = *win; - if (win->cond) - wout->cond = copy_expr_(c, win->cond); + wout->cond = copy_expr_(c, win->cond); copy_block(c, &wout->body, &win->body, 0); } break; case EXPR_FOR: { @@ -8,7 +8,6 @@ /* @TODO: -remove while loops with no condition- they're weird initialization functions (maybe #init(-50), where -50 is the priority and <0 is reserved for standard library) detect circular declarations (A ::= B; B ::= A) either detect circular #includes or set a #include depth limit (maybe sometimes you want finite circular includes with #if) @@ -1411,23 +1411,18 @@ static Status parse_expr(Parser *p, Expression *e, Token *end) { e->kind = EXPR_WHILE; WhileExpr *w = e->while_ = parser_malloc(p, sizeof *w); ++t->token; - if (token_is_kw(t->token, KW_LBRACE)) { - /* infinite loop */ - w->cond = NULL; - } else { - Token *cond_end = expr_find_end(p, EXPR_CAN_END_WITH_LBRACE); - if (!cond_end) return false; - if (!token_is_kw(cond_end, KW_LBRACE)) { - t->token = cond_end; - tokr_err(t, "Expected { to open while body."); - return false; - } - Expression *cond = parser_new_expr(p); - w->cond = cond; - - if (!parse_expr(p, cond, cond_end)) - return false; + Token *cond_end = expr_find_end(p, EXPR_CAN_END_WITH_LBRACE); + if (!cond_end) return false; + if (!token_is_kw(cond_end, KW_LBRACE)) { + t->token = cond_end; + tokr_err(t, "Expected { to open while body."); + return false; } + Expression *cond = parser_new_expr(p); + w->cond = cond; + + if (!parse_expr(p, cond, cond_end)) + return false; if (!parse_block(p, &w->body, 0)) return false; w->body.kind = BLOCK_WHILE; goto success; @@ -2733,7 +2728,7 @@ static void fprint_expr(FILE *out, Expression *e) { case EXPR_WHILE: { WhileExpr *w = e->while_; fprintf(out, "while "); - if (w->cond) fprint_expr(out, w->cond); + fprint_expr(out, w->cond); fprint_block(out, &w->body); } break; case EXPR_FOR: { @@ -2174,7 +2174,7 @@ static Status types_expr(Typer *tr, Expression *e) { case EXPR_WHILE: { WhileExpr *w = e->while_; bool ret = true; - if (w->cond && !types_expr(tr, w->cond)) + if (!types_expr(tr, w->cond)) ret = false; if (!types_block(tr, &w->body)) ret = false; |