From 68aeb4d929dd949a51a2b87e540e2a43bb9bba3c Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Thu, 9 Jan 2025 18:26:09 +0100 Subject: [PATCH 01/13] added test3_max.corec --- Codes_en_COREC/tests/test3_max.corec | 70 ++++++++++++++++++++++++++++ corec.y | 8 ++-- lib.c | 1 + 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 Codes_en_COREC/tests/test3_max.corec diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec new file mode 100644 index 0000000..9061c95 --- /dev/null +++ b/Codes_en_COREC/tests/test3_max.corec @@ -0,0 +1,70 @@ +// Aff 1D arrays + +// array1[15] = 10.10; bug ? + +prog Test3 { + def Main{ + Loc : (array1,50) + Rec : { + + array1[0] = 5.32; + array1[1] = 12; + array1[2] = 7.89; + array1[3] = 3.14; + array1[4] = 42; + array1[5] = 18.27; + array1[6] = 1.11; + array1[7] = 6; + array1[8] = 21.95; + array1[9] = 14.01; + array1[10] = 9; + array1[11] = 2.71; + array1[12] = 8.56; + array1[13] = 17; + array1[14] = 4.44; + array1[15] = 10.11; + array1[16] = 3.33; + array1[17] = 22; + array1[18] = 19.5; + array1[19] = 7.77; + array1[20] = 13.37; + array1[21] = 5; + array1[22] = 25.25; + array1[23] = 8; + array1[24] = 20.2; + array1[25] = 11.11; + array1[26] = 15; + array1[27] = 30.3; + array1[28] = 16.66; + array1[29] = 4; + array1[30] = 18; + array1[31] = 7.07; + array1[32] = 12.34; + array1[33] = 9.87; + array1[34] = 14; + array1[35] = 6.66; + array1[36] = 5.55; + array1[37] = 19; + array1[38] = 2.22; + array1[39] = 20; + array1[40] = 3; + array1[41] = 18.88; + array1[42] = 4.04; + array1[43] = 22.22; + array1[44] = 8.8; + array1[45] = 16; + array1[46] = 11.23; + array1[47] = 7.5; + array1[48] = 6.25; + array1[49] = 9.9; + + printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); + printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + + printstr("\nObtained Result : \n"); + print(array1) // contain all the values exchanges here if the code is correct + } + } +} + diff --git a/corec.y b/corec.y index 86af826..77b2a6c 100644 --- a/corec.y +++ b/corec.y @@ -941,10 +941,10 @@ COND : fprintf(stderr, "BUG\n"); break; } - name_t end_condition = snprintf(end_condition, sizeof(end_condition), "end_condition_%d", t->condition); - struct symbol * label = symtable_put_label(SYMTAB,end_condition); - name_t else_condition = snprintf(else_condition, sizeof(else_condition), "else_condition_%d", t->condition); - struct symbol * label = symtable_put_label(SYMTAB,else_condition); + // name_t end_condition = snprintf(end_condition, sizeof(end_condition), "end_condition_%d", t->condition); + // struct symbol * label = symtable_put_label(SYMTAB,end_condition); + // name_t else_condition = snprintf(else_condition, sizeof(else_condition), "else_condition_%d", t->condition); + // struct symbol * label = symtable_put_label(SYMTAB,else_condition); // TODO ahndle and gencode for if and else branches } diff --git a/lib.c b/lib.c index 1b2680c..2f201c6 100644 --- a/lib.c +++ b/lib.c @@ -316,6 +316,7 @@ static void symbol_dump(struct symbol* s, FILE* fout) break; case LABEL: fprintf(fout,"%s",s->u.name); + break; // ? default: fprintf(stderr,"BUG SYMBOL DUMP NOT RECOGNIZED\n"); break; -- GitLab From f095cb8c902616fc29540dca97ea51b32ed104e3 Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Thu, 9 Jan 2025 18:27:26 +0100 Subject: [PATCH 02/13] rectified test3_max.corec --- Codes_en_COREC/tests/test3_max.corec | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec index 9061c95..e58a1ef 100644 --- a/Codes_en_COREC/tests/test3_max.corec +++ b/Codes_en_COREC/tests/test3_max.corec @@ -58,9 +58,11 @@ prog Test3 { array1[48] = 6.25; array1[49] = 9.9; - printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); - printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); - printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + printstr("Expected result : \n5.32 12 7.89 3.14 42 18.27 1.11 6 21.95 14.01 9 2.71 8.56 17 4.44 1"); + + // printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + // printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); + // printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); printstr("\nObtained Result : \n"); print(array1) // contain all the values exchanges here if the code is correct -- GitLab From f5cbf9d81793fdfc1c57220ebbb393a02d9b2a44 Mon Sep 17 00:00:00 2001 From: CLAVEL JULIEN <julien.clavel@etu.unistra.fr> Date: Thu, 9 Jan 2025 18:53:11 +0100 Subject: [PATCH 03/13] fixed printstr limitation now can go up to 1023 chars --- corec.l | 4 ++-- corec.y | 3 ++- lib.h | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/corec.l b/corec.l index ff6f3aa..e670699 100644 --- a/corec.l +++ b/corec.l @@ -81,8 +81,8 @@ in {return in_dom;} ["]([^"]|[\\]["])*["] { if ( yyleng > 1023 ) fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 1023), truncated\n",lineNumber,yytext); - strncpy(yylval.strval,yytext,1023); - yylval.strval[1024] = '\0'; + strncpy(yylval.chain,yytext,1023); + yylval.chain[1023] = '\0'; return chaine; } diff --git a/corec.y b/corec.y index 77b2a6c..2ab8bc8 100644 --- a/corec.y +++ b/corec.y @@ -35,6 +35,7 @@ void exit_safely(){ long int intval; float floatval; name_t strval; + name_64_t chain; struct { struct symbol* ptr; @@ -75,7 +76,7 @@ void exit_safely(){ %token <strval> ID1 %token <strval> ID2 -%token <strval> chaine +%token <chain> chaine %token <intval> entier %token <floatval> flottant diff --git a/lib.h b/lib.h index 81097d9..caefa6a 100644 --- a/lib.h +++ b/lib.h @@ -23,6 +23,8 @@ struct array { struct symbol* dim1size; }; + + enum var_type{ INTEGER, // -> the var is an INTEGER FLOAT // -> the var is a FLOAT -- GitLab From f0bcb954e8a54f965539fbeb9d26c4261d485440 Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Fri, 10 Jan 2025 15:56:31 +0100 Subject: [PATCH 04/13] new line at 20 char works --- Codes_en_COREC/tests/test3_max.corec | 8 ++------ lib.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec index e58a1ef..83d92ef 100644 --- a/Codes_en_COREC/tests/test3_max.corec +++ b/Codes_en_COREC/tests/test3_max.corec @@ -22,7 +22,7 @@ prog Test3 { array1[12] = 8.56; array1[13] = 17; array1[14] = 4.44; - array1[15] = 10.11; + array1[15] = 10.0; array1[16] = 3.33; array1[17] = 22; array1[18] = 19.5; @@ -58,11 +58,7 @@ prog Test3 { array1[48] = 6.25; array1[49] = 9.9; - printstr("Expected result : \n5.32 12 7.89 3.14 42 18.27 1.11 6 21.95 14.01 9 2.71 8.56 17 4.44 1"); - - // printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); - // printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); - // printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 10.11 3.33 22.00 19.50 7.77 \n13.37 5.00 25.25 8.00 20.20 11.11 15.00 30.30 16.66 4.00 18.00 7.07 12.34 9.87 14.00 6.66 5.55 19.00 2.22 20.00 \n3.00 18.88 4.04 22.22 8.80 16.00 11.23 7.50 6.25 9.90"); printstr("\nObtained Result : \n"); print(array1) // contain all the values exchanges here if the code is correct diff --git a/lib.c b/lib.c index 2f201c6..8953ac8 100644 --- a/lib.c +++ b/lib.c @@ -744,6 +744,8 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // putting array address in $t1 load_symbol_macro("$t1","NULL",c->quads[i].sym1,t,fout); fprintf(fout,"%sli $t2, 0\n",tabulation); + // initilize t4 to be used as the counter of elements printed on one line (at 20 we print a newline) + fprintf(fout,"%sli $t4, 0\n",tabulation); // we avoid having 2 identical labels by putting the quad index at the end of it @@ -764,10 +766,26 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // increment to next address fprintf(fout,"%saddi $t2, $t2, 4\n",tabulation); + // increment the counter of elements printed on this line + fprintf(fout,"%saddi $t4, $t4, 1\n",tabulation); + fprintf(fout,"%sli $t5, 20\n",tabulation); + fprintf(fout,"%sbeq $t4, $t5, print_array_loop_aux%d\n",tabulation,i); + // jump to the start of the loop fprintf(fout,"%sj print_array_loop%d\n",tabulation,i); // /!\ end of the loop /!\ -- remove_indent(); + + // auxiliary function that enables us to have a new line every 20 floats printed + fprintf(fout,"%sprint_array_loop_aux%d:\n",tabulation,i); + add_indent(1,c,t,fout); + fprintf(fout,"%sLF\n",tabulation); + // we reset the counter of elements printed on one line + fprintf(fout,"%sli $t4, 0\n",tabulation); + // jump back to the print loop + fprintf(fout,"%sj print_array_loop%d\n",tabulation,i); + remove_indent(); + fprintf(fout,"%send_array_loop%d:\n",tabulation,i); fprintf(fout,"%sLF\n",tabulation); break; -- GitLab From 3befe10080eff70fd347e4356da81b867060be21 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Wed, 8 Jan 2025 00:14:25 +0100 Subject: [PATCH 05/13] add gencode to the grammar for read --- corec.y | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/corec.y b/corec.y index 2ab8bc8..aec86e0 100644 --- a/corec.y +++ b/corec.y @@ -381,6 +381,11 @@ I : | AFFECTATION // Affectation of variables | CALL // Call of functions or variables | read_f left_parenthesis ID1 right_parenthesis // Read + { + struct symbol* id_read = symtable_get(SYMTAB,$3,symb_scope_function); + + gencode(CODE,CALL_READ,id_read,NULL,NULL); + } | print left_parenthesis {isPrintCall = 1;} CALL right_parenthesis // Print variable (printing out var of functions ins't implement yet) { gencode(CODE,CALL_PRINT,$4.ptr,NULL,NULL); -- GitLab From 4203131cecd9ce66b8b5b16ad8fe72b3ada0817b Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Wed, 8 Jan 2025 00:15:09 +0100 Subject: [PATCH 06/13] add print_read for MIPS code generation, works for int only now --- lib.c | 39 +++++++++++++++++++++++++++++++++++++++ lib.h | 8 +++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib.c b/lib.c index 8953ac8..7bb0a14 100644 --- a/lib.c +++ b/lib.c @@ -384,6 +384,10 @@ static void quad_dump(struct quad * q, FILE* fout) fprintf(fout,"printstr "); symbol_dump(q->sym1,fout); break; + case CALL_READ: + fprintf(fout,"read "); + symbol_dump(q->sym1,fout); + break; case COPY: symbol_dump(q->sym1,fout); fprintf(fout," := "); @@ -532,6 +536,9 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ case CALL_PRINTSTR: print_printstr(t,c,fout,i); break; + case CALL_READ: + print_read(t,c,fout,i); + break; case COPY: print_copy(t,c,fout,i); break; @@ -829,6 +836,38 @@ void print_reg(char * reg,FILE * fout, enum var_type ty){ fprintf(fout,"%ssyscall\n",tabulation); } +// -------- READ RELATED -------- + +void print_read(struct symtable * t,struct code * c, FILE* fout,int i) +{ + switch(c->quads[i].sym1->kind){ + case ARRAY: + #ifdef LIBDEBUG + fprintf(fout,"%s# float read section\n",tabulation); + #endif + fprintf(fout,"%sli $v0, 6\n",tabulation); + fprintf(fout,"%ssyscall\n",tabulation); + fprintf(fout,"%ssw $v0, %s\n",tabulation,c->quads[i].sym1->u.name); + fprintf(fout,"%saddi $t0, $v0, 4\n",tabulation); + fprintf(fout,"%sla $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + fprintf(fout,"%slwc1 $f0, 0($t1)\n",tabulation); + fprintf(fout,"%sswc1 $f0, ($t0)\n",tabulation); + #ifdef LIBDEBUG + fprintf(fout,"%s# end of float read section\n",tabulation); + #endif + break; + case NAME_LOC: + fprintf(fout,"%sli $v0, 5\n",tabulation); + fprintf(fout,"%ssyscall\n",tabulation); + fprintf(fout,"%ssw $v0, %s\n",tabulation,c->quads[i].sym1->u.name); + break; + default: + fprintf(stderr,"Can not assign value into this kind\n"); + break; + } + +} + // -------- FUNCTIONS RELATED -------- diff --git a/lib.h b/lib.h index caefa6a..196cde2 100644 --- a/lib.h +++ b/lib.h @@ -109,7 +109,7 @@ void symtable_free(struct symtable * t); * COPY_ARRAY -> tmp=sym1 array=sym2 element=sym3 -> tmp= array[element] */ struct quad { - enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_MAIN, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR, CONDITION, OPREL_LT, OPREL_GT, OPREL_LE, OPREL_GE, OPREL_EQ} kind; + enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_MAIN, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR, CALL_READ, CONDITION, OPREL_LT, OPREL_GT, OPREL_LE, OPREL_GE, OPREL_EQ} kind; struct symbol* sym1; struct symbol* sym2; struct symbol* sym3; @@ -184,6 +184,12 @@ void print_printstr(struct symtable * t, struct code * c, FILE* fout,int i); */ void print_reg(char * reg,FILE * fout, enum var_type ty); +// -------- READ RELATED -------- + +/** @brief print_read makes MIPS code to read an integer or a float and save it to a local variable + */ +void print_read(struct symtable * t,struct code * c, FILE* fout,int i); + // -------- FUNCTIONS RELATED -------- // -------- ARRAY RELATED -------- -- GitLab From 3b97d53e3b625edf367e243e9065163ae563054a Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Wed, 8 Jan 2025 00:15:36 +0100 Subject: [PATCH 07/13] change test to test an int read and a float read --- Codes_en_COREC/tests/test9.corec | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Codes_en_COREC/tests/test9.corec b/Codes_en_COREC/tests/test9.corec index a183de2..562894d 100644 --- a/Codes_en_COREC/tests/test9.corec +++ b/Codes_en_COREC/tests/test9.corec @@ -1,11 +1,15 @@ -// Conditions (if else ternaire..) +// Saisie -prog test4 { +prog test9 { def Main { - Rec : { - printstr("Saisie clavie :"); - read(saisie); - print(saisie) - } + Loc: (f,1), saisie + Rec : { + printstr("Saisie clavier :"); + read(saisie); + print(saisie); + printstr("Saisie float clavier:"); + read(f); + print(f) + } } } -- GitLab From a4c679da5ae1c55d82aabc6ced0cfb80f003b279 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Thu, 9 Jan 2025 00:42:32 +0100 Subject: [PATCH 08/13] Read float works --- lib.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib.c b/lib.c index 7bb0a14..5b605f3 100644 --- a/lib.c +++ b/lib.c @@ -845,20 +845,22 @@ void print_read(struct symtable * t,struct code * c, FILE* fout,int i) #ifdef LIBDEBUG fprintf(fout,"%s# float read section\n",tabulation); #endif + // System call read float fprintf(fout,"%sli $v0, 6\n",tabulation); fprintf(fout,"%ssyscall\n",tabulation); - fprintf(fout,"%ssw $v0, %s\n",tabulation,c->quads[i].sym1->u.name); - fprintf(fout,"%saddi $t0, $v0, 4\n",tabulation); - fprintf(fout,"%sla $t1, %s\n",tabulation,c->quads[i].sym1->u.name); - fprintf(fout,"%slwc1 $f0, 0($t1)\n",tabulation); + // Assign given value to float variable + fprintf(fout,"%slw $t2, %s\n",tabulation,c->quads[i].sym1->u.name); + fprintf(fout,"%saddi $t0, $t2, 0\n",tabulation); fprintf(fout,"%sswc1 $f0, ($t0)\n",tabulation); #ifdef LIBDEBUG fprintf(fout,"%s# end of float read section\n",tabulation); #endif break; case NAME_LOC: + // System call read int fprintf(fout,"%sli $v0, 5\n",tabulation); fprintf(fout,"%ssyscall\n",tabulation); + // Assign given value to int variable fprintf(fout,"%ssw $v0, %s\n",tabulation,c->quads[i].sym1->u.name); break; default: -- GitLab From be2287029cb140b4ca089ce0a2e27374ee971ad4 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Thu, 9 Jan 2025 15:07:36 +0100 Subject: [PATCH 09/13] documentation --- README.md | 5 ++++- docs/tests.md | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 931fa20..defef78 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,12 @@ sudo apt install bison - float value as singleton array. - Cast integer in float if integer affectation to arrays or multiplication of a float and an integer. (Implicit) ### 6. branch test6 -- Affectation multi dimensional array. **(TO DO :TRANSLATE IN MIPS)** +- Affectation multi dimensional array. ### 7. branch test7 - Operations on multi dimensional array. **(TO DO :TRANSLATE IN MIPS)** +### 9.branch test9 +- Read an int into an int variable +- Read a float into a float variable See in the [test report](./docs/tests.md) file the report on COREC tests features and error detection. diff --git a/docs/tests.md b/docs/tests.md index 0c3b096..bf98698 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -9,7 +9,7 @@ 6. Declaration of local ND arrays and assignement of values. 7. Operation with ND arrays. 8. -9. +9. Read an int or a float ## Error detection 1. Fuction without a rec section. -- GitLab From 33f0fa148232b6745ea6af3970c4e3223b02c073 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Thu, 9 Jan 2025 15:08:33 +0100 Subject: [PATCH 10/13] change test7 to have acceptable variable names --- Codes_en_COREC/tests/test7.corec | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Codes_en_COREC/tests/test7.corec b/Codes_en_COREC/tests/test7.corec index c87a950..ace050d 100644 --- a/Codes_en_COREC/tests/test7.corec +++ b/Codes_en_COREC/tests/test7.corec @@ -2,27 +2,27 @@ prog Test7{ def Main{ - Loc : a=2, (2Darray,3,3), (3Darray,a,a,a)//, 3Darray[0,0,0] = 0 //BUG HERE + Loc : a=2, (array2D,3,3), (array3D,a,a,a)//, array3D[0,0,0] = 0 //BUG HERE Rec : { - 2Darray[0,0] = 0; - 2Darray[0,1] = 1; - 2Darray[0,2] = a; - 2Darray[1,0] = 3.0; - 2Darray[1,1] = 4.5; - 2Darray[1,2] = 5; + array2D[0,0] = 0; + array2D[0,1] = 1; + array2D[0,2] = a; + array2D[1,0] = 3.0; + array2D[1,1] = 4.5; + array2D[1,2] = 5; - print(2Darray); + print(array2D); - 3Darray[0,0,0] = 0; - 3Darray[0,0,1] = 1; - 3Darray[0,1,0] = a; - 3Darray[0,1,1] = 2Darray[1,2]; - 3Darray[1,0,0] = 2Darray[0,2]*2Darray[0,2]+2Darray[0,2]-2Darray[0,1]*2; - 3Darray[1,0,1] = 5; - 3Darray[1,1,0] = 2Darray[1,2]+2Darray[0,1]; - 3Darray[1,1,1] = 7; + array3D[0,0,0] = 0; + array3D[0,0,1] = 1; + array3D[0,1,0] = a; + array3D[0,1,1] = array2D[1,2]; + array3D[1,0,0] = array2D[0,2]*array2D[0,2]+array2D[0,2]-array2D[0,1]*2; + array3D[1,0,1] = 5; + array3D[1,1,0] = array2D[1,2]+array2D[0,1]; + array3D[1,1,1] = 7; - print(3Darray); + print(array3D); printstr("Fini") } } -- GitLab From 98a794299c41c85eb6e7b7209412cf1cdca85306 Mon Sep 17 00:00:00 2001 From: CLAVEL JULIEN <julien.clavel@etu.unistra.fr> Date: Fri, 10 Jan 2025 14:12:48 +0000 Subject: [PATCH 11/13] test 8 passed, conditions implemented successfully --- Codes_en_COREC/tests/test1.corec | 6 +- Codes_en_COREC/tests/test2.corec | 22 +-- Codes_en_COREC/tests/test3.corec | 2 +- Codes_en_COREC/tests/test4.corec | 6 +- Codes_en_COREC/tests/test5.corec | 6 +- Codes_en_COREC/tests/test8.corec | 97 +++++++++- Codes_en_COREC/tests/test9.corec | 4 +- Makefile | 3 + README.md | 4 +- corec.y | 123 +++++++++---- corecDev.l | 22 ++- docs/tests.md | 2 +- lib.c | 293 +++++++++++++++++++++++++++++-- lib.h | 74 +++++++- 14 files changed, 563 insertions(+), 101 deletions(-) diff --git a/Codes_en_COREC/tests/test1.corec b/Codes_en_COREC/tests/test1.corec index d8c7220..3e2bcc8 100644 --- a/Codes_en_COREC/tests/test1.corec +++ b/Codes_en_COREC/tests/test1.corec @@ -7,12 +7,12 @@ prog Test1 { a = 10; b = 2; - printstr("Affichage de variables, a: "); + printstr("Printing variable, a: "); print(a); - printstr("Affichage de variables, b: "); + printstr("Printing variable, b: "); print(b); - printstr("Affichage de variables, c: "); + printstr("Printing variable, c: "); print(c) } } diff --git a/Codes_en_COREC/tests/test2.corec b/Codes_en_COREC/tests/test2.corec index 1cbbcef..a3a442a 100644 --- a/Codes_en_COREC/tests/test2.corec +++ b/Codes_en_COREC/tests/test2.corec @@ -7,44 +7,46 @@ prog Test2{ a = 10; b = 2; + printstr("a = 10, b = 2\n"); + c = a+b; - printstr("Addition, attendu 12: "); + printstr("a+b, expect 12 : "); print(c); c = a-b; - printstr("Substraction, attendu 8: "); + printstr("a-b, expect 8: "); print(c); c = a*b; - printstr("Multiplication, attendu 20: "); + printstr("a*b, expect 20: "); print(c); c = a/b; - printstr("Division, attendu 5: "); + printstr("a/b, expect 5: "); print(c); a += b; - printstr("Addition aff, attendu 12: "); + printstr("a += b aff, expect 12: "); print(a); a *= b; - printstr("Addition mult, attendu 24: "); + printstr("a *= b (a=12*2), expect 24: "); print(a); a /= b; - printstr("Addition mult, attendu 12: "); + printstr("a /= b (a=24/2), expect 12: "); print(a); a -= 4; - printstr("Addition mult, attendu 8: "); + printstr("a -= 4 (a=12-4), expect 8: "); print(a); c = a*a+2+b*b; - printstr("Addition mult, attendu 70: "); + printstr("a*a + 2 + b*b (8*8 + 2 + 2*2), expect 70: "); print(c); c= a*(a+b)-c/2; - printstr("Addition mult, attendu 45: "); + printstr(" a*(a+b)-c/2 (8*(8+2)-70/2), expect 45: "); print(c) } } diff --git a/Codes_en_COREC/tests/test3.corec b/Codes_en_COREC/tests/test3.corec index 93c55f2..21ad140 100644 --- a/Codes_en_COREC/tests/test3.corec +++ b/Codes_en_COREC/tests/test3.corec @@ -16,7 +16,7 @@ prog Test3 { f[0] = 1.0; // Implicit array of dim 1 length 1 -> f[0] array1[2] = f; - printstr("Expected result : 2.0 1.15 1.0 5.0 2.0 \nObtained Result : "); + printstr("Expected result : 2.00 1.15 1.00 5.00 2.00 \nObtained Result : "); print(array1) // contain all the values exchanges here if the code is correct } } diff --git a/Codes_en_COREC/tests/test4.corec b/Codes_en_COREC/tests/test4.corec index 1985bf0..e98a493 100644 --- a/Codes_en_COREC/tests/test4.corec +++ b/Codes_en_COREC/tests/test4.corec @@ -13,21 +13,21 @@ prog Test4 { array[1] -= 2; array[2] /= 2; array[3] *= 2; - printstr("Expected : 4.0 0.0 1.0 4.0 0.0 0.0 \nObtained : "); + printstr("Expected : 4.00 0.00 1.00 4.00 0.00 0.00 \nObtained : "); print(array); array[1] = array[0] + float; array[2] = array[2] - float; array[3] = array[0] / float; array[4] = array[0] * float; - printstr("Expected : 4.0 7.0 -2.0 1.3333334 12.0 0.0 \nObtained : "); + printstr("Expected : 4.00 7.00 -2.00 1.33 12.00 0.00 \nObtained : "); print(array); array[2] = array[0] + array[1]; array[3] = array[0] - array[1]; array[4] = array[0] / array[1]; array[5] = array[0] * array[1]; - printstr("Expected : 4.0 7.0 11.0 -3.0 0.5714286 28.0 \nObtained : "); + printstr("Expected : 4.00 7.00 11.00 -3.00 0.57 28.00 \nObtained : "); print(array) } } diff --git a/Codes_en_COREC/tests/test5.corec b/Codes_en_COREC/tests/test5.corec index f293225..881dcd4 100644 --- a/Codes_en_COREC/tests/test5.corec +++ b/Codes_en_COREC/tests/test5.corec @@ -8,15 +8,15 @@ prog Test6 { f2 = 1.3; f1 += f2*2; - printstr("Expected : 1.2 + 1.3 * 2 = 3.8\nObtained : 1.2 + 1.3 * 2 = "); + printstr("Expected : 1.2 + 1.3 * 2 = 3.80\nObtained : 1.2 + 1.3 * 2 = "); print(f1); f2 = f2*2; - printstr("Expected : 1.3 * 2 = 2.6\nObtained : "); + printstr("Expected : 1.3 * 2 = 2.60\nObtained : "); print(f2); f1 /= f2+f2*4; - printstr("Expected : 3.8 / (2.6 + 2.6 * 4) = 0.29230767\nObtained : "); + printstr("Expected : 3.8 / (2.6 + 2.6 * 4) = 0.29\nObtained : "); print(f1) } } diff --git a/Codes_en_COREC/tests/test8.corec b/Codes_en_COREC/tests/test8.corec index 5fc568f..5d5baad 100644 --- a/Codes_en_COREC/tests/test8.corec +++ b/Codes_en_COREC/tests/test8.corec @@ -2,24 +2,105 @@ prog test8{ def Main { + Loc : a, b, c, (array,3) Rec : { a = 10; b = 2; + c = 17; + array = 0.12; printstr("a = 10 | b = 2"); - printstr("a<b ?"); + printstr("a>b ? "); a>b? { - printstr("oui") + printstr("YES\n") } - :printstr("non"); + :printstr("NO\n"); + + printstr("b equal 2 ? "); + b==2?printstr("YES\n"):printstr("NO\n"); + + printstr("advanced test, modify c if you want to test other branchings,\n"); + printstr("c=17 shouldn\'t work if one of the conditions isn't well implemented\n"); + c>10?{ + printstr("c > 10\n"); + c>=15?{ + printstr("c >= 15\n"); + c<19? { + printstr("c < 19\n"); + c<=16? { + printstr("c <= 16\n"); + c==15?printstr("c == 15"):printstr("c == 16") + } : { + printstr("c > 16\n"); + c==17?printstr("c == 17"):printstr("c == 18") + } + } : { + c==20?printstr("c == 20 "): { + printstr("c >= 19\n"); + c==19?printstr("c == 19 "):printstr("c must be <=20 (and >=0)") + } + } + } : { + printstr("c < 15\n"); + c>12? { + printstr("c > 12\n"); + c==13?printstr("c == 13"):printstr("c == 14") + } : { + printstr("c <= 12\n"); + c==12?printstr("c == 12"):printstr("c == 11") + } + } + } : { + printstr("c <= 10\n"); + c<1? { + printstr("c < 1\n"); + c==0?printstr("c == 0"):printstr("c must be >=0 (and <=20)") + } : { + printstr("c >= 1\n"); + c<=1?printstr("c == 1"): { + printstr("c > 1\n"); + c>=10?printstr("c == 10"): { + printstr("c < 10\n"); + c>2? { + printstr("c > 2\n"); + c<4?printstr("c == 3"): { + printstr("c >= 4\n"); + c==5?printstr("c == 5"):{ + printstr("c != 5\n"); + c>6?{ + printstr("c > 6\n"); + c==7?printstr("c == 7"): { + printstr("c != 7\n"); + c==8?printstr("c == 8"): { + printstr("c != 8\n"); + c<8?printstr("c == 6"):printstr("c == 9") + } + } + }:{ + printstr("c <= 6\n"); + c==6?printstr("c == 6"):printstr("c == 4") + } + } + } + }:printstr("c == 2") + } + } + } + }; + + printstr("\n\ntest conditions on float :\nIs 0.12 [array] greater than 0.1 : "); + array>0.1?printstr("YES\n"):printstr("NO\n"); + printstr("Is 0.12 [array] greater or equal to 0.12 : "); + array>=0.12?printstr("YES\n"):printstr("NO\n"); + printstr("Is 0.24 [array*2] equal to 0.24 : "); + array*2==0.24?printstr("YES\n"):printstr("NO\n"); + printstr("Is 0.11 [array-0.1] lesser than 0.12 [array] : "); + array-0.1<array?printstr("YES\n"):printstr("NO\n"); + printstr("Is 0.06 [array/2] lesser or equal to 0.06 [array-0.06] : "); + array/2<=array-0.06?printstr("YES\n"):printstr("NO\n") - printstr("b egal a 2 ?"); - b==2? - { - printstr("oui") - }: } } } diff --git a/Codes_en_COREC/tests/test9.corec b/Codes_en_COREC/tests/test9.corec index 562894d..72bbfd1 100644 --- a/Codes_en_COREC/tests/test9.corec +++ b/Codes_en_COREC/tests/test9.corec @@ -4,10 +4,10 @@ prog test9 { def Main { Loc: (f,1), saisie Rec : { - printstr("Saisie clavier :"); + printstr("Integer input asked:"); read(saisie); print(saisie); - printstr("Saisie float clavier:"); + printstr("Float input asked:"); read(f); print(f) } diff --git a/Makefile b/Makefile index 0d4f308..d0ee398 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,9 @@ test_corec: all ./$(Compilateur_COREC) -tos < $(DIR_IN)/SystTriang.corec > $(DIR_OUT)/output7.asm test: all # Change to allDev if u want to display the lex tokens + ./$(Compilateur_COREC) -tos -o $(DIR_OUT)/output$(num).asm < $(DIR_IN)/tests/test$(num).corec + +testDev: allDev # Change to allDev if u want to display the lex tokens ./$(Compilateur_COREC) -tos -o $(DIR_OUT)/output$(num).asm < $(DIR_IN)/tests/test$(num).corec testTo: all diff --git a/README.md b/README.md index defef78..32880ef 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,9 @@ sudo apt install bison ### 6. branch test6 - Affectation multi dimensional array. ### 7. branch test7 -- Operations on multi dimensional array. **(TO DO :TRANSLATE IN MIPS)** +- Operations on multi dimensional array. +### 8. branch test8 +- conditional code execution ### 9.branch test9 - Read an int into an int variable - Read a float into a float variable diff --git a/corec.y b/corec.y index aec86e0..ff00be2 100644 --- a/corec.y +++ b/corec.y @@ -11,6 +11,12 @@ struct symbol* symb_scope_function; // Boolean if we are in a print call uint8_t isPrintCall = 0; +// stack of the IF ELSE calls +struct labelStack * condition_stack; + +int indent; +char tabulation[INDENT_BUF]; + // Store the dimension or indexes when we call or declare an array struct symbol** tabArrayDim = NULL; // Store the length of the array @@ -35,7 +41,7 @@ void exit_safely(){ long int intval; float floatval; name_t strval; - name_64_t chain; + wardLabels label; struct { struct symbol* ptr; @@ -61,7 +67,8 @@ void exit_safely(){ struct { uint8_t opnum; } opaff; -} + +}; %token prog %token def @@ -124,8 +131,9 @@ void exit_safely(){ %type <ptrNameDim> DLIST ARRAY ELIST %type <ptrValE> E T F CALL %type <ptrArray> ARRAYREF -%type <opaff> OPAFF -%type <opaff> OPREL +%type <opaff> OPAFF OPREL + + %% @@ -364,13 +372,13 @@ REC : } ; BLOCKINST : // Bloc of instructions - left_curly_bracket LISTEI right_curly_bracket + left_curly_bracket LISTEI right_curly_bracket | I ; LISTEI : LISTEI semicolon I // Last instruction hasn't a semilocon after | I - | %empty // Error detection -> Rec section empty + | %empty // Error detection -> Rec TODO section empty Rec isn't the only BLOCK using LISTEI { fprintf(stderr, "Error at line %u in function '%s': Unexpected end of rec section (shouldn't be empty).\n",lineNumber,symb_scope_function->u.name); exit_safely(); @@ -918,47 +926,21 @@ F : ; COND : - E OPREL E ternary_then B ternary_else B + CONDCHECK THEN B ELSE B { - struct symbol * boolean = newtemp(SYMTAB,INTEGER); - - switch($2.opnum){ - case 1: - gencode(CODE,OPREL_LT,boolean,$1.ptr,$3.ptr); - break; - - case 2: - gencode(CODE,OPREL_GT,boolean,$1.ptr,$3.ptr); - break; - - case 3: - gencode(CODE,OPREL_LE,boolean,$1.ptr,$3.ptr); - break; - - case 4: - gencode(CODE,OPREL_GE,boolean,$1.ptr,$3.ptr); - break; - - case 5: - gencode(CODE,OPREL_EQ,boolean,$1.ptr,$3.ptr); - break; - - default: - fprintf(stderr, "BUG\n"); - break; - } - // name_t end_condition = snprintf(end_condition, sizeof(end_condition), "end_condition_%d", t->condition); - // struct symbol * label = symtable_put_label(SYMTAB,end_condition); - // name_t else_condition = snprintf(else_condition, sizeof(else_condition), "else_condition_%d", t->condition); - // struct symbol * label = symtable_put_label(SYMTAB,else_condition); + // ENDIF label + gencode(CODE,RM_INDENT,NULL,NULL,NULL); + gencode(CODE,LABEL_W,condition_stack->elt.lbl2,NULL,NULL); - // TODO ahndle and gencode for if and else branches + pop_stack(&condition_stack); // we pop the current condition's labels since the condition is now over } ; B : BLOCKINST | %empty ; + + OPREL : inf { @@ -985,7 +967,70 @@ OPREL : ID : ID1 | ID2 + ; + +CONDCHECK: + E OPREL E { + + wardLabels tmp; + tmp.cond_number = SYMTAB->condition; + tmp.lbl1 = symtable_put_label(SYMTAB, "else", tmp.cond_number, symb_scope_function); + tmp.lbl2 = symtable_put_label(SYMTAB, "endif", tmp.cond_number, symb_scope_function); + switch($2.opnum){ + case 1: + gencode(CODE,OPREL_LT,tmp.lbl1,$1.ptr,$3.ptr); + break; + + case 2: + gencode(CODE,OPREL_GT,tmp.lbl1,$1.ptr,$3.ptr); + break; + + case 3: + gencode(CODE,OPREL_LE,tmp.lbl1,$1.ptr,$3.ptr); + break; + + case 4: + gencode(CODE,OPREL_GE,tmp.lbl1,$1.ptr,$3.ptr); + break; + + case 5: + gencode(CODE,OPREL_EQ,tmp.lbl1,$1.ptr,$3.ptr); + break; + + default: + fprintf(stderr, "BUG\n"); + break; + } + push_stack(&condition_stack,tmp); + (SYMTAB->condition) ++; + // go to else si la condition est fausse + + } + ; + +THEN : + ternary_then + { + + struct symbol * if_label = symtable_put_label(SYMTAB, "if", condition_stack->elt.cond_number, symb_scope_function); + // lavel de la sectrion IF + gencode(CODE, LABEL_W, if_label, NULL, NULL); + gencode(CODE,ADD_INDENT,NULL,NULL,NULL); + } + ; + +ELSE : + ternary_else + { + // goto endif juste après le code du IF + gencode(CODE, GOTO_LABEL, condition_stack->elt.lbl2, NULL, NULL); + gencode(CODE,RM_INDENT,NULL,NULL,NULL); + // label de la section ELSE + gencode(CODE, LABEL_W, condition_stack->elt.lbl1, NULL, NULL); + gencode(CODE,ADD_INDENT,NULL,NULL,NULL); + } + ; %% void yyerror(const char * s) diff --git a/corecDev.l b/corecDev.l index 3e0143e..25e59e2 100644 --- a/corecDev.l +++ b/corecDev.l @@ -63,14 +63,20 @@ in {printf("in_dom"); return in_dom;} "/" {printf("divide"); return divide;} "%" {printf("mod"); return mod;} -([0-9]*[[:alpha:]]+|[[:alpha:]])[[:alnum:]]* { - printf("ID"); - if ( yyleng > 63 ) - fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 31), truncated.\n",lineNumber,yytext); - strncpy(yylval.strval,yytext,63); - yylval.strval[64] = '\0'; - return ID; - } +[[:alpha:]][[:alnum:]]* { + if ( yyleng > 63 ) + fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 63), truncated.\n",lineNumber,yytext); + strncpy(yylval.strval,yytext,63); + yylval.strval[64] = '\0'; + return ID1; + } +[0-9]*[[:alpha:]]+[[:alnum:]]* { + if ( yyleng > 63 ) + fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 63), truncated.\n",lineNumber,yytext); + strncpy(yylval.strval,yytext,63); + yylval.strval[64] = '\0'; + return ID2; + } ["]([^"]|[\\]["])*["] { printf("chaine"); if ( yyleng > 1023 ) diff --git a/docs/tests.md b/docs/tests.md index bf98698..2bc6b33 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -8,7 +8,7 @@ 5. Operation with float. 6. Declaration of local ND arrays and assignement of values. 7. Operation with ND arrays. -8. +8. if else and condition checks 9. Read an int or a float ## Error detection diff --git a/lib.c b/lib.c index 5b605f3..82948a7 100644 --- a/lib.c +++ b/lib.c @@ -1,9 +1,7 @@ #include "lib.h" -// TODO make it in a cleaner way -int indent = 0; -char tabulation[INDENT_BUF] = ""; + #define INDENT_MAKER struct symtable * symtable_new() @@ -20,7 +18,9 @@ struct symtable * symtable_new() exit(1); } t->temporary = 0; + t->condition = 0; t->size = 0; + init_indent(); return t; } @@ -97,7 +97,7 @@ struct symbol* symtable_get(struct symtable * t, const char * id, struct symbol* return &(t->symbols[i]); } return NULL; - } + } return &(t->symbols[i]); } @@ -193,13 +193,14 @@ struct symbol *newtemp(struct symtable * t, enum var_type ty) return s; } -struct symbol* symtable_put_label(struct symtable * t, const char * label_name, struct symbol* func_id) +struct symbol* symtable_put_label(struct symtable * t, const char* var_name, int cond_id, struct symbol* func_id) { if(t->size==t->capacity) symtable_grow(t); struct symbol *s = &(t->symbols[t->size]); s->kind = LABEL; - strcpy(s->u.name,label_name); + sprintf(s->u.name,"%s_%d",var_name,cond_id); + strcpy(s->scope_function,func_id->u.name); ++ (t->size); return s; } @@ -316,7 +317,7 @@ static void symbol_dump(struct symbol* s, FILE* fout) break; case LABEL: fprintf(fout,"%s",s->u.name); - break; // ? + break; default: fprintf(stderr,"BUG SYMBOL DUMP NOT RECOGNIZED\n"); break; @@ -417,6 +418,59 @@ static void quad_dump(struct quad * q, FILE* fout) symbol_dump(q->sym3,fout); fprintf(fout,"]"); break; + case OPREL_EQ: + symbol_dump(q->sym1,fout); + fprintf(fout," := "); + symbol_dump(q->sym2,fout); + fprintf(fout," == "); + symbol_dump(q->sym3,fout); + break; + case OPREL_GE: + symbol_dump(q->sym1,fout); + fprintf(fout," := "); + symbol_dump(q->sym2,fout); + fprintf(fout," >= "); + symbol_dump(q->sym3,fout); + break; + case OPREL_GT: + symbol_dump(q->sym1,fout); + fprintf(fout," := "); + symbol_dump(q->sym2,fout); + fprintf(fout," > "); + symbol_dump(q->sym3,fout); + break; + case OPREL_LE: + symbol_dump(q->sym1,fout); + fprintf(fout," := "); + symbol_dump(q->sym2,fout); + fprintf(fout," <= "); + symbol_dump(q->sym3,fout); + break; + case OPREL_LT: + symbol_dump(q->sym1,fout); + fprintf(fout," := "); + symbol_dump(q->sym2,fout); + fprintf(fout," < "); + symbol_dump(q->sym3,fout); + break; + case LABEL_W: + if(q->sym1!=NULL) + { + symbol_dump(q->sym1,fout); + fprintf(fout," :"); + } + break; + case GOTO_LABEL: + if(q->sym1!=NULL) + { + fprintf(fout,"goto "); + symbol_dump(q->sym1,fout); + } + break; + case ADD_INDENT: + break; + case RM_INDENT: + break; default: fprintf(stderr,"BUG quad_dump(...) : q->kind inconnu\n"); break; @@ -482,8 +536,8 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ // else if (t->symbols[i].kind == LABEL) { // fprintf(fout,"\t%s: .word 0\n",t->symbols[i].u.name); // } - else{ //NAME - fprintf(fout,"\ttemp%d: .word 0\n",i); + else{ // TODO check whether if in else shouldn't be printed or not ? + // fprintf(fout,"\ttemp%d: .word 0\n",i); } } @@ -528,7 +582,7 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ break; case DEF_MAIN: print_main_def(fout); - add_indent(1,c,t,fout); + add_indent(c,t,fout); break; case CALL_PRINT: print_print(t,c,fout,i); @@ -542,6 +596,33 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ case COPY: print_copy(t,c,fout,i); break; + case OPREL_EQ: + print_EQ_check(t,c,fout,i); + break; + case OPREL_GT: + print_GT_check(t,c,fout,i); + break; + case OPREL_GE: + print_GE_check(t,c,fout,i); + break; + case OPREL_LT: + print_LT_check(t,c,fout,i); + break; + case OPREL_LE: + print_LE_check(t,c,fout,i); + break; + case LABEL_W: + print_label_w(t,c,fout,i); + break; + case GOTO_LABEL: + print_goto(t,c,fout,i); + break; + case ADD_INDENT: + add_indent(c,t,fout); + break; + case RM_INDENT: + remove_indent(); + break; default: fprintf(stderr,"#BUG %d\n",c->quads[i].kind); break; @@ -712,7 +793,7 @@ void print_uop_minus(struct code * c, struct symtable* t, FILE* fout,int i) #endif if(ISFLOAT(c->quads[i].sym2)) { // float uop minus - if(!ISFLOAT(c->quads[i].sym2)) + if(!ISFLOAT(c->quads[i].sym2)) { // if sym2 is an integer convert it to a float and store it in $f0 convert_int_to_float("$f0","$t0",c->quads[i].sym2,t,fout); } else { // store it in $f0 @@ -737,6 +818,145 @@ void print_uop_minus(struct code * c, struct symtable* t, FILE* fout,int i) #endif } +// -------- COMPARISONS / OPREL RELATED -------- + +void print_EQ_check(struct symtable* t, struct code * c, FILE* fout,int i) +{ + #ifdef LIBDEBUG + fprintf(fout,"%s# equality check\n",tabulation); + #endif + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + if(ISFLOAT(c->quads[i].sym2)|| ISFLOAT(c->quads[i].sym3)) + { + load_float_bop_macro("$f0","$f1","$t0",c->quads[i].sym2,c->quads[i].sym3,t,fout); + fprintf(fout,"%sc.eq.s $f0, $f1\n",tabulation); + fprintf(fout,"%sbc1f %s\n",tabulation,c->quads[i].sym1->u.name); + } else { + // Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + // we wanna branch to the else when condition is false s owe do the opposite test == => != + fprintf(fout,"%sbne $t0, $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + } +} + +void print_GT_check(struct symtable* t, struct code * c, FILE* fout,int i) +{ + #ifdef LIBDEBUG + fprintf(fout,"%s# Greater than check\n",tabulation); + #endif + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + if(ISFLOAT(c->quads[i].sym2)|| ISFLOAT(c->quads[i].sym3)) + { + load_float_bop_macro("$f0","$f1","$t0",c->quads[i].sym2,c->quads[i].sym3,t,fout); + // we go to else if lesser or equal is true since it's the oposite of greater than + fprintf(fout,"%sc.le.s $f0, $f1\n",tabulation); + fprintf(fout,"%sbc1t %s\n",tabulation,c->quads[i].sym1->u.name); + } else { + // Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + // we go to else if lesser or equal is true since it's the oposite of greater than + fprintf(fout,"%sble $t0, $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + } +} +void print_GE_check(struct symtable* t, struct code * c, FILE* fout,int i) +{ + #ifdef LIBDEBUG + fprintf(fout,"%s# Greater or equal check\n",tabulation); + #endif + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + if(ISFLOAT(c->quads[i].sym2)|| ISFLOAT(c->quads[i].sym3)) + { + load_float_bop_macro("$f0","$f1","$t0",c->quads[i].sym2,c->quads[i].sym3,t,fout); + // we go to else if lesser than is true since it's the oposite of greater or equal + fprintf(fout,"%sc.lt.s $f0, $f1\n",tabulation); + fprintf(fout,"%sbc1t %s\n",tabulation,c->quads[i].sym1->u.name); + } else { + // Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + // we go to else if lesser than is true since it's the oposite of greater or equal + fprintf(fout,"%sblt $t0, $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + } +} +void print_LT_check(struct symtable* t, struct code * c, FILE* fout,int i) +{ + #ifdef LIBDEBUG + fprintf(fout,"%s# Lesser than check\n",tabulation); + #endif + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + if(ISFLOAT(c->quads[i].sym2)|| ISFLOAT(c->quads[i].sym3)) + { + load_float_bop_macro("$f0","$f1","$t0",c->quads[i].sym2,c->quads[i].sym3,t,fout); + // we go to else if lesser than is false + fprintf(fout,"%sc.lt.s $f0, $f1\n",tabulation); + fprintf(fout,"%sbc1f %s\n",tabulation,c->quads[i].sym1->u.name); + } else { + // Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + // we go to else if greater or equal is true since it's the oposite of lesser than + fprintf(fout,"%sbge $t0, $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + } +} +void print_LE_check(struct symtable* t, struct code * c, FILE* fout,int i) +{ + #ifdef LIBDEBUG + fprintf(fout,"%s# Lesser or equal check\n",tabulation); + #endif + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + if(ISFLOAT(c->quads[i].sym2)|| ISFLOAT(c->quads[i].sym3)) + { + load_float_bop_macro("$f0","$f1","$t0",c->quads[i].sym2,c->quads[i].sym3,t,fout); + // we go to else if lesser or equal is false + fprintf(fout,"%sc.le.s $f0, $f1\n",tabulation); + fprintf(fout,"%sbc1f %s\n",tabulation,c->quads[i].sym1->u.name); + } else { + // Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + // we go to else if greater than is true since it's the oposite of lesser or equal + fprintf(fout,"%sbgt $t0, $t1, %s\n",tabulation,c->quads[i].sym1->u.name); + } +} + +// -------- BRANCHING RELATED -------- + +void print_label_w(struct symtable* t, struct code * c, FILE* fout,int i) +{ + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + fprintf(fout,"%s%s:\n",tabulation,c->quads[i].sym1->u.name); +} + +void print_goto(struct symtable* t, struct code * c, FILE* fout,int i) +{ + if(c->quads[i].sym1->kind!=LABEL) + { + fprintf(stdout,"error gave a non label symbol, exiting\n"); + free_exit(1,c,t,fout); + } + fprintf(fout,"%sj %s\n",tabulation,c->quads[i].sym1->u.name); +} // -------- PRINT RELATED -------- void print_print(struct symtable * t,struct code * c, FILE* fout,int i) @@ -758,7 +978,7 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // we avoid having 2 identical labels by putting the quad index at the end of it // /!\ start of the loop /!\ -- fprintf(fout,"%sprint_array_loop%d:\n",tabulation,i); - add_indent(1,c,t,fout); + add_indent(c,t,fout); // bge by security to avoid miss matching (which shouldn't be happening) fprintf(fout,"%sbge $t2, $t0, end_array_loop%d\n",tabulation,i); @@ -882,7 +1102,7 @@ void print_copy_array(struct symtable * t, struct code * c, FILE* fout,int i) #endif if(c->quads[i].sym2->kind != ARRAY){ fprintf(stdout,"Tried to do an Var = tab[index] operation but right part isn't an array should address this issue in the corec.y file\n"); - exit(1); + free_exit(1,c,t,fout); } // adress of the array element we want to copy load_tab_addr_macro("$t0", "$t1", "$t2", c->quads[i].sym2,c->quads[i].sym3,t,fout); @@ -905,7 +1125,7 @@ void print_allocate_array(struct symtable * t, struct code * c, FILE* fout,int i #endif if(c->quads[i].sym1->kind != ARRAY){ fprintf(stdout,"Tried to don an tab[index] = Var operation but left part isn't an array should address this issue in the corec.y file\n"); - exit(1); + free_exit(1,c,t,fout); } // number for the sbrk primitive call fprintf(fout,"%sli $v0, 9\n",tabulation); @@ -928,7 +1148,7 @@ void print_aff_array(struct symtable * t, struct code * c, FILE* fout,int i) #endif if(c->quads[i].sym1->kind != ARRAY){ fprintf(stdout,"Tried to do an tab[index] = Var operation but left part isn't an array should address this issue in the corec.y file\n"); - exit(1); + free_exit(1,c,t,fout); } // V alternative, effectue directement le*4 en calcul vu qu'on devrait passer par un load immediate dans tout les cas // fprintf(fout,"%sla $t0, %s\n",tabulation,c->quads[i].sym1->u.name); @@ -1097,7 +1317,6 @@ void print_SPACE_macro(FILE* fout){ fprintf(fout,".end_macro\n"); } - // ETC FEEL FREE TO ADD MORE SECTIONS // -------- DEBUG RELATED -------- @@ -1153,16 +1372,48 @@ void debug_register_strings_declaration(FILE * fout){ } #endif +// -------- ERROR CHECK -------- + +void push_stack(labelStack ** stack, wardLabels new_elt) +{ + labelStack * new; + CHK( new = malloc(sizeof(labelStack)) , NULL ); + new->elt.cond_number = new_elt.cond_number; + new->elt.lbl1 = new_elt.lbl1; + new->elt.lbl2 = new_elt.lbl2; + new->previous = (*stack); + (*stack) = new; +} + +void pop_stack(labelStack ** stack) +{ + if((*stack) == NULL) + { + fprintf(stdout,"Trying to pop an empty stack \n"); + return; + } + labelStack * tmp = (*stack); + (*stack) = tmp->previous; + free(tmp); +} + +void raler(int err) +{ + if (err == 1) + perror (""); + exit(EXIT_FAILURE); + +} /************************* NAME TO DEFINE FUNCTIONS *************************/ -void add_indent(int code, struct code* c, struct symtable* t, FILE* fname){ +void add_indent(struct code* c, struct symtable* t, FILE* fname){ if(indent >= 1022) { printf("more than 1023 indentations ! write better code.\n"); - free_exit(code,c,t,fname); + free_exit(1,c,t,fname); } tabulation[indent] = '\t'; indent++; @@ -1178,4 +1429,10 @@ int remove_indent(){ indent--; tabulation[indent] = '\0'; return 0; +} + +void init_indent() +{ + indent=0; + tabulation[indent]='\0'; } \ No newline at end of file diff --git a/lib.h b/lib.h index 196cde2..22a2a02 100644 --- a/lib.h +++ b/lib.h @@ -11,6 +11,9 @@ extern struct symbol* symb_scope_function; #define LIBDEBUG #define ISFLOAT(X) ( X->kind == CONSTANT_FLOAT || X->kind == ARRAY || X->kind == NAME_FLOAT ) + + + /* TABLE DES SYMBOLES */ typedef char name_t[64]; @@ -41,6 +44,7 @@ enum kind{ LABEL // -> use the name attribut in the union u }; + // Struct symbol -> store a symbol struct symbol { enum kind kind; // attribut kind : the type of symbol; @@ -56,11 +60,13 @@ struct symbol { }; + + // Store the symbol table struct symtable { unsigned int capacity; unsigned int temporary; - unsigned int condition; // TODO find if necessary and how to name the different labels + unsigned int condition; unsigned int size; struct symbol* symbols; }; @@ -85,7 +91,7 @@ struct symbol* symtable_put_array(struct symtable * t, const char* var_name, uns // Add to the symtable a temporary function which is used to transform multiples addresses instruction to a group of 3 addresses instructions (uses NAME kind) struct symbol* newtemp(struct symtable * t,enum var_type ty); // Add to the symtable a label to branch different blocks of code -struct symbol* symtable_put_label(struct symtable * t, const char * label_name, struct symbol* func_id); +struct symbol* symtable_put_label(struct symtable * t, const char* var_name, int cond_id, struct symbol* func_id); // Get a pointer of a symbol with attribut name in the symtable (if 's' isn't in the table, return NULL) // (func_id is used for the scope of function on the local variable) @@ -109,7 +115,7 @@ void symtable_free(struct symtable * t); * COPY_ARRAY -> tmp=sym1 array=sym2 element=sym3 -> tmp= array[element] */ struct quad { - enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_MAIN, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR, CALL_READ, CONDITION, OPREL_LT, OPREL_GT, OPREL_LE, OPREL_GE, OPREL_EQ} kind; + enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_MAIN, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR, CALL_READ, CONDITION, OPREL_LT, OPREL_GT, OPREL_LE, OPREL_GE, OPREL_EQ, LABEL_W, GOTO_LABEL, ADD_INDENT, RM_INDENT} kind; struct symbol* sym1; struct symbol* sym2; struct symbol* sym3; @@ -170,6 +176,30 @@ void print_bop_mod(struct code * c, struct symtable* t, FILE* fout,int i); // add the UOP minus arithmetic operation TODO explain in more details void print_uop_minus(struct code * c, struct symtable* t, FILE* fout,int i); +// -------- COMPARISONS / OPREL RELATED -------- +/** @brief add the code to branch to symbol1 label if symbol2 and symbol3 aren't equal + */ +void print_EQ_check(struct symtable* t, struct code * c, FILE* fout,int i); +/** @brief add the code to branch to symbol1 label if symbol2 isn't > symbol3 + */ +void print_GT_check(struct symtable* t, struct code * c, FILE* fout,int i); +/** @brief add the code to branch to symbol1 label if symbol2 isn't >= symbol3 + */ +void print_GE_check(struct symtable* t, struct code * c, FILE* fout,int i); +/** @brief add the code to branch to symbol1 label if symbol2 isn't < symbol3 + */ +void print_LT_check(struct symtable* t, struct code * c, FILE* fout,int i); +/** @brief add the code to branch to symbol1 label if symbol2 isn't <= symbol3 + */ +void print_LE_check(struct symtable* t, struct code * c, FILE* fout,int i); + +// -------- BRANCHING RELATED -------- +/** @brief add the code to put the sym1 label into the mips code + */ +void print_label_w(struct symtable* t, struct code * c, FILE* fout,int i); +/** @brief add the code to go to sym1 label + */ +void print_goto(struct symtable* t, struct code * c, FILE* fout,int i); // -------- PRINT RELATED -------- // add the code to print the asked variable @@ -268,12 +298,48 @@ void debug_print_start(FILE * fout); void debug_print_end(FILE * fout); void debug_register_strings_declaration(FILE * fout); #endif + +// -------- STACK RELATED -------- +typedef struct { + int cond_number; + struct symbol * lbl1; + struct symbol * lbl2; +} wardLabels; + +typedef struct labelStack{ + wardLabels elt; + struct labelStack * previous; +} labelStack; + +extern labelStack * condition_stack; + +/** @brief add an element on top of the stack + */ +void push_stack(labelStack ** stack, wardLabels new_elt); + +/** @brief pop the top element of the stack + */ +void pop_stack(labelStack ** stack); + +// -------- ERROR CHECK -------- + +#define CHK(op,x) do { if ((op) == (x)) raler (1); } while (0) + +void raler(int err); + + // ETC FEEL FREE TO ADD MORE SECTIONS /************************* NAME TO DEFINE FUNCTIONS *************************/ +// TODO make it in a cleaner way +extern int indent; +extern char tabulation[INDENT_BUF]; + // add a tabulation to the indentation string -void add_indent(int code, struct code* c, struct symtable* t, FILE* fname); +void add_indent(struct code* c, struct symtable* t, FILE* fname); // remove a tabulation from the indentation string int remove_indent(); +// init the indentation tab +void init_indent(); -- GitLab From db5751fde2a73d0591e5ea43cae2a4b9a693af53 Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Thu, 9 Jan 2025 18:26:09 +0100 Subject: [PATCH 12/13] added test3_max.corec --- Codes_en_COREC/tests/test3_max.corec | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec index 83d92ef..9061c95 100644 --- a/Codes_en_COREC/tests/test3_max.corec +++ b/Codes_en_COREC/tests/test3_max.corec @@ -22,7 +22,7 @@ prog Test3 { array1[12] = 8.56; array1[13] = 17; array1[14] = 4.44; - array1[15] = 10.0; + array1[15] = 10.11; array1[16] = 3.33; array1[17] = 22; array1[18] = 19.5; @@ -58,7 +58,9 @@ prog Test3 { array1[48] = 6.25; array1[49] = 9.9; - printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 10.11 3.33 22.00 19.50 7.77 \n13.37 5.00 25.25 8.00 20.20 11.11 15.00 30.30 16.66 4.00 18.00 7.07 12.34 9.87 14.00 6.66 5.55 19.00 2.22 20.00 \n3.00 18.88 4.04 22.22 8.80 16.00 11.23 7.50 6.25 9.90"); + printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); + printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); printstr("\nObtained Result : \n"); print(array1) // contain all the values exchanges here if the code is correct -- GitLab From 17ba549384637ac1714cbc66d1a3c1dc693d4264 Mon Sep 17 00:00:00 2001 From: Constantin Anderssen <constantin.anderssen@etu.unistra.fr> Date: Fri, 10 Jan 2025 16:16:28 +0100 Subject: [PATCH 13/13] corrected rebase errors --- Codes_en_COREC/tests/test3_max.corec | 4 +--- lib.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Codes_en_COREC/tests/test3_max.corec b/Codes_en_COREC/tests/test3_max.corec index 9061c95..3663189 100644 --- a/Codes_en_COREC/tests/test3_max.corec +++ b/Codes_en_COREC/tests/test3_max.corec @@ -58,9 +58,7 @@ prog Test3 { array1[48] = 6.25; array1[49] = 9.9; - printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 ");//10.11 3.33 22 19.5 7.77 \n 13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 12.34 9.87 14 6.66 5.55 19 2.22 20 \n 3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); - printstr("10.11 3.33 22 19.5 7.77 \n13.37 5 25.25 8 20.2 11.11 15 30.3 16.66 4 18 7.07 "); - printstr("12.34 9.87 14 6.66 5.55 19 2.22 20 \n3 18.88 4.04 22.22 8.8 16 11.23 7.5 6.25 9.9"); + printstr("Expected result : \n5.32 12.00 7.89 3.14 42.00 18.27 1.11 6.00 21.95 14.01 9.00 2.71 8.56 17.00 4.44 10.11 3.33 22.00 19.50 7.77 \n13.37 5.00 25.25 8.00 20.20 11.11 15.00 30.30 16.66 4.00 18.00 7.07 12.34 9.87 14.00 6.66 5.55 19.00 2.22 20.00 \n3.00 18.88 4.04 22.22 8.80 16.00 11.23 7.50 6.25 9.90"); printstr("\nObtained Result : \n"); print(array1) // contain all the values exchanges here if the code is correct diff --git a/lib.c b/lib.c index 82948a7..b6b0264 100644 --- a/lib.c +++ b/lib.c @@ -1005,7 +1005,7 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // auxiliary function that enables us to have a new line every 20 floats printed fprintf(fout,"%sprint_array_loop_aux%d:\n",tabulation,i); - add_indent(1,c,t,fout); + add_indent(c,t,fout); fprintf(fout,"%sLF\n",tabulation); // we reset the counter of elements printed on one line fprintf(fout,"%sli $t4, 0\n",tabulation); -- GitLab