Build a quick draft of how the symbol value should be computed.
This commit is contained in:
+39
-11
@@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user