void nt_var_def (token tok) { ///////////////////////////////////////////////////////////////////////RULE6 if (tok -> type == t_var_id) { /* Pripravím si key pre ukladanie */ char * key = createKey ("V", tok -> val_str); /* Rozhodnem sa s ktorou tabulkou idem pracovať */ tNodePtr * currTS = NULL; currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; /* Uložím premennú do odpovedajúcej tabulky symbolov */ saveSymbol (&*currTS, key, tok -> val_str, t_var_id, 0, true); /* Porovnanie terminálov */ match (tok, t_var_id); match (tok, t_colon); /* Priradenie typu práve uloženej premennej */ /* searchType=1 hovorí, že ide o premennú */ nt_type (tok, key); /* Porovnanie zvyšného terminálu */ match (tok, t_semicolon); //Sem pride instrukcia tData currentVar = searchSymbol(&*currTS, key)->data; if (localIL==NULL){ insertInst (&IL, I_VAR, currentVar, NULL, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_VAR s ukazatelom %u do IL %u\n", ¤tVar,&IL); } else{ insertInst (localIL, I_VAR, currentVar, NULL, NULL); printf("LOCAL\n");printf("Vlozil som instrukciu I_VAR s ukazatelom %u do IL %u\n", ¤tVar,localIL); } free (key); } else { printf ("nt_var_def\n"); errorHandler (errSyn); } }
bool saveSymbol (tNodePtr * currTS, char * key, char * name, int type, int argCount, bool errOnRedef) { /* Otestujem či už náhodou neexistuje daný symbol */ if (searchSymbol (currTS, key) == 0) { /* Alokujem si miesto pre data pre nový symbol v tabulke */ tData newsymbol = malloc (sizeof (struct tData)); /* Ak alokácia zlyhala alebo predaný name či key je NULL volám chybu 99 */ /* Ak alokácia prebehla bez problémov a názvy sú neprázdne, pokračujem */ if (newsymbol != NULL || key != NULL || name != NULL) { /*Do dátovej štruktúry uložím dáta predané funkcii*/ newsymbol -> name = name; newsymbol -> type = type; newsymbol -> nextArg = NULL; newsymbol -> argCount = argCount; newsymbol -> localTSadr = NULL; newsymbol -> localILadr = NULL; //newsymbol -> content.integer = rand() % 100; /*Uložím do aktuálnej tabulky nový symbol ktorý som si práve pripravil*/ /*Ak vloženie zlyhá, vraciam internú chybu errInt(99) */ if (insertSymbol (currTS, key, newsymbol) == 0) { printf ("jkebol saveSymbol\n"); errorHandler (errInt); } return true; } else { printf ("jebol saveSymbol 2\n"); errorHandler (errInt); } } else if (errOnRedef == true) errorHandler (errSemDef); return false; }
void Kernel::launchInitPhase(void) { LibraryHandle handle = loadLibrary(m_initFilename); if( ! handle ) throw std::system_error(EACCES, std::system_category(), m_initFilename + m_moduleExtension + ", " + libraryError() ); IKernel::InitFunction fct = (IKernel::InitFunction) searchSymbol(handle, "init"); if( ! fct ) throw std::system_error(ENOEXEC, std::system_category(), m_initFilename + m_moduleExtension + ", " + libraryError() ); fct( *this); closeLibrary(handle); }
int nt_assign (token tok) { if (tok->type == t_expr_int || tok->type == t_expr_str || tok->type == t_expr_dou || tok->type == t_fun_id || tok->type == t_var_id || tok->type == t_l_parrent) { ///////////////////////////////////////////////////////////////////////RULE23 if (tok->type == t_expr_int || tok->type == t_expr_str || tok->type == t_expr_dou || tok->type == t_var_id || tok->type == t_l_parrent) { //printf("TYPE JE %d\n",tok->type); //printf("--idem priradenie\n"); //printf("--idem skusit precedenceParser s tokentom %d \n",tok->type); //printf("localTS je momentalne %d\n",&*localTS ); int kokot = precedenceParser(NULL); //printf("--precedenceParser presiel a vratil: %d\n",kokot ); //printf("--stav tok je %d\n",tok->type ); //int typ=tok->type; //match(tok,tok->type); terminalis(kokot,NULL); switch (kokot) { case 41: return sym_var_int;break; case 42: return sym_var_str;break; case 43: return sym_var_rea;break; case 44: return sym_var_boo;break; default: errorHandler(errSemDef);return 0; } } ////////////////////////////////////////////////////////////////////////RULE24 else { char * key = createKey ("F",tok->val_str); tNodePtr hledam = searchSymbol(&rootTS, key); //printf("IDEM HLADAT %s\n",key ); if (hledam==0) { printf("Hladal som key %s\n",key ); errorHandler(errSemDef); } //printf("\n funkcia %s ma %d argumentu \n",key,hledam->data->argCount ); match(tok,t_fun_id); match(tok,t_l_parrent); //printf("idem vytvorit pole\n"); tContent **contentArr=malloc(sizeof(struct tContent*)*100); printf("vytvoril som doublepole %u\n____________________\n",&contentArr); //globalArr=contentArr; contentArr[0]=&hledam->data->nextArg->data->content; //elegeblege[0]=; printf("KOKOOOOOOOOOOOOOOOOT %d\n",contentArr[0]->integer); /*for (int i = 0; i<100; i++) { contentArr[i]=malloc(sizeof(struct tContent)); }*/ //struct tContent *(*contentArrPtr)[] = &contentArr; nt_term_list(tok, key, contentArr); pocetArg = 0; //printf("factorial ma content %d\n", hledam->data->content.integer); //termy su overene idem ich nahradit printf("\nhledam->data->argCount je %d\n",hledam->data->argCount); j=0; //povodne argsRead=0; match(tok,t_r_parrent); //treba ale este predat premenne if (localIL==NULL) { insertInst (&IL, I_FCE, hledam->data, contentArr, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_FCE s ukazatelom %u do IL %u\n", &hledam->data,&IL); } else { insertInst (localIL, I_FCE, hledam->data, contentArr, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_FCE s ukazatelom %u do IL %u\n", &hledam->data,localIL); } free(key); return hledam->data->type; } } else { printf("syn error in nt_assign\n"); errorHandler(errSyn); } return -1; }
void nt_stmt (token tok) { if (tok->type == t_if || tok->type == t_begin || tok->type == t_write || tok->type == t_while || tok->type == t_readln || tok->type == t_var_id ) { tNodePtr hledam = NULL; tNodePtr *currTS = NULL; char * key = NULL; int precedenceResult = 0; switch (tok->type) { ////////////////////////////////////////////////////////////////////////////////RULE17 case 8: nt_body (tok); break; ////////////////////////////////////////////////////////////////////////////////RULE18 case 20: key = createKey ("V", tok->val_str); currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; hledam = searchSymbol (&*currTS, key); if (hledam == 0 && searchGlobalOnly == false) hledam = searchSymbol(&rootTS, key); if (hledam!=0) if (hledam->data->type==t_var_id || (hledam->data->type >= 1 && hledam->data->type <= 4)) { //printf("\n\nSnazim sa priradit do premennej typu %d\n\n",hledam->data->type); match (tok,t_var_id); } else { printf ("nedefnovana premenna\n"); errorHandler (errSemDef); return; } match (tok,t_assign); //SEMANTICKA KONTROLA int semControlVar = nt_assign (tok); //printf("semkontrol je %d\n",semControlVar ); //printf("skontroloval som\n"); if (semControlVar != hledam->data->type+4 && semControlVar != hledam->data->type+12 && semControlVar != hledam->data->type) { printf("nesedeli typy\n"); errorHandler (errSemTypArg); } //else printf("ok\n"); //Tu je rozhodovanie ktoru instrukciu mam zavolat podla typu precedencie int intype=0; switch (semControlVar) { case 1: intype=I_ASGNI;break; case 3: intype=I_ASGNS;break; case 2: intype=I_ASGNR;break; case 4: intype=I_ASGNB;break; } if (localIL==NULL) insertInst (&IL, intype, NULL, NULL, &hledam); else insertInst (&IL, intype, NULL, NULL, &hledam); break; free (key); break; ////////////////////////////////////////////////////////////////////////////////RULE19 case 13: match (tok,t_if); //printf("local ts je %d\n",&localTS ); precedenceResult = precedenceParser(); if (precedenceResult!=t_expr_boo)//docasne sem dam integer lebo bool este neni hotovy v precedenci ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ errorHandler(errSemTypArg); //Volanie instrukcie na bool spravil filip terminalis(precedenceResult,NULL); match (tok,t_then); //Interpret musi tieto dve tabulky potom uvolnit //Tabulka inst pre then tInsList thenIL;//vytvorim novu tabulku tInsList *revert=&*localIL;//odpamatam su aktualnu lokalnu InitList (&thenIL);//inicializujem novu localIL=&thenIL;//nova sa stane aktivnou lokalnou printf("localIL je teraz %u\n",&*localIL ); nt_body (tok);//nt body donej nahadze instrukcie z tela thenu localIL=revert;//obnovi sa povodna lokalna printf("localIL je teraz %u\n",&*localIL ); match (tok,t_else); //Tabulka inst pre else tInsList elseIL; //vytvorim novu tabulku revert=&*localIL;//odpamatam su aktualnu lokalnu InitList (&elseIL);//inicializujem novu localIL=&elseIL; //nova sa stane aktivnou lokalnou printf("localIL je teraz %u\n",&*localIL ); nt_body (tok); //nt body donej nahadze instrukcie z tela elsu localIL=revert; //obnovi sa povodna lokalna printf("localIL je teraz %u\n",&*localIL ); //volanie instrukcie if (localIL==NULL) insertInst (&IL, I_IF, &thenIL, &elseIL, NULL); else insertInst (&*localIL, I_IF, &thenIL, &elseIL, NULL); break; ////////////////////////////////////////////////////////////////////////////////RULE20 case 16: match (tok,t_while); precedenceResult = precedenceParser(); if (precedenceResult!=t_expr_boo)//docasne sem dam integer lebo bool este neni hotovy v precedenci ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ errorHandler(errSyn); terminalis(precedenceResult,NULL); match (tok,t_do); nt_body (tok); break; ////////////////////////////////////////////////////////////////////////////////RULE21 case 18: match (tok,t_readln); match (tok,t_l_parrent); //Kontrola či premenná už je v tabulke key = createKey ("V", tok->val_str); currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; hledam = searchSymbol (&*currTS, key); if (hledam == 0 && searchGlobalOnly == false) hledam = searchSymbol(&rootTS, key); if (hledam!=0) if (hledam->data->type==t_var_id || (hledam->data->type >= 1 && hledam->data->type <= 4))//toto reaguje len na typove premenne { match (tok,t_var_id); } else { printf ("nedefnovana premenna pre readln\n"); errorHandler (errSemDef); return; } //SEMANTICKA KONTROLA if (hledam->data->type < 1 || hledam->data->type > 3) { printf("tuna nemoze byt bool\n"); errorHandler (errSemTypArg); } match (tok,t_r_parrent); free (key); break; ////////////////////////////////////////////////////////////////////////////////RULE22 case 19: match (tok,t_write); match (tok,t_l_parrent); tContent **doublePointer=malloc(sizeof(struct tContent*)*100); nt_term_list (tok, "Fwrite", doublePointer); /*bolo tu povodne argc*/ pocetArg = 0; match (tok,t_r_parrent); break; } } else { printf("syntax error in nt_stmt\n"); errorHandler(errSyn); } }
void nt_fun_body (token tok, bool nextMustBeBody, char * key) { /* Zoznam terminálov na ktoré funkcia reaguje */ if (tok -> type == t_forward || tok -> type == t_begin || tok -> type == t_var ) { ///////////////////////////////////////////////////////////////////RULE9 +++treba zaistit ze neskor bude definovana /* Ak som miesto tela funkcie dostal kľúčové slovo FORWARD a zároveň ** ** nextMustBeBody bolo false, čo znamená že forward deklarácia k tej- ** ** to funkcii ešte nebola, takže može byť teraz */ if (tok -> type == t_forward && nextMustBeBody == false) { /* Zvýšim globálne počítadlo fwd deklarácií čakajúcich na telo */ fwdDeclarations++; /* K typu aktuálnej funkie pripočítam 4, čím sa dostane do stavu ** ** 9 až 12 čo znamená že ide o funkciu ktorá má forward deklará- ** ** cie a čaká na riadne telo */ searchSymbol (&rootTS, key) -> data -> type += 4; /* forward */ match (tok, t_forward); } //////////////////////////////////////////////////////////////////RULE10 /* Else znamená že prišlo VAR alebo BEGIN */ else { // Potrebujem tabulku instrukcii tInsList newLocalIL; tInsList *newLocalILptr=&newLocalIL; InitList (&newLocalIL); /* Do "hledam" si uložím symbol aktuálnej funkcie */ tNodePtr hledam = searchSymbol(&rootTS, key); hledam->data->localILadr=newLocalILptr; localIL=newLocalILptr; printf("localIL je teraz %u\n",&*localIL ); /* Ak už teraz musí prísť telo, čiže ak aktuálna funkcia už mala ** ** forward deklaráciu, tak to znamená že tým že mi sem neprišlo ** ** forward, môžem túto funkciu zmeniť z typu 9 až 12 na 13 až 16, ** ** čo znamená že funkcia má aj typ aj telo. Následne znížim glo- ** ** bálne počátadlo dopredne deklarovaných funkcií fwdDeclarations */ if (nextMustBeBody == true) { hledam -> data -> type += 4; fwdDeclarations--; } /* Globálnu premennú searchGlobalOnly nastavím na false, pretože ** ** pôjdem spracovávať telo funkcie ktoré bude využívať lokálnu ** ** tabuľku symbolov */ searchGlobalOnly = false; /* Pravidlo pre deklarácie lokálnych premenných a samotné telo */ nt_var_def_block (tok); nt_body (tok); /* Akonáhle sa dostanem von z tela funkcie, opäť ma zaujímajú len ** ** globálne premenné a funkcie */ searchGlobalOnly = true; /* Do symbolu funkcie uložím ukazateľ na jej lokálnu tabuľku sym- ** ** bolov a vynuluje globálny ukazateľ */ //(hledam -> data -> localTSadr) = localTS; localIL = NULL; //disposeTable(&localTS); } } /* Prípad keď prišiel nesprávny terminál */ else { printf ("synerror in nt_fun_def_list\n"); errorHandler (errSyn); } }
void nt_fun_def_list (token tok) { if (tok->type == t_function || tok->type == t_begin) { /* Premenná ktorá rozhoduje či môže prísť forward deklarácia alebo nie*/ bool nextMustBeBody = false; char * rememberVarName = NULL; ///////////////////////////////////////////////////////////////////RULE7 if (tok -> type == t_function) { match (tok, t_function); char * key = createKey ("F", tok -> val_str); /* Ak podmienka je true, znamená to, že funkcia ešte nebola de- */ /* klarovaná a teda jej deklarácia prebehla v poriadku. Dodatočne */ /* sa k nej dodeklarovala lokálna premenná s rovnakým menom v lo- */ /* kálnej tabulke */ if (saveSymbol (&rootTS, key, tok -> val_str, t_fun_id, 0, false) == true) { //Kedze vytvaram novu funkciu potrebujem novu lokalnu tabulku tNodePtr newLocalTS; init(&newLocalTS); //musel som tam nieco vloyit lebo nad prazdnou to blblo saveSymbol(&newLocalTS, "placeholder", "testname", 0,0,true); localTS=newLocalTS; //Do funkcie ulozim jej lokalnuTS //printf("localTS je teraz %d\n",&*localTS ); searchSymbol(&rootTS, key)->data->localTSadr=newLocalTS; /*Uloženie lokálnej premennej patriacej k práve uloženej fun.*/ char *funVarKey = createKey ("V", tok -> val_str); rememberVarName = createKey ("V", tok -> val_str); saveSymbol (&localTS, funVarKey, tok->val_str, t_var_id, 0, true); //printf("vnorena kokotina nasla %s\n",searchSymbol(&(searchSymbol(&rootTS, key)->data->localTSadr),funVarKey)->data->name); free (funVarKey); } /* V opačnom prípade ide buď o redeklaráciu alebo doplnenie for- */ /* ward deklarácie */ else { localTS = searchSymbol (&rootTS, key) -> data -> localTSadr; //printf("kotrola priradil som %d\n",&*localTS ); //printf("Som v inej a nasiel som%s\n",searchSymbol(&localTS, "test")->data->name); /* Ak v rámci programu bola aspoň jedna forward deklarácia, */ /* tak v tejto deklarácií už musí byť telo funkcie, inak by */ /* šlo o redeklaráciu */ if (fwdDeclarations != 0) { nextMustBeBody = true; } /* Ak podmienka nebola splnená, znamená to, že ide o redef. */ else { printf ("Redefinition of function %s\n", tok->val_str); errorHandler (errSemDef); } } match (tok, t_fun_id); match (tok, t_l_parrent); /* Spracovanie parametrov funkcie */ nt_param_list (tok, nextMustBeBody, key); /* Nulujem počítadlo aktuálneho param, lebo skončili parametre */ //bolo tu povodne argsRead //pocetArg = 0; /* Ak ide o prvú delkaráciu funkcie, uložím počet jej argumentov ** ** do jej symbolu do premennej argCount */ if (nextMustBeBody == false) { searchSymbol (&rootTS, key) -> data -> argCount = pocetArg; printf("uloil som %d\n",searchSymbol(&rootTS, key)->data->argCount ); /* Vynulujem počítadlo argumentov lebo som skončil paramlist */ } pocetArg = 0; /* ) : */ match (tok, t_r_parrent); match (tok, t_colon); /* Priradím alebo skontrolujem typ funkcie, viac info v nt_type */ nt_type (tok, key); /* Nakoniec priradím správny typ rovnomennej lokálnej premennej ** ** rovnomennej funkcie. V case som musel použiť magické čísla, ** ** pretože case nepodporuje konštanty. Čísla 5 až 8 predstavujú ** ** funkciu, ktorá už má priradený typ, ale ešte neprišlo telo. ** ** Čísla 9 až 12 sú pre funkcie ktoré majú zatiaľ len forward de- ** ** klaráciu a čísla 13 až 16 sú pre funkcie ktoré už majú aj typ, ** ** aj riadne telo. Typy idú v poradí INTEGER, REAL, STRING, BOO- ** ** LEAN. */ if (rememberVarName != NULL) switch (searchSymbol (&rootTS, key) -> data -> type) { case 5: case 9: case 13: searchSymbol (&localTS, rememberVarName) -> data -> type = sym_var_int; break; case 6: case 10: case 14: searchSymbol (&localTS, rememberVarName) -> data -> type = sym_var_rea; break; case 7: case 11: case 15: searchSymbol (&localTS, rememberVarName) -> data -> type = sym_var_str; break; case 8: case 12: case 16: searchSymbol (&localTS, rememberVarName) -> data -> type = sym_var_boo; break; default: errorHandler (errSemTypArg); break; } /* ; */ match (tok, t_semicolon); /* Do symbolu funkcie uložím ukazateľ na jej lokálnu tabuľku sym. */ //searchSymbol (&rootTS, key) -> data -> localTSadr = localTS; /* Spracovanie tela funkcie. nextMustBeBody mi hovorí či už musí ** ** prísť telo funkcie (true) alebo môže byť forward (false) ** ** premenná key sa predáva aby som vedel názov aktuálne funkcie */ nt_fun_body (tok, nextMustBeBody, key); /* Vynulujem globálnu TS, pre istotu */ localTS = NULL; /* Uvoľnenie alokovaných stringov */ free (key); free (rememberVarName); /* ; */ match (tok, t_semicolon); /* Rekurzívne volanie tejto istej funkcie pre prípad, že by v kó- ** ** de bolo viac deklarácií funkcií po sebe */ nt_fun_def_list(tok); } ///////////////////////////////////////////////////////////////////RULE8 else /* Eps. pravidlo - Deklarácie užívateľských funkcií niesú povinné */ return; } /* Prípad keď prišiel nesprávny terminál */ else { printf ("nt_fun_def_list\n"); errorHandler (errSyn); } }
void nt_type (token tok, char * key) { if (tok->type == t_integer || tok->type == t_real || tok->type == t_string || tok->type == t_boolean) { tNodePtr hledam; if (searchGlobalOnly==true) { hledam=searchSymbol(&rootTS, key); } else { hledam=searchSymbol(&localTS, key); } if (hledam==0) errorHandler(errSemDef); else switch (tok->type) { //////////////////////////////////////////////////////////RULE33 case 26: match(tok,t_integer); if (hledam->data->type==t_var_id) hledam->data->type=1; else if (hledam->data->type==t_fun_id) hledam->data->type=5; break; //////////////////////////////////////////////////////////RULE34 case 27: match(tok,t_real); if (hledam->data->type==t_var_id) hledam->data->type=2; else if (hledam->data->type==t_fun_id) hledam->data->type=6; break; //////////////////////////////////////////////////////////RULE35 case 28: match(tok,t_string); if (hledam->data->type==t_var_id) hledam->data->type=3; else if (hledam->data->type==t_fun_id) hledam->data->type=7; break; //////////////////////////////////////////////////////////RULE36 case 29: match(tok,t_boolean); if (hledam->data->type==t_var_id) hledam->data->type=4; else if (hledam->data->type==t_fun_id) hledam->data->type=8; break; } } else { printf("syntax error in nt_type\n"); errorHandler(errSyn); } }
void nt_param (token tok, bool testOnly, char * currentFunctionKey) { if (tok->type == t_var_id) { pocetArg++; //pvoodne argsRead++; char *key = createKey("V",tok->val_str); //printf("PRAMETER key je %s\n",key ); //char *key = createKey () ////////////////////////////////////////////SPRACOVANIE Parametru if (testOnly==false)//ak sa idu nove ukladat { saveSymbol (&localTS, key, tok->val_str, t_var_id, pocetArg /*povodne argsRead*/, true); //printf("&&&&&&&&&&&&&&&&&&& ulozil som %s s argcountom %d\n",key,argsRead ); match (tok, t_var_id); match (tok, t_colon); bool temp=searchGlobalOnly; searchGlobalOnly=false; nt_type(tok, key); searchGlobalOnly=temp; //pocetArg++; //tuna ide ulozenie ukazatela na parameter dalsi tNodePtr currentFunction = searchSymbol(&rootTS, currentFunctionKey); if (currentFunction->data->nextArg==NULL) currentFunction->data->nextArg=searchSymbol(&localTS, key); else { while (currentFunction->data->nextArg!=NULL) { currentFunction=currentFunction->data->nextArg; } currentFunction->data->nextArg=searchSymbol(&localTS, key); } } else // ak sa len kontroluju z minulej fwd hlavicky { tNodePtr hledam=searchSymbol(&localTS, key); if (hledam==0) { printf("Error hlavicky sa lisia ty kokot\n"); errorHandler(errSemTypArg); } else { match (tok, t_var_id); match (tok, t_colon); match (tok, hledam->data->type+25); } } ///////////////////////////////////////////////Premenna ulozena //searchSymbol(&rootTS, currentFunctionKey)->data->nextArg=key; //printf("podarilo sa \n"); free(key); } else { printf("synerror in nt_param\n"); errorHandler(errSyn); } }
void nt_term (token tok, char *currentFunctionKey, tContent **contentArr) { if (tok->type == t_var_id || tok->type == t_expr_int || tok->type == t_expr_dou || tok->type == t_expr_str) { int comparison1; int comparison2; if (tok->type == t_var_id) { //printf("prislo varid\n____________________\n"); pocetArg++; //povodne argsRead++; //printf("\n---IDEM KONTROLOVAT %d. PARAMETER vo funkcii %s\n",argsRead, currentFunctionKey); char * key = createKey("V",tok->val_str); //printf("premenna ktoru idem hladat je %s\n",key ); //tNodePtr reload = searchSymbol (&rootTS,currentFunctionKey)->data->localTSadr; //printf("%s je key\n",key ); tNodePtr hledam = NULL; if (searchGlobalOnly==1) hledam=(searchSymbol(&rootTS, key)); else { hledam=(searchSymbol(&localTS, key)); if (hledam==0) hledam=(searchSymbol(&rootTS, key)); } if (hledam!=0) { comparison1 = hledam->data->type; //printf("\n--overene %s ", hledam->data->name ); printf("varid\n"); contentArr[j]=&hledam->data->content; //printf("uloxzil som ukayatel s hodnotou %d\n",contentArr[j]->integer); j++; //printf("COMPARISON1 je %d\n",comparison1 ); }else errorHandler(99); if (strcmp(currentFunctionKey, "Fwrite") !=0 && strcmp(currentFunctionKey, "Fcopy") !=0 && strcmp(currentFunctionKey, "Flength")!=0 && strcmp(currentFunctionKey, "Fsort") !=0 && strcmp(currentFunctionKey, "Ffind") !=0) { //teraz potrebujem najst argument s cicslom argsread tNodePtr hledam2=searchSymbol(&rootTS, currentFunctionKey); if (hledam2 == 0) errorHandler(errSemTypArg); tNodePtr currentFunction = searchSymbol(&rootTS, currentFunctionKey); for (int i = 1; i</*pvoodne argsRead*/pocetArg;i++) { currentFunction=currentFunction->data->nextArg; } //printf("som na parametri cislo %d a mal by vyhovovat %s\n",argsRead,currentFunction->data->nextArg->data->name); comparison2 = currentFunction->data->nextArg->data->type; if (comparison1!=comparison2) { printf("copmarizony sa dojebali %d!=%d %s\n",comparison1,comparison2,currentFunction->data->nextArg->data->name); errorHandler(errSemTypArg); } else {//idem ulozit content pre volanie } } else //printf("---Write kontrola preskocena\n"); if (comparison1<1 || comparison1>4) errorHandler(errSemTypArg); //printf("-----------------------------------mal som dostat %d aa bolo %d\n", comparison1,comparison2); //printf("hledam dva ma v sebe adresu name %d\n",hledam2->data->localTSadr ); match (tok, t_var_id); //musi sa tu skontrolovat typ vyrazu free(key); } else { pocetArg++; comparison1=tok->type; printf("comparison 1 je %d\n",comparison1 ); if (strcmp(currentFunctionKey, "Fwrite") !=0 && strcmp(currentFunctionKey, "Fcopy") !=0 && strcmp(currentFunctionKey, "Flength")!=0 && strcmp(currentFunctionKey, "Fsort") !=0 && strcmp(currentFunctionKey, "Ffind") !=0) { //teraz potrebujem najst argument s cicslom argsread tNodePtr hledam2=searchSymbol(&rootTS, currentFunctionKey); if (hledam2 == 0) errorHandler(errSemTypArg); tNodePtr currentFunction = searchSymbol(&rootTS, currentFunctionKey); for (int i = 1; i</*pvoodne argsRead*/pocetArg;i++) { currentFunction=currentFunction->data->nextArg; } //printf("som na parametri cislo %d a mal by vyhovovat %s\n",argsRead,currentFunction->data->nextArg->data->name); comparison2 = currentFunction->data->nextArg->data->type; if (comparison1!=comparison2) { printf("copmarizony sa dojebali %d!=%d %s\n",comparison1,comparison2,currentFunction->data->nextArg->data->name); errorHandler(errSemTypArg); } else {//idem ulozit content pre volanie } } else //printf("---Write kontrola preskocena\n"); if (comparison1<41 || comparison1>44) { printf("zly typ\n"); errorHandler(errSemTypArg); } //printf("ELSE\n"); //printf("\nTYP JE TU %d a currentFunctionKey je %s\n",tok->type,currentFunctionKey ); //printf("tu som\n"); match (tok, tok->type); //printf("couldnt may=tch\n"); } } else { printf("syn error in nt_term\n"); errorHandler(errSyn); } }
int zpracuj(token tok, tOpData *column) { // zjisteni typu tokenu, nastaveni inxdexu //printf("zpracovavam\n"); char *key; switch(tok->type) { case 31: column->element = PLUS; break; case 32: column->element = MINUS; break; case 33: column->element = MUL; break; case 34: column->element = DIV; break; case 4: column->element = LEFT; break; case 5: column->element = RIGHT; break; case 35: column->element = LESS; break; case 36: column->element = MORE; break; case 37: column->element = LESSEQUAL; break; case 38: column->element = MOREEQUAL; break; case 39: column->element = EQUAL; break; case 40: column->element = NONEQUAL; break; case 3: // strednik case 14: // then case 17: // do column->element = DOLAR; break; case 20: column->element = ID; if((key = malloc(sizeof(char)*(strlen(tok->val_str) + 1))) == NULL) { errorHandler(errInt); return -1; } strcat(key, "V"); strcat(key, tok->val_str); if((searchSymbol(&localTS, key)) != 0) printf("Nasel jsem v lokalni tabulce symbolu.\n"); else { if((searchSymbol(&rootTS, key)) != 0) printf("Nasel jsem v globalni tabulce symbolu.\n"); else { printf("Promenna nebyla nalezena.\n"); return -1; } } case 41: // integer, bool case 42: case 43: column->element = ID; if((column->symbol = malloc(sizeof(struct tData))) != NULL) { if(tok->type == t_expr_int) column->symbol->type = tok->type; if(tok->type == t_expr_dou) column->symbol->type = tok->type; if(tok->type == t_expr_str) column->symbol->type = tok->type; } break; default: errorHandler(errSyn); return -1; } return 0; }
/*Writes coresponing code to file, or throws appropriate error */ void processOpcode( int code, int * steer, char * pArg1, char * pArg2, char * pArg3, char * pArg4) { int output = 0; int DR = -1; int SR = -1; int SR1 = -1; int SR2 = -1; int BR = -1; int num = -1; Symbol* temp = NULL; if(code <= 15 && code >= 0) output = code<<12; switch (code) { case 0: /* BR if steering is 1-7 and NOP if 0 */ if(*pArg2 != '\0' || *pArg3 != '\0' || *pArg4 !='\0'){ printf("Error: invalid argument\n"); closeFiles(); exit(4); } if(*steer==0 && *pArg1 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } if(*steer != 0) { /* br with nzp bits specified by steering */ output += *steer<<9; temp = searchSymbol(pArg1); if(temp == NULL) /*Label not found in symbol tabel */ { printf("Error: Unidentified Label\n"); closeFiles(); exit(1); } num = genOffset(temp->location,9); /*genOffset will throw out of bounds error if necisarry */ output += (num&511); } break; case 1: /*ADD */ DR = RegNum(pArg1); SR1 = RegNum(pArg2); /*Too many arguments, or invalid */ if(*pArg4 != '\0'||SR1 == -1 || DR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (DR<<9)+(SR1<<6); /*Common to all implementations */ SR2=RegNum(pArg3); if(SR2 == -1) /*Immediate Value */ { num = toNum(pArg3); if(num>15 || num < -16) /*5 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output+= (1<<5) + (num&31); } else /*Register value */ { output+=SR2; } break; case 2: /*LDB*/ DR = RegNum(pArg1); BR = RegNum(pArg2); if(*pArg4 != '\0'|| BR == -1 || DR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (DR<<9)+(BR<<6); num = toNum(pArg3); if(num>31 || num < -32) /* 6 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output += (num&63); break; case 3: /*STB*/ SR = RegNum(pArg1); BR = RegNum(pArg2); if(*pArg4 != '\0'|| BR == -1 || SR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (SR<<9)+(BR<<6); num = toNum(pArg3); if(num>31 || num < -32) /* 6 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output += (num&63); break; case 4: /* JSR & JSRR */ if(*pArg2 != '\0' || *pArg3 != '\0' || *pArg4 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += *steer << 11; if(*steer == 1) { /* JSR */ temp = searchSymbol(pArg1); if(temp == NULL) { /*Label not found in symbol tabel */ printf("Error: Unidentified Label\n"); closeFiles(); exit(1); } num = genOffset(temp->location,11); /*genOffset will throw out of bounds error if necisarry */ output += (num&2047); } else { /* JSRR */ BR = RegNum(pArg1); output += BR<<6; } break; case 5: /*AND */ DR = RegNum(pArg1); SR1 = RegNum(pArg2); /*Too many arguments, or invalid */ if(*pArg4 != '\0'||SR1 == -1 || DR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (DR<<9)+(SR1<<6); /*Common to all implementations */ SR2=RegNum(pArg3); if(SR2 == -1) /*Immediate Value */ { num = toNum(pArg3); if(num>15 || num < -16) /*5 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output+= (1<<5) + (num & 31); } else /*Register value */ { output+=SR2; } break; case 6: /*LDW*/ DR = RegNum(pArg1); BR = RegNum(pArg2); if(*pArg4 != '\0'|| BR == -1 || DR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (DR<<9)+(BR<<6); num = toNum(pArg3); if(num>31 || num < -32) /* 6 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output += (num&63); break; case 7: /*STW*/ SR = RegNum(pArg1); BR = RegNum(pArg2); if(*pArg4 != '\0'|| BR == -1 || SR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (SR<<9)+(BR<<6); num = toNum(pArg3); if(num>31 || num < -32) /* 6 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output += (num&63); break; case 8: /* RTI */ /* do nothing: bits 11-0 all 0 */ break; case 9: /*XOR and NOT */ DR = RegNum(pArg1); SR1 = RegNum(pArg2); /*Too many arguments, or invalid */ if(*pArg4 != '\0'||SR1 == -1 || DR == -1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += (DR<<9)+(SR1<<6); /*Common to all implementations */ if(*steer == 0) /*XOR*/ { SR2=RegNum(pArg3); if(SR2 == -1) /*Immediate Value */ { num = toNum(pArg3); if(num>15 || num < -16) /*5 Bits */ { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } output+= (1<<5) + (num&31); } else /*Register value */ { output+=SR2; } } else /*NOT function */ { if(*pArg3 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output += 63; } break; /*Opcodes 10 and 11 are invalid */ case 12: /*JMP */ if(*pArg2 != '\0' ||*pArg3 != '\0' || *pArg4 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } if(*steer == 0) { SR1 = RegNum(pArg1); if(SR1==-1) /*Must be valid register */ { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output+=SR1<<6; } else /*RET */ { if(*pArg1 != '\0') /*Should not have anything here */ { printf("Error: invalid argument\n"); closeFiles(); exit(4); } output+=7<<6; } break; case 13: /*Shift */ DR = RegNum(pArg1); SR1 = RegNum(pArg2); num = toNum(pArg3); if(*pArg4 != '\0' || DR==-1||SR1==-1) { printf("Error: invalid argument\n"); closeFiles(); exit(4); } if(num>15||num<0) { printf("Error: Immediate Value Out of Bounds\n"); closeFiles(); exit(3); } /*Steering integer also acts as steering bits */ output += (DR<<9)+(SR1<<6)+((*steer)<<4)+num; break; case 14: /*LEA */ DR = RegNum(pArg1); temp = searchSymbol(pArg2); if(temp == NULL) /*Label not found in symbol tabel */ { printf("Error: Unidentified Label\n"); closeFiles(); exit(1); } if(DR==-1 || *pArg3 != '\0' || *pArg4 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } num = genOffset(temp->location,9); /*genOffset will throw out of bounds error if necisarry */ output+=(DR<<9)+(num&511); break; case 15: /*TRAP */ /*Must be hex */ if(*pArg2 != '\0' || *pArg3 != '\0' || *pArg4 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } if(*steer == 1) /*HALT */ { if(*pArg1 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } num= 37; } else { if(*pArg1 != 'x') /*Must be a hex value */ { printf("Error: TRAP must be a hex value\n"); closeFiles(); exit(3); } num = toNum(pArg1); if(num>255||num<0) /*>0 and no more than 8 digits */ { printf("Error: TRAP Value Out of Bounds\n"); closeFiles(); exit(3); } } output+=num; break; case 16: /*.FILL */ if(*pArg2 != '\0' || *pArg3 != '\0' || *pArg4 != '\0') { printf("Error: invalid argument\n"); closeFiles(); exit(4); } num = toNum(pArg1); if(num>65535|| num< -32768) /*Can be signed or unsigned */ { printf("Error: TRAP Value Out of Bounds\n"); closeFiles(); exit(3); } output = num; break; default: printf("Error: invalid opcode\n"); closeFiles(); exit(2); } fprintf( outfile, "0x%0.4X\n", output ); /*Prints out formated Hex version of code */ }
/*Throws error if invalid label */ void addLabel(char** label) { int size; int k; char* labelLoc; if(numSymbols>=MAX_SYMBOLS) { printf("Error: Too many symbols\n",label); closeFiles(); exit(4); } size = strlen(*label); if(size > 20) /*Oversized Label */ { printf("Error: The Label %s is too long; Must be less than 20 characters\n",label); closeFiles(); exit(4); } if(searchSymbol(*label) != NULL) /*Label is opcode */ { printf("Error: The Label %s is used multiple times\n",label); closeFiles(); exit(4); } for(k = 0; k<numIllegal;k++) /*Label is illegal */ { if(strcmp(*label, illegalLabel[k])==0) { printf("Error: The Label %s is an illegal label\n",label); closeFiles(); exit(4); } } if(**label == 'x' || !isalpha(**label)) /*First letter of label not a letter, or is x */ { if(strcmp(*label, illegalLabel[k])==0) { printf("Error: The Label %s is an illegal label\n",label); closeFiles(); exit(4); } } for(k=1; k<size;k++) /*Checks for non-alphanumeric label */ { if(!isalnum(*(*label + k))) { printf("Error: The Label %s is an illegal label\n",label); closeFiles(); exit(4); } } if(RegNum(*label)!= -1) { printf("Error: The Label %s is a register name\n",label); closeFiles(); exit(4); } labelLoc=(char*)malloc((size+1)*sizeof(char)); /*Allocate memory for label */ strcpy(labelLoc,*label); table[numSymbols].label=labelLoc; /*Add label to symbol tabel */ table[numSymbols].location=PC; numSymbols++; }