tSymbol* insertBlankConstant(tFunction* function){ tSymbol *symb=mmuMalloc(sizeof(tSymbol)); symb->data=NULL; symb->key.data=NULL; listInsertLast(&(function->constants),symb); return symb; }
/** * Creates a copy of target list. * * The new copy will contain all the elements from the source list in the same * order and will use the same functions as the original list for copying and * freeing elements. * * @param list The target list to copy * @return * NULL if a NULL was sent or a memory allocation failed. * A List containing the same elements with same order as list otherwise. */ List listCopy(List list) { List copy = NULL; if (list != NULL) { copy = listCreate(list->copyElementFunc, list->freeElementFunc); } if ((copy != NULL) && (list->First != NULL)) { ListItem item_to_copy = list->First; while ((item_to_copy != NULL) && (copy != NULL)) { if (listInsertLast(copy, item_to_copy->Element) == LIST_SUCCESS) { if (list->Current == item_to_copy){ int size = GetListLength(copy); listGetFirst(copy); for (int i = 1; i < size; i++) { listGetNext(copy); } } item_to_copy = item_to_copy->Next; } else { listDestroy(copy); copy = NULL; } } } return copy; }
int igenerate(tIlist *L,tInstrType type, void * op1, void * op2, void * op3){ int result = EOK; tInstr i = {type,op1,op2,op3}; result = listInsertLast(L, i); return result; }
tSymbol * functionInsertConstant(tFunction *function,tString data,tKeyword type){ if(function==NULL)return NULL; tSymbol *symb=mmuMalloc(sizeof(tSymbol)); if (symb == NULL) return NULL; symb->data=mmuMalloc(sizeof(tSymbolData)); if (symb->data == NULL) return NULL; switch(type){ case LEX_STRING:{ symb->data->type = DT_STRING; if (strCopyString(&data,&(symb->data->data.sData)) != ERROR_OK) {return NULL;} } break; case LEX_NUMBER:{ char *endptr = NULL; symb->data->type = DT_NUMBER; symb->data->data.dData=strtod(data.data, &endptr); // ATOF SE NEPOUZIVA, je to HNUSNA fce if (*endptr != '\0' || strcmp(endptr, data.data) == 0) { //*err = ERROR_SYNTAX; // toto je nejspis neco jinyho, ty tu kua nemas err...a ani ho nemas jak vratit mmuFree(symb); return NULL; } } break; case KW_NIL:{ symb->data->type = DT_NIL; } break; case KW_TRUE:{ symb->data->type = DT_BOOL; symb->data->data.bData = TRUE; } break; case KW_FALSE:{ symb->data->type = DT_BOOL; symb->data->data.bData = FALSE; } break; case LEX_ID:{ //funkce pro TypeOf symb->data->type = DT_FUNCTION; } break; default: return NULL; } symb->key.data=NULL; listInsertLast(&(function->constants),symb); return symb; }
int main() { TTable table; tableInit(&table); printf("\nTabulka by mela byt prazdna: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func1")); printf("\nJedna funkce: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func2")); printf("\nDve funkce: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func3")); printf("\nVsechny: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); // test heldani { TFunction *fceSearch; printf("\nObsahuje tabulka funkci %s? \t", "func1"); fceSearch = tableSearchFunction(&table, strCreateString("func1")); if(fceSearch != NULL) { printf("ANO\n"); printf(" Obsahuje funkce promenou %s?\t", "var1func1"); if(functionSearchVar(fceSearch, strCreateString("var1func1")) != NULL) printf("ANO"); else printf("NE"); } else printf("NE\n"); printf("\nObsahuje tabulka funkci %s? \t", "funcX"); fceSearch = tableSearchFunction(&table, strCreateString("funcX")); if(fceSearch != NULL) { printf("ANO\n"); printf(" Obsahuje funkce promenou %s?\t", "var1func1"); if(functionSearchVar(fceSearch, strCreateString("var1func1")) != NULL) printf("ANO"); else printf("NE"); } else printf("NE\n"); printf("\n----------------------------\n"); } // test zásobníku: printf("TEST ZASOBNIKU:\n"); printf("----------------------------\n"); TStack s; stackInit(&s); prazdnyStack(s); TFunction *fce = tableSearchFunction(&table, strCreateString("func2")); TVar *id = functionSearchVar(fce, strCreateString("var1func2")); stackPush(&s, (void*)id); prazdnyStack(s); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var2func2")); stackPush(&s, (void*)id); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var3func2")); stackPush(&s, (void*)id); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var1func2")); stackPush(&s, (void*)id); tiskniStack(&s); TVar *data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); tiskniStack(&s); stackDelete (&s); prazdnyStack(s); tiskniStack(&s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); printf("----------------------------\n"); // test seznamu: printf("TEST SEZNAMU:\n"); printf("----------------------------\n"); TList L; listInit (&L); id = functionSearchVar(fce, strCreateString("var1func2")); TLItem *uk2 = listGetActive (&L); listSetActive(&L, uk2); aktivniList(L); listInsertLast(&L, id); tiskniList(&L); void *uk4 = listCopyLast(&L); printf("Zkopírováno: %s\n",((TVar*)uk4)->name); aktivniList(L); listFirst(&L); tiskniList(&L); listLast(&L); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var2func2")); listPostInsert(&L, id); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var3func2")); listInsertLast(&L, id); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var1func2")); listInsertLast(&L, id); tiskniList(&L); listFirst(&L); listSucc(&L); TLItem *uk = listGetActive (&L); tiskniList(&L); listLast(&L); tiskniList(&L); listSetActive(&L, uk); tiskniList(&L); aktivniList(L); listDeleteFirst(&L); tiskniList(&L); void *uk3 = listCopyLast(&L); printf("Zkopírováno: %s\n",((TVar*)uk3)->name); listSucc(&L); tiskniList(&L); listActualize(&L, uk3); tiskniList(&L); listDispose (&L); printf("----------------------------\n"); // konec testu seznamu printf("\nSmazu: \n"); tableClear(&table); tablePrintOrder(table); printf("\n----------------------------\n"); }