Wrote the very first part of the tree walking code, more just to make sure the logic is sound.

This commit is contained in:
2023-09-01 10:04:21 -05:00
parent b810d7803c
commit 21c2bf046e
+16 -9
View File
@@ -119,6 +119,8 @@ void FreeSymbol(Symbol* symbol) {
free(symbol); free(symbol);
} }
int TryGetTokenNodeValue(const TokenNode* node, const SymbolTable* table, unsigned short* value);
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 || !symbol->Token || !table) return 0;
if (symbol->Token->Class == CharacterClass) return 0; if (symbol->Token->Class == CharacterClass) return 0;
@@ -144,16 +146,10 @@ int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned s
return 1; return 1;
case PunctuationClass: case PunctuationClass:
//check for math operator if (!TryGetTokenNodeValue(root->Left, table, &left)) return 0;
if (!TryGetSymbol(root->Left->Token->Lemexe, table, &s)) return 0; if (!TryGetTokenNodeValue(root->Right, table, &right)) return 0;
if (!TryGetSymbolValue(s, table, &left)) return 0; *value = left + right;
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; break;
default: default:
return 0; return 0;
@@ -162,6 +158,17 @@ int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned s
return 1; return 1;
} }
int TryGetTokenNodeValue(const TokenNode* node, const SymbolTable* table, unsigned short* value) {
const Token* token = node->Token;
if (token->Class == NumberClass) {
*value = token->Value.Number;
return 1;
}
return 0;
}
TokenNode* CreateTokenNode(const Token* token){ TokenNode* CreateTokenNode(const Token* token){
TokenNode* node = calloc(1, sizeof(TokenNode)); TokenNode* node = calloc(1, sizeof(TokenNode));