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
+18 -11
View File
@@ -119,6 +119,8 @@ void FreeSymbol(Symbol* 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) {
if (!symbol || !symbol->Token || !table) return 0;
if (symbol->Token->Class == CharacterClass) return 0;
@@ -144,17 +146,11 @@ int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned s
return 1;
case PunctuationClass:
//check for math operator
if (!TryGetSymbol(root->Left->Token->Lemexe, table, &s)) return 0;
if (!TryGetTokenNodeValue(root->Left, table, &left)) return 0;
if (!TryGetTokenNodeValue(root->Right, table, &right)) 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;
*value = left + right;
break;
default:
return 0;
}
@@ -162,6 +158,17 @@ int TryGetSymbolValue(const Symbol* symbol, const SymbolTable* table, unsigned s
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* node = calloc(1, sizeof(TokenNode));
@@ -173,4 +180,4 @@ TokenNode* CreateTokenNode(const Token* token){
node->Token = token;
return node;
}
}