Massive refactoring to simplify this whole setup. The Token List will be modified in place and reused in the Parser to normalize the list and generate a symbols table. Normalizing the token list will make sure the syntax is valid and the symbols table will have the relative offsets and length of symbol values. At least this is all the plan but one step at a time.

This commit is contained in:
2023-03-06 21:51:36 -06:00
parent 1b246e90bc
commit 5e9e1bdec4
7 changed files with 201 additions and 232 deletions
+14 -25
View File
@@ -25,7 +25,7 @@ SymbolTable* CreateSymbolTable(void){
return table;
}
Symbol* CreateSymbol(char* name, SymbolType type) {
Symbol* CreateSymbol(char* name, int address, int resolved) {
Symbol* symbol = calloc(1, sizeof(Symbol));
if (!symbol) {
@@ -34,40 +34,29 @@ Symbol* CreateSymbol(char* name, SymbolType type) {
return NULL;
}
symbol->Type = type;
symbol->Address = address;
symbol->Name = name;
symbol->Resolved = resolved;
return symbol;
}
SymbolString* CreateSymbolString(char* string, int terminated) {
if (!string) return NULL;
int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) {
*outSymbol = NULL;
SymbolString* s = calloc(1, sizeof(SymbolString));
if (!s) {
fprintf(stderr, "Failed to calloc string for symbol. %s.\n", strerror(errno));
return NULL;
}
s->String = string;
s->Terminated = terminated;
return s;
}
Symbol* TryGetSymbol(char* name, SymbolTable* table) {
if (!name || !table) return NULL;
if (!name || !table) return 0;
for(int i = 0; i < table->Size; i++) {
if (strcmp(table->Symbols[i]->Name, name) == 0) return table->Symbols[i];
if (strcmp(table->Symbols[i]->Name, name) == 0) {
*outSymbol = table->Symbols[i];
return 1;
}
}
return NULL;
return 0;
}
Symbol* AddSymbolToTable(char* name, SymbolType type, SymbolTable* table) {
Symbol* AddSymbolToTable(char* name, int address, int resolved, SymbolTable* table) {
//if (!name || !value || !table || length == 0) return NULL;
for(int i = 0; i < table->Size; i++) {
@@ -79,7 +68,7 @@ Symbol* AddSymbolToTable(char* name, SymbolType type, SymbolTable* table) {
}
if (table->Capacity < table->Size + 1) {
Symbol** newBlock = realloc(table->Symbols, sizeof(Symbol*) * table->Capacity * 2);//calloc(table->Size * 2, sizeof(Symbol*));
Symbol** newBlock = realloc(table->Symbols, sizeof(Symbol*) * table->Capacity * 2);
if (!newBlock) {
fprintf(stderr, "Failed to realloc space for a new symbol '%s'. %s.\n", name, strerror(errno));
@@ -91,7 +80,7 @@ Symbol* AddSymbolToTable(char* name, SymbolType type, SymbolTable* table) {
table->Symbols = newBlock;
}
Symbol* symbol = CreateSymbol(name, type);
Symbol* symbol = CreateSymbol(name, address, resolved);
table->Symbols[table->Size] = symbol;
table->Size++;