Started to work on setting up Symbol value resolution which will be based on an abstract syntax tree that can handle undefined values if unknown symbols are used in an expression.
This commit is contained in:
@@ -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
|
||||
+5
-3
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -83,6 +83,4 @@ void RemoveToken(int index, TokenList* list) {
|
||||
list->size--;
|
||||
|
||||
list->content[list->size] = NULL;
|
||||
|
||||
FreeToken(token);
|
||||
}
|
||||
Reference in New Issue
Block a user