Calculator  Step 6
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  node_impl(node_impl&&) = default;
26  node_impl& operator=(node_impl&&) = default;
27 
28  void print(std::ostream& stream, int indent) const;
29 
30  number evaluate() const;
31 
32  std::string to_string() const;
33 
34  identifier_list const& get_parameters() const;
35 
36  void save(std::ostream& stream) const;
37 
39  static std::shared_ptr<node_impl> read_node(std::istream& stream);
40 private:
41  node_impl(node_impl const&) = delete;
42  node_impl& operator=(node_impl const&) = delete;
43 
44  virtual void print_node(std::ostream& stream, int indent) const = 0;
45  virtual number evaluate_node() const = 0;
46  virtual std::string evaluate_string() const;
47  virtual identifier_list const& evaluate_parameters() const;
48  virtual void save_node(std::ostream& stream) const = 0;
49 };
50 
54 class node_void : public node_impl {
55 public:
56  node_void();
57  node_void(std::istream& stream);
58 private:
59  virtual void print_node(std::ostream& stream, int indent) const override;
60  virtual number evaluate_node() const override;
61  virtual std::string evaluate_string() const override;
62  virtual void save_node(std::ostream& stream) const override;
63 };
64 
68 class node_number : public node_impl {
69 public:
71  node_number(std::istream& stream);
72  number value() const;
73 private:
74  virtual void print_node(std::ostream& stream, int indent) const override;
75  virtual number evaluate_node() const override;
76  virtual void save_node(std::ostream& stream) const override;
77 
79 };
80 
84 class node_identifier : public node_impl {
85 public:
86  node_identifier(std::string name);
87  node_identifier(std::istream& stream);
88  std::string const& name() const;
89 private:
90  virtual void print_node(std::ostream& stream, int indent) const override;
91  virtual number evaluate_node() const override;
92  virtual std::string evaluate_string() const override;
93  virtual void save_node(std::ostream& stream) const override;
94 
95  std::string name_;
96 };
97 
101 class node_function : public node_impl {
102 public:
104  node_function(std::istream& stream);
105  identifier_list const& parameters() const;
106  node definition() const;
107 private:
108  virtual void print_node(std::ostream& stream, int indent) const override;
109  virtual number evaluate_node() const override;
110  virtual identifier_list const& evaluate_parameters() const override;
111  virtual void save_node(std::ostream& stream) const override;
112 
115 };
116 
119 public:
121  node_function_call(std::istream& stream);
122  std::string name() const;
123  node_list const& arguments() const;
124 private:
125  virtual void print_node(std::ostream& stream, int indent) const override;
126  virtual number evaluate_node() const override;
127  virtual void save_node(std::ostream& stream) const override;
128 
129  std::string name_;
131 };
132 
134 class node_unary : public node_impl {
135 public:
137  node operand() const;
138 protected:
139  node_unary(std::istream& stream);
140  number evaluate_operand() const;
141 private:
142  virtual void print_node(std::ostream& stream, int indent) const override = 0;
143  virtual number evaluate_node() const override = 0;
144  virtual void save_node(std::ostream& stream) const override = 0;
145 
147 };
148 
150 class node_binary : public node_impl {
151 public:
153  node left() const;
154  node right() const;
155 protected:
156  node_binary(std::istream& stream);
157  number evaluate_left() const;
158  number evaluate_right() const;
159 private:
160  virtual void print_node(std::ostream& stream, int indent) const override = 0;
161  virtual number evaluate_node() const override = 0;
162  virtual void save_node(std::ostream& stream) const override = 0;
163 
166 };
167 
169 class node_negate : public node_unary {
170 public:
172  node_negate(std::istream& stream);
173 private:
174  virtual void print_node(std::ostream& stream, int indent) const override;
175  virtual number evaluate_node() const override;
176  virtual void save_node(std::ostream& stream) const override;
177 };
178 
180 class node_add : public node_binary {
181 public:
183  node_add(std::istream& stream);
184 private:
185  virtual void print_node(std::ostream& stream, int indent) const override;
186  virtual number evaluate_node() const override;
187  virtual void save_node(std::ostream& stream) const override;
188 };
189 
191 class node_subtract : public node_binary {
192 public:
194  node_subtract(std::istream& stream);
195 private:
196  virtual void print_node(std::ostream& stream, int indent) const override;
197  virtual number evaluate_node() const override;
198  virtual void save_node(std::ostream& stream) const override;
199 };
200 
202 class node_multiply : public node_binary {
203 public:
205  node_multiply(std::istream& stream);
206 private:
207  virtual void print_node(std::ostream& stream, int indent) const override;
208  virtual number evaluate_node() const override;
209  virtual void save_node(std::ostream& stream) const override;
210 };
211 
213 class node_divide : public node_binary {
214 public:
216  node_divide(std::istream& stream);
217 private:
218  virtual void print_node(std::ostream& stream, int indent) const override;
219  virtual number evaluate_node() const override;
220  virtual void save_node(std::ostream& stream) const override;
221 };
222 
223 #endif
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:95
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:456
void save(std::ostream &stream) const
Definition: node_impl.cpp:57
node_unary(node operand)
Definition: node_impl.cpp:351
std::string name() const
Definition: node_impl.cpp:295
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:487
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:177
number evaluate_operand() const
Definition: node_impl.cpp:365
node_list const & arguments() const
Definition: node_impl.cpp:301
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:535
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:473
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:151
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:276
virtual number evaluate_node() const override
Definition: node_impl.cpp:107
node_binary(node left, node right)
Definition: node_impl.cpp:371
Definition: node.hpp:28
number evaluate() const
Definition: node_impl.cpp:24
std::string const & name() const
Definition: node_impl.cpp:171
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:189
node_identifier(std::string name)
Definition: node_impl.cpp:160
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:518
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:245
number evaluate_right() const
Definition: node_impl.cpp:397
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:18
node operand_
Definition: node_impl.hpp:146
virtual number evaluate_node() const override
Definition: node_impl.cpp:183
virtual number evaluate_node() const override
Definition: node_impl.cpp:254
number evaluate_left() const
Definition: node_impl.cpp:391
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:21
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:139
virtual ~node_impl()
Definition: node_impl.cpp:15
number value() const
Definition: node_impl.cpp:133
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:195
node right() const
Definition: node_impl.cpp:385
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:202
virtual number evaluate_node() const override
Definition: node_impl.cpp:512
virtual number evaluate_node() const override
Definition: node_impl.cpp:145
node definition() const
Definition: node_impl.cpp:228
node_multiply(node left, node right)
Definition: node_impl.cpp:496
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:260
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:307
node_number(number value)
Definition: node_impl.cpp:125
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:119
node_impl & operator=(node_impl &&)=default
virtual number evaluate_node() const override
Definition: node_impl.cpp:419
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:412
node_list arguments_
Definition: node_impl.hpp:130
virtual number evaluate_node() const override
Definition: node_impl.cpp:481
virtual void print_node(std::ostream &stream, int indent) const =0
virtual number evaluate_node() const =0
identifier_list parameters_
Definition: node_impl.hpp:113
node_subtract(node left, node right)
Definition: node_impl.cpp:465
virtual void save_node(std::ostream &stream) const =0
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:442
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:266
number value_
Definition: node_impl.hpp:78
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:15
std::string name_
Definition: node_impl.hpp:129
virtual number evaluate_node() const override
Definition: node_impl.cpp:320
std::string to_string() const
Definition: node_impl.cpp:30
node operand() const
Definition: node_impl.cpp:359
virtual number evaluate_node() const override
Definition: node_impl.cpp:543
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:425
node_negate(node operand)
Definition: node_impl.cpp:404
virtual number evaluate_node() const override=0
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:504
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:549
virtual number evaluate_node() const override=0
node_divide(node left, node right)
Definition: node_impl.cpp:527
virtual void print_node(std::ostream &stream, int indent) const override=0
node_add(node left, node right)
Definition: node_impl.cpp:434
node left() const
Definition: node_impl.cpp:379
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:342
identifier_list const & parameters() const
Definition: node_impl.cpp:222
virtual void print_node(std::ostream &stream, int indent) const override=0
virtual number evaluate_node() const override
Definition: node_impl.cpp:450