It works, basic but works, need to move out logic to places, like ASTNode array shouldn't be on the lexer or future logic for fractions and error handling in the evaluator. Just the things at the top of my head

This commit is contained in:
2026-03-25 11:30:12 -06:00
parent 845673fb0e
commit 92d142b9cf
5 changed files with 67 additions and 28 deletions

View File

@@ -0,0 +1,22 @@
#include "evaluator.h"
#include "lexer.h"
#include <stdint.h>
uint64_t evaluate(ASTNode *tree) {
if (tree->type == NODE_BINARY_OP) {
switch (tree->data.binary.op) {
case OP_ADD:
return evaluate(tree->data.binary.left) + evaluate(tree->data.binary.left);
case OP_SUB:
return evaluate(tree->data.binary.left) - evaluate(tree->data.binary.left);
case OP_MUL:
return evaluate(tree->data.binary.left) * evaluate(tree->data.binary.left);
case OP_DIV:
return evaluate(tree->data.binary.left) / evaluate(tree->data.binary.left);
}
} else {
return tree->data.integer;
}
}