#include "symtab.h" static u32_t hash_func(const void* _key) { const symtab_key_t* key = (symtab_key_t*)_key; return rt_strhash(key->strp_name); } static int key_cmp(const void* _key1, const void* _key2) { const symtab_key_t* key1 = (symtab_key_t*)_key1; const symtab_key_t* key2 = (symtab_key_t*)_key2; if (rt_strcmp(key1->strp_name, key2->strp_name) == 0) { return 0; } return 1; } void init_symtab(symtab_t* symtab) { symtab->cur_scope = NULL; symtab->gid = 1; init_hashtable(&symtab->global_table); symtab->global_table.hash_func = hash_func; symtab->global_table.key_cmp = key_cmp; init_hashtable(&symtab->local_table); symtab->local_table.hash_func = hash_func; symtab->local_table.key_cmp = key_cmp; } void symtab_destroy(symtab_t* symtab) { // TODO } void symtab_enter_scope(symtab_t* symtab) { scope_t *scope = (scope_t*)salloc_alloc(sizeof(scope_t)); scope->parent = symtab->cur_scope; scope->uid = symtab->gid++; init_hashtable(&scope->table); scope->table.hash_func = hash_func; scope->table.key_cmp = key_cmp; symtab->cur_scope = scope; } void symtab_leave_scope(symtab_t* symtab) { Assert(symtab->cur_scope != NULL); scope_t *parent = symtab->cur_scope->parent; hashtable_destory(&symtab->cur_scope->table); salloc_free(symtab->cur_scope); symtab->cur_scope = parent; } void* symtab_get(symtab_t* symtab, symtab_key_t* key) { for (scope_t* scope = symtab->cur_scope; scope != NULL; scope = scope->parent) { void* val = hashtable_get(&scope->table, key); if (val != NULL) { return val; } } return NULL; } void* symtab_add(symtab_t* symtab, symtab_key_t* key, void* val) { return hashtable_set(&symtab->cur_scope->table, key, val); }