diff --git a/includes/symbols_table.h b/includes/symbols_table.h index 84c353b..65daea6 100644 --- a/includes/symbols_table.h +++ b/includes/symbols_table.h @@ -12,11 +12,18 @@ #define SYMBOLSTABLE_DEFAULT_CAPACITY 128 +typedef struct __token_node { + Token* Token; + struct __token_node* Left; + struct __token_node* Right; +} TokenNode; + typedef struct _symbol { char* Name; int Address; int Length; Token* Token; + TokenNode* ValueExpression; } Symbol; typedef struct { @@ -26,10 +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); void FreeSymbolTable(SymbolTable* table); void FreeSymbol(Symbol* symbol); int SymbolResolved(Symbol* symbol); +int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned short** value); #endif \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 3179817..a7355de 100644 --- a/src/parser.c +++ b/src/parser.c @@ -23,7 +23,7 @@ void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options); void ExpectRegister(void); Symbol* ExpectIdentifier(ExpectOptions options, int expectUnresolved); void ExpectCharacterClass(Symbol* symbol); -void ExpectLineEndOrFileEnd(ExpectOptions options); +Token* ExpectLineEndOrFileEnd(ExpectOptions options); SymbolTable* SymbolsTable; @@ -489,10 +489,10 @@ void ExpectCharacterClass(Symbol* symbol) { ExpectLineEndOrFileEnd(RemoveExpected); } -void ExpectLineEndOrFileEnd(ExpectOptions options) { +Token* ExpectLineEndOrFileEnd(ExpectOptions options) { Token* token = PeekToken(); - if (token->EndOfFile) return; + if (token->EndOfFile) return token; if (token->Class != PunctuationClass || token->Value.Punctuation != NewLine) { fprintf(stderr, "[Line %d] Syntax error, expected line break.\n", token->LineNumber); @@ -501,6 +501,8 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) { if (options & RemoveExpected) RemoveCurrentToken(); if (options & ForwardParser) AdvanceParser(); + + return token; } void PrintSymbols(void) { diff --git a/src/symbols_table.c b/src/symbols_table.c index d9ac537..acba3a0 100644 --- a/src/symbols_table.c +++ b/src/symbols_table.c @@ -117,4 +117,34 @@ void FreeSymbol(Symbol* symbol) { if (!symbol) return; free(symbol); +} + +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; + + for(;;) { + if (current->Class == PunctuationClass && current->Value.Punctuation == NewLine) break; + Token* next = current->Next; + + + } + + return 1; +} + +TokenNode* CreateTokenNode(Token* token){ + TokenNode* node = calloc(1, sizeof(TokenNode)); + + if (!node) { + fprintf(stderr, "Failed to calloc memory for a TokenNode. %s.\n", strerror(errno)); + return NULL; + } + + node->Token = token; + + return node; } \ No newline at end of file diff --git a/src/token.c b/src/token.c index c95557b..a6bc85a 100644 --- a/src/token.c +++ b/src/token.c @@ -83,6 +83,4 @@ void RemoveToken(int index, TokenList* list) { list->size--; list->content[list->size] = NULL; - - FreeToken(token); } \ No newline at end of file