1
+ #include " evaluator.hpp"
2
+
3
+ long double evaluate (Expression* expression) {
4
+ long double test_eval, ans;
5
+ switch (expression->getType ())
6
+ {
7
+ case CONST_E:
8
+ {
9
+ Const_E *cExpr = (Const_E*) expression;
10
+ ans = cExpr->getValue ();
11
+ break ;
12
+ }
13
+ case NEG_E:
14
+ {
15
+ Neg_E *nExpr = (Neg_E*) expression;
16
+ ans = -evaluate (nExpr->getExpr ());
17
+ break ;
18
+ }
19
+ case BINARYOP_E:
20
+ {
21
+ BinaryOp_E *bExpr = (BinaryOp_E*) expression;
22
+ Expression *lExpr = bExpr->getLeftExpr (), *rExpr = bExpr->getRightExpr ();
23
+ switch (bExpr->getOperation ()) {
24
+ case ADD:
25
+ ans = evaluate (lExpr) + evaluate (rExpr);
26
+ break ;
27
+ case SUB:
28
+ ans = evaluate (lExpr) - evaluate (rExpr);
29
+ break ;
30
+ case MUL:
31
+ ans = evaluate (lExpr) * evaluate (rExpr);
32
+ break ;
33
+ case DIV:
34
+ test_eval = evaluate (rExpr);
35
+ if (test_eval == 0 ) {
36
+ std::cerr << " [EVAL ERROR] : DIVISION BY ZERO [ " << prettyPrint (expression, false ) << " ]" << std::endl;
37
+ exit (EXIT_FAILURE);
38
+ }
39
+ ans = evaluate (lExpr) / test_eval;
40
+ break ;
41
+ case EXP:
42
+ ans = std::pow (evaluate (lExpr), evaluate (rExpr));
43
+ break ;
44
+ case MOD:
45
+ ans = std::fmod (evaluate (lExpr), evaluate (rExpr));
46
+ break ;
47
+ default :
48
+ std::cerr << " Unexpected binary operation! Are you trying to use NEG as an infix operator?" << std::endl;
49
+ exit (EXIT_FAILURE);
50
+ break ;
51
+ }
52
+ break ;
53
+ }
54
+ default :
55
+ {
56
+ std::cerr << " Unexpected expression type! Are you trying to evaluate an expression with no type?" << std::endl;
57
+ exit (EXIT_FAILURE);
58
+ break ;
59
+ }
60
+ }
61
+ delete expression;
62
+ // std::cout << "del expr" << std::endl;
63
+ return ans;
64
+ }
65
+
66
+ std::string prettyPrint (Expression* expression, bool parens)
67
+ {
68
+ std::string lparen = " " , rparen = " " ;
69
+ if (parens) {
70
+ lparen = " (" ;
71
+ rparen = " )" ;
72
+ }
73
+ switch (expression->getType ())
74
+ {
75
+ case CONST_E:
76
+ {
77
+ Const_E *cExpr = (Const_E*) expression;
78
+ return std::to_string (cExpr->getValue ());
79
+ break ;
80
+ }
81
+ case NEG_E:
82
+ {
83
+ Neg_E *nExpr = (Neg_E*) expression;
84
+ return lparen + " -" + prettyPrint (nExpr->getExpr (), true ) + rparen;
85
+ break ;
86
+ }
87
+ case BINARYOP_E:
88
+ {
89
+ BinaryOp_E *bExpr = (BinaryOp_E*) expression;
90
+ Expression *lExpr = bExpr->getLeftExpr (), *rExpr = bExpr->getRightExpr ();
91
+ switch (bExpr->getOperation ()) {
92
+ case ADD:
93
+ return lparen + prettyPrint (lExpr, true ) + " + " + prettyPrint (rExpr, true ) + rparen;
94
+ break ;
95
+ case SUB:
96
+ return lparen + prettyPrint (lExpr, true ) + " - " + prettyPrint (rExpr, true ) + rparen;
97
+ break ;
98
+ case MUL:
99
+ return lparen + prettyPrint (lExpr, true ) + " * " + prettyPrint (rExpr, true ) + rparen;
100
+ break ;
101
+ case DIV:
102
+ return lparen + prettyPrint (lExpr, true ) + " / " + prettyPrint (rExpr, true ) + rparen;
103
+ break ;
104
+ case EXP:
105
+ return lparen + prettyPrint (lExpr, true ) + " ^ " + prettyPrint (rExpr, true ) + rparen;
106
+ break ;
107
+ case MOD:
108
+ return lparen + prettyPrint (lExpr, true ) + " % " + prettyPrint (rExpr, true ) + rparen;
109
+ break ;
110
+ default :
111
+ std::cerr << " Unexpected binary operation! Are you trying to print NEG as an infix operator?" << std::endl;
112
+ exit (EXIT_FAILURE);
113
+ break ;
114
+ }
115
+ break ;
116
+ }
117
+ default :
118
+ {
119
+ std::cerr << " Unexpected expression type! Are you trying to print an expression with no type?" << std::endl;
120
+ exit (EXIT_FAILURE);
121
+ break ;
122
+ }
123
+ }
124
+ std::cerr << " PRETTYPRINT ERROR" << std::endl;
125
+ exit (EXIT_FAILURE);
126
+ }
0 commit comments