From 9ba26ce8a29e4e12a3387e26b241ab6a86eb5afd Mon Sep 17 00:00:00 2001 From: mike lowis Date: Thu, 29 Apr 2021 23:08:22 -0400 Subject: [PATCH] cleaned up loops --- cerise/src/grammar.c | 63 +++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index d0f8187..51b1547 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -282,7 +282,7 @@ RULE(type) RULE(var_decl) { (void)item; - while (1) + do { int nsyms = 0; Symbol* sym = NULL; @@ -290,19 +290,15 @@ RULE(var_decl) char* name = NULL; bool export = false; - while (1) + do { name = expect_text(p, IDENT); export = accept(p, '*'); sym = symbol_new(p, name, SYM_VAR, export); sym->global = 1; nsyms++; - - if (!accept(p, ',')) - { - break; - } } + while (accept(p, ',')); expect(p, ':'); name = expect_text(p, IDENT); @@ -315,12 +311,8 @@ RULE(var_decl) codegen_global(p, sym->name, sym->type); sym = sym->next; } - - if (!matches(p, IDENT)) - { - break; - } } + while (matches(p, IDENT)); } RULE(type_decl) @@ -329,22 +321,16 @@ RULE(type_decl) bool export = false; Symbol* sym = NULL; - while (1) + do { name = expect_text(p, IDENT); export = accept(p, '*'); sym = symbol_new(p, name, SYM_TYPE, export); expect(p, '='); type(p, item); -// expression(p, item); -// sym->imm = item->imm; -// sym->type = item->type; - - if (!matches(p, IDENT)) - { - break; - } + sym->type = item->type; } + while (matches(p, IDENT)); } RULE(const_decl) @@ -353,7 +339,7 @@ RULE(const_decl) bool export = false; Symbol* sym = NULL; - while (1) + do { name = expect_text(p, IDENT); export = accept(p, '*'); @@ -363,17 +349,13 @@ RULE(const_decl) expression(p, item); sym->imm = item->imm; sym->type = item->type; - - if (!matches(p, IDENT)) - { - break; - } } + while (matches(p, IDENT)); } RULE(statement_seq) { - while (1) + do { if (matches(p, IDENT)) { @@ -401,38 +383,45 @@ RULE(statement_seq) expect(p, IF); expression(p, item); check_bool(p, item); + // CFJump(item) expect(p, THEN); statement_seq(p, &(Item){0}); + // L = 0 while (accept(p, ELSIF)) { + // FJump(L) + // FixLink(item->imm.i) expression(p, item); check_bool(p, item); + // CFJump(item) expect(p, THEN); statement_seq(p, &(Item){0}); } if (accept(p, ELSE)) { + // FJump(L) + // FixLink(item->imm.i) statement_seq(p, &(Item){0}); } + else + { + // FixLink(item->imm.i) + } expect(p, END); } else { error(p, "expected a statement"); } - - if (matches(p, END) || matches(p, ELSE) || matches(p, ELSIF)) - { - break; - } } + while (!matches(p, END) && !matches(p, ELSE) && !matches(p, ELSIF)); } RULE(import_list) { (void)item; expect(p, IMPORT); - while (1) + do { Module* m = calloc(1, sizeof(Module)); m->name = expect_text(p, IDENT); @@ -443,12 +432,8 @@ RULE(import_list) m->next = p->imports; p->imports = m; } - - if (!matches(p, IDENT)) - { - break; - } } + while (matches(p, IDENT)); /* reverse the list so when we init it happens in the right order */ Module* imports = p->imports; -- 2.49.0