aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-06-06 15:10:11 +0200
committerpacien2018-06-06 15:10:11 +0200
commitc0feca77799b859a5331417595ac0190bb999e0d (patch)
tree3e7b45d90ae8608949e1c67c8269f1aaa19412f6
parent84c850e3a264d8bb1694c64348c445009b83b007 (diff)
downloadtpc-compiler-c0feca77799b859a5331417595ac0190bb999e0d.tar.gz
extract while instr
-rw-r--r--src/generator.c14
-rw-r--r--src/generator.h4
-rw-r--r--src/tpc.y6
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
80void gen_epilogue() { 80void 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
527void gen_while_start(int id) {
528 fprintf(output,".upwhile%d:\n", id);
529}
530
531void gen_while_cond(int id) {
532 fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", id);
533}
534
535void 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);
38void gen_if_end(int idx); 38void gen_if_end(int idx);
39void gen_ifelse_end(int idx); 39void gen_ifelse_end(int idx);
40 40
41void gen_while_start(int id);
42void gen_while_cond(int id);
43void gen_while_end(int id);
44
41int gen_assign(const char ident[], Scope scope); 45int gen_assign(const char ident[], Scope scope);
42 46
43void gen_or(int left, int right, int idx); 47void gen_or(int left, int right, int idx);
diff --git a/src/tpc.y b/src/tpc.y
index 16dc5ad..1e6b6e2 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -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;
143IfHandling: { gen_if_start($<num>$ = num_if++); }; 143IfHandling: { gen_if_start($<num>$ = num_if++); };