2026-03-25 11:30:12 -06:00
|
|
|
#include "evaluator.h"
|
|
|
|
|
#include "lexer.h"
|
|
|
|
|
#include <stdint.h>
|
2026-04-13 07:57:36 -06:00
|
|
|
|
2026-03-25 11:30:12 -06:00
|
|
|
|
2026-03-25 12:25:15 -06:00
|
|
|
int64_t evaluate(ASTNode *tree) {
|
2026-03-25 11:30:12 -06:00
|
|
|
if (tree->type == NODE_BINARY_OP) {
|
2026-03-26 09:25:40 -06:00
|
|
|
Operator op = tree->data.binary.op;
|
|
|
|
|
ASTNode *left = tree->data.binary.left;
|
|
|
|
|
ASTNode *right = tree->data.binary.right;
|
|
|
|
|
|
|
|
|
|
switch (op) {
|
2026-03-25 11:30:12 -06:00
|
|
|
case OP_ADD:
|
2026-03-26 09:25:40 -06:00
|
|
|
return evaluate(left) + evaluate(right);
|
2026-03-25 11:30:12 -06:00
|
|
|
case OP_SUB:
|
2026-03-26 09:25:40 -06:00
|
|
|
return evaluate(left) - evaluate(right);
|
2026-03-25 11:30:12 -06:00
|
|
|
case OP_MUL:
|
2026-03-26 09:25:40 -06:00
|
|
|
return evaluate(left) * evaluate(right);
|
2026-03-25 11:30:12 -06:00
|
|
|
case OP_DIV:
|
2026-03-26 09:25:40 -06:00
|
|
|
return evaluate(left) / evaluate(right);
|
2026-03-25 11:30:12 -06:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-03-26 09:25:40 -06:00
|
|
|
int64_t return_val = tree->data.integer;
|
|
|
|
|
return return_val;
|
2026-03-25 11:30:12 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|