From 6d17dffcdf13b888ba9048013e26202c7059e38b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 8 Sep 2022 20:56:02 +0000 Subject: [PATCH] Added a SymbolTable object (untested at the moment) and redefined the Symbol object. I think when a Symbol is made only the size of it should matter to the code that will assemble the final binary. --- includes/symbols_table.h | 29 ++++++++++++ includes/token.h | 2 - src/symbols_table.c | 97 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 includes/symbols_table.h create mode 100644 src/symbols_table.c diff --git a/includes/symbols_table.h b/includes/symbols_table.h new file mode 100644 index 0000000..a15bfe9 --- /dev/null +++ b/includes/symbols_table.h @@ -0,0 +1,29 @@ +#ifndef SYMBOLSTABLE_H +#define SYMBOLSTABLE_H + +#include +#include +#include +#include + +#define SYMBOLSTABLE_DEFAULT_CAPACITY 128 + +typedef struct { + void* Value; + char* Name; + int Length; +} Symbol; + +typedef struct { + Symbol** Symbols; + int Size; + int Capacity; +} SymbolTable; + +SymbolTable* CreateSymbolTable(void); +Symbol* TryGetSymbol(char* name, SymbolTable* table); +Symbol* AddSymbolToTable(char* name, void* value, int length, SymbolTable* table); +void FreeSymbolTable(SymbolTable* table); +void FreeSymbol(Symbol* symbol); + +#endif \ No newline at end of file diff --git a/includes/token.h b/includes/token.h index 3c05a09..d50cc8b 100644 --- a/includes/token.h +++ b/includes/token.h @@ -6,8 +6,6 @@ #include #include -#define REGISTEROFFSET 29 - typedef enum { PLUS, MINUS, diff --git a/src/symbols_table.c b/src/symbols_table.c new file mode 100644 index 0000000..f634270 --- /dev/null +++ b/src/symbols_table.c @@ -0,0 +1,97 @@ +#include "../includes/symbols_table.h" +#include +#include +#include + +SymbolTable* CreateSymbolTable(void){ + SymbolTable* table = calloc(1, sizeof(SymbolTable)); + + if (!table) { + fprintf(stderr, "Failed to calloc memory for a SymbolTable. %s.\n", strerror(errno)); + return NULL; + } + + table->Symbols = calloc(SYMBOLSTABLE_DEFAULT_CAPACITY, sizeof(Symbol*)); + + if (!table->Symbols) { + fprintf(stderr, "Failed to calloc Symbol list. %s.\n", strerror(errno)); + free(table); + return NULL; + } + + table->Capacity = SYMBOLSTABLE_DEFAULT_CAPACITY; + table->Size = 0; + + return table; +} + +Symbol* CreateSymbol(char* name, void* value, int length) { + Symbol* symbol = calloc(1, sizeof(Symbol)); + + if (!symbol) { + fprintf(stderr, "Failed to create Symbol '%s'. %s.\n", name, strerror(errno)); + + return NULL; + } + + symbol->Length = length; + symbol->Name = name; + symbol->Value = value; + + return symbol; +} + +Symbol* TryGetSymbol(char* name, SymbolTable* table) { + if (!name || !table) return NULL; + + for(int i = 0; i < table->Size; i++) { + if (strcmp(table->Symbols[i]->Name, name) == 0) return table->Symbols[i]; + } + + return NULL; +} + +Symbol* AddSymbolToTable(char* name, void* value, int length, SymbolTable* table) { + if (!name || !value || !table || length == 0) return NULL; + + for(int i = 0; i < table->Size; i++) { + if (strcmp(table->Symbols[i]->Name, name) == 0) { + //TODO: Do we update or throw some kind of an error? + + return table->Symbols[i]; + } + } + + if (table->Capacity < table->Size + 1) { + Symbol** newBlock = realloc(table->Symbols, sizeof(Symbol*) * table->Capacity * 2);//calloc(table->Size * 2, sizeof(Symbol*)); + + if (!newBlock) { + fprintf(stderr, "Failed to realloc space for a new symbol '%s'. %s.\n", name, strerror(errno)); + + return NULL; + } + + table->Capacity *= 2; + table->Symbols = newBlock; + } + + Symbol* symbol = CreateSymbol(name, value, length); + + table->Symbols[table->Size] = symbol; + + return symbol; +} + +void FreeSymbolTable(SymbolTable* table) { + if (!table) return; + + for(int i = 0; i < table->Size; i++) FreeSymbol(table->Symbols[i]); + + free(table); +} + +void FreeSymbol(Symbol* symbol) { + if (!symbol) return; + + free(symbol); +} \ No newline at end of file