init basic
This commit is contained in:
45
ccompiler/frontend/parser/symtab/symtab.c
Normal file
45
ccompiler/frontend/parser/symtab/symtab.c
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user