diff --git a/includes/symbols_table.h b/includes/symbols_table.h index 65daea6..c25ba26 100644 --- a/includes/symbols_table.h +++ b/includes/symbols_table.h @@ -13,16 +13,16 @@ #define SYMBOLSTABLE_DEFAULT_CAPACITY 128 typedef struct __token_node { - Token* Token; + const Token* Token; struct __token_node* Left; struct __token_node* Right; } TokenNode; typedef struct _symbol { - char* Name; + const char* Name; int Address; int Length; - Token* Token; + const Token* Token; TokenNode* ValueExpression; } Symbol; @@ -33,12 +33,12 @@ typedef struct { } SymbolTable; SymbolTable* CreateSymbolTable(void); -TokenNode* CreateTokenNode(Token* token); -int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol); -Symbol* AddSymbolToTable(char* name, int address, SymbolTable* table); +TokenNode* CreateTokenNode(const Token* token); +int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol); +Symbol* AddSymbolToTable(const char* name, int address, SymbolTable* table); void FreeSymbolTable(SymbolTable* table); void FreeSymbol(Symbol* symbol); -int SymbolResolved(Symbol* symbol); -int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value); +int SymbolResolved(const Symbol* symbol); +int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value); #endif \ No newline at end of file diff --git a/includes/token.h b/includes/token.h index 6d3345d..201ffd9 100644 --- a/includes/token.h +++ b/includes/token.h @@ -50,7 +50,7 @@ typedef struct __token { Registers Register; Mnemonic Mnemonic; Directive Directive; - int Number; + unsigned short Number; } Value; struct __token* Prev; diff --git a/src/symbols_table.c b/src/symbols_table.c index acba3a0..46400b7 100644 --- a/src/symbols_table.c +++ b/src/symbols_table.c @@ -3,7 +3,7 @@ #include #include -int SymbolResolved(Symbol* symbol) { +int SymbolResolved(const Symbol* symbol) { if (!symbol) return 0; return symbol->Token != NULL; @@ -42,7 +42,7 @@ SymbolTable* CreateSymbolTable(void){ return table; } -Symbol* CreateSymbol(char* name, int address) { +Symbol* CreateSymbol(const char* name, int address) { Symbol* symbol = calloc(1, sizeof(Symbol)); if (!symbol) { @@ -58,7 +58,7 @@ Symbol* CreateSymbol(char* name, int address) { return symbol; } -int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) { +int TryGetSymbol(const char* name, const SymbolTable* table, Symbol** outSymbol) { *outSymbol = NULL; if (!name || !table) return 0; @@ -73,7 +73,7 @@ int TryGetSymbol(char* name, SymbolTable* table, Symbol** outSymbol) { return 0; } -Symbol* AddSymbolToTable(char* name, int address, SymbolTable* table) { +Symbol* AddSymbolToTable(const char* name, int address, SymbolTable* table) { //if (!name || !value || !table || length == 0) return NULL; for(int i = 0; i < table->Size; i++) { @@ -119,24 +119,52 @@ void FreeSymbol(Symbol* symbol) { free(symbol); } -int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value) { +int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short* value) { if (!symbol || !symbol->Token || !table) return 0; if (symbol->Token->Class == CharacterClass) return 0; - unsigned short acc = 0; - Token* current = symbol->Token->Next; + unsigned short left = 0; + unsigned short right = 0; + + TokenNode* root = symbol->ValueExpression; + Symbol* s = NULL; - for(;;) { - if (current->Class == PunctuationClass && current->Value.Punctuation == NewLine) break; - Token* next = current->Next; + if (!root || !root->Token) return 0; + switch (root->Token->Class) { + case NumberClass: + *value = root->Token->Value.Number; + return 1; + break; + case LabelClass: + case IdentifierClass: + if (!TryGetSymbol(root->Token->Lemexe, table, &s)) return 0; + + if (!TryGetSymbolValue(s, table, value)) return 0; + + return 1; + break; + case PunctuationClass: //TODO: ()'s need to be handled + //check for math operator + if (!TryGetSymbol(root->Left->Token->Lemexe, table, &s)) return 0; + + if (!TryGetSymbolValue(s, table, &left)) return 0; + + if (!TryGetSymbol(root->Right->Token->Lemexe, table, &s)) return 0; + + if (!TryGetSymbolValue(s, table, &right)) return 0; + + *value = left + right; //TODO: actually check what op we need to do. + break; + default: + return 0; } return 1; } -TokenNode* CreateTokenNode(Token* token){ +TokenNode* CreateTokenNode(const Token* token){ TokenNode* node = calloc(1, sizeof(TokenNode)); if (!node) {