-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.cpp
30 lines (29 loc) · 1.13 KB
/
evaluator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "parser.cpp"
namespace Evaluator {
long double evaluate(const Node *tree) {
switch (tree->tokenType) {
case TokenTypes::INTEGER:
return std::stoi(tree->value);
case TokenTypes::DOUBLE:
return std::stold(tree->value);
case TokenTypes::OP1:
if (tree->value == "*") {
return evaluate(((BinaryNode *)tree)->left) *
evaluate(((BinaryNode *)tree)->right);
} else if (tree->value == "/") {
return evaluate(((BinaryNode *)tree)->left) /
evaluate(((BinaryNode *)tree)->right);
}
case TokenTypes::OP2:
if (tree->value == "+") {
return evaluate(((BinaryNode *)tree)->left) +
evaluate(((BinaryNode *)tree)->right);
} else if (tree->value == "-") {
return evaluate(((BinaryNode *)tree)->left) -
evaluate(((BinaryNode *)tree)->right);
}
default:
return 0;
}
}
}