Calculator  Step 5
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 <memory>
6 #include <string>
7 
8 #include "node.hpp"
9 
20 class node_impl {
21 public:
22  node_impl();
23  virtual ~node_impl();
24 
25  void print(std::ostream& stream, int indent) const;
26 
27  double evaluate() const;
28 
29  std::string to_string() const;
30 
31  identifier_list const& get_parameters() const;
32 
33  void save(std::ostream& stream) const;
34 
36  static std::shared_ptr<node_impl> read_node(std::istream& stream);
37 private:
38  node_impl(node_impl const& n);
39  node_impl& operator=(node_impl const& n);
40 
41  virtual void print_node(std::ostream& stream, int indent) const = 0;
42  virtual double evaluate_node() const = 0;
43  virtual std::string evaluate_string() const;
44  virtual identifier_list const& evaluate_parameters() const;
45  virtual void save_node(std::ostream& stream) const = 0;
46 };
47 
51 class node_void : public node_impl {
52 public:
53  node_void();
54  node_void(std::istream& stream);
55 private:
56  virtual void print_node(std::ostream& stream, int indent) const override;
57  virtual double evaluate_node() const override;
58  virtual std::string evaluate_string() const override;
59  virtual void save_node(std::ostream& stream) const override;
60 };
61 
65 class node_number : public node_impl {
66 public:
67  node_number(double value);
68  node_number(std::istream& stream);
69  double value() const;
70 private:
71  virtual void print_node(std::ostream& stream, int indent) const override;
72  virtual double evaluate_node() const override;
73  virtual void save_node(std::ostream& stream) const override;
74 
75  double value_;
76 };
77 
81 class node_identifier : public node_impl {
82 public:
83  node_identifier(std::string name);
84  node_identifier(std::istream& stream);
85  std::string const& name() const;
86 private:
87  virtual void print_node(std::ostream& stream, int indent) const override;
88  virtual double evaluate_node() const override;
89  virtual std::string evaluate_string() const override;
90  virtual void save_node(std::ostream& stream) const override;
91 
92  std::string name_;
93 };
94 
98 class node_function : public node_impl {
99 public:
101  node_function(std::istream& stream);
102  identifier_list const& parameters() const;
103  node definition() const;
104 private:
105  virtual void print_node(std::ostream& stream, int indent) const override;
106  virtual double evaluate_node() const override;
107  virtual identifier_list const& evaluate_parameters() const override;
108  virtual void save_node(std::ostream& stream) const override;
109 
112 };
113 
116 public:
118  node_function_call(std::istream& stream);
119  std::string name() const;
120  node_list const& arguments() const;
121 private:
122  virtual void print_node(std::ostream& stream, int indent) const override;
123  virtual double evaluate_node() const override;
124  virtual void save_node(std::ostream& stream) const override;
125 
126  std::string name_;
128 };
129 
131 class node_unary : public node_impl {
132 public:
134  node operand() const;
135 protected:
136  node_unary(std::istream& stream);
137  double evaluate_operand() const;
138 private:
139  virtual void print_node(std::ostream& stream, int indent) const override = 0;
140  virtual double evaluate_node() const override = 0;
141  virtual void save_node(std::ostream& stream) const override = 0;
142 
144 };
145 
147 class node_binary : public node_impl {
148 public:
150  node left() const;
151  node right() const;
152 protected:
153  node_binary(std::istream& stream);
154  double evaluate_left() const;
155  double evaluate_right() const;
156 private:
157  virtual void print_node(std::ostream& stream, int indent) const override = 0;
158  virtual double evaluate_node() const override = 0;
159  virtual void save_node(std::ostream& stream) const override = 0;
160 
163 };
164 
166 class node_negate : public node_unary {
167 public:
169  node_negate(std::istream& stream);
170 private:
171  virtual void print_node(std::ostream& stream, int indent) const override;
172  virtual double evaluate_node() const override;
173  virtual void save_node(std::ostream& stream) const override;
174 };
175 
177 class node_add : public node_binary {
178 public:
180  node_add(std::istream& stream);
181 private:
182  virtual void print_node(std::ostream& stream, int indent) const override;
183  virtual double evaluate_node() const override;
184  virtual void save_node(std::ostream& stream) const override;
185 };
186 
188 class node_subtract : public node_binary {
189 public:
191  node_subtract(std::istream& stream);
192 private:
193  virtual void print_node(std::ostream& stream, int indent) const override;
194  virtual double evaluate_node() const override;
195  virtual void save_node(std::ostream& stream) const override;
196 };
197 
199 class node_multiply : public node_binary {
200 public:
202  node_multiply(std::istream& stream);
203 private:
204  virtual void print_node(std::ostream& stream, int indent) const override;
205  virtual double evaluate_node() const override;
206  virtual void save_node(std::ostream& stream) const override;
207 };
208 
210 class node_divide : public node_binary {
211 public:
213  node_divide(std::istream& stream);
214 private:
215  virtual void print_node(std::ostream& stream, int indent) const override;
216  virtual double evaluate_node() const override;
217  virtual void save_node(std::ostream& stream) const override;
218 };
219 
220 #endif
virtual double evaluate_node() const override
Definition: node_impl.cpp:184
node_number(double value)
Definition: node_impl.cpp:125
virtual void save_node(std::ostream &stream) const override=0
virtual void save_node(std::ostream &stream) const override=0
std::string name_
Definition: node_impl.hpp:92
double evaluate_right() const
Definition: node_impl.cpp:398
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:457
void save(std::ostream &stream) const
Definition: node_impl.cpp:57
node_unary(node operand)
Definition: node_impl.cpp:352
std::string name() const
Definition: node_impl.cpp:296
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:488
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:113
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:178
virtual double evaluate_node() const =0
virtual double evaluate_node() const override
Definition: node_impl.cpp:255
node_list const & arguments() const
Definition: node_impl.cpp:302
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:536
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:474
double evaluate_left() const
Definition: node_impl.cpp:392
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:154
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:277
double value_
Definition: node_impl.hpp:75
node_binary(node left, node right)
Definition: node_impl.cpp:372
virtual double evaluate_node() const override=0
Definition: node.hpp:26
virtual double evaluate_node() const override
Definition: node_impl.cpp:482
virtual double evaluate_node() const override
Definition: node_impl.cpp:513
std::string const & name() const
Definition: node_impl.cpp:172
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:101
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:190
node_identifier(std::string name)
Definition: node_impl.cpp:161
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:519
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:246
virtual double evaluate_node() const override
Definition: node_impl.cpp:321
virtual double evaluate_node() const override
Definition: node_impl.cpp:148
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:18
node operand_
Definition: node_impl.hpp:143
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
virtual double evaluate_node() const override=0
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:142
virtual ~node_impl()
Definition: node_impl.cpp:15
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:196
node right() const
Definition: node_impl.cpp:386
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:203
double evaluate_operand() const
Definition: node_impl.cpp:366
node definition() const
Definition: node_impl.cpp:229
node_impl & operator=(node_impl const &n)
not implemented
node_multiply(node left, node right)
Definition: node_impl.cpp:497
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:261
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:308
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:119
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:413
node_list arguments_
Definition: node_impl.hpp:127
virtual void print_node(std::ostream &stream, int indent) const =0
identifier_list parameters_
Definition: node_impl.hpp:110
node_subtract(node left, node right)
Definition: node_impl.cpp:466
double evaluate() const
Definition: node_impl.cpp:24
virtual void save_node(std::ostream &stream) const =0
virtual double evaluate_node() const override
Definition: node_impl.cpp:107
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:443
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:267
virtual double evaluate_node() const override
Definition: node_impl.cpp:544
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
double value() const
Definition: node_impl.cpp:136
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
std::string name_
Definition: node_impl.hpp:126
std::string to_string() const
Definition: node_impl.cpp:30
node operand() const
Definition: node_impl.cpp:360
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:426
node_negate(node operand)
Definition: node_impl.cpp:405
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:505
virtual double evaluate_node() const override
Definition: node_impl.cpp:451
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:550
node_divide(node left, node right)
Definition: node_impl.cpp:528
virtual void print_node(std::ostream &stream, int indent) const override=0
node_add(node left, node right)
Definition: node_impl.cpp:435
node left() const
Definition: node_impl.cpp:380
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:343
identifier_list const & parameters() const
Definition: node_impl.cpp:223
virtual void print_node(std::ostream &stream, int indent) const override=0
virtual double evaluate_node() const override
Definition: node_impl.cpp:420