Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit ca65893

Browse files
committed
Kontrola navratovych typov, kontrola builtin
1 parent 30b4e7d commit ca65893

File tree

4 files changed

+202
-70
lines changed

4 files changed

+202
-70
lines changed

parser.c

+176-69
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
673673
int RecurCallResult = -1;
674674
st_element_t *Variable; //To save pointer on variable in hashTable
675675
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
677677

678678
if ((ScannerInt = getToken(CurrentToken)) != SUCCESS){
679679
return ScannerInt;
@@ -791,14 +791,14 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
791791
//Zrejme bude vracat aj posledny nacitany token ktorym by mal byt EOL
792792
//takze to treba ceknut
793793

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;
797797
}
798798

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;
802802
}
803803

804804
return Stats(CurrentToken, ToCheck, GlobalTable);
@@ -829,66 +829,9 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
829829
return ScannerInt;
830830
}
831831

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;
892835
}
893836

894837
//TODO Kedy predat riadenie precedencnej analyze?
@@ -972,7 +915,7 @@ int Stats(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *GlobalT
972915
//TODO nejake navestia...
973916

974917
//Check EOL
975-
if (ScannerInt = getToken(CurrentToken) != SUCCESS){
918+
if ((ScannerInt = getToken(CurrentToken)) != SUCCESS){
976919
return ScannerInt;
977920
}
978921
if (CurrentToken->type != TOK_endOfLine){
@@ -1117,10 +1060,12 @@ int IfStat(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *Global
11171060
/**
11181061
* @brief: Function to check Function CalledFunction
11191062
* Check arguments, types and conversions
1063+
* ID (<params>) EOL
11201064
* @param CurrentToken is pointer to the structure where is current loaded token
11211065
* @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
11221067
**/
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){
11241069
//Expect '('
11251070
if ((ScannerInt = getToken(CurrentToken)) != SUCCESS){
11261071
return ScannerInt;
@@ -1272,6 +1217,30 @@ int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_local
12721217
pNumber++;
12731218
}
12741219
}
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+
12751244
//EOL
12761245
if ((ScannerInt = getToken(CurrentToken)) != SUCCESS){
12771246
return ScannerInt;
@@ -1282,6 +1251,144 @@ int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_local
12821251
return SUCCESS;
12831252
}
12841253

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+
12851392
int main(){
12861393
int ret = parse();
12871394
printf("return %d\n", ret);

parser.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ int IfStat(token_t *CurrentToken, struct check ToCheck, st_globalTable_t *Global
9494
* @param CurrentToken is pointer to the structure where is current loaded token
9595
* @param CalledFunction is pointer to function in Hash Table, function that is called
9696
**/
97-
int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_localTable_t *CalledFunction);
97+
int FuncCallCheck(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_localTable_t *CalledFunction, st_element_t *Variable);
98+
99+
/**@brief Function to resolve assignment:
100+
* if we need to pass <expresion> to expr.c or if it`s function call
101+
* @param CurrentToken is current loaded token
102+
* @param GlobalTable is global table of functions
103+
* @oaram variable is pointer to element in local hashTable, its variable we are assigning
104+
* @return SUCCESS or type of error
105+
*/
106+
int ResAssignInParser(token_t *CurrentToken, st_globalTable_t *GlobalTable, st_element_t *Variable);
98107

99108
#endif

str.c

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//jednoducha knihovna pro praci s nekonecne dlouhymi retezci
2+
//ZOBRANE Z JEDNODUCHEHO INTERPRETA
23
#include <string.h>
34
#include <malloc.h>
45
#include "str.h"
@@ -48,6 +49,20 @@ int strAddChar(string *s1, char c)
4849
return STR_SUCCESS;
4950
}
5051

52+
int strCopyConst(string *s, char *c){
53+
//strClear(s);
54+
int newLength = strlen(c);
55+
if (newLength >= s->allocSize)
56+
{
57+
if ((s->str = (char*) realloc(s->str, newLength + 1)) == NULL)
58+
return STR_ERROR;
59+
s->allocSize = newLength + 1;
60+
}
61+
strcpy(s->str, c);
62+
s->length = newLength;
63+
return STR_SUCCESS;
64+
}
65+
5166
int strCopyString(string *s1, string *s2)
5267
// prekopiruje retezec s2 do s1
5368
{

str.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int strAddChar(string *s1, char c);
1919
int strCopyString(string *s1, string *s2);
2020
int strCmpString(string *s1, string *s2);
2121
int strCmpConstStr(string *s1, char *s2);
22+
int strCopyConst(string *s, char *c);
2223

2324
char *strGetStr(string *s);
2425
int strGetLength(string *s);

0 commit comments

Comments
 (0)