Calculator Step 5

- 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 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:
100 node_function(identifier_list parameters, node definition);
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
110 identifier_list parameters_;
111 node definition_;
112 };
113
115 class node_function_call : public node_impl {
116 public:
117 node_function_call(std::string name, node_list arguments);
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_;
127 node_list arguments_;
128 };
129
131 class node_unary : public node_impl {
132 public:
133 node_unary(node operand);
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
143 node operand_;
144 };
145
147 class node_binary : public node_impl {
148 public:
149 node_binary(node left, node right);
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
161 node left_;
162 node right_;
163 };
164
166 class node_negate : public node_unary {
167 public:
168 node_negate(node operand);
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:
179 node_add(node left, node right);
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:
190 node_subtract(node left, node right);
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:
201 node_multiply(node left, node right);
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:
212 node_divide(node left, node right);
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
node_identifier::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:184
node_number::node_number
node_number(double value)
Definition: node_impl.cpp:125
node_binary::left_
node left_
Definition: node_impl.hpp:161
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:92
node_binary::evaluate_right
double evaluate_right() const
Definition: node_impl.cpp:398
node_subtract
Definition: node_impl.hpp:188
node_add::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:457
node_impl::save
void save(std::ostream &stream) const
Definition: node_impl.cpp:57
node_function::definition_
node definition_
Definition: node_impl.hpp:111
node_void
Definition: node_impl.hpp:51
node_unary::node_unary
node_unary(node operand)
Definition: node_impl.cpp:352
node_divide
Definition: node_impl.hpp:210
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
node_subtract::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:488
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:166
node_identifier::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:178
node_impl::evaluate_node
virtual double evaluate_node() const =0
node_function::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:255
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:302
node_divide::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:536
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:474
node_binary::evaluate_left
double evaluate_left() const
Definition: node_impl.cpp:392
node_number::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:154
node_function_call::node_function_call
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:277
node_void::node_void
node_void()
Definition: node_impl.cpp:93
node_number::value_
double value_
Definition: node_impl.hpp:75
node_binary::node_binary
node_binary(node left, node right)
Definition: node_impl.cpp:372
node_impl
Definition: node_impl.hpp:20
node_unary::evaluate_node
virtual double evaluate_node() const override=0
node
Definition: node.hpp:26
node_subtract::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:482
node_multiply::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:513
node_identifier::name
std::string const & name() const
Definition: node_impl.cpp:172
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:190
node_identifier::node_identifier
node_identifier(std::string name)
Definition: node_impl.cpp:161
node_multiply::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:519
node_function::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:246
node_identifier
Definition: node_impl.hpp:81
node_function_call::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:321
node_number::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:148
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:143
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
node_impl::read_node
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
node_binary::evaluate_node
virtual double evaluate_node() const override=0
node_number::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:142
node_binary
Definition: node_impl.hpp:147
node_impl::~node_impl
virtual ~node_impl()
Definition: node_impl.cpp:15
node_identifier::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:196
node_unary
Definition: node_impl.hpp:131
node_binary::right
node right() const
Definition: node_impl.cpp:386
node_function::node_function
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:203
node_unary::evaluate_operand
double evaluate_operand() const
Definition: node_impl.cpp:366
node_function::definition
node definition() const
Definition: node_impl.cpp:229
node_impl::operator=
node_impl & operator=(node_impl const &n)
not implemented
node_multiply::node_multiply
node_multiply(node left, node right)
Definition: node_impl.cpp:497
node_multiply
Definition: node_impl.hpp:199
node_function::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:261
node_function_call::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:308
node_void::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:119
node_negate::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:413
node_number
Definition: node_impl.hpp:65
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:127
node_impl::print_node
virtual void print_node(std::ostream &stream, int indent) const =0
node_function_call
Definition: node_impl.hpp:115
node_function::parameters_
identifier_list parameters_
Definition: node_impl.hpp:110
node_binary::right_
node right_
Definition: node_impl.hpp:162
node_subtract::node_subtract
node_subtract(node left, node right)
Definition: node_impl.cpp:466
node_impl::evaluate
double evaluate() const
Definition: node_impl.cpp:24
node_impl::save_node
virtual void save_node(std::ostream &stream) const =0
node_void::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:107
node_add::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:443
node_add
Definition: node_impl.hpp:177
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:267
node_divide::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:544
node_impl::get_parameters
identifier_list const & get_parameters() const
Definition: node_impl.cpp:36
node_number::value
double value() const
Definition: node_impl.cpp:136
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
node_function_call::name_
std::string name_
Definition: node_impl.hpp:126
node_impl::to_string
std::string to_string() const
Definition: node_impl.cpp:30
node_unary::operand
node operand() const
Definition: node_impl.cpp:360
node_negate::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:426
node_negate::node_negate
node_negate(node operand)
Definition: node_impl.cpp:405
node_function
Definition: node_impl.hpp:98
node_multiply::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:505
node_add::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:451
node_divide::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:550
node_divide::node_divide
node_divide(node left, node right)
Definition: node_impl.cpp:528
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:435
node_binary::left
node left() const
Definition: node_impl.cpp:380
node_function_call::save_node
virtual void save_node(std::ostream &stream) const override
Definition: node_impl.cpp:343
node_function::parameters
identifier_list const & parameters() const
Definition: node_impl.cpp:223
node_unary::print_node
virtual void print_node(std::ostream &stream, int indent) const override=0
node_negate::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:420