-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.cpp
126 lines (124 loc) · 3.67 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "evaluator.hpp"
long double evaluate(Expression* expression) {
long double test_eval, ans;
switch (expression->getType())
{
case CONST_E:
{
Const_E *cExpr = (Const_E*) expression;
ans = cExpr->getValue();
break;
}
case NEG_E:
{
Neg_E *nExpr = (Neg_E*) expression;
ans = -evaluate(nExpr->getExpr());
break;
}
case BINARYOP_E:
{
BinaryOp_E *bExpr = (BinaryOp_E*) expression;
Expression *lExpr = bExpr->getLeftExpr(), *rExpr = bExpr->getRightExpr();
switch(bExpr->getOperation()) {
case ADD:
ans = evaluate(lExpr) + evaluate(rExpr);
break;
case SUB:
ans = evaluate(lExpr) - evaluate(rExpr);
break;
case MUL:
ans = evaluate(lExpr) * evaluate(rExpr);
break;
case DIV:
test_eval = evaluate(rExpr);
if (test_eval == 0) {
std::cerr << "[EVAL ERROR] : DIVISION BY ZERO [ " << prettyPrint(expression, false) << " ]" << std::endl;
exit(EXIT_FAILURE);
}
ans = evaluate(lExpr) / test_eval;
break;
case EXP:
ans = std::pow(evaluate(lExpr), evaluate(rExpr));
break;
case MOD:
ans = std::fmod(evaluate(lExpr), evaluate(rExpr));
break;
default:
std::cerr << "Unexpected binary operation! Are you trying to use NEG as an infix operator?" << std::endl;
exit(EXIT_FAILURE);
break;
}
break;
}
default:
{
std::cerr << "Unexpected expression type! Are you trying to evaluate an expression with no type?" << std::endl;
exit(EXIT_FAILURE);
break;
}
}
delete expression;
//std::cout << "del expr" << std::endl;
return ans;
}
std::string prettyPrint(Expression* expression, bool parens)
{
std::string lparen = "", rparen = "";
if (parens) {
lparen = "(";
rparen = ")";
}
switch (expression->getType())
{
case CONST_E:
{
Const_E *cExpr = (Const_E*) expression;
return std::to_string(cExpr->getValue());
break;
}
case NEG_E:
{
Neg_E *nExpr = (Neg_E*) expression;
return lparen + "-" + prettyPrint(nExpr->getExpr(), true) + rparen;
break;
}
case BINARYOP_E:
{
BinaryOp_E *bExpr = (BinaryOp_E*) expression;
Expression *lExpr = bExpr->getLeftExpr(), *rExpr = bExpr->getRightExpr();
switch(bExpr->getOperation()) {
case ADD:
return lparen + prettyPrint(lExpr, true) + " + " + prettyPrint(rExpr, true) + rparen;
break;
case SUB:
return lparen + prettyPrint(lExpr, true) + " - " + prettyPrint(rExpr, true) + rparen;
break;
case MUL:
return lparen + prettyPrint(lExpr, true) + " * " + prettyPrint(rExpr, true) + rparen;
break;
case DIV:
return lparen + prettyPrint(lExpr, true) + " / " + prettyPrint(rExpr, true) + rparen;
break;
case EXP:
return lparen + prettyPrint(lExpr, true) + " ^ " + prettyPrint(rExpr, true) + rparen;
break;
case MOD:
return lparen + prettyPrint(lExpr, true) + " % " + prettyPrint(rExpr, true) + rparen;
break;
default:
std::cerr << "Unexpected binary operation! Are you trying to print NEG as an infix operator?" << std::endl;
exit(EXIT_FAILURE);
break;
}
break;
}
default:
{
std::cerr << "Unexpected expression type! Are you trying to print an expression with no type?" << std::endl;
exit(EXIT_FAILURE);
break;
}
}
std::cerr << "PRETTYPRINT ERROR" << std::endl;
exit(EXIT_FAILURE);
}