diff options
-rw-r--r-- | src/generator.c | 14 | ||||
-rw-r--r-- | src/generator.h | 4 | ||||
-rw-r--r-- | src/tpc.y | 6 |
3 files changed, 20 insertions, 4 deletions
diff --git a/src/generator.c b/src/generator.c index e4b79e3..9948966 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -74,7 +74,7 @@ void gen_prologue_continue(bool *bss_done) { | |||
74 | fprintf(output, "pop rsi\n"); | 74 | fprintf(output, "pop rsi\n"); |
75 | fprintf(output, "pop rbp\n"); | 75 | fprintf(output, "pop rbp\n"); |
76 | fprintf(output, "ret\n"); | 76 | fprintf(output, "ret\n"); |
77 | *bss_done = 1; | 77 | *bss_done = true; |
78 | } | 78 | } |
79 | 79 | ||
80 | void gen_epilogue() { | 80 | void gen_epilogue() { |
@@ -523,3 +523,15 @@ int gen_char(int value, Scope scope) { | |||
523 | fprintf(output, "push %d\n", value); | 523 | fprintf(output, "push %d\n", value); |
524 | return CHAR; | 524 | return CHAR; |
525 | } | 525 | } |
526 | |||
527 | void gen_while_start(int id) { | ||
528 | fprintf(output,".upwhile%d:\n", id); | ||
529 | } | ||
530 | |||
531 | void gen_while_cond(int id) { | ||
532 | fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", id); | ||
533 | } | ||
534 | |||
535 | void gen_while_end(int id) { | ||
536 | fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", id, id); | ||
537 | } | ||
diff --git a/src/generator.h b/src/generator.h index e1fd714..94b03c8 100644 --- a/src/generator.h +++ b/src/generator.h | |||
@@ -38,6 +38,10 @@ void gen_if_start(int idx); | |||
38 | void gen_if_end(int idx); | 38 | void gen_if_end(int idx); |
39 | void gen_ifelse_end(int idx); | 39 | void gen_ifelse_end(int idx); |
40 | 40 | ||
41 | void gen_while_start(int id); | ||
42 | void gen_while_cond(int id); | ||
43 | void gen_while_end(int id); | ||
44 | |||
41 | int gen_assign(const char ident[], Scope scope); | 45 | int gen_assign(const char ident[], Scope scope); |
42 | 46 | ||
43 | void gen_or(int left, int right, int idx); | 47 | void gen_or(int left, int right, int idx); |
@@ -135,9 +135,9 @@ Instr: | |||
135 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } | 135 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } |
136 | | IF '(' Exp IfHandling')' Instr | 136 | | IF '(' Exp IfHandling')' Instr |
137 | ELSE IfEndHandling Instr IfElseEndHandling | 137 | ELSE IfEndHandling Instr IfElseEndHandling |
138 | | WHILE { fprintf(output,".upwhile%d:\n", num_while); } | 138 | | WHILE { gen_while_start(num_while); } |
139 | '(' Exp { fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", num_while); } | 139 | '(' Exp { gen_while_cond(num_while); } |
140 | ')' Instr { fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", num_while, num_while); num_while++; } | 140 | ')' Instr { gen_while_end(num_while++); } |
141 | | '{' SuiteInstr '}' | 141 | | '{' SuiteInstr '}' |
142 | ; | 142 | ; |
143 | IfHandling: { gen_if_start($<num>$ = num_if++); }; | 143 | IfHandling: { gen_if_start($<num>$ = num_if++); }; |