Calculator Step 3

- Main Page
- Classes
- Files

- File List
- File Members

node_impl.cpp
Go to the documentation of this file.
1 #include <iomanip>
2 #include <ostream>
3 #include <sstream>
4
5 #include "node.hpp"
6 #include "node_impl.hpp"
7 #include "variables.hpp"
8
9 node_impl::node_impl()
10 {}
11
12 node_impl::~node_impl()
13 {}
14
15 void node_impl::print(std::ostream& stream, int indent)
16 const
17 {
18 print_node(stream, indent);
19 }
20
21 double node_impl::evaluate()
22 const
23 {
24 return evaluate_node();
25 }
26
27 std::string node_impl::to_string()
28 const
29 {
30 return evaluate_string();
31 }
32
33 std::string node_impl::evaluate_string()
34 const
35 {
36 std::ostringstream stream{};
37 stream << evaluate();
38 return stream.str();
39 }
40
41
42 node_void::node_void()
43 : node_impl{}
44 {}
45
46 void node_void::print_node(std::ostream& stream, int indent)
47 const
48 {
49 stream << std::setw(indent) << "" << "void\n";
50 }
51
52 double node_void::evaluate_node()
53 const
54 {
55 return 0.0;
56 }
57
58 std::string node_void::evaluate_string()
59 const
60 {
61 return std::string{};
62 }
63
64
65 node_number::node_number(double value)
66 : node_impl{}, value_{value}
67 {}
68
69 double node_number::value()
70 const
71 {
72 return value_;
73 }
74
75 void node_number::print_node(std::ostream& stream, int indent)
76 const
77 {
78 stream << std::setw(indent) << "" << value() << '\n';
79 }
80
81 double node_number::evaluate_node()
82 const
83 {
84 return value();
85 }
86
87
88 node_identifier::node_identifier(std::string identifier)
89 : node_impl{}, identifier_{std::move(identifier)}
90 {}
91
92 std::string const& node_identifier::identifier()
93 const
94 {
95 return identifier_;
96 }
97
98 void node_identifier::print_node(std::ostream& stream, int indent)
99 const
100 {
101 stream << std::setw(indent) << "" << identifier() << '\n';
102 }
103
104 double node_identifier::evaluate_node()
105 const
106 {
107 return get_variable(identifier());
108 }
109
110 std::string node_identifier::evaluate_string()
111 const
112 {
113 return identifier();
114 }
115
116
117 node_assign::node_assign(node identifier, node value)
118 : node_impl{}, identifier_{identifier}, value_{value}
119 {}
120
121 node node_assign::identifier()
122 const
123 {
124 return identifier_;
125 }
126
127 node node_assign::value()
128 const
129 {
130 return value_;
131 }
132
133 std::string node_assign::get_identifier()
134 const
135 {
136 return identifier().to_string();
137 }
138
139 double node_assign::evaluate_value()
140 const
141 {
142 return value().evaluate();
143 }
144
145 void node_assign::print_node(std::ostream& stream, int indent)
146 const
147 {
148 stream << std::setw(indent) << "" << get_identifier() << ":=\n";
149 value().print(stream, indent + 2);
150 }
151
152 double node_assign::evaluate_node()
153 const
154 {
155 double result( evaluate_value() );
156 set_variable(get_identifier(), result);
157 return result;
158 }
159
160
161
162 node_unary::node_unary(node operand)
163 : node_impl{}, operand_{operand}
164 {}
165
166 node node_unary::operand()
167 const
168 {
169 return operand_;
170 }
171
172 double node_unary::evaluate_operand()
173 const
174 {
175 return operand().evaluate();
176 }
177
178 node_binary::node_binary(node left, node right)
179 : node_impl{}, left_{left}, right_{right}
180 {}
181
182 node node_binary::left()
183 const
184 {
185 return left_;
186 }
187
188 node node_binary::right()
189 const
190 {
191 return right_;
192 }
193
194 double node_binary::evaluate_left()
195 const
196 {
197 return left().evaluate();
198 }
199
200 double node_binary::evaluate_right()
201 const
202 {
203 return right().evaluate();
204 }
205
206
207 node_negate::node_negate(node operand)
208 : node_unary{operand}
209 {}
210
211 void node_negate::print_node(std::ostream& stream, int indent)
212 const
213 {
214 stream << std::setw(indent) << "" << "-\n";
215 operand().print(stream, indent + 2);
216 }
217
218 double node_negate::evaluate_node()
219 const
220 {
221 return -evaluate_operand();
222 }
223
224
225 node_add::node_add(node left, node right)
226 : node_binary{left, right}
227 {}
228
229 void node_add::print_node(std::ostream& stream, int indent)
230 const
231 {
232 stream << std::setw(indent) << "" << "+\n";
233 left().print(stream, indent + 2);
234 right().print(stream, indent + 2);
235 }
236
237 double node_add::evaluate_node()
238 const
239 {
240 return evaluate_left() + evaluate_right();
241 }
242
243
244 node_subtract::node_subtract(node left, node right)
245 : node_binary{left, right}
246 {}
247
248 void node_subtract::print_node(std::ostream& stream, int indent)
249 const
250 {
251 stream << std::setw(indent) << "" << "-\n";
252 left().print(stream, indent + 2);
253 right().print(stream, indent + 2);
254 }
255
256 double node_subtract::evaluate_node()
257 const
258 {
259 return evaluate_left() - evaluate_right();
260 }
261
262
263 node_multiply::node_multiply(node left, node right)
264 : node_binary{left, right}
265 {}
266
267 void node_multiply::print_node(std::ostream& stream, int indent)
268 const
269 {
270 stream << std::setw(indent) << "" << "*\n";
271 left().print(stream, indent + 2);
272 right().print(stream, indent + 2);
273 }
274
275 double node_multiply::evaluate_node()
276 const
277 {
278 return evaluate_left() * evaluate_right();
279 }
280
281
282 node_divide::node_divide(node left, node right)
283 : node_binary{left, right}
284 {}
285
286 void node_divide::print_node(std::ostream& stream, int indent)
287 const
288 {
289 stream << std::setw(indent) << "" << "/\n";
290 left().print(stream, indent + 2);
291 right().print(stream, indent + 2);
292 }
293
294 double node_divide::evaluate_node()
295 const
296 {
297 return evaluate_left() / evaluate_right();
298 }
node_identifier::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:104
node_number::node_number
node_number(double value)
Definition: node_impl.cpp:65
node_assign::identifier_
node identifier_
Definition: node_impl.hpp:93
node_binary::left_
node left_
Definition: node_impl.hpp:124
node_binary::evaluate_right
double evaluate_right() const
Definition: node_impl.cpp:200
node_unary::node_unary
node_unary(node operand)
Definition: node_impl.cpp:162
node_void::evaluate_string
std::string evaluate_string() const override
Definition: node_impl.cpp:58
node_impl::evaluate_string
virtual std::string evaluate_string() const
Definition: node_impl.cpp:33
node_identifier::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:98
node_impl::evaluate_node
virtual double evaluate_node() const =0
node_assign::value
node value() const
Definition: node_impl.cpp:127
node_divide::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:286
node_impl::node_impl
node_impl()
Definition: node_impl.cpp:9
node_subtract::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:248
node_binary::evaluate_left
double evaluate_left() const
Definition: node_impl.cpp:194
node_identifier::node_identifier
node_identifier(std::string identifier)
Definition: node_impl.cpp:88
node_assign::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:145
node_void::node_void
node_void()
Definition: node_impl.cpp:42
set_variable
void set_variable(std::string name, double value)
Definition: variables.cpp:23
node_number::value_
double value_
Definition: node_impl.hpp:60
node_binary::node_binary
node_binary(node left, node right)
Definition: node_impl.cpp:178
node_impl
Definition: node_impl.hpp:17
node
Definition: node.hpp:16
node_subtract::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:256
node_multiply::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:275
node_void::print_node
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:46
node_identifier::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:110
node_assign::value_
node value_
Definition: node_impl.hpp:94
node_number::evaluate_node
double evaluate_node() const override
Definition: node_impl.cpp:81
node_impl::print
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:15
node_unary::operand_
node operand_
Definition: node_impl.hpp:108
node_assign::node_assign
node_assign(node identifier, node value)
Definition: node_impl.cpp:117
node_number::print_node
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:75
node_binary
Definition: node_impl.hpp:112
node_impl::~node_impl
virtual ~node_impl()
Definition: node_impl.cpp:12
node_impl.hpp
node_unary
Definition: node_impl.hpp:98
node_binary::right
node right() const
Definition: node_impl.cpp:188
node_unary::evaluate_operand
double evaluate_operand() const
Definition: node_impl.cpp:172
node_assign::get_identifier
std::string get_identifier() const
Definition: node_impl.cpp:133
node_identifier::identifier_
std::string identifier_
Definition: node_impl.hpp:75
node_multiply::node_multiply
node_multiply(node left, node right)
Definition: node_impl.cpp:263
node_assign::evaluate_value
double evaluate_value() const
Definition: node_impl.cpp:139
node::print
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:44
node_negate::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:211
node_impl::print_node
virtual void print_node(std::ostream &stream, int indent) const =0
node_assign::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:152
node_binary::right_
node right_
Definition: node_impl.hpp:125
node_subtract::node_subtract
node_subtract(node left, node right)
Definition: node_impl.cpp:244
node_impl::evaluate
double evaluate() const
Definition: node_impl.cpp:21
node_assign::identifier
node identifier() const
Definition: node_impl.cpp:121
node_void::evaluate_node
double evaluate_node() const override
Definition: node_impl.cpp:52
node_add::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:229
node::to_string
std::string to_string() const
Definition: node.cpp:56
node.hpp
node::evaluate
double evaluate() const
Definition: node.cpp:50
node_divide::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:294
node_number::value
double value() const
Definition: node_impl.cpp:69
node_impl::to_string
std::string to_string() const
Definition: node_impl.cpp:27
node_unary::operand
node operand() const
Definition: node_impl.cpp:166
node_negate::node_negate
node_negate(node operand)
Definition: node_impl.cpp:207
get_variable
double get_variable(std::string const &name)
Definition: variables.cpp:18
variables.hpp
node_multiply::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:267
node_add::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:237
node_divide::node_divide
node_divide(node left, node right)
Definition: node_impl.cpp:282
node_add::node_add
node_add(node left, node right)
Definition: node_impl.cpp:225
node_binary::left
node left() const
Definition: node_impl.cpp:182
node_negate::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:218
node_identifier::identifier
std::string const & identifier() const
Definition: node_impl.cpp:92