Calculator Step 6

- Main Page
- Classes
- Files

- File List
- File Members

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:
70 node_number(number value);
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
78 number value_;
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:
103 node_function(identifier_list parameters, node definition);
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
113 identifier_list parameters_;
114 node definition_;
115 };
116
118 class node_function_call : public node_impl {
119 public:
120 node_function_call(std::string name, node_list arguments);
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_;
130 node_list arguments_;
131 };
132
134 class node_unary : public node_impl {
135 public:
136 node_unary(node operand);
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
146 node operand_;
147 };
148
150 class node_binary : public node_impl {
151 public:
152 node_binary(node left, node right);
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
164 node left_;
165 node right_;
166 };
167
169 class node_negate : public node_unary {
170 public:
171 node_negate(node operand);
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:
182 node_add(node left, node right);
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:
193 node_subtract(node left, node right);
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:
204 node_multiply(node left, node right);
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:
215 node_divide(node left, node right);
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
node_binary::left_
node left_
Definition: node_impl.hpp:164
node_binary::save_node
virtual void save_node(std::ostream &stream) const override=0
node_unary::save_node
virtual void save_node(std::ostream &stream) const override=0
node_identifier::name_
std::string name_
Definition: node_impl.hpp:95
node_subtract
Definition: node_impl.hpp:191
node_add::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:456
node_impl::save
void save(std::ostream &stream) const
Definition: node_impl.cpp:57
node_function::definition_
node definition_
Definition: node_impl.hpp:114
node_void
Definition: node_impl.hpp:54
node_unary::node_unary
node_unary(node operand)
Definition: node_impl.cpp:351
node_divide
Definition: node_impl.hpp:213
node_function_call::name
std::string name() const
Definition: node_impl.cpp:295
node_subtract::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:487
node_void::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:113
node_impl::evaluate_string
virtual std::string evaluate_string() const
Definition: node_impl.cpp:42
node_negate
Definition: node_impl.hpp:169
node_identifier::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:177
node_unary::evaluate_operand
number evaluate_operand() const
Definition: node_impl.cpp:365
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:301
node_divide::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:535
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:473
number
Definition: number.hpp:16
node_number::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:151
node_function_call::node_function_call
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:276
node_void::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:107
node_void::node_void
node_void()
Definition: node_impl.cpp:93
node_binary::node_binary
node_binary(node left, node right)
Definition: node_impl.cpp:371
node_impl
Definition: node_impl.hpp:20
node
Definition: node.hpp:28
node_impl::evaluate
number evaluate() const
Definition: node_impl.cpp:24
node_identifier::name
std::string const & name() const
Definition: node_impl.cpp:171
node_void::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:101
node_identifier::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:189
node_identifier::node_identifier
node_identifier(std::string name)
Definition: node_impl.cpp:160
node_multiply::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:518
node_function::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:245
node_identifier
Definition: node_impl.hpp:84
node_binary::evaluate_right
number evaluate_right() const
Definition: node_impl.cpp:397
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:146
node_identifier::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:183
node_function::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:254
node_binary::evaluate_left
number evaluate_left() const
Definition: node_impl.cpp:391
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:21
node_impl::read_node
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
node_number::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:139
node_binary
Definition: node_impl.hpp:150
node_impl::~node_impl
virtual ~node_impl()
Definition: node_impl.cpp:15
node_number::value
number value() const
Definition: node_impl.cpp:133
node_identifier::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:195
node_unary
Definition: node_impl.hpp:134
node_binary::right
node right() const
Definition: node_impl.cpp:385
node_function::node_function
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:202
node_multiply::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:512
node_number::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:145
node_function::definition
node definition() const
Definition: node_impl.cpp:228
node_multiply::node_multiply
node_multiply(node left, node right)
Definition: node_impl.cpp:496
node_multiply
Definition: node_impl.hpp:202
node_function::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:260
node_function_call::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:307
node_number::node_number
node_number(number value)
Definition: node_impl.cpp:125
node_void::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:119
node_impl::operator=
node_impl & operator=(node_impl &&)=default
node_negate::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:419
node_negate::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:412
node_number
Definition: node_impl.hpp:68
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:130
node_subtract::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:481
node_impl::print_node
virtual void print_node(std::ostream &stream, int indent) const =0
node_impl::evaluate_node
virtual number evaluate_node() const =0
node_function_call
Definition: node_impl.hpp:118
node_function::parameters_
identifier_list parameters_
Definition: node_impl.hpp:113
node_binary::right_
node right_
Definition: node_impl.hpp:165
node_subtract::node_subtract
node_subtract(node left, node right)
Definition: node_impl.cpp:465
node_impl::save_node
virtual void save_node(std::ostream &stream) const =0
node_add::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:442
node_add
Definition: node_impl.hpp:180
node.hpp
node_impl::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
node_function::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:266
node_number::value_
number value_
Definition: node_impl.hpp:78
node_impl::get_parameters
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:15
node_function_call::name_
std::string name_
Definition: node_impl.hpp:129
node_function_call::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:320
node_impl::to_string
std::string to_string() const
Definition: node_impl.cpp:30
node_unary::operand
node operand() const
Definition: node_impl.cpp:359
node_divide::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:543
node_negate::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:425
node_negate::node_negate
node_negate(node operand)
Definition: node_impl.cpp:404
node_function
Definition: node_impl.hpp:101
node_binary::evaluate_node
virtual number evaluate_node() const override=0
node_multiply::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:504
node_divide::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:549
node_unary::evaluate_node
virtual number evaluate_node() const override=0
node_divide::node_divide
node_divide(node left, node right)
Definition: node_impl.cpp:527
node_binary::print_node
virtual void print_node(std::ostream &stream, int indent) const override=0
node_add::node_add
node_add(node left, node right)
Definition: node_impl.cpp:434
node_binary::left
node left() const
Definition: node_impl.cpp:379
node_function_call::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:342
node_function::parameters
identifier_list const & parameters() const
Definition: node_impl.cpp:222
node_unary::print_node
virtual void print_node(std::ostream &stream, int indent) const override=0
node_add::evaluate_node
virtual number evaluate_node() const override
Definition: node_impl.cpp:450