Skip to content

Commit

Permalink
symbol 토대 구성완료, qsort 구현만 남음
Browse files Browse the repository at this point in the history
  • Loading branch information
limdongjin committed Apr 5, 2019
1 parent b4fef50 commit f67a606
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 14 deletions.
9 changes: 9 additions & 0 deletions command_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ shell_status command_execute(Command *user_command, State *state_store) {
return execute_assemble(user_command, state_store);
case TYPE_TYPE:
return execute_type(user_command);
case TYPE_SYMBOL:
return execute_symbol(state_store);
default:
break;
}
Expand Down Expand Up @@ -247,4 +249,11 @@ shell_status execute_type(Command* user_command){

fclose(fp);
return EXECUTE_SUCCESS;
}

shell_status execute_symbol(State *state_store) {
if(!state_store->is_symbol_table) return EXECUTE_FAIL;

print_symbols(state_store->symbol_table_state);
return EXECUTE_SUCCESS;
}
2 changes: 2 additions & 0 deletions command_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ shell_status execute_assemble(Command *user_command, State* state_store);

shell_status execute_type(Command* user_command);

shell_status execute_symbol(State *state_store);

#endif
2 changes: 2 additions & 0 deletions command_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ shell_status command_mapping_type(Command *user_command){
user_command->type = TYPE_ASSEMBLE;
} else if(COMPARE_STRING(first_token, "type")){
user_command->type = TYPE_TYPE;
} else if(COMPARE_STRING(first_token, "symbol")){
user_command->type = TYPE_SYMBOL;
} else {
return INVALID_COMMAND_TYPE;
}
Expand Down
2 changes: 1 addition & 1 deletion command_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum shell_command_type{
TYPE_HELP, TYPE_DIR, TYPE_QUIT, TYPE_HISTORY,
TYPE_DUMP, TYPE_EDIT, TYPE_FILL, TYPE_RESET,
TYPE_OPCODE, TYPE_OPCODELIST, TYPE_ASSEMBLE,
TYPE_TYPE
TYPE_TYPE, TYPE_SYMBOL
};

// command 를 구조체로 구조화 하여 표현.
Expand Down
7 changes: 4 additions & 3 deletions command_validate_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ shell_status validate_parameters(Command *user_command){
user_command->type == TYPE_HISTORY ||
user_command->type == TYPE_DIR ||
user_command->type == TYPE_RESET ||
user_command->type == TYPE_OPCODELIST) &&
user_command->token_cnt > 1
)
user_command->type == TYPE_OPCODELIST ||
user_command->type == TYPE_SYMBOL
) &&
user_command->token_cnt > 1)
return INVALID_PARAMETERS;
if(user_command->type == TYPE_TYPE && user_command->token_cnt != 2)
return INVALID_PARAMETERS;
Expand Down
7 changes: 4 additions & 3 deletions state.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ State* construct_state(){
build_opcode_table(state_obj->opcode_table_state);

state_obj->symbol_table_state = construct_symbol_table();

state_obj->is_symbol_table = false;
return state_obj;
}

Expand Down Expand Up @@ -56,16 +56,17 @@ bool assemble_file(State *state_store, char *asm_file_name){
free(state_store->symbol_table_state);
state_store->symbol_table_state = construct_symbol_table();

// [TODO] 적절한 에러 처리 필요함.
if(!assemble_pass1(state_store, asm_file_name)) {
state_store->is_symbol_table = false;
return false;
}

// [TODO] 적절한 에러 처리 필요함.
if(!assemble_pass2(state_store, asm_file_name)){
state_store->is_symbol_table = false;
return false;
}

state_store->is_symbol_table = true;
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions state.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct state {

// symbol 정보 저장
SymbolTable* symbol_table_state;

bool is_symbol_table;
} State;

/*
Expand Down
26 changes: 19 additions & 7 deletions symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ Symbol * find_symbol_by_name(SymbolTable *table, char *name){
int hash = (int)hash_string(name, 40);
int i;

// print_symbols(table);

SymNode** cur = &(table->list[hash]->head);
Symbol* symb;

Expand All @@ -66,7 +64,7 @@ Symbol * find_symbol_by_name(SymbolTable *table, char *name){
cur = &((*cur)->next);
continue;
}
// fprintf(stdout, "target: %s , cur: %s\n", name, symb->label);

if(COMPARE_STRING(symb->label, name)){
return symb;
}
Expand All @@ -77,24 +75,38 @@ Symbol * find_symbol_by_name(SymbolTable *table, char *name){

void print_symbols(SymbolTable* table){
int size = table->size;
for(int i = 0;i < size;i++){
Symbol *list[1200] = {0};
int i = 0;

for(i = 0;i < size;i++){
printf("%2d : ", i);
SymNode** cur = &(table->list[i]->head);
Symbol* symb;
for(int j=0;j<table->list[i]->size+1;j++){
symb = (*cur)->data;
// if(opc->value == -1) cnt -= 1;

if(COMPARE_STRING(symb->label, "---nono--")){
cur=&((*cur)->next);
continue;
}
printf("[%s,%02d] ",symb->label,symb->location_counter);
// printf("gogo %s %d\n", opc->mnemonic_name, opc->value);
// cnt += 1;
if(j != table->list[i]->size)
printf(" -> ");
list[i++] = symb;
cur = &((*cur)->next);
}
printf("\n");
}

qsort(list, i, sizeof(Symbol*), symb_compare_func);

for (int k = 0; k < i; ++k)
printf ("\t%s\t%04X\n", list[k]->label, list[k]->location_counter);
}

static int symb_compare_func(const void *a, const void *b){
Symbol **left = (Symbol **) a;
Symbol **right = (Symbol **) b;

return strcmp ((*left)->label, (*right)->label);
}
3 changes: 3 additions & 0 deletions symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ bool insert_symbol(SymbolTable* table, Symbol* symbol);
Symbol * find_symbol_by_name(SymbolTable *table, char *name);
//bool find_symbol(SymbolTable* table, Symbol* symb);
void print_symbols(SymbolTable* table);

static int symb_compare_func(const void *a, const void *b);

#endif

0 comments on commit f67a606

Please sign in to comment.