From 10aea299fe118d852962e71f1a0e6a23b6105562 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 10 May 2020 14:45:19 -0400 Subject: remove while loops with no condition --- cgen.c | 9 ++------- copy.c | 3 +-- main.c | 1 - parse.c | 29 ++++++++++++----------------- types.c | 2 +- 5 files changed, 16 insertions(+), 28 deletions(-) diff --git a/cgen.c b/cgen.c index 1bd9e26..6ac08d4 100644 --- a/cgen.c +++ b/cgen.c @@ -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; diff --git a/copy.c b/copy.c index a8b63a2..ccb4289 100644 --- a/copy.c +++ b/copy.c @@ -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: { diff --git a/main.c b/main.c index 1d83b41..49b1cc2 100644 --- a/main.c +++ b/main.c @@ -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) diff --git a/parse.c b/parse.c index 4112492..3e0a5dd 100644 --- a/parse.c +++ b/parse.c @@ -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: { diff --git a/types.c b/types.c index 31de794..d04279b 100644 --- a/types.c +++ b/types.c @@ -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; -- cgit v1.2.3