@@ -673,7 +673,7 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
673
673
int RecurCallResult = -1 ;
674
674
st_element_t * Variable ; //To save pointer on variable in hashTable
675
675
struct check SolveProblems ; //struct to solve problem with conflicts in while and if blocks
676
- st_localTable_t * CalledFunction ; //Pointer to hash table, for work with function Call
676
+ // st_localTable_t *CalledFunction; //Pointer to hash table, for work with function Call
677
677
678
678
if ((ScannerInt = getToken (CurrentToken )) != SUCCESS ){
679
679
return ScannerInt ;
@@ -791,14 +791,14 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
791
791
//Zrejme bude vracat aj posledny nacitany token ktorym by mal byt EOL
792
792
//takze to treba ceknut
793
793
794
- //expr_main(int context, token_t *parserToken, st_globalTable_t *st_global, string *func_name, st_element_t *Variable);
795
- if ((RecurCallResult = expr_main ( EXPRESION_CONTEXT_ARIGH , CurrentToken , GlobalTable , & FunctionID , Variable )) != SUCCESS ){
796
- return RecurCallResult ;
794
+ //Get token and resolve if pass to expr or deal with function call
795
+ if ((ScannerInt = getToken ( CurrentToken )) != SUCCESS ){
796
+ return ScannerInt ;
797
797
}
798
798
799
- //Check token from expresion
800
- if (CurrentToken -> type != TOK_endOfLine ){
801
- return SYN_ERROR ;
799
+ //Call function which choose between function call and expresion
800
+ if (( RecurCallResult = ResAssignInParser ( CurrentToken , GlobalTable , Variable )) != SUCCESS ){
801
+ return RecurCallResult ;
802
802
}
803
803
804
804
return Stats (CurrentToken , ToCheck , GlobalTable );
@@ -829,66 +829,9 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
829
829
return ScannerInt ;
830
830
}
831
831
832
- //Check for Builtin Function
833
- switch (CurrentToken -> type ){
834
-
835
- //If we get ID we need to check if it`s function call or just expresion
836
- case TOK_identifier :
837
- //Check if it`s function call
838
- if ((CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal )) == NULL ){
839
- //If we don`t find function, check if we find variable in current function
840
- if (st_find_element (GlobalTable , & FunctionID , CurrentToken -> value .stringVal ) == NULL ){
841
- fprintf (stderr , "Prva premenna je hned nedefinovana\n" );
842
- return SEM_ERROR_FUNC ;
843
- }
844
- //fprintf(stderr, "Spracovava expresion\n");
845
- //TODO Call expresion
846
- //expr_main(int context, token_t *parserToken, st_globalTable_t *st_global, string *func_name, st_element_t *Variable);
847
- if ((RecurCallResult = expr_main (EXPRESION_CONTEXT_ARIGH , CurrentToken , GlobalTable , & FunctionID , Variable )) != SUCCESS ){
848
- return RecurCallResult ;
849
- }
850
-
851
- //Check token from expresion
852
- if (CurrentToken -> type != TOK_endOfLine ){
853
- return SYN_ERROR ;
854
- }
855
-
856
- }else {
857
- //Test if function vas declared or defined -> just because of recurcive call of function without declaration
858
- if ((CalledFunction -> declared || CalledFunction -> defined ) == false){
859
- fprintf (stderr , "Rekurzivne volanie funkcie ktora nebola deklarovana\n" );
860
- return SEM_ERROR_FUNC ;
861
- }
862
-
863
- //Function was found check params
864
- if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction )) != SUCCESS ){
865
- return RecurCallResult ;
866
- }
867
- }
868
-
869
- break ;
870
-
871
- //Case built in functions
872
- case KW_length :
873
- case KW_subStr :
874
- case KW_asc :
875
- case KW_chr :
876
- //TODO buildin
877
- break ;
878
-
879
- default :
880
- //TODO Call expresion
881
- //expr_main(int context, token_t *parserToken, st_globalTable_t *st_global, string *func_name, st_element_t *Variable);
882
- if ((RecurCallResult = expr_main (EXPRESION_CONTEXT_ARIGH , CurrentToken , GlobalTable , & FunctionID , Variable )) != SUCCESS ){
883
- return RecurCallResult ;
884
- }
885
-
886
- //Check token from expresion
887
- if (CurrentToken -> type != TOK_endOfLine ){
888
- return SYN_ERROR ;
889
- }
890
- break ;
891
-
832
+ //Call function which choose between function call and expresion
833
+ if ((RecurCallResult = ResAssignInParser (CurrentToken , GlobalTable , Variable )) != SUCCESS ){
834
+ return RecurCallResult ;
892
835
}
893
836
894
837
//TODO Kedy predat riadenie precedencnej analyze?
@@ -972,7 +915,7 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
972
915
//TODO nejake navestia...
973
916
974
917
//Check EOL
975
- if (ScannerInt = getToken (CurrentToken ) != SUCCESS ){
918
+ if (( ScannerInt = getToken (CurrentToken ) ) != SUCCESS ){
976
919
return ScannerInt ;
977
920
}
978
921
if (CurrentToken -> type != TOK_endOfLine ){
@@ -1117,10 +1060,12 @@ int IfStat(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *Global
1117
1060
/**
1118
1061
* @brief: Function to check Function CalledFunction
1119
1062
* Check arguments, types and conversions
1063
+ * ID (<params>) EOL
1120
1064
* @param CurrentToken is pointer to the structure where is current loaded token
1121
1065
* @param CalledFunction is pointer to function in Hash Table, function that is called
1066
+ * @oaram variable is pointer to element in local hashTable, its variable we are assigning in, here to check data type
1122
1067
**/
1123
- int FuncCallCheck (token_t * CurrentToken , st_globalTable_t * GlobalTable , st_localTable_t * CalledFunction ){
1068
+ int FuncCallCheck (token_t * CurrentToken , st_globalTable_t * GlobalTable , st_localTable_t * CalledFunction , st_element_t * Variable ){
1124
1069
//Expect '('
1125
1070
if ((ScannerInt = getToken (CurrentToken )) != SUCCESS ){
1126
1071
return ScannerInt ;
@@ -1272,6 +1217,30 @@ int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_local
1272
1217
pNumber ++ ;
1273
1218
}
1274
1219
}
1220
+
1221
+ //Test of return type of function and type of Variable we are assigning in
1222
+ if (Variable -> el_type != CalledFunction -> func_type ){
1223
+ //If one is string -> no conversions
1224
+ if (Variable -> el_type == st_string || CalledFunction -> func_type == st_string ){
1225
+ fprintf (stderr , "Len jedna z dvojice funkcia/premenna do ktorej sa funkcia priraduje, ma typ string\n" );
1226
+ return SEM_ERROR_COMP ;
1227
+ }
1228
+
1229
+ //If Variable is int -> function is double
1230
+ if (Variable -> el_type == st_integer ){
1231
+ fprintf (stderr , "Navratovy typ funkce dobule prevadzam na int\n" );
1232
+ //TODO konverzia
1233
+ }
1234
+
1235
+ if (Variable -> el_type == st_decimal ){
1236
+ fprintf (stderr , "Navratov typ funkce int prevadzam na float\n" );
1237
+ //TODO konverzia
1238
+ }
1239
+
1240
+ }else {
1241
+ //TODO len prirad
1242
+ }
1243
+
1275
1244
//EOL
1276
1245
if ((ScannerInt = getToken (CurrentToken )) != SUCCESS ){
1277
1246
return ScannerInt ;
@@ -1282,6 +1251,144 @@ int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_local
1282
1251
return SUCCESS ;
1283
1252
}
1284
1253
1254
+
1255
+ /**@brief Function to resolve assignment:
1256
+ * if we need to pass <expresion> to expr.c or if it`s function call
1257
+ * @param CurrentToken is current loaded token
1258
+ * @param GlobalTable is global table of functions
1259
+ * @oaram variable is pointer to element in local hashTable, its variable we are assigning
1260
+ * @return SUCCESS or type of error
1261
+ */
1262
+ int ResAssignInParser (token_t * CurrentToken , st_globalTable_t * GlobalTable , st_element_t * Variable ){
1263
+ int RecurCallResult = -1 ;
1264
+ st_localTable_t * CalledFunction ;
1265
+ //string BuiltinFuncName;
1266
+
1267
+ //Check for Builtin Function
1268
+ switch (CurrentToken -> type ){
1269
+
1270
+ //If we get ID we need to check if it`s function call or just expresion
1271
+ case TOK_identifier :
1272
+ //Check if it`s function call
1273
+ if ((CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal )) == NULL ){
1274
+ //If we don`t find function, check if we find variable in current function
1275
+ if (st_find_element (GlobalTable , & FunctionID , CurrentToken -> value .stringVal ) == NULL ){
1276
+ fprintf (stderr , "Prva premenna je hned nedefinovana\n" );
1277
+ return SEM_ERROR_FUNC ;
1278
+ }
1279
+ //fprintf(stderr, "Spracovava expresion\n");
1280
+
1281
+ //expr_main(int context, token_t *parserToken, st_globalTable_t *st_global, string *func_name, st_element_t *Variable);
1282
+ if ((RecurCallResult = expr_main (EXPRESION_CONTEXT_ARIGH , CurrentToken , GlobalTable , & FunctionID , Variable )) != SUCCESS ){
1283
+ return RecurCallResult ;
1284
+ }
1285
+
1286
+ //Check token from expresion
1287
+ if (CurrentToken -> type != TOK_endOfLine ){
1288
+ return SYN_ERROR ;
1289
+ }
1290
+
1291
+ }else {
1292
+ //Test if function vas declared or defined -> just because of recurcive call of function without declaration
1293
+ if ((CalledFunction -> declared || CalledFunction -> defined ) == false){
1294
+ fprintf (stderr , "Rekurzivne volanie funkcie ktora nebola deklarovana\n" );
1295
+ return SEM_ERROR_FUNC ;
1296
+ }
1297
+
1298
+ //Function was found check params, it checks also EOL
1299
+ if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction , Variable )) != SUCCESS ){
1300
+ return RecurCallResult ;
1301
+ }
1302
+
1303
+ //TODO Skontrolovat navratovy typ mozem to urobit vo FuncCallCheck?
1304
+ }
1305
+
1306
+ break ;
1307
+
1308
+ //Case built in functions
1309
+ case KW_length :
1310
+
1311
+ //Save name to Token
1312
+ if (strCopyConst (CurrentToken -> value .stringVal , "length" )){
1313
+ return INTERNAL_ERROR ;
1314
+ }
1315
+
1316
+ //Find function in HashTable, no need to controle, because builtin function is in hashTable
1317
+ CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal );
1318
+
1319
+ //Check params and return type, it checks also EOL
1320
+ if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction , Variable )) != SUCCESS ){
1321
+ return RecurCallResult ;
1322
+ }
1323
+ break ;
1324
+
1325
+ case KW_subStr :
1326
+
1327
+ //Save name to Token
1328
+ if (strCopyConst (CurrentToken -> value .stringVal , "substr" )){
1329
+ return INTERNAL_ERROR ;
1330
+ }
1331
+
1332
+ //Find function in HashTable, no need to controle, because builtin function is in hashTable
1333
+ CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal );
1334
+
1335
+ //Check params and return type, it checks also EOL
1336
+ if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction , Variable )) != SUCCESS ){
1337
+ return RecurCallResult ;
1338
+ }
1339
+ break ;
1340
+
1341
+ case KW_asc :
1342
+
1343
+ //Save name to Token
1344
+ if (strCopyConst (CurrentToken -> value .stringVal , "asc" )){
1345
+ return INTERNAL_ERROR ;
1346
+ }
1347
+
1348
+ //Find function in HashTable, no need to controle, because builtin function is in hashTable
1349
+ CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal );
1350
+
1351
+ //Check params and return type, it checks also EOL
1352
+ if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction , Variable )) != SUCCESS ){
1353
+ return RecurCallResult ;
1354
+ }
1355
+ break ;
1356
+
1357
+ case KW_chr :
1358
+
1359
+ //Save name to Token
1360
+ if (strCopyConst (CurrentToken -> value .stringVal , "chr" )){
1361
+ return INTERNAL_ERROR ;
1362
+ }
1363
+
1364
+ //Find function in HashTable, no need to controle, because builtin function is in hashTable
1365
+ CalledFunction = st_find_func (GlobalTable , CurrentToken -> value .stringVal );
1366
+
1367
+ //Check params and return type, it checks also EOL
1368
+ if ((RecurCallResult = FuncCallCheck (CurrentToken , GlobalTable , CalledFunction , Variable )) != SUCCESS ){
1369
+ return RecurCallResult ;
1370
+ }
1371
+
1372
+ break ;
1373
+
1374
+ default :
1375
+ //TODO Call expresion
1376
+ //expr_main(int context, token_t *parserToken, st_globalTable_t *st_global, string *func_name, st_element_t *Variable);
1377
+ if ((RecurCallResult = expr_main (EXPRESION_CONTEXT_ARIGH , CurrentToken , GlobalTable , & FunctionID , Variable )) != SUCCESS ){
1378
+ return RecurCallResult ;
1379
+ }
1380
+
1381
+ //Check token from expresion
1382
+ if (CurrentToken -> type != TOK_endOfLine ){
1383
+ return SYN_ERROR ;
1384
+ }
1385
+ break ;
1386
+ }
1387
+ return SUCCESS ;
1388
+ }
1389
+
1390
+
1391
+
1285
1392
int main (){
1286
1393
int ret = parse ();
1287
1394
printf ("return %d\n" , ret );
0 commit comments