50 lines
1.3 KiB
C
50 lines
1.3 KiB
C
// symtab.c
|
|
#include "../../frontend.h"
|
|
#include "scope.h"
|
|
#include "symtab.h"
|
|
|
|
typedef symtab_t symtab_t;
|
|
typedef struct Scope Scope;
|
|
|
|
void init_symtab(symtab_t* symtab) {
|
|
symtab->global_scope = scope_create(NULL);
|
|
symtab->cur_scope = symtab->global_scope;
|
|
}
|
|
|
|
void del_symtab(symtab_t* symtab) {
|
|
scope_destroy(symtab->global_scope);
|
|
}
|
|
|
|
void symtab_enter_scope(symtab_t* 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(symtab_t* 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(symtab_t* symtab, const char* name, void* ast_node, int can_duplicate) {
|
|
struct Scope* scope = symtab->cur_scope;
|
|
void* node = scope_lookup_current(scope, name);
|
|
if (node != NULL) {
|
|
if (!can_duplicate) {
|
|
error("duplicate symbol %s", name);
|
|
}
|
|
return node;
|
|
}
|
|
|
|
scope_insert(scope, name, ast_node);
|
|
return node;
|
|
}
|
|
|
|
void* symtab_lookup_symbol(symtab_t* symtab, const char* name) {
|
|
return scope_lookup(symtab->cur_scope, name);
|
|
}
|