Calculator Step 4

- Main Page
- Classes
- Files

- File List
- File Members

node_impl.cpp
Go to the documentation of this file.
1 #include <iomanip>
2 #include <iterator>
3 #include <ostream>
4 #include <sstream>
5 #include <utility>
6
7 #include "calc_error.hpp"
8 #include "node.hpp"
9 #include "node_impl.hpp"
10 #include "variables.hpp"
11
12 node_impl::node_impl()
13 {}
14
15 node_impl::~node_impl()
16 {}
17
18 void node_impl::print(std::ostream& stream, int indent)
19 const
20 {
21 print_node(stream, indent);
22 }
23
24 double node_impl::evaluate()
25 const
26 {
27 return evaluate_node();
28 }
29
30 std::string node_impl::to_string()
31 const
32 {
33 return evaluate_string();
34 }
35
36 identifier_list const& node_impl::get_parameters()
37 const
38 {
39 return evaluate_parameters();
40 }
41
42 std::string node_impl::evaluate_string()
43 const
44 {
45 std::ostringstream stream{};
46 stream << evaluate();
47 return stream.str();
48 }
49
50 identifier_list const& node_impl::evaluate_parameters()
51 const
52 {
53 static identifier_list const empty{};
54 return empty;
55 }
56
57
58 node_void::node_void()
59 : node_impl()
60 {}
61
62 void node_void::print_node(std::ostream& stream, int indent)
63 const
64 {
65 stream << std::setw(indent) << "" << "void\n";
66 }
67
68 double node_void::evaluate_node()
69 const
70 {
71 return 0.0;
72 }
73
74 std::string node_void::evaluate_string()
75 const
76 {
77 return std::string{};
78 }
79
80
81 node_number::node_number(double value)
82 : node_impl{}, value_{value}
83 {}
84
85 double node_number::value()
86 const
87 {
88 return value_;
89 }
90
91 void node_number::print_node(std::ostream& stream, int indent)
92 const
93 {
94 stream << std::setw(indent) << "" << value() << '\n';
95 }
96
97 double node_number::evaluate_node()
98 const
99 {
100 return value();
101 }
102
103 node_identifier::node_identifier(std::string name)
104 : node_impl{}, name_{std::move(name)}
105 {}
106
107 std::string const& node_identifier::name()
108 const
109 {
110 return name_;
111 }
112
113 void node_identifier::print_node(std::ostream& stream, int indent)
114 const
115 {
116 stream << std::setw(indent) << "" << "identifier:" << name() << '\n';
117 }
118
119 double node_identifier::evaluate_node()
120 const
121 {
122 return get_variable(name()).evaluate();
123 }
124
125 std::string node_identifier::evaluate_string()
126 const
127 {
128 return name();
129 }
130
131
132 node_function::node_function(identifier_list parameters, node definition)
133 : node_impl{}, parameters_{std::move(parameters)}, definition_{definition}
134 {}
135
136 identifier_list const& node_function::parameters()
137 const
138 {
139 return parameters_;
140 }
141
142 node node_function::definition()
143 const
144 {
145 return definition_;
146 }
147
148 void print_identifier_list(std::ostream& stream, identifier_list const& identifiers)
149 {
150 stream << '(';
151 char const* sep = "";
152 for (auto const& id : identifiers) {
153 stream << sep << id;
154 sep = ", ";
155 }
156 stream << ')';
157 }
158
159 void node_function::print_node(std::ostream& stream, int indent)
160 const
161 {
162 stream << std::setw(indent) << "" << "fun\n";
163 print_identifier_list(stream, parameters());
164 stream << '=';
165 definition().print(stream, indent + 2);
166 }
167
168 double node_function::evaluate_node()
169 const
170 {
171 return definition().evaluate();
172 }
173
174 identifier_list const& node_function::evaluate_parameters()
175 const
176 {
177 return parameters();
178 }
179
180
181 node_function_call::node_function_call(std::string name, node_list arguments)
182 : node_impl{}, name_{std::move(name)}, arguments_{std::move(arguments)}
183 {}
184
185 std::string node_function_call::name()
186 const
187 {
188 return name_;
189 }
190
191 node_list const& node_function_call::arguments()
192 const
193 {
194 return arguments_;
195 }
196
197 void node_function_call::print_node(std::ostream& stream, int indent)
198 const
199 {
200 stream << std::setw(indent) << "" << name() << "(\n";
201 std::size_t index{0};
202 for (auto const& arg : arguments()) {
203 stream << std::setw(indent+1) << "" << "arg " << index << ": ";
204 arg.print(stream, indent + 2);
205 ++index;
206 }
207 stream << std::setw(indent) << "" << ")\n";
208 }
209
210 double node_function_call::evaluate_node()
211 const
212 {
213 // Create a local symbol table, assigning all the node values to the parameters.
214 node function{get_function(name())};
215 identifier_list const& parameters{ function.get_parameters() };
216 if (parameters.size() != arguments().size())
217 throw function_error{name(), parameters.size(), arguments().size()};
218 else
219 {
220 // Create a local symbol table by assigning the arguments to the function parameters.
221 symbol_table locals{};
222 identifier_list::const_iterator parm{parameters.begin()};
223 for (auto const& arg : arguments()) {
224 locals.emplace(*parm, arg);
225 ++parm;
226 }
227 set_symbol_table syms{locals};
228 return function.evaluate();
229 }
230 }
231
232
233
234 node_unary::node_unary(node operand)
235 : node_impl(), operand_(operand)
236 {}
237
238 node node_unary::operand()
239 const
240 {
241 return operand_;
242 }
243
244 double node_unary::evaluate_operand()
245 const
246 {
247 return operand().evaluate();
248 }
249
250 node_binary::node_binary(node left, node right)
251 : left_(left), right_(right)
252 {}
253
254 node node_binary::left()
255 const
256 {
257 return left_;
258 }
259
260 node node_binary::right()
261 const
262 {
263 return right_;
264 }
265
266 double node_binary::evaluate_left()
267 const
268 {
269 return left().evaluate();
270 }
271
272 double node_binary::evaluate_right()
273 const
274 {
275 return right().evaluate();
276 }
277
278
279 node_negate::node_negate(node operand)
280 : node_unary(operand)
281 {}
282
283 void node_negate::print_node(std::ostream& stream, int indent)
284 const
285 {
286 stream << std::setw(indent) << "" << "-\n";
287 operand().print(stream, indent + 2);
288 }
289
290 double node_negate::evaluate_node()
291 const
292 {
293 return -evaluate_operand();
294 }
295
296
297 node_add::node_add(node left, node right)
298 : node_binary(left, right)
299 {}
300
301 void node_add::print_node(std::ostream& stream, int indent)
302 const
303 {
304 stream << std::setw(indent) << "" << "+\n";
305 left().print(stream, indent + 2);
306 right().print(stream, indent + 2);
307 }
308
309 double node_add::evaluate_node()
310 const
311 {
312 return evaluate_left() + evaluate_right();
313 }
314
315
316 node_subtract::node_subtract(node left, node right)
317 : node_binary(left, right)
318 {}
319
320 void node_subtract::print_node(std::ostream& stream, int indent)
321 const
322 {
323 stream << std::setw(indent) << "" << "-\n";
324 left().print(stream, indent + 2);
325 right().print(stream, indent + 2);
326 }
327
328 double node_subtract::evaluate_node()
329 const
330 {
331 return evaluate_left() - evaluate_right();
332 }
333
334
335 node_multiply::node_multiply(node left, node right)
336 : node_binary(left, right)
337 {}
338
339 void node_multiply::print_node(std::ostream& stream, int indent)
340 const
341 {
342 stream << std::setw(indent) << "" << "*\n";
343 left().print(stream, indent + 2);
344 right().print(stream, indent + 2);
345 }
346
347 double node_multiply::evaluate_node()
348 const
349 {
350 return evaluate_left() * evaluate_right();
351 }
352
353
354 node_divide::node_divide(node left, node right)
355 : node_binary(left, right)
356 {}
357
358 void node_divide::print_node(std::ostream& stream, int indent)
359 const
360 {
361 stream << std::setw(indent) << "" << "/\n";
362 left().print(stream, indent + 2);
363 right().print(stream, indent + 2);
364 }
365
366 double node_divide::evaluate_node()
367 const
368 {
369 return evaluate_left() / evaluate_right();
370 }
node_identifier::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:119
node_number::node_number
node_number(double value)
Definition: node_impl.cpp:81
node_binary::left_
node left_
Definition: node_impl.hpp:141
node_identifier::name_
std::string name_
Definition: node_impl.hpp:80
node_binary::evaluate_right
double evaluate_right() const
Definition: node_impl.cpp:272
get_function
node get_function(std::string const &name)
Definition: variables.cpp:61
node_function::definition_
node definition_
Definition: node_impl.hpp:97
node_unary::node_unary
node_unary(node operand)
Definition: node_impl.cpp:234
node_function_call::name
std::string name() const
Definition: node_impl.cpp:185
node_void::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:74
node_impl::evaluate_string
virtual std::string evaluate_string() const
Definition: node_impl.cpp:42
node_identifier::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:113
node_impl::evaluate_node
virtual double evaluate_node() const =0
node_function::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:168
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:191
node_divide::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:358
node_impl::node_impl
node_impl()
Definition: node_impl.cpp:12
node_subtract::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:320
node_binary::evaluate_left
double evaluate_left() const
Definition: node_impl.cpp:266
node_function_call::node_function_call
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:181
node_void::node_void
node_void()
Definition: node_impl.cpp:58
node_number::value_
double value_
Definition: node_impl.hpp:65
node_binary::node_binary
node_binary(node left, node right)
Definition: node_impl.cpp:250
node_impl
Definition: node_impl.hpp:17
node
Definition: node.hpp:26
calc_error.hpp
node_subtract::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:328
node_multiply::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:347
node_identifier::name
std::string const & name() const
Definition: node_impl.cpp:107
node_void::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:62
node_identifier::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:125
node_identifier::node_identifier
node_identifier(std::string name)
Definition: node_impl.cpp:103
node_function::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:159
node_function_call::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:210
node_number::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:97
node_impl::print
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:18
node_unary::operand_
node operand_
Definition: node_impl.hpp:125
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
set_symbol_table
Definition: variables.hpp:19
node_number::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:91
node_binary
Definition: node_impl.hpp:129
node_impl::~node_impl
virtual ~node_impl()
Definition: node_impl.cpp:15
node_impl.hpp
node_unary
Definition: node_impl.hpp:115
node_binary::right
node right() const
Definition: node_impl.cpp:260
node_function::node_function
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:132
node_unary::evaluate_operand
double evaluate_operand() const
Definition: node_impl.cpp:244
node_function::definition
node definition() const
Definition: node_impl.cpp:142
node_multiply::node_multiply
node_multiply(node left, node right)
Definition: node_impl.cpp:335
node_function::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:174
function_error
Definition: calc_error.hpp:34
node_function_call::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:197
node::print
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:51
print_identifier_list
void print_identifier_list(std::ostream &stream, identifier_list const &identifiers)
Definition: node_impl.cpp:148
node_negate::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:283
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:111
node_impl::print_node
virtual void print_node(std::ostream &stream, int indent) const =0
node_function::parameters_
identifier_list parameters_
Definition: node_impl.hpp:96
node_binary::right_
node right_
Definition: node_impl.hpp:142
node_subtract::node_subtract
node_subtract(node left, node right)
Definition: node_impl.cpp:316
node_impl::evaluate
double evaluate() const
Definition: node_impl.cpp:24
node_void::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:68
node_add::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:301
node.hpp
node::evaluate
double evaluate() const
Definition: node.cpp:57
node_impl::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
node_divide::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:366
node::get_parameters
identifier_list const & get_parameters() const
Definition: node.cpp:69
node_impl::get_parameters
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
node_number::value
double value() const
Definition: node_impl.cpp:85
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
node_function_call::name_
std::string name_
Definition: node_impl.hpp:110
node_impl::to_string
std::string to_string() const
Definition: node_impl.cpp:30
node_unary::operand
node operand() const
Definition: node_impl.cpp:238
node_negate::node_negate
node_negate(node operand)
Definition: node_impl.cpp:279
variables.hpp
node_multiply::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:339
node_add::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:309
node_divide::node_divide
node_divide(node left, node right)
Definition: node_impl.cpp:354
node_add::node_add
node_add(node left, node right)
Definition: node_impl.cpp:297
node_binary::left
node left() const
Definition: node_impl.cpp:254
node_function::parameters
identifier_list const & parameters() const
Definition: node_impl.cpp:136
get_variable
node get_variable(std::string const &name)
Definition: variables.cpp:45
symbol_table
std::map< std::string, node > symbol_table
Definition: variables.hpp:13
node_negate::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:290