fix/refactor: Modified evaluate and changed it to evaluate_tree

So i did what the last commit said, also fixed parse_expr because it was
still using malloc for allocating new nodes so i made it use arena_alloc
like it should, did the very first tests so it's all good, i think is
readdy to merge.
This commit is contained in:
2026-04-13 08:44:30 -06:00
parent e4ec102cb9
commit 7ad4eba123
7 changed files with 52 additions and 36 deletions

View File

@@ -1,9 +1,11 @@
#include "evaluator.h"
#include "arena.h"
#include "lexer.h"
#include "parser.h"
#include <stdint.h>
int64_t evaluate(ASTNode *tree) {
int64_t evaluate_tree(ASTNode *tree) {
if (tree->type == NODE_BINARY_OP) {
Operator op = tree->data.binary.op;
ASTNode *left = tree->data.binary.left;
@@ -11,18 +13,24 @@ int64_t evaluate(ASTNode *tree) {
switch (op) {
case OP_ADD:
return evaluate(left) + evaluate(right);
return evaluate_tree(left) + evaluate_tree(right);
case OP_SUB:
return evaluate(left) - evaluate(right);
return evaluate_tree(left) - evaluate_tree(right);
case OP_MUL:
return evaluate(left) * evaluate(right);
return evaluate_tree(left) * evaluate_tree(right);
case OP_DIV:
return evaluate(left) / evaluate(right);
return evaluate_tree(left) / evaluate_tree(right);
}
} else {
int64_t return_val = tree->data.integer;
return return_val;
}
int64_t return_val = tree->data.integer;
return return_val;
}
int64_t evaluate(ParseResult context) {
int64_t result = evaluate_tree(context.tree);
arena_destroy(&context.arena);
return result;
}