# HG changeset patch # User John Tsiombikas # Date 1426460809 -7200 # Node ID bebbe71ba949deca217a98bf5957caf2aaf5a392 # Parent ae1c30fa39f389b57ce48802e0c3bb70c4459884 mostly completed tutor5.txt, without the hundred different looping constructs, which I don't care about diff -r ae1c30fa39f3 -r bebbe71ba949 src/main.c --- a/src/main.c Mon Mar 16 00:13:54 2015 +0200 +++ b/src/main.c Mon Mar 16 01:06:49 2015 +0200 @@ -13,9 +13,10 @@ void init(void); void program(void); -void block(void); -void doif(void); +void block(int brklb); +void doif(int brklb); void dowhile(void); +void dobreak(int brklb); void condition(void); void expression(void); void assignment(void); @@ -94,7 +95,7 @@ void program(void) { - block(); + block(-1); if(look != 'e') { expected("End"); } @@ -106,18 +107,22 @@ emitln("%c", get_name()); } -void block(void) +void block(int brklb) { while(look != 'e' && look != 'l') { switch(look) { case 'i': - doif(); + doif(brklb); break; case 'w': dowhile(); break; + case 'b': + dobreak(brklb); + break; + default: other(); break; @@ -125,7 +130,7 @@ } } -void doif(void) +void doif(int brklb) { int lb1, lb2; @@ -136,14 +141,14 @@ lb2 = lb1; emitln("jz %s", labelstr(lb1)); - block(); + block(brklb); if(look == 'l') { match('l'); lb2 = newlabel(); emitln("jmp %s", labelstr(lb2)); postlabel(lb1); - block(); + block(brklb); } match('e'); postlabel(lb2); @@ -159,12 +164,21 @@ postlabel(lbtop); condition(); emitln("jz %s", labelstr(lbend)); - block(); + block(lbend); match('e'); emitln("jmp %s", labelstr(lbtop)); postlabel(lbend); } +void dobreak(int brklb) +{ + match('b'); + if(brklb == -1) { + stop("invalid break, not in loop"); + } + emitln("jmp %s", labelstr(brklb)); +} + void condition(void) { emitln("");