-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.hpp
348 lines (288 loc) · 7.4 KB
/
expression.hpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// $Id: expression.h 48 2009-09-05 08:07:10Z tb $
/** \file expression.h Implements an example calculator class group. */
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <map>
#include <vector>
#include <ostream>
#include <stdexcept>
#include <cmath>
/** CalcNode is the abstract base class for calculation nodes. From it the
* different nullary, unary and binary nodes are derived. */
class CalcNode
{
public:
/// required for virtual functions. in the derived classes the operands are
/// deleted.
virtual ~CalcNode()
{
}
/// evaluate the complete calculation tree and return the floating point
/// result value
virtual double evaluate() const = 0;
/// output the calculation tree to the given stream. tries to format the
/// output to make tree levels visible.
virtual void print(std::ostream &os, unsigned int depth=0) const = 0;
/// helper function for print() which makes the indention string
static inline std::string indent(unsigned int d)
{
return std::string(d * 2, ' ');
}
};
/** Calculation node always returning a constant value. */
class CNConstant : public CalcNode
{
/// the constant value returned
double value;
public:
/// construct a constant calculation node from a value
explicit CNConstant(double _value)
: CalcNode(), value(_value)
{
}
virtual double evaluate() const
{
return value;
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << value << std::endl;
}
};
/** Calculation node negating the value of the operand subtree. */
class CNNegate : public CalcNode
{
/// calculation subtree
CalcNode* node;
public:
explicit CNNegate(CalcNode* _node)
: CalcNode(), node(_node)
{
}
virtual ~CNNegate()
{
delete node;
}
virtual double evaluate() const
{
return - node->evaluate();
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "- negate" << std::endl;
node->print(os, depth+1);
}
};
/** Calculation node adding two operand nodes. */
class CNAdd : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNAdd(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNAdd()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return left->evaluate() + right->evaluate();
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "+ add" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculation node subtracting two operand nodes. */
class CNSubtract : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNSubtract(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNSubtract()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return left->evaluate() - right->evaluate();
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "- subtract" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculation node multiplying two operand nodes. */
class CNMultiply : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNMultiply(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNMultiply()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return left->evaluate() * right->evaluate();
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "* multiply" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculation node dividing two operand nodes. */
class CNDivide : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNDivide(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNDivide()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return left->evaluate() / right->evaluate();
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "/ divide" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculation node calculating the remainder of an integer division of two
* operand nodes. */
class CNModulo : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNModulo(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNModulo()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return std::fmod(left->evaluate(), right->evaluate());
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "% modulo" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculation node raising one operand to the power of the second. */
class CNPower : public CalcNode
{
/// left calculation operand
CalcNode* left;
/// right calculation operand
CalcNode* right;
public:
explicit CNPower(CalcNode* _left, CalcNode* _right)
: CalcNode(), left(_left), right(_right)
{
}
virtual ~CNPower()
{
delete left;
delete right;
}
virtual double evaluate() const
{
return std::pow(left->evaluate(), right->evaluate());
}
virtual void print(std::ostream &os, unsigned int depth) const
{
os << indent(depth) << "^ power" << std::endl;
left->print(os, depth+1);
right->print(os, depth+1);
}
};
/** Calculator context used to save the parsed expressions. This context is
* passed along to the example::Driver class and fill during parsing via bison
* actions. */
class CalcContext
{
public:
/// type of the variable storage
typedef std::map<std::string, double> variablemap_type;
/// variable storage. maps variable string to doubles
variablemap_type variables;
/// array of unassigned expressions found by the parser. these are then
/// outputted to the user.
std::vector<CalcNode*> expressions;
/// free the saved expression trees
~CalcContext()
{
clearExpressions();
}
/// free all saved expression trees
void clearExpressions()
{
for(unsigned int i = 0; i < expressions.size(); ++i)
{
delete expressions[i];
}
expressions.clear();
}
/// check if the given variable name exists in the storage
bool existsVariable(const std::string &varname) const
{
return variables.find(varname) != variables.end();
}
/// return the given variable from the storage. throws an exception if it
/// does not exist.
double getVariable(const std::string &varname) const
{
variablemap_type::const_iterator vi = variables.find(varname);
if (vi == variables.end())
throw(std::runtime_error("Unknown variable."));
else
return vi->second;
}
};
#endif // EXPRESSION_H