From 4cc53658a9503191f41945914e6223ccc5a95717 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Tue, 5 Jun 2018 23:03:13 +0200 Subject: While handling --- res/test_while.tpc | 13 +++++++++++++ src/generator.c | 8 ++++---- src/tpc.y | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 res/test_while.tpc 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 @@ +/* test-table-symboles.tpc */ + +/* Test file for simplified translator of a declaration of variables in C */ + +entier main(void) { + entier i; + i = 0; + while(i<=5){ + print(i); + i = i + 1; + } + return 0; +} 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) { fprintf(output, "jmp .false%d\n", idx); fprintf(output, ".true%d:\n", idx); fprintf(output, "push 1\n"); - fprintf(output, ".false%d:", idx); + fprintf(output, ".false%d:\n", idx); } void gen_and(int left, int right, int idx) { @@ -284,7 +284,7 @@ void gen_and(int left, int right, int idx) { fprintf(output, "jmp .true%d\n", idx); fprintf(output, ".false%d:\n", idx); fprintf(output, "push 0\n"); - fprintf(output, ".true%d:", idx); + fprintf(output, ".true%d:\n", idx); } 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) { else exit(1); // TODO: error on unexpected op - fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:", idx, idx, + fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx, idx, idx); } @@ -321,7 +321,7 @@ void gen_order(const char op[], int left, int right, int idx) { else exit(1); // TODO: error on unexpected op - fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:", idx, idx, + fprintf(output, "push 0\njmp .false%d\n.true%d:\npush 1\n.false%d:\n", idx, idx, idx); } diff --git a/src/tpc.y b/src/tpc.y index d8fd1f1..f84c0a6 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -126,7 +126,7 @@ Instr: | PRINT '(' Exp ')' ';' { gen_print($3);} | IF '(' Exp IfHandling')' Instr { gen_if_label($4); } | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling -| WHILE '(' Exp ')' Instr +| 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++;} | '{' SuiteInstr '}' ; IfHandling: { gen_if_start($$ = num_if++); }; -- cgit v1.2.3