diff options
author | Adam NAILI | 2018-06-05 23:03:13 +0200 |
---|---|---|
committer | Adam NAILI | 2018-06-05 23:03:13 +0200 |
commit | 4cc53658a9503191f41945914e6223ccc5a95717 (patch) | |
tree | 3e0f146fdcfc66f35c8bc3a5d7a1aa2d2d1d50bb | |
parent | 864653f00dff0a8a1f37d8e9a732c1da8309a930 (diff) | |
download | tpc-compiler-4cc53658a9503191f41945914e6223ccc5a95717.tar.gz |
While handling
-rw-r--r-- | res/test_while.tpc | 13 | ||||
-rw-r--r-- | src/generator.c | 8 | ||||
-rw-r--r-- | src/tpc.y | 2 |
3 files changed, 18 insertions, 5 deletions
diff --git a/res/test_while.tpc b/res/test_while.tpc new file mode 100644 index 0000000..25d04c6 --- /dev/null +++ b/res/test_while.tpc | |||
@@ -0,0 +1,13 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | |||
5 | entier main(void) { | ||
6 | entier i; | ||
7 | i = 0; | ||
8 | while(i<=5){ | ||
9 | print(i); | ||
10 | i = i + 1; | ||
11 | } | ||
12 | return 0; | ||
13 | } | ||
diff --git a/src/generator.c b/src/generator.c index 3ecfe0d..dc85517 100644 --- a/src/generator.c +++ b/src/generator.c | |||
@@ -266,7 +266,7 @@ void gen_or(int left, int right, int idx) { | |||
266 | fprintf(output, "jmp .false%d\n", idx); | 266 | fprintf(output, "jmp .false%d\n", idx); |
267 | fprintf(output, ".true%d:\n", idx); | 267 | fprintf(output, ".true%d:\n", idx); |
268 | fprintf(output, "push 1\n"); | 268 | fprintf(output, "push 1\n"); |
269 | fprintf(output, ".false%d:", idx); | 269 | fprintf(output, ".false%d:\n", idx); |
270 | } | 270 | } |
271 | 271 | ||
272 | void gen_and(int left, int right, int idx) { | 272 | void gen_and(int left, int right, int idx) { |
@@ -284,7 +284,7 @@ void gen_and(int left, int right, int idx) { | |||
284 | fprintf(output, "jmp .true%d\n", idx); | 284 | fprintf(output, "jmp .true%d\n", idx); |
285 | fprintf(output, ".false%d:\n", idx); | 285 | fprintf(output, ".false%d:\n", idx); |
286 | fprintf(output, "push 0\n"); | 286 | fprintf(output, "push 0\n"); |
287 | fprintf(output, ".true%d:", idx); | 287 | fprintf(output, ".true%d:\n", idx); |
288 | } | 288 | } |
289 | 289 | ||
290 | void gen_eq(const char op[], int left, int right, int idx) { | 290 | void gen_eq(const char op[], int left, int right, int idx) { |
@@ -300,7 +300,7 @@ void gen_eq(const char op[], int left, int right, int idx) { | |||
300 | else | 300 | else |
301 | exit(1); // TODO: error on unexpected op | 301 | exit(1); // TODO: error on unexpected op |
302 | 302 | ||
303 | fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:", idx, idx, | 303 | fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx, idx, |
304 | idx); | 304 | idx); |
305 | } | 305 | } |
306 | 306 | ||
@@ -321,7 +321,7 @@ void gen_order(const char op[], int left, int right, int idx) { | |||
321 | else | 321 | else |
322 | exit(1); // TODO: error on unexpected op | 322 | exit(1); // TODO: error on unexpected op |
323 | 323 | ||
324 | fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:", idx, idx, | 324 | fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx, idx, |
325 | idx); | 325 | idx); |
326 | } | 326 | } |
327 | 327 | ||
@@ -126,7 +126,7 @@ Instr: | |||
126 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} | 126 | | PRINT '(' Exp ')' ';' { gen_print($<type>3);} |
127 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } | 127 | | IF '(' Exp IfHandling')' Instr { gen_if_label($<num>4); } |
128 | | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling | 128 | | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling |
129 | | WHILE '(' Exp ')' Instr | 129 | | WHILE {fprintf(output,".upwhile%d:\n",num_while);}'(' Exp {fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n",num_while);}')' Instr {fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n",num_while,num_while);num_while++;} |
130 | | '{' SuiteInstr '}' | 130 | | '{' SuiteInstr '}' |
131 | ; | 131 | ; |
132 | IfHandling: { gen_if_start($<num>$ = num_if++); }; | 132 | IfHandling: { gen_if_start($<num>$ = num_if++); }; |