Calculator  Step 3
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 
10 {}
11 
13 {}
14 
15 void node_impl::print(std::ostream& stream, int indent)
16 const
17 {
18  print_node(stream, indent);
19 }
20 
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 
34 const
35 {
36  std::ostringstream stream{};
37  stream << evaluate();
38  return stream.str();
39 }
40 
41 
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 
53 const
54 {
55  return 0.0;
56 }
57 
59 const
60 {
61  return std::string{};
62 }
63 
64 
66 : node_impl{}, value_{value}
67 {}
68 
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 
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 
105 const
106 {
107  return get_variable(identifier());
108 }
109 
111 const
112 {
113  return identifier();
114 }
115 
116 
118 : node_impl{}, identifier_{identifier}, value_{value}
119 {}
120 
122 const
123 {
124  return identifier_;
125 }
126 
128 const
129 {
130  return value_;
131 }
132 
134 const
135 {
136  return identifier().to_string();
137 }
138 
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 
153 const
154 {
155  double result( evaluate_value() );
156  set_variable(get_identifier(), result);
157  return result;
158 }
159 
160 
161 
163 : node_impl{}, operand_{operand}
164 {}
165 
167 const
168 {
169  return operand_;
170 }
171 
173 const
174 {
175  return operand().evaluate();
176 }
177 
179 : node_impl{}, left_{left}, right_{right}
180 {}
181 
183 const
184 {
185  return left_;
186 }
187 
189 const
190 {
191  return right_;
192 }
193 
195 const
196 {
197  return left().evaluate();
198 }
199 
201 const
202 {
203  return right().evaluate();
204 }
205 
206 
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 
219 const
220 {
221  return -evaluate_operand();
222 }
223 
224 
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 
238 const
239 {
240  return evaluate_left() + evaluate_right();
241 }
242 
243 
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 
257 const
258 {
259  return evaluate_left() - evaluate_right();
260 }
261 
262 
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 
276 const
277 {
278  return evaluate_left() * evaluate_right();
279 }
280 
281 
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 
295 const
296 {
297  return evaluate_left() / evaluate_right();
298 }
virtual double evaluate_node() const override
Definition: node_impl.cpp:104
node_number(double value)
Definition: node_impl.cpp:65
node identifier_
Definition: node_impl.hpp:93
double evaluate_right() const
Definition: node_impl.cpp:200
node_unary(node operand)
Definition: node_impl.cpp:162
std::string evaluate_string() const override
Definition: node_impl.cpp:58
virtual std::string evaluate_string() const
Definition: node_impl.cpp:33
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:98
virtual double evaluate_node() const =0
node value() const
Definition: node_impl.cpp:127
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:286
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:248
double evaluate_left() const
Definition: node_impl.cpp:194
node_identifier(std::string identifier)
Definition: node_impl.cpp:88
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:145
void set_variable(std::string name, double value)
Definition: variables.cpp:23
double value_
Definition: node_impl.hpp:60
node_binary(node left, node right)
Definition: node_impl.cpp:178
Definition: node.hpp:16
virtual double evaluate_node() const override
Definition: node_impl.cpp:256
virtual double evaluate_node() const override
Definition: node_impl.cpp:275
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:46
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:110
double evaluate_node() const override
Definition: node_impl.cpp:81
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:15
node operand_
Definition: node_impl.hpp:108
node_assign(node identifier, node value)
Definition: node_impl.cpp:117
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:75
virtual ~node_impl()
Definition: node_impl.cpp:12
node right() const
Definition: node_impl.cpp:188
double evaluate_operand() const
Definition: node_impl.cpp:172
std::string get_identifier() const
Definition: node_impl.cpp:133
std::string identifier_
Definition: node_impl.hpp:75
node_multiply(node left, node right)
Definition: node_impl.cpp:263
double evaluate_value() const
Definition: node_impl.cpp:139
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:44
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:211
virtual void print_node(std::ostream &stream, int indent) const =0
virtual double evaluate_node() const override
Definition: node_impl.cpp:152
node_subtract(node left, node right)
Definition: node_impl.cpp:244
double evaluate() const
Definition: node_impl.cpp:21
node identifier() const
Definition: node_impl.cpp:121
double evaluate_node() const override
Definition: node_impl.cpp:52
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:229
std::string to_string() const
Definition: node.cpp:56
double evaluate() const
Definition: node.cpp:50
virtual double evaluate_node() const override
Definition: node_impl.cpp:294
double value() const
Definition: node_impl.cpp:69
std::string to_string() const
Definition: node_impl.cpp:27
node operand() const
Definition: node_impl.cpp:166
node_negate(node operand)
Definition: node_impl.cpp:207
double get_variable(std::string const &name)
Definition: variables.cpp:18
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:267
virtual double evaluate_node() const override
Definition: node_impl.cpp:237
node_divide(node left, node right)
Definition: node_impl.cpp:282
node_add(node left, node right)
Definition: node_impl.cpp:225
node left() const
Definition: node_impl.cpp:182
virtual double evaluate_node() const override
Definition: node_impl.cpp:218
std::string const & identifier() const
Definition: node_impl.cpp:92