diff options
Diffstat (limited to '')
-rw-r--r-- | masm/symtbl.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/masm/symtbl.c b/masm/symtbl.c new file mode 100644 index 0000000..b75c752 --- /dev/null +++ b/masm/symtbl.c @@ -0,0 +1,57 @@ +#include <merror.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "parse.h" + +#define SYMTBL_INIT_LEN 24 + +int symtbl_init(struct symbol_table *sym_tbl) +{ + sym_tbl->len = SYMTBL_INIT_LEN; + sym_tbl->count = 0; + sym_tbl->symbols = malloc(sizeof(struct symbol) * SYMTBL_INIT_LEN); + + if (sym_tbl->symbols == NULL) { + ERROR("cannot alloc"); + return M_ERROR; + } + + return M_SUCCESS; +} + +void symtbl_free(struct symbol_table *sym_tbl) +{ + free(sym_tbl->symbols); +} + +int symtbl_push(struct symbol_table *sym_tbl, struct symbol sym) +{ + if (sym_tbl->count >= sym_tbl->len) { + sym_tbl->len *= 2; + sym_tbl->symbols = realloc(sym_tbl->symbols, + sizeof(struct symbol) * sym_tbl->len); + if (sym_tbl->symbols == NULL) { + ERROR("cannot relloc"); + return M_ERROR; + } + } + + sym_tbl->symbols[sym_tbl->count++] = sym; + return M_SUCCESS; +} + +int symtbl_find(struct symbol_table *sym_tbl, struct symbol **ptr, + const char name[MAX_LEX_LENGTH]) +{ + for (uint32_t i = 0; i < sym_tbl->count; i++) { + struct symbol *sym = &sym_tbl->symbols[i]; + if (strcmp(sym->name, name) == 0) { + if (ptr != NULL) + *ptr = sym; + return M_SUCCESS; + } + } + return M_ERROR; +} |