diff --git a/Codes_en_COREC/tests/test7.corec b/Codes_en_COREC/tests/test7.corec index c87a95039655cba802ea03e2aeb31dc5b44a2f29..ace050de38c673fa2a7f4c7a569a3a0ad25ac02d 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") } } diff --git a/Codes_en_COREC/tests/test9.corec b/Codes_en_COREC/tests/test9.corec index a183de23fee52ffa30cf46fca025580a9fd946d6..562894dabe2f639881d02cc06dde1ed02c631e32 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) + } } } diff --git a/README.md b/README.md index 931fa201543d3bbd56e08f5e75f78fb3867a5cef..defef7834faaeb37ab00b29cf5f8ba58b49b8405 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/corec.y b/corec.y index 86af826fa78c71b87b95c0ea1afdeb99e7dfc424..24b6aadcca7d7310c56088b8279e1df44063a556 100644 --- a/corec.y +++ b/corec.y @@ -380,6 +380,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); diff --git a/docs/tests.md b/docs/tests.md index 0c3b096cefa52995a305a77c874e677427211ba7..bf98698d618e7757cacbd3ab11c69ea7f2c139cd 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. diff --git a/lib.c b/lib.c index 1b2680caea7e6610303eb462a2152b04919aab79..071790f6a9f5f3adcfe6f2e5ca4b8cdef8d5c694 100644 --- a/lib.c +++ b/lib.c @@ -383,6 +383,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," := "); @@ -531,6 +535,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; @@ -810,6 +817,40 @@ 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 + // System call read float + fprintf(fout,"%sli $v0, 6\n",tabulation); + fprintf(fout,"%ssyscall\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: + fprintf(stderr,"Can not assign value into this kind\n"); + break; + } + +} + // -------- FUNCTIONS RELATED -------- diff --git a/lib.h b/lib.h index 81097d9e1e80f484bb267f916bd210cb10fc32f8..9744129475a5db58a7e1d0286ffd67ce1afe4672 100644 --- a/lib.h +++ b/lib.h @@ -107,7 +107,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; @@ -182,6 +182,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 --------