2025-03-05 15:45:19 +08:00

46 lines
1.2 KiB
C

// symtab.c
#include "../../frontend.h"
#include "scope.h"
#include "symtab.h"
typedef struct SymbolTable SymbolTable;
typedef struct Scope Scope;
void init_symtab(SymbolTable* symtab) {
symtab->global_scope = scope_create(NULL);
symtab->cur_scope = symtab->global_scope;
}
void del_symtab(SymbolTable* symtab) {
scope_destroy(symtab->global_scope);
}
void symtab_enter_scope(SymbolTable* symtab) {
struct Scope* scope = scope_create(symtab->cur_scope);
scope->base_offset = symtab->cur_scope->base_offset + symtab->cur_scope->cur_offset;
symtab->cur_scope = scope;
}
void symtab_leave_scope(SymbolTable* symtab) {
Scope * scope = symtab->cur_scope;
if (scope == NULL) {
error("cannot leave NULL scope or global scope");
}
symtab->cur_scope = symtab->cur_scope->parent;
scope_destroy(scope);
}
void symtab_add_symbol(SymbolTable* symtab, const char* name, void* ast_node) {
struct Scope* scope = symtab->cur_scope;
if (scope_lookup_current(scope, name) != NULL) {
// TODO WARNING
// return NULL;
}
scope_insert(scope, name, ast_node);
}
void* symtab_lookup_symbol(SymbolTable* symtab, const char* name) {
return scope_lookup(symtab->cur_scope, name);
}