Calculator  Step 4
node_impl.hpp
Go to the documentation of this file.
1 #ifndef NODE_IMPL_HPP_
2 #define NODE_IMPL_HPP_
3 
4 #include <cstdlib>
5 #include <string>
6 
7 #include "node.hpp"
8 
17 class node_impl {
18 public:
19  node_impl();
20  node_impl(node_impl&&) = default;
21  node_impl& operator=(node_impl&&) = default;
22  virtual ~node_impl();
23 
24  void print(std::ostream& stream, int indent) const;
25 
26  double evaluate() const;
27 
28  std::string to_string() const;
29 
30  identifier_list const& get_parameters() const;
31 
32 private:
33  node_impl(node_impl const&) = delete;
34  node_impl& operator=(node_impl const&) = delete;
35 
36  virtual void print_node(std::ostream& stream, int indent) const = 0;
37  virtual double evaluate_node() const = 0;
38  virtual std::string evaluate_string() const;
39  virtual identifier_list const& evaluate_parameters() const;
40 };
41 
45 class node_void : public node_impl {
46 public:
47  node_void();
48 private:
49  virtual void print_node(std::ostream& stream, int indent) const override;
50  virtual double evaluate_node() const override;
51  virtual std::string evaluate_string() const override;
52 };
53 
57 class node_number : public node_impl {
58 public:
59  node_number(double value);
60  double value() const;
61 private:
62  virtual void print_node(std::ostream& stream, int indent) const override;
63  virtual double evaluate_node() const override;
64 
65  double value_;
66 };
67 
71 class node_identifier : public node_impl {
72 public:
73  node_identifier(std::string name);
74  std::string const& name() const;
75 private:
76  virtual void print_node(std::ostream& stream, int indent) const override;
77  virtual double evaluate_node() const override;
78  virtual std::string evaluate_string() const override;
79 
80  std::string name_;
81 };
82 
86 class node_function : public node_impl {
87 public:
89  identifier_list const& parameters() const;
90  node definition() const;
91 private:
92  virtual void print_node(std::ostream& stream, int indent) const override;
93  virtual double evaluate_node() const override;
94  virtual identifier_list const& evaluate_parameters() const override;
95 
98 };
99 
102 public:
104  std::string name() const;
105  node_list const& arguments() const;
106 private:
107  virtual void print_node(std::ostream& stream, int indent) const override;
108  virtual double evaluate_node() const override;
109 
110  std::string name_;
112 };
113 
115 class node_unary : public node_impl {
116 public:
118  node operand() const;
119 protected:
120  double evaluate_operand() const;
121 private:
122  virtual void print_node(std::ostream& stream, int indent) const override = 0;
123  virtual double evaluate_node() const override = 0;
124 
126 };
127 
129 class node_binary : public node_impl {
130 public:
132  node left() const;
133  node right() const;
134 protected:
135  double evaluate_left() const;
136  double evaluate_right() const;
137 private:
138  virtual void print_node(std::ostream& stream, int indent) const override = 0;
139  virtual double evaluate_node() const override = 0;
140 
143 };
144 
146 class node_negate : public node_unary {
147 public:
149 private:
150  virtual void print_node(std::ostream& stream, int indent) const override;
151  virtual double evaluate_node() const override;
152 };
153 
155 class node_add : public node_binary {
156 public:
158 private:
159  virtual void print_node(std::ostream& stream, int indent) const override;
160  virtual double evaluate_node() const override;
161 };
162 
164 class node_subtract : public node_binary {
165 public:
167 private:
168  virtual void print_node(std::ostream& stream, int indent) const override;
169  virtual double evaluate_node() const override;
170 };
171 
173 class node_multiply : public node_binary {
174 public:
176 private:
177  virtual void print_node(std::ostream& stream, int indent) const override;
178  virtual double evaluate_node() const override;
179 };
180 
182 class node_divide : public node_binary {
183 public:
185 private:
186  virtual void print_node(std::ostream& stream, int indent) const override;
187  virtual double evaluate_node() const override;
188 };
189 
190 #endif
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 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
virtual double evaluate_node() const override=0
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 double evaluate_node() const override=0
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
node_impl & operator=(node_impl &&)=default
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
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_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
virtual void print_node(std::ostream &stream, int indent) const override=0
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
virtual void print_node(std::ostream &stream, int indent) const override=0
virtual double evaluate_node() const override
Definition: node_impl.cpp:290