Calculator  Step 4
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 
13 {}
14 
16 {}
17 
18 void node_impl::print(std::ostream& stream, int indent)
19 const
20 {
21  print_node(stream, indent);
22 }
23 
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 
37 const
38 {
39  return evaluate_parameters();
40 }
41 
43 const
44 {
45  std::ostringstream stream{};
46  stream << evaluate();
47  return stream.str();
48 }
49 
51 const
52 {
53  static identifier_list const empty{};
54  return empty;
55 }
56 
57 
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 
69 const
70 {
71  return 0.0;
72 }
73 
75 const
76 {
77  return std::string{};
78 }
79 
80 
82 : node_impl{}, value_{value}
83 {}
84 
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 
98 const
99 {
100  return value();
101 }
102 
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 
120 const
121 {
122  return get_variable(name()).evaluate();
123 }
124 
126 const
127 {
128  return name();
129 }
130 
131 
133 : node_impl{}, parameters_{std::move(parameters)}, definition_{definition}
134 {}
135 
137 const
138 {
139  return parameters_;
140 }
141 
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";
164  stream << '=';
165  definition().print(stream, indent + 2);
166 }
167 
169 const
170 {
171  return definition().evaluate();
172 }
173 
175 const
176 {
177  return parameters();
178 }
179 
180 
182 : node_impl{}, name_{std::move(name)}, arguments_{std::move(arguments)}
183 {}
184 
186 const
187 {
188  return name_;
189 }
190 
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 
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 
235 : node_impl(), operand_(operand)
236 {}
237 
239 const
240 {
241  return operand_;
242 }
243 
245 const
246 {
247  return operand().evaluate();
248 }
249 
251 : left_(left), right_(right)
252 {}
253 
255 const
256 {
257  return left_;
258 }
259 
261 const
262 {
263  return right_;
264 }
265 
267 const
268 {
269  return left().evaluate();
270 }
271 
273 const
274 {
275  return right().evaluate();
276 }
277 
278 
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 
291 const
292 {
293  return -evaluate_operand();
294 }
295 
296 
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 
310 const
311 {
312  return evaluate_left() + evaluate_right();
313 }
314 
315 
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 
329 const
330 {
331  return evaluate_left() - evaluate_right();
332 }
333 
334 
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 
348 const
349 {
350  return evaluate_left() * evaluate_right();
351 }
352 
353 
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 
367 const
368 {
369  return evaluate_left() / evaluate_right();
370 }
virtual double evaluate_node() const override
Definition: node_impl.cpp:119
node_number(double value)
Definition: node_impl.cpp:81
std::string name_
Definition: node_impl.hpp:80
double evaluate_right() const
Definition: node_impl.cpp:272
node get_function(std::string const &name)
Definition: variables.cpp:61
node definition_
Definition: node_impl.hpp:97
node_unary(node operand)
Definition: node_impl.cpp:234
std::string name() const
Definition: node_impl.cpp:185
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:74
virtual std::string evaluate_string() const
Definition: node_impl.cpp:42
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:113
virtual double evaluate_node() const =0
virtual double evaluate_node() const override
Definition: node_impl.cpp:168
node_list const & arguments() const
Definition: node_impl.cpp:191
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:358
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:320
double evaluate_left() const
Definition: node_impl.cpp:266
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:181
double value_
Definition: node_impl.hpp:65
node_binary(node left, node right)
Definition: node_impl.cpp:250
Definition: node.hpp:26
virtual double evaluate_node() const override
Definition: node_impl.cpp:328
virtual double evaluate_node() const override
Definition: node_impl.cpp:347
std::string const & name() const
Definition: node_impl.cpp:107
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:62
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:125
node_identifier(std::string name)
Definition: node_impl.cpp:103
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:159
virtual double evaluate_node() const override
Definition: node_impl.cpp:210
virtual double evaluate_node() const override
Definition: node_impl.cpp:97
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:18
node operand_
Definition: node_impl.hpp:125
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:91
virtual ~node_impl()
Definition: node_impl.cpp:15
node right() const
Definition: node_impl.cpp:260
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:132
double evaluate_operand() const
Definition: node_impl.cpp:244
node definition() const
Definition: node_impl.cpp:142
node_multiply(node left, node right)
Definition: node_impl.cpp:335
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:174
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:197
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:51
void print_identifier_list(std::ostream &stream, identifier_list const &identifiers)
Definition: node_impl.cpp:148
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:283
node_list arguments_
Definition: node_impl.hpp:111
virtual void print_node(std::ostream &stream, int indent) const =0
identifier_list parameters_
Definition: node_impl.hpp:96
node_subtract(node left, node right)
Definition: node_impl.cpp:316
double evaluate() const
Definition: node_impl.cpp:24
virtual double evaluate_node() const override
Definition: node_impl.cpp:68
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:301
double evaluate() const
Definition: node.cpp:57
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
virtual double evaluate_node() const override
Definition: node_impl.cpp:366
identifier_list const & get_parameters() const
Definition: node.cpp:69
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
double value() const
Definition: node_impl.cpp:85
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
std::string name_
Definition: node_impl.hpp:110
std::string to_string() const
Definition: node_impl.cpp:30
node operand() const
Definition: node_impl.cpp:238
node_negate(node operand)
Definition: node_impl.cpp:279
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:339
virtual double evaluate_node() const override
Definition: node_impl.cpp:309
node_divide(node left, node right)
Definition: node_impl.cpp:354
node_add(node left, node right)
Definition: node_impl.cpp:297
node left() const
Definition: node_impl.cpp:254
identifier_list const & parameters() const
Definition: node_impl.cpp:136
node get_variable(std::string const &name)
Definition: variables.cpp:45
std::map< std::string, node > symbol_table
Definition: variables.hpp:13
virtual double evaluate_node() const override
Definition: node_impl.cpp:290