int main() { sTableInit(); sStackInit(); // generate entries SymbolTableEntry* entry, *entry2, *result, *result2; Lexeme ll = "abc"; int tt = 1; printf("%d\n", s_stack->top); fflush(stdout); entry = sNewEntry ( ll, tt, sStackLevel, sStackTopId ); entry2 = sNewEntry ( "xyz", 2, 1, 1 ); printf("key=%s\nbind=%s\nSlevel=%d\nScopeId=%d\n", entry->key, entry->bind,entry->scope[0],entry->scope[1]); printf("key=%s\nbind=%s\n", entry2->key, entry2->bind); // insert to s_table sTableInsert( entry); // lookup SymbolTableKey key; sNewKey(entry->lex, entry->scope[1], key); result = s_table_lookup(s_table, key); // check printf("key=%s\nbind=%s\n", result->key, result->bind); // remove bool istat = s_table_remove(s_table, entry); if (istat) printf("remove done\n"); else printf("remove false\n"); // lookup 2 , should fail result2 = s_table_lookup(s_table, key); printf("Should be NULL %p\n", result2); // cleanup sTableDestory(); // test stack ScopeId ttt; s_stack_push(s_stack, 1); s_stack_push(s_stack, 2); s_stack_push(s_stack, 3); s_stack_push(s_stack, 4); printf("top=%d, present=%d\n", s_stack->top, s_stack->present); ttt = s_stack_pop(s_stack); printf("pop:%d\n",ttt); // printf("top=%d, present=%d\n", s_stack->top, s_stack->present); // s_stack_reset(s_stack); while(s_stack->present>=0) { printf("down:%d\n",s_stack_down(s_stack)); } s_stack_reset(s_stack); printf("top=%d, present=%d\n", s_stack->top, s_stack->present); // sStackDestroy(); }
static inline cpu_mul(Cpu *cpu){ int a = s_stack_pop(&cpu->mem); int b = s_stack_pop(&cpu->mem); int result = a * b; sign_reg higher_order = result >> 16; sign_reg lower_order = result & 0xffff; printf("Result %d, higher %d, lower %d\n", result, higher_order, lower_order); if (s_errno == EMPTY){ p_errno = STACK; } else { s_stack_push(&cpu->mem, higher_order); s_stack_push(&cpu->mem, lower_order); } }
static inline cpu_div(Cpu *cpu){ sign_reg a = s_stack_pop(&cpu->mem); sign_reg b = s_stack_pop(&cpu->mem); if (s_errno == EMPTY){ p_errno = STACK; } else if (a != 0){ sign_reg quotient = b / a; sign_reg remainder = b % a; s_stack_push(&cpu->mem, remainder); s_stack_push(&cpu->mem, quotient); } else { p_errno = DIV_BY_0; } }
// init Scope Stack int s_stack_init (SymbolTableStack** stack) { SymbolTableStack* tstack = (SymbolTableStack*) malloc ( sizeof(SymbolTableStack) ); tstack->stack = g_array_new (1,1,sizeof(ScopeId)); tstack->top = -1; tstack->present = -1; s_stack_push( tstack, s_table_new_scopeid () ); *stack = tstack; return 0; }
static inline cpu_sub(Cpu *cpu){ sign_reg a = s_stack_pop(&cpu->mem); sign_reg b = s_stack_pop(&cpu->mem); if (s_errno == EMPTY){ p_errno = STACK; } else { s_stack_push(&cpu->mem, b - a); } }
static inline cpu_push_reg(Cpu *cpu, int reg_num){ if (s_stack_push(&cpu->mem, cpu->r[reg_num])){ p_errno = STACK; } }
static inline cpu_push_num(Cpu *cpu, reg num){ if (s_stack_push(&cpu->mem, num)){ p_errno = STACK; } }