diff --git a/Codes_en_COREC/2mm.corec b/Codes_en_COREC/2mm.corec index 7a33612f5278399fe3907c51eba73b797a7f77db..c00888f5383b76cbd4e6616b3ab9674bbf3d0120 100644 --- a/Codes_en_COREC/2mm.corec +++ b/Codes_en_COREC/2mm.corec @@ -21,7 +21,7 @@ prog 2mm { Rec: d[i,j]=(i*(j+2) % NK) / NK } - def Eq1 { [in: (a,NI,NK), (b,NK,NJ), NI,NJ,NK, alpha; out: (tmp,NI,NJ)] + def Eq1 { [in: (a,NI,NK), (b,NK,NJ), NI,NJ,NK, (alpha,1); out: (tmp,NI,NJ)] Dom: i in [0..NI-1], j in [0..NJ-1], k in [0..NK-1] Rec: { k==0?tmp[i,j]=0: ; @@ -29,7 +29,7 @@ prog 2mm { } } - def Eq2 { [in: (c,NJ,NL), (tmp,NI,NJ), NI,NJ,NL, beta; inout: (d,NI,NL)] + def Eq2 { [in: (c,NJ,NL), (tmp,NI,NJ), NI,NJ,NL, (beta,1); inout: (d,NI,NL)] Dom: i in [0..NI-1], j in [0..NL-1], k in [0..NJ-1] Rec: { k==0?d[i,j]*=beta: ; @@ -38,7 +38,7 @@ prog 2mm { } def Main { - Loc: alpha=1.5, beta=1.2, + Loc: (alpha,1)=1.5, (beta,1)=1.2, NI,NJ,NK,NL Rec: { printstr("Entrez une valeur entière NI : "); diff --git a/Codes_en_COREC/Fact.corec b/Codes_en_COREC/Fact.corec index 0167b5636429113e3d6f7afc1c24059a312c542f..34b25ae5f8ec8e095be078d43d43bc4ad204579e 100644 --- a/Codes_en_COREC/Fact.corec +++ b/Codes_en_COREC/Fact.corec @@ -2,7 +2,7 @@ prog factorielle { def Fact { [in: N; out: (f,N)] Rec: N==0?f[N]=1:{ N=N-1; - f[N+1]=(N+1)*Fact + f[N+1]=(N+1)*OutFact(Fact) } } diff --git a/Codes_en_COREC/Fibonacci.corec b/Codes_en_COREC/Fibonacci.corec index 347f696d5870f1621302b07c7e0d38cfb95cdeb8..de0f0456aeff23b48e6e18949da0e6225f6f90b5 100644 --- a/Codes_en_COREC/Fibonacci.corec +++ b/Codes_en_COREC/Fibonacci.corec @@ -3,9 +3,9 @@ prog fibonacci { Loc: N1,N2 Rec: N==0?f[N]=0:N==1?f[N]=1:{ N=N-1; - N1=Fib; + N1=OutFact(Fib); N=N-1; - N2=Fib; + N2=OutFact(Fib); f[N+2]=N1+N2 } } diff --git a/Codes_en_COREC/tests/test1.corec b/Codes_en_COREC/tests/test1.corec index b0af3f5c3f17d00e244cd1c03bac4e87d2ca2f97..d8c72207fa6db6a8587ab9d82ca32d297349de46 100644 --- a/Codes_en_COREC/tests/test1.corec +++ b/Codes_en_COREC/tests/test1.corec @@ -5,7 +5,7 @@ prog Test1 { Loc : a, c=2, b Rec : { a = 10; - b = 2.2; + b = 2; printstr("Affichage de variables, a: "); print(a); diff --git a/Codes_en_COREC/tests/test10.corec b/Codes_en_COREC/tests/test10.corec index 5dbec421a54901cfb0e348e8087bd5192a1f2678..559c3141f8b81abc32675b5ac9ef3719d7a7c3cf 100644 --- a/Codes_en_COREC/tests/test10.corec +++ b/Codes_en_COREC/tests/test10.corec @@ -1 +1 @@ -// Call multiple function +// Call function arguments in out inout diff --git a/Codes_en_COREC/tests/test11.corec b/Codes_en_COREC/tests/test11.corec index 64dab45985c5b3832e40b4ed52fd1b0bbc0c5c8b..5dbec421a54901cfb0e348e8087bd5192a1f2678 100644 --- a/Codes_en_COREC/tests/test11.corec +++ b/Codes_en_COREC/tests/test11.corec @@ -1 +1 @@ -// Call function with the 2nd syntax +// Call multiple function diff --git a/Codes_en_COREC/tests/test12.corec b/Codes_en_COREC/tests/test12.corec index 131ce31a266dacda3852ed2c9fc71b8491e83603..64dab45985c5b3832e40b4ed52fd1b0bbc0c5c8b 100644 --- a/Codes_en_COREC/tests/test12.corec +++ b/Codes_en_COREC/tests/test12.corec @@ -1 +1 @@ -// Call function recursive +// Call function with the 2nd syntax diff --git a/Codes_en_COREC/tests/test13.corec b/Codes_en_COREC/tests/test13.corec new file mode 100644 index 0000000000000000000000000000000000000000..131ce31a266dacda3852ed2c9fc71b8491e83603 --- /dev/null +++ b/Codes_en_COREC/tests/test13.corec @@ -0,0 +1 @@ +// Call function recursive diff --git a/Codes_en_COREC/tests/test3.corec b/Codes_en_COREC/tests/test3.corec index ff8cabe9f06323213dea71529ec45a6edfdb12b9..44a6266665041df0a2f4738a87d4bf60561fd629 100644 --- a/Codes_en_COREC/tests/test3.corec +++ b/Codes_en_COREC/tests/test3.corec @@ -1,20 +1,23 @@ -// Aff arrays +// Aff 1D arrays prog Test3 { def Main{ - Loc : (array1,5), (array2,2+4/2) + Loc : (array1,5), (array2,2+4/2), (f,1) Rec : { + array1 = 1; // Implicitement array1[0] array1[4] = 2; - array2[0] = 1.15; + f = 1.15; // Implicit array of dim 1 length 1 -> f[0] + array2[0] = f; array2[1] = 5; array1[3] = array2[1]; - array1[2] = 1; + f[0] = 1.0; // Implicit array of dim 1 length 1 -> f[0] + array1[2] = f; - print((array1,5)) + print(array1) } } } diff --git a/Codes_en_COREC/tests/test4.corec b/Codes_en_COREC/tests/test4.corec index cf9ee6ec88630164a9b684dc2546c68185bce949..61dfdca1f8f58b40f0d62f39f42e558822de8a7c 100644 --- a/Codes_en_COREC/tests/test4.corec +++ b/Codes_en_COREC/tests/test4.corec @@ -1,52 +1,44 @@ -// Opp on arrays +// Opp on 1D arrays prog Test4 { def Main{ - Loc : (array,3) + Loc : (array,6), (float,1), float=3.0 Rec : { - array[0] = 2; + array = 2; - array[0] += 2; - printstr("Attendu 4 :"); - print((array,3)); - array[0] -= 2; - printstr("Attendu 2 :"); - print((array,3)); - array[0] /= 2; - printstr("Attendu 1 :"); - print((array,3)); - array[0] *= 2; - printstr("Attendu 2 :"); - print((array,3)); - - array[1] = array[0] + 3; - printstr("Attendu 5 :"); - print((array,3)); + array += float; + printstr("Attendu 4.00"); + array[1] -= 2; + printstr("Attendu 2.00"); + array[2] /= 2; + printstr("Attendu 1.00"); + array[3] *= 2; + printstr("Attendu 2.00"); - array[1] = array[0] - 3; - printstr("Attendu 2 :"); - print((array,3)); + print(array); + + array[1] = array[0] + float; + printstr("Attendu 5.00 :"); + array[2] = array[0] - float; + printstr("Attendu 2.00 :"); - array[1] = array[0] / 3; + array[3] = array[0] / float; printstr("Attendu 0.66 :"); - print((array,3)); - array[1] = array[0] * 3; + array[4] = array[0] * float; printstr("Attendu 2.00 :"); - print((array,3)); - + + print(array); array[2] = array[0] + array[1]; - printstr("Attendu 4 :"); - print((array,3)); - array[2] = array[0] - array[1]; - printstr("Attendu 0 :"); - print((array,3)); - array[2] = array[0] / array[1]; - printstr("Attendu 1 :"); - print((array,3)); - array[2] = array[0] * array[1]; - printstr("Attendu 4 :"); - print((array,3)) + printstr("Attendu 4.00 :"); + array[3] = array[0] - array[1]; + printstr("Attendu 0.00 :"); + array[4] = array[0] / array[1]; + printstr("Attendu 1.00 :"); + array[5] = array[0] * array[1]; + printstr("Attendu 4.00 :"); + + print(array) } } } diff --git a/Codes_en_COREC/tests/test5.corec b/Codes_en_COREC/tests/test5.corec index c984638df609aea40f6bbd52b454cd46f3df51c5..5adf5deab9e8507517e3cf4cbea1fa66c08323e4 100644 --- a/Codes_en_COREC/tests/test5.corec +++ b/Codes_en_COREC/tests/test5.corec @@ -1,25 +1,9 @@ -// Conditions (if else ternaire..) +// Opp on arrays -prog test5{ - def Main { +prog Test5 { + def Main{ Rec : { - a = 10; - b = 2; - - printstr("a = 10 | b = 2"); - printstr("a<b ?"); - - a>b? - { - printstr("oui") - } - :printstr("non"); - - printstr("b egal a 2 ?"); - b==2? - { - printstr("oui") - }: + printstr("A faire") } } } diff --git a/Codes_en_COREC/tests/test6.corec b/Codes_en_COREC/tests/test6.corec index a183de23fee52ffa30cf46fca025580a9fd946d6..c984638df609aea40f6bbd52b454cd46f3df51c5 100644 --- a/Codes_en_COREC/tests/test6.corec +++ b/Codes_en_COREC/tests/test6.corec @@ -1,11 +1,25 @@ // Conditions (if else ternaire..) -prog test4 { - def Main { +prog test5{ + def Main { Rec : { - printstr("Saisie clavie :"); - read(saisie); - print(saisie) + a = 10; + b = 2; + + printstr("a = 10 | b = 2"); + printstr("a<b ?"); + + a>b? + { + printstr("oui") + } + :printstr("non"); + + printstr("b egal a 2 ?"); + b==2? + { + printstr("oui") + }: } } } diff --git a/Codes_en_COREC/tests/test7.corec b/Codes_en_COREC/tests/test7.corec index 0c8d007c1353a0a44cf90a9fb05f46b8204837ac..a183de23fee52ffa30cf46fca025580a9fd946d6 100644 --- a/Codes_en_COREC/tests/test7.corec +++ b/Codes_en_COREC/tests/test7.corec @@ -1 +1,11 @@ -// For with Dom +// Conditions (if else ternaire..) + +prog test4 { + def Main { + Rec : { + printstr("Saisie clavie :"); + read(saisie); + print(saisie) + } + } +} diff --git a/Codes_en_COREC/tests/test8.corec b/Codes_en_COREC/tests/test8.corec index 2cef160a05d110af5a22b5ef4c19db0f7cf826e4..0c8d007c1353a0a44cf90a9fb05f46b8204837ac 100644 --- a/Codes_en_COREC/tests/test8.corec +++ b/Codes_en_COREC/tests/test8.corec @@ -1 +1 @@ -// Call function +// For with Dom diff --git a/Codes_en_COREC/tests/test9.corec b/Codes_en_COREC/tests/test9.corec index 559c3141f8b81abc32675b5ac9ef3719d7a7c3cf..2cef160a05d110af5a22b5ef4c19db0f7cf826e4 100644 --- a/Codes_en_COREC/tests/test9.corec +++ b/Codes_en_COREC/tests/test9.corec @@ -1 +1 @@ -// Call function arguments in out inout +// Call function diff --git a/Codes_en_COREC/testsErrors/test10Err.corec b/Codes_en_COREC/testsErrors/test10Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..fcbe86b2c5a77a6857e1bc9d0e4efffe20302c29 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test10Err.corec @@ -0,0 +1,8 @@ +prog Test10Err { + def Main{ + Loc : (array,5) + Rec : { + array[2.0] = 2 + } + } +} diff --git a/Codes_en_COREC/testsErrors/test11Err.corec b/Codes_en_COREC/testsErrors/test11Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..55d50b47f64da1caea12d090dfc2a3487a355d22 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test11Err.corec @@ -0,0 +1,8 @@ +prog Test11Err { + def Main{ + Loc : (array,2.3) + Rec : { + array[2] = 2 + } + } +} diff --git a/Codes_en_COREC/testsErrors/test12Err.corec b/Codes_en_COREC/testsErrors/test12Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..6019f03a247edb613b3fe29306d6e40bfd69e8e3 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test12Err.corec @@ -0,0 +1,8 @@ +prog Test12Err { + def Main{ + Loc : a = 1.2 + Rec : { + print(a) + } + } +} diff --git a/Codes_en_COREC/testsErrors/test13Err.corec b/Codes_en_COREC/testsErrors/test13Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..62483c82b8081d33c28f755afe60f19543f08046 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test13Err.corec @@ -0,0 +1,9 @@ +prog Test13Err { + def Main{ + Loc : a = 1 + Rec : { + a = 2.1 * 1; + print(a) + } + } +} diff --git a/Codes_en_COREC/testsErrors/test14Err.corec b/Codes_en_COREC/testsErrors/test14Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..993583b68f6c7f62e9f73e320682f25386eb56e5 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test14Err.corec @@ -0,0 +1,9 @@ +prog Test14Err { + def Main{ + Loc : a = 1 + Rec : { + a += 2.1; + print(a) + } + } +} diff --git a/Codes_en_COREC/testsErrors/test15Err.corec b/Codes_en_COREC/testsErrors/test15Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..82dd8a0cfb0bf8f19457ee5140c2ec0c1e70b0f6 --- /dev/null +++ b/Codes_en_COREC/testsErrors/test15Err.corec @@ -0,0 +1,8 @@ +prog Test15Err { + def Main{ + Loc : a, a = 1.2 + Rec : { + print(a) + } + } +} diff --git a/Codes_en_COREC/testsErrors/test16Err.corec b/Codes_en_COREC/testsErrors/test16Err.corec new file mode 100644 index 0000000000000000000000000000000000000000..483ecbfe50eeb2ef438e84520b39f982b15f58cd --- /dev/null +++ b/Codes_en_COREC/testsErrors/test16Err.corec @@ -0,0 +1,8 @@ +prog Test16Err { + def Main{ + Loc : (a,1), a=2.0, b = a + Rec : { + print(b) + } + } +} diff --git a/Codes_en_COREC/testsErrors/test1Err.corec b/Codes_en_COREC/testsErrors/test1Err.corec index f22af3e5ebfee9f672fdbd96fb991de9f66bdab4..cb2ba059427ebf6340a79833464788a6cf55ccce 100644 --- a/Codes_en_COREC/testsErrors/test1Err.corec +++ b/Codes_en_COREC/testsErrors/test1Err.corec @@ -1,5 +1,5 @@ -prog Test4 { +prog Test1Err { def Main{ } } diff --git a/Codes_en_COREC/testsErrors/test2Err.corec b/Codes_en_COREC/testsErrors/test2Err.corec index acf9691ce0bb4c1e8468283fa4280f3ca2d0252c..cc01f56846c3e790f65298c19310379bccc2c843 100644 --- a/Codes_en_COREC/testsErrors/test2Err.corec +++ b/Codes_en_COREC/testsErrors/test2Err.corec @@ -1,4 +1,4 @@ -prog Test2 { +prog Test2Err { def Main{ Rec : { } diff --git a/Codes_en_COREC/testsErrors/test3Err.corec b/Codes_en_COREC/testsErrors/test3Err.corec index b4f816bae041f972fe1870a4238fb5f24045204a..eebb8b3eec112a6b7bed5b31800011e4e9cc2e91 100644 --- a/Codes_en_COREC/testsErrors/test3Err.corec +++ b/Codes_en_COREC/testsErrors/test3Err.corec @@ -1,9 +1,9 @@ -prog Test3 { +prog Test3Err { def Main{ Loc : a, a=2, b // a deja declaré Rec : { a = 10; - b = 2.2; + b = 2; printstr("Affichage de variables, a: "); print(a); diff --git a/Codes_en_COREC/testsErrors/test4Err.corec b/Codes_en_COREC/testsErrors/test4Err.corec index 6f8d05866ab9c6db95cc4659fde35c273f8c9c36..fdee3ae646f0a8d2fb433ad354f9a00b98d1aad4 100644 --- a/Codes_en_COREC/testsErrors/test4Err.corec +++ b/Codes_en_COREC/testsErrors/test4Err.corec @@ -1,9 +1,9 @@ -prog Test4 { +prog Test4Err { def T{ [] Loc : a, c=2, b Rec : { a = 10; - b = 2.2; + b = 2; printstr("Affichage de variables, a: "); print(a); diff --git a/Codes_en_COREC/testsErrors/test5Err.corec b/Codes_en_COREC/testsErrors/test5Err.corec index d5bf1c66dd1d5c4e574153998070c3810c297563..435750ff34ccaccf08dc2430c711dfed9de405b2 100644 --- a/Codes_en_COREC/testsErrors/test5Err.corec +++ b/Codes_en_COREC/testsErrors/test5Err.corec @@ -1,9 +1,9 @@ -prog Test5 { +prog Test5Err { def T{ [] Loc : a, c=2, b Rec : { a = 10; - b = 2.2; + b = 2; printstr("Affichage de variables, a: "); print(a); @@ -19,7 +19,7 @@ prog Test5 { Loc : a, c=2, b Rec : { a = 10; - b = 2.2; + b = 2; printstr("Affichage de variables, a: "); print(a); diff --git a/Codes_en_COREC/testsErrors/test6Err.corec b/Codes_en_COREC/testsErrors/test6Err.corec index 9ed736d2a7c703a12ffc61281dc3e76f57ccf0a1..2d6d5011250519c181621ef3359258a0714b727e 100644 --- a/Codes_en_COREC/testsErrors/test6Err.corec +++ b/Codes_en_COREC/testsErrors/test6Err.corec @@ -1,4 +1,4 @@ -prog Test6 { +prog Test6Err { def Main{ Loc : a Rec : { diff --git a/Codes_en_COREC/testsErrors/test7Err.corec b/Codes_en_COREC/testsErrors/test7Err.corec index 185e8869c9b5809a1fb62bbb7e4d4ca41ba939ff..d55f3a18e7d43184290fbc1f95d90574db295b5a 100644 --- a/Codes_en_COREC/testsErrors/test7Err.corec +++ b/Codes_en_COREC/testsErrors/test7Err.corec @@ -1,4 +1,4 @@ -prog Test7 { +prog Test7Err { def Main{ Loc : Rec : { diff --git a/Codes_en_COREC/testsErrors/test8Err.corec b/Codes_en_COREC/testsErrors/test8Err.corec index 04671651bf9b0c7484e0a9b5c921e0c0a4e22904..1dcbc6313259a2670fb082a3e4c21c9906bc0fab 100644 --- a/Codes_en_COREC/testsErrors/test8Err.corec +++ b/Codes_en_COREC/testsErrors/test8Err.corec @@ -1,4 +1,4 @@ -prog Test8 { +prog Test8Err { def T{ [] Loc : (array,1) Rec : { diff --git a/Codes_en_COREC/testsErrors/test9Err.corec b/Codes_en_COREC/testsErrors/test9Err.corec index 34a1d28ce017dcd183d98485f9a4cf63806e6ac9..041b6726d178d034407cf69a375e6782830bc131 100644 --- a/Codes_en_COREC/testsErrors/test9Err.corec +++ b/Codes_en_COREC/testsErrors/test9Err.corec @@ -1,4 +1,4 @@ -prog Test9 { +prog Test9Err { def Main{ Loc : (array,5) Rec : { diff --git a/Makefile b/Makefile index a14889613afe327c8b838b8efee73421567a91ff..c2aff38f958b35c41376c65973c4a39e6d369f50 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ test_corec: allDev ./$(Compilateur_COREC) -tos < $(DIR_IN)/ProdMat.corec > $(DIR_OUT)/output6.asm ./$(Compilateur_COREC) -tos < $(DIR_IN)/SystTriang.corec > $(DIR_OUT)/output7.asm -test: allDev +test: all ./$(Compilateur_COREC) -tos -o $(DIR_OUT)/output$(num).asm < $(DIR_IN)/tests/test$(num).corec testErr: all diff --git a/README.md b/README.md index 11d6a7e3acce8f42d3cb52a74918e3f4f4e35d6b..c1407c9862e78dd77e9e8dba069047140caa7e2d 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,30 @@ sudo apt install bison Download [MARS : MIPS java simulator on moodle unistra](https://moodle.unistra.fr/pluginfile.php/916605/mod_folder/content/0/Mars4_5.jar?forcedownload=1). ## Implemented Features -### Branche gencode_1 -- Definition of local variable in the loc section of functions. **(TO DO :TRANSLATE IN MIPS)** -- Affectation of float and integer value. **(TO DO :TRANSLATE IN MIPS)** +### Branch gencode_1 +- Definition of local variable in the loc section of functions. **(TO DO :TRANSLATE IN MIPS but do not do here the float value)** +- Affectation of integer value. **(TO DO :TRANSLATE IN MIPS but do not do here the float value)** - Print and printstr functions. **(TO DO :TRANSLATE IN MIPS)** -### Branche gencode_2 +### Branch gencode_2 - Operation on variable (except arrays) -> (+,-,*,/). **(TO DO :TRANSLATE IN MIPS)** - Assignement operation on variable (except arrays) -> (+=,-=,*=,/=). **(TO DO :TRANSLATE IN MIPS)** -### Branche gencode_3 +### Branch gencode_3-4 - Affectation and definition of local 1D array. **(TO DO :TRANSLATE IN MIPS)** - Operation with and on local 1D array. **(TO DO :TRANSLATE IN MIPS)** -- print function works on array **(NEED TO VERIFY IF THE SHAPE IS GOOD)**. **(TO DO :TRANSLATE IN MIPS)** +- print function works on array. **(TO DO :TRANSLATE IN MIPS)** + +- float value as singleton array. **(TO DO :TRANSLATE IN MIPS)** +- Multi dimensional array. **(TO BE SOON)** **(TO DO :TRANSLATE IN MIPS)** + +## To Do +- To be soon : Arrays are implictly float number. +- To be soon : Float are array of dim 1 and length 1. +1. Translate intermediate code of the gencode_1 branch in mips. +2. Translate intermediate code of the gencode_2 branch in mips. +3. Translate intermediate code of the gencode_3 branch in mips. +4. Add to the symtable in the beginning of the programm COREC key words (def, prog, Loc, Rec, ...) to recognize without the lex. + + ## Makefile commands - `make` -> compile the source files diff --git a/corec.l b/corec.l index d97b2c94d6ef3b38dc19894f765f664125eb22a1..572b155d1560e7587af83f2d14141284e99f275a 100644 --- a/corec.l +++ b/corec.l @@ -64,22 +64,22 @@ in {return in_dom;} "%" {return mod;} ([0-9]*[[:alpha:]]+|[[:alpha:]])[[:alnum:]]* { - if ( yyleng > 31 ) + if ( yyleng > 63 ) fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 31), truncated.\n",lineNumber,yytext); - strncpy(yylval.strval,yytext,31); - yylval.strval[31] = '\0'; + strncpy(yylval.strval,yytext,63); + yylval.strval[64] = '\0'; return ID; } -["]([^"]|[\\]["])*["] { - 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[1023] = '\0'; - return chaine; - } +["]([^"]|[\\]["])*["] { + 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'; + return chaine; + } -(([1-9][0-9]*)|0)[.][0-9]*[1-9] {yylval.floatval = atof(yytext); return flottant;} -[1-9][0-9]*|0 {yylval.intval = atoi(yytext); return entier;} +(([1-9][0-9]*)|0)[.](([0-9]*[1-9])|[0]+) {yylval.floatval = atof(yytext); return flottant;} +[1-9][0-9]*|0 {yylval.intval = atoi(yytext); return entier;} [[:space:]] ; diff --git a/corec.y b/corec.y index 67c46893a7758e792124f4d13edd5d1274678c7d..cffcb346140ce3a1a32be1e53b4077ce74d4e9e0 100644 --- a/corec.y +++ b/corec.y @@ -7,6 +7,8 @@ extern int yylex(); struct symbol* symb_scope_function; +uint8_t isPrintCall = 0; + %} %define parse.error verbose @@ -22,6 +24,11 @@ struct symbol* symb_scope_function; struct symbol* ptr; } ptrVal; + struct { + struct symbol* ptr; + uint8_t isEArray; + } ptrValE; + struct { struct symbol* ptr1; struct symbol* ptr2; @@ -90,7 +97,8 @@ struct symbol* symb_scope_function; %left mult divide mod %right usub formatmod -%type <ptrVal> F T E CALL DLIST ELIST +%type <ptrVal> DLIST ELIST +%type <ptrValE> E T F CALL %type <ptrArray> ARRAYREF ARRAY %type <opaff> OPAFF @@ -153,14 +161,44 @@ DECL : id = symtable_put_loc(SYMTAB,$1,symb_scope_function); } | ID aff E - { + { struct symbol* id = symtable_get(SYMTAB,$1,symb_scope_function); - if ( id != NULL ){ - fprintf(stderr, "Error at line %u in function '%s': Variable '%s' is already declared.\n",lineNumber,symb_scope_function->u.name,$1); - exit(1); + if($3.ptr->kind == CONSTANT_FLOAT){ // Test if affectation a float to an array + fprintf(fname,"FLOAT LOC\n"); + if(id == NULL){ + fprintf(stderr, "Error at line %u in function '%s': Try to declare variable '%s' as an float (array of dimension and lenght 1) but the size isn't declare. You may prefer to do this : '(%s,1), '%s'=%0.2f'.\n",lineNumber,symb_scope_function->u.name,$1,$1,$1,$3.ptr->u.value_float); + exit(1); + } + else if(id->kind != ARRAY){ + fprintf(stderr, "Error at line %u in function '%s': Try assigning at variable '%s' a float value (an array of dimension and lenght 1) but '%s' isn't a float variable.\n",lineNumber,symb_scope_function->u.name,$1,$1); + exit(1); + } + } + else if($3.ptr->kind == ARRAY || $3.isEArray){ + if(id == NULL){ + fprintf(stderr, "Error at line %u in function '%s': Try to declare variable '%s' as an integer but the affectation value is an array.\n",lineNumber,symb_scope_function->u.name,$1); + exit(1); + } + else if(id->kind != ARRAY){ + fprintf(stderr, "Error at line %u in function '%s': Try assigning an array variable '%s' to a non array type variable '%s'.\n",lineNumber,symb_scope_function->u.name,$3.ptr->u.arr.name,$1); + exit(1); + } + } + else{ // Test if affectation value isn't a float and array + if (id != NULL){ + fprintf(stderr, "Error at line %u in function '%s': Variable '%s' is already declared.\n",lineNumber,symb_scope_function->u.name,$1); + exit(1); + } + + id = symtable_put_loc(SYMTAB,$1,symb_scope_function); } - id = symtable_put_loc(SYMTAB,$1,symb_scope_function); - gencode(CODE,COPY,id,$3.ptr,NULL); + + if(id->kind == ARRAY){ // Handle if ID is an implicit array(array -> array[0]) + struct symbol* const_0 = symtable_const_int(SYMTAB,0); + gencode(CODE,AFF_ARRAY,id,const_0,$3.ptr); + } + else + gencode(CODE,COPY,id,$3.ptr,NULL); } | ARRAY { @@ -183,11 +221,19 @@ ARRAY : DLIST : DLIST comma E { + if($3.ptr->kind == CONSTANT_FLOAT){ + fprintf(stderr, "Error at line %u in function '%s': Try to allocate an array with a float length value.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_PLUS,$$.ptr,$1.ptr,$3.ptr); } | E { + if($1.ptr->kind == CONSTANT_FLOAT){ + fprintf(stderr, "Error at line %u in function '%s': Try to allocate an array with a float length value.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = $1.ptr; } ; @@ -253,14 +299,10 @@ I : | AFFECTATION | CALL // appel de fonction | read_f left_parenthesis ID right_parenthesis - | print left_parenthesis CALL right_parenthesis - { - gencode(CODE,CALL_PRINT,$3.ptr,NULL,NULL); - } - | print left_parenthesis ARRAY right_parenthesis - { - gencode(CODE,CALL_PRINT,$3.ptr1,NULL,NULL); - } + | print left_parenthesis {isPrintCall = 1;} CALL right_parenthesis + { + gencode(CODE,CALL_PRINT,$4.ptr,NULL,NULL); + } | print left_parenthesis PRECINST right_parenthesis | printstr left_parenthesis chaine right_parenthesis { @@ -325,7 +367,7 @@ AFFECTATION : break; case 5: - fprintf(stderr, "Error at line %u: Unrecognized operation assignment (only +=, -=, *= and /= are possible).\n",lineNumber-1); + fprintf(stderr, "Error at line %u in function '%s': Unrecognized operation assignment (only +=, -=, *= and /= are possible).\n",lineNumber-1,symb_scope_function->u.name); exit(1); break; @@ -339,6 +381,11 @@ AFFECTATION : } | ID OPAFF E { + if($3.ptr->kind == CONSTANT_FLOAT){ + fprintf(stderr, "Error at line %u in function '%s': Cannot affect an float to an non array variable '%s'.\n",lineNumber-1,symb_scope_function->u.name,$1); + exit(1); + } + // Get the variable ID struct symbol* id = symtable_get(SYMTAB,$1,symb_scope_function); if ( id == NULL ){ @@ -410,19 +457,23 @@ CALL : | ID { // Appel de variable + struct symbol* id = symtable_get(SYMTAB,$1,symb_scope_function); if ( id == NULL ){ fprintf(stderr, "Error at line %u in function '%s': Variable '%s' is not declared, neither as an argument, nor as a local variable.\n",lineNumber,symb_scope_function->u.name,$1); exit(1); } - if(id->kind == ARRAY){ + if(id->kind == ARRAY && !isPrintCall){ + $$.isEArray = 1; $$.ptr = newtemp(SYMTAB); struct symbol* const_0 = symtable_const_int(SYMTAB,0); gencode(CODE,COPY_ARRAY,$$.ptr,id,const_0); } else $$.ptr = id; + + isPrintCall = 0; } ; LCALL : @@ -446,14 +497,16 @@ ARRAYREF : ID left_bracket ELIST right_bracket } // ELIST >= 0 && CONSTANT_INT - if($3.ptr->kind == CONSTANT_DOUBLE){ - fprintf(stderr, "Error at line %u in function '%s': Array '%s' can not be reached with a float number\n",lineNumber,symb_scope_function->u.name,$1); + if($3.ptr->kind == CONSTANT_FLOAT){ + fprintf(stderr, "Error at line %u in function '%s': Array '%s' can not be accessed with a float number\n",lineNumber,symb_scope_function->u.name,$1); exit(1); } else if($3.ptr->kind == CONSTANT_INT){ - if($3.ptr->u.value_int >= $$.ptr1->u.arr.size->u.value_int){ - fprintf(stderr, "Error at line %u in function '%s': Array '%s' can not be reached with a number superior than the size of the array\n",lineNumber,symb_scope_function->u.name,$1); - exit(1); + if($$.ptr1->u.arr.size[0]->kind == CONSTANT_INT){ + if($3.ptr->u.value_int >= $$.ptr1->u.arr.size[0]->u.value_int){ + fprintf(stderr, "Error at line %u in function '%s': Array '%s' can not be accessed with a number superior than the size of the array\n",lineNumber,symb_scope_function->u.name,$1); + exit(1); + } } } @@ -477,11 +530,19 @@ ELIST : E : E plus T { + if($1.ptr->kind != $3.ptr->kind && ($3.ptr->kind == CONSTANT_INT || $3.ptr->kind==CONSTANT_FLOAT) && ($1.ptr->kind == CONSTANT_INT || $1.ptr->kind==CONSTANT_FLOAT) ){ + fprintf(stderr, "Error at line %u in function '%s': Try to perfom an operation with two operands of differents types.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_PLUS,$$.ptr,$1.ptr,$3.ptr); } | E sub T { + if($1.ptr->kind != $3.ptr->kind && ($3.ptr->kind == CONSTANT_INT || $3.ptr->kind==CONSTANT_FLOAT) && ($1.ptr->kind == CONSTANT_INT || $1.ptr->kind==CONSTANT_FLOAT) ){ + fprintf(stderr, "Error at line %u in function '%s': Try to perfom an operation with two operands of differents types.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_MINUS,$$.ptr,$1.ptr,$3.ptr); } @@ -493,28 +554,42 @@ E : | T { $$.ptr = $1.ptr; + $$.isEArray = $1.isEArray; } ; T : T mult F { + if($1.ptr->kind != $3.ptr->kind && ($3.ptr->kind == CONSTANT_INT || $3.ptr->kind==CONSTANT_FLOAT) && ($1.ptr->kind == CONSTANT_INT || $1.ptr->kind==CONSTANT_FLOAT) ){ + fprintf(stderr, "Error at line %u in function '%s': Try to perfom an operation with two operands of differents types.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_MULT,$$.ptr,$1.ptr,$3.ptr); } | T divide F { + if($1.ptr->kind != $3.ptr->kind && ($3.ptr->kind == CONSTANT_INT || $3.ptr->kind==CONSTANT_FLOAT) && ($1.ptr->kind == CONSTANT_INT || $1.ptr->kind==CONSTANT_FLOAT) ){ + fprintf(stderr, "Error at line %u in function '%s': Try to perfom an operation with two operands of differents types.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_DIV,$$.ptr,$1.ptr,$3.ptr); } | T mod F { + if($1.ptr->kind != $3.ptr->kind && ($3.ptr->kind == CONSTANT_INT || $3.ptr->kind==CONSTANT_FLOAT) && ($1.ptr->kind == CONSTANT_INT || $1.ptr->kind==CONSTANT_FLOAT) ){ + fprintf(stderr, "Error at line %u in function '%s': Try to perfom an operation with two operands of differents types.\n",lineNumber,symb_scope_function->u.name); + exit(1); + } $$.ptr = newtemp(SYMTAB); gencode(CODE,BOP_MOD,$$.ptr,$1.ptr,$3.ptr); } | F { $$.ptr = $1.ptr; + $$.isEArray = $1.isEArray; } ; @@ -525,10 +600,12 @@ F : } | CALL { + $$.isEArray = $1.isEArray; $$.ptr = $1.ptr; } | ARRAYREF { + $$.ptr = newtemp(SYMTAB); gencode(CODE,COPY_ARRAY,$$.ptr,$1.ptr1,$1.ptr2); } diff --git a/corecDev.l b/corecDev.l index 47e8981003b0efc8d25887a882213883cda3078e..3e0143e135b57fff8adbeb85df52fdc557d404f7 100644 --- a/corecDev.l +++ b/corecDev.l @@ -63,24 +63,24 @@ in {printf("in_dom"); return in_dom;} "/" {printf("divide"); return divide;} "%" {printf("mod"); return mod;} -([0-9]*[[:alpha:]]+|[[:alpha:]])[[:alnum:]]* { - printf("ID"); - if ( yyleng > 31 ) - fprintf(stderr,"Error at line %u: Identifier '%s' too long (> 31), truncated.\n",lineNumber,yytext); - strncpy(yylval.strval,yytext,31); - yylval.strval[31] = '\0'; - return ID; - } -["]([^"]|[\\]["])*["] { - printf("chaine"); - 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[1023] = '\0'; - return chaine; - } -(([1-9][0-9]*)|0)[.][0-9]*[1-9] {printf("flottant"); yylval.floatval = atof(yytext); return flottant;} -[1-9][0-9]*|0 {printf("entier"); yylval.intval = atoi(yytext); return entier;} +([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; + } +["]([^"]|[\\]["])*["] { + printf("chaine"); + 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[1023] = '\0'; + return chaine; + } +(([1-9][0-9]*)|0)[.](([0-9]*[1-9])|[0]+) {printf("flottant"); yylval.floatval = atof(yytext); return flottant;} +[1-9][0-9]*|0 {printf("entier"); yylval.intval = atoi(yytext); return entier;} [[:space:]] {printf(" ");} diff --git a/docs/projet_sujet.pdf b/docs/projet_sujet.pdf index 9de81b705c8eb1cb867bb0141975443b75bc67f0..71d84338e4ba5bdec7b89196bd3505b8b2e05420 100644 Binary files a/docs/projet_sujet.pdf and b/docs/projet_sujet.pdf differ diff --git a/docs/tests.md b/docs/tests.md index c218837df770df049b38505329ae4559c7eaa978..5777fa3b865824e7c65c269e030db33369548658 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -21,7 +21,13 @@ 7. Array isn't declared. 8. Scope of local array. 9. Out of range in a local array. -10. +10. Reached array memory with a float value. +11. Allocate a array with a float value. +12. Float is array of dim 1, length 1. Defined in Loc a=FLOAT_VALUE should be OK only if before, the user has declared (a,1). +13. Same but an affectation assign a float value to an integer variable. +14. Try assigning a float value to a non-array variable. +15. Try assigning a float value to a non-array variable in Loc section. +16. Try assigning to a non array value an array. ## Navigation Return to the main documentation -> [click here](./../README.md). \ No newline at end of file diff --git a/lib.c b/lib.c index ffe039fb50e23d14e66005d0f82d83fc2caedc9a..a789a6348f7c41a4d5b6d964f1824d0aeca0559d 100644 --- a/lib.c +++ b/lib.c @@ -4,8 +4,16 @@ struct symtable * symtable_new() { struct symtable * t = malloc(sizeof(struct symtable)); + if(t == NULL){ + fprintf(stderr,"Bug memory allocation failed\n"); + exit(1); + } t->capacity = 1024; t->symbols = malloc(t->capacity*sizeof(struct symbol)); + if(t->symbols == NULL){ + fprintf(stderr,"Bug memory allocation failed\n"); + exit(1); + } t->temporary = 0; t->size = 0; return t; @@ -25,7 +33,13 @@ static void symtable_grow(struct symtable * t) struct symbol* symtable_const_int(struct symtable * t, long int v) { unsigned int i; - for ( i=0 ; i<t->size && t->symbols[i].u.value_int != v; i++ ); + for ( i=0 ; i<t->size; i++ ){ + if(t->symbols[i].kind == CONSTANT_INT){ + if(t->symbols[i].u.value_int == v){ + break; + } + } + } if(i==t->size) { if(t->size==t->capacity) @@ -42,17 +56,23 @@ struct symbol* symtable_const_int(struct symtable * t, long int v) } } -struct symbol* symtable_const_double(struct symtable * t, double v) +struct symbol* symtable_const_double(struct symtable * t, float v) { unsigned int i; - for ( i=0 ; i<t->size && t->symbols[i].u.value_double != v; i++ ); + for ( i=0 ; i<t->size; i++ ){ + if(t->symbols[i].kind == CONSTANT_FLOAT){ + if(t->symbols[i].u.value_int == v){ + break; + } + } + } if(i==t->size) { if(t->size==t->capacity) symtable_grow(t); struct symbol *s = &(t->symbols[t->size]); - s->kind = CONSTANT_DOUBLE; - s->u.value_double = v; + s->kind = CONSTANT_FLOAT; + s->u.value_float = v; ++ (t->size); return s; } @@ -118,7 +138,12 @@ struct symbol* symtable_put_array(struct symtable * t, const char * var_id, stru symtable_grow(t); struct symbol *s = &(t->symbols[t->size]); s->kind = ARRAY; - s->u.arr.size = size; + s->u.arr.size = (struct symbol**) calloc(5,sizeof(struct symbol*)); + if(s->u.arr.size == NULL){ + fprintf(stderr,"Bug memory allocation failed\n"); + exit(1); + } + s->u.arr.size[0] = size; strcpy(s->u.arr.name,var_id); strcpy(s->scope_function,func_id->u.name); ++ (t->size); @@ -132,8 +157,8 @@ void symtable_dump(struct symtable * t, FILE* fout) { if(t->symbols[i].kind==CONSTANT_INT) fprintf(fout,"# %p = %ld\n",&(t->symbols[i]),t->symbols[i].u.value_int); - else if(t->symbols[i].kind==CONSTANT_DOUBLE) - fprintf(fout,"# %p = %0.2lf\n",&(t->symbols[i]),t->symbols[i].u.value_double); + else if(t->symbols[i].kind==CONSTANT_FLOAT) + fprintf(fout,"# %p = %0.2f\n",&(t->symbols[i]),t->symbols[i].u.value_float); else if(t->symbols[i].kind==NAME) fprintf(fout,"# %p = %s\n",&(t->symbols[i]),t->symbols[i].u.name); else if(t->symbols[i].kind==NAME_LOC) @@ -142,15 +167,15 @@ void symtable_dump(struct symtable * t, FILE* fout) fprintf(fout,"# %p = %s\n",&(t->symbols[i]),t->symbols[i].u.chaine); else if(t->symbols[i].kind==ARRAY){ fprintf(fout,"# %p = (%s,",&(t->symbols[i]),t->symbols[i].u.arr.name); - if(t->symbols[i].u.arr.size->kind == NAME_LOC || t->symbols[i].u.arr.size->kind == NAME) - fprintf(fout,"%s",t->symbols[i].u.arr.size->u.name); - else if(t->symbols[i].u.arr.size->kind == CONSTANT_INT) - fprintf(fout,"%ld",t->symbols[i].u.arr.size->u.value_int); - else if(t->symbols[i].u.arr.size->kind == CONSTANT_DOUBLE) - fprintf(fout,"%0.2lf",t->symbols[i].u.arr.size->u.value_double); + if(t->symbols[i].u.arr.size[0]->kind == NAME_LOC || t->symbols[i].u.arr.size[0]->kind == NAME) + fprintf(fout,"%s",t->symbols[i].u.arr.size[0]->u.name); + else if(t->symbols[i].u.arr.size[0]->kind == CONSTANT_INT) + fprintf(fout,"%ld",t->symbols[i].u.arr.size[0]->u.value_int); + else if(t->symbols[i].u.arr.size[0]->kind == CONSTANT_FLOAT) + fprintf(fout,"%0.2f",t->symbols[i].u.arr.size[0]->u.value_float); else{ - printf("BUG array size kind:%d\n",t->symbols[i].u.arr.size->kind); - exit(1); + printf("BUG array size kind:%d\n",t->symbols[i].u.arr.size[0]->kind); + exit(1); } fprintf(fout,") (in the scope of the '%s' function)\n",t->symbols[i].scope_function); } @@ -160,6 +185,12 @@ void symtable_dump(struct symtable * t, FILE* fout) void symtable_free(struct symtable * t) { + for (unsigned int i=0 ; i<t->size; i++ ){ + if(t->symbols[i].kind == ARRAY){ + if(t->symbols[i].u.arr.size != NULL) + free(t->symbols[i].u.arr.size); + } + } free(t->symbols); free(t); } @@ -220,8 +251,8 @@ static void symbol_dump(struct symbol* s, FILE* fout) case CONSTANT_INT: fprintf(fout,"%ld",s->u.value_int); break; - case CONSTANT_DOUBLE: - fprintf(fout,"%0.2lf",s->u.value_double); + case CONSTANT_FLOAT: + fprintf(fout,"%0.2f",s->u.value_float); break; case NAME_LOC: fprintf(fout,"%s",s->u.name); @@ -359,8 +390,8 @@ void code_mips_dump(struct symtable * t, struct code * c){ if(t->symbols[i].kind == CONSTANT_INT){ //INTEGER CONSTANT fprintf(stdout,"\ttemp%d: .word %ld\n",i,t->symbols[i].u.value_int); } - else if(t->symbols[i].kind == CONSTANT_DOUBLE){ //FLOAT CONSTANT - fprintf(stdout,"\ttemp%d: .word %0.2lf\n",i,t->symbols[i].u.value_double); + else if(t->symbols[i].kind == CONSTANT_FLOAT){ //FLOAT CONSTANT + fprintf(stdout,"\ttemp%d: .word %0.2f\n",i,t->symbols[i].u.value_float); } else{ //NAME fprintf(stdout,"\ttemp%d: .word 0\n",i); diff --git a/lib.h b/lib.h index c5f2003bf8a70b69dbe8f0e03d56124073581b31..f1d26c219f114c633ac07edbf465bfff0aae248c 100644 --- a/lib.h +++ b/lib.h @@ -8,19 +8,19 @@ extern struct symbol* symb_scope_function; /* TABLE DES SYMBOLES */ -typedef char name_t[32]; +typedef char name_t[64]; typedef char name_64_t[1024]; struct array { name_t name; - struct symbol* size; + struct symbol** size; }; // Struct symbol -> store a symbol struct symbol { enum { NAME, // -> use the name attribut in the union u CONSTANT_INT, // -> use the value_int attribut in the union u - CONSTANT_DOUBLE, // -> use the value_double attribut in the union u + CONSTANT_FLOAT, // -> use the value_float attribut in the union u CHAIN, // -> use the chaine attribut in the union u NAME_LOC, // -> use the name attribut, the symbol is just defined ARRAY // -> use the name attribut in the union u @@ -29,7 +29,7 @@ struct symbol { name_t name; name_64_t chaine; long int value_int; - double value_double; + float value_float; struct array arr; } u; @@ -50,8 +50,8 @@ struct symtable * symtable_new(); // Add a symbol in the table of a integer (CONSTANT_INT) struct symbol* symtable_const_int(struct symtable * t, long int v); -// Add a symbol in the table of a double (CONSTANT_DOUBLE) -struct symbol* symtable_const_double(struct symtable * t, double v); +// Add a symbol in the table of a float (CONSTANT_FLOAT) +struct symbol* symtable_const_double(struct symtable * t, float v); // Add a symbol in the table of a function (NAME) struct symbol* symtable_put_name(struct symtable * t, const char * s); // Add a symbol in the table of a chaine of caracters (CHAIN)