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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user