/** * Prepis programu wordcount z C++ do C zo zadania. */ int main(void) { htab_t *t=htab_init(HTAB_SIZE); if(!t) { fprintf(stderr, "[wordcount]\t-htable was not allocated\n"); return EXIT_FAILURE; } char word[WORD_LIMIT+1]={0}; while(fgetw(word, WORD_LIMIT, stdin)!=EOF) { if(!htab_lookup(t, word)) { fprintf(stderr, "[wordcount]\t-[htab_lookup] allocation fail\n"); htab_free(t); return EXIT_FAILURE; } } htab_foreach(t, &print_items); //htab_statistics(t); /// vypis statistiky udajov tabulky htab_free(t); return EXIT_SUCCESS; }
int its_function() { int answer; htab_item *item; answer = not_function; token = get_token(); switch (token->type) { case TOKEN_IDENTIFIER: item = htab_lookup(G.g_globalTab, token->data); if (item == NULL) { find_var(token, 0); } else { answer = external_function; } break; case TOKEN_LENGTH: case TOKEN_SUBSTR: case TOKEN_CONCAT: case TOKEN_FIND: case TOKEN_SORT: answer = internal_function; } unget_token(token); return answer; }
void generate_external_function(TVariable *ret_var, Tins_list *act_ins_list) { TList_item *actual_ins; htab_item *h_item; TFunction *f_stored; int f_sto_param_count; TVariable *f_stored_var; TVariable *f_readed_var; token = get_token(); h_item = htab_lookup(G.g_globalTab, token->data); f_stored = h_item->data.function; skip_token(TOKEN_LROUND_BRACKET); f_sto_param_count = f_stored->params_stack->used; for(int i = 0; i < f_sto_param_count; i++) { f_stored_var = f_stored->params_stack->data[i]; f_readed_var = get_next_para(f_stored_var->var_type); if (i < f_sto_param_count-1) { skip_token(TOKEN_COMMA); } actual_ins = create_ins(INS_PUSH_PARAM, f_readed_var, NULL, NULL); list_insert(act_ins_list, actual_ins); } actual_ins = create_ins(INS_CALL, (TVariable*)h_item, NULL, NULL); list_insert(act_ins_list, actual_ins); actual_ins = create_ins(INS_ASSIGN, ret_var, f_stored->return_var, NULL); list_insert(act_ins_list, actual_ins); skip_token(TOKEN_RROUND_BRACKET); }
/* * Read words from stdin and print number of occurrences * @param argc number of arguments * @param argv arguments * @return success code * */ int main(int argc, char* argv[]) { // This size is used, because it is enought for entire book (tested on Alice's Adventures in Wonderland and Pride and Prejudice) // and average number is between 0.5-2 - is used more than half and is average not more than 2 items htab_t* htab = htab_init(7000); htab_listitem* item; char word[128]; while(fgetw(word, 127,stdin) != EOF) { item = htab_lookup(htab,word); item->data++; } htab_foreach(htab, &print_word); //htab_statistics(htab); htab_free(htab); return 0; }
/* Return variable from actual function stack or new variable created from constant value */ TVariable *find_var(/*const */TToken *tok, const int clean_data_if_found) { switch (tok->type) { case TOKEN_IDENTIFIER: for(int i = G.g_frameStack->used-1; i >= 0; i--) { TFunction *f = G.g_frameStack->data[i]; htab_item *found = htab_lookup(f->local_tab, tok->data); if(found) { if (clean_data_if_found == 1) { free(tok->data); } return found->data.variable; } } my_exit_error(E_SEMANTIC_DEF, 4); case TOKEN_STRING_VALUE: case TOKEN_INT_VALUE: case TOKEN_DOUBLE_VALUE: return token_to_const(tok); } return 0; }