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.
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef SYMBOLSTABLE_H
|
||||||
|
#define SYMBOLSTABLE_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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
|
||||||
@@ -6,8 +6,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define REGISTEROFFSET 29
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
PLUS,
|
PLUS,
|
||||||
MINUS,
|
MINUS,
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
#include "../includes/symbols_table.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user