Calculator Step 4

- 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 <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:
88 node_function(identifier_list parameters, node definition);
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
96 identifier_list parameters_;
97 node definition_;
98 };
99
101 class node_function_call : public node_impl {
102 public:
103 node_function_call(std::string name, node_list arguments);
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_;
111 node_list arguments_;
112 };
113
115 class node_unary : public node_impl {
116 public:
117 node_unary(node operand);
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
125 node operand_;
126 };
127
129 class node_binary : public node_impl {
130 public:
131 node_binary(node left, node right);
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
141 node left_;
142 node right_;
143 };
144
146 class node_negate : public node_unary {
147 public:
148 node_negate(node operand);
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:
157 node_add(node left, node right);
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:
166 node_subtract(node left, node right);
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:
175 node_multiply(node left, node right);
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:
184 node_divide(node left, node right);
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
node_identifier::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:119
node_number::node_number
node_number(double value)
Definition: node_impl.cpp:81
node_binary::left_
node left_
Definition: node_impl.hpp:141
node_identifier::name_
std::string name_
Definition: node_impl.hpp:80
node_binary::evaluate_right
double evaluate_right() const
Definition: node_impl.cpp:272
node_subtract
Definition: node_impl.hpp:164
node_function::definition_
node definition_
Definition: node_impl.hpp:97
node_void
Definition: node_impl.hpp:45
node_unary::node_unary
node_unary(node operand)
Definition: node_impl.cpp:234
node_divide
Definition: node_impl.hpp:182
node_function_call::name
std::string name() const
Definition: node_impl.cpp:185
node_void::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:74
node_impl::evaluate_string
virtual std::string evaluate_string() const
Definition: node_impl.cpp:42
node_negate
Definition: node_impl.hpp:146
node_identifier::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:113
node_impl::evaluate_node
virtual double evaluate_node() const =0
node_function::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:168
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:191
node_divide::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:358
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:320
node_binary::evaluate_left
double evaluate_left() const
Definition: node_impl.cpp:266
node_function_call::node_function_call
node_function_call(std::string name, node_list arguments)
Definition: node_impl.cpp:181
node_void::node_void
node_void()
Definition: node_impl.cpp:58
node_number::value_
double value_
Definition: node_impl.hpp:65
node_binary::node_binary
node_binary(node left, node right)
Definition: node_impl.cpp:250
node_impl
Definition: node_impl.hpp:17
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:328
node_multiply::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:347
node_identifier::name
std::string const & name() const
Definition: node_impl.cpp:107
node_void::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:62
node_identifier::evaluate_string
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:125
node_identifier::node_identifier
node_identifier(std::string name)
Definition: node_impl.cpp:103
node_function::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:159
node_identifier
Definition: node_impl.hpp:71
node_function_call::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:210
node_number::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:97
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:125
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
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:91
node_binary
Definition: node_impl.hpp:129
node_impl::~node_impl
virtual ~node_impl()
Definition: node_impl.cpp:15
node_unary
Definition: node_impl.hpp:115
node_binary::right
node right() const
Definition: node_impl.cpp:260
node_function::node_function
node_function(identifier_list parameters, node definition)
Definition: node_impl.cpp:132
node_unary::evaluate_operand
double evaluate_operand() const
Definition: node_impl.cpp:244
node_function::definition
node definition() const
Definition: node_impl.cpp:142
node_multiply::node_multiply
node_multiply(node left, node right)
Definition: node_impl.cpp:335
node_multiply
Definition: node_impl.hpp:173
node_function::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const override
Definition: node_impl.cpp:174
node_function_call::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:197
node_impl::operator=
node_impl & operator=(node_impl &&)=default
node_negate::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:283
node_number
Definition: node_impl.hpp:57
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:111
node_impl::print_node
virtual void print_node(std::ostream &stream, int indent) const =0
node_function_call
Definition: node_impl.hpp:101
node_function::parameters_
identifier_list parameters_
Definition: node_impl.hpp:96
node_binary::right_
node right_
Definition: node_impl.hpp:142
node_subtract::node_subtract
node_subtract(node left, node right)
Definition: node_impl.cpp:316
node_impl::evaluate
double evaluate() const
Definition: node_impl.cpp:24
node_void::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:68
node_add::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:301
node_add
Definition: node_impl.hpp:155
node.hpp
node_impl::evaluate_parameters
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
node_divide::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:366
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:85
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:110
node_impl::to_string
std::string to_string() const
Definition: node_impl.cpp:30
node_unary::operand
node operand() const
Definition: node_impl.cpp:238
node_negate::node_negate
node_negate(node operand)
Definition: node_impl.cpp:279
node_function
Definition: node_impl.hpp:86
node_multiply::print_node
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:339
node_add::evaluate_node
virtual double evaluate_node() const override
Definition: node_impl.cpp:309
node_divide::node_divide
node_divide(node left, node right)
Definition: node_impl.cpp:354
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:297
node_binary::left
node left() const
Definition: node_impl.cpp:254
node_function::parameters
identifier_list const & parameters() const
Definition: node_impl.cpp:136
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:290