Calculator  Step 3
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  virtual ~node_impl();
21 
22  void print(std::ostream& stream, int indent) const;
23 
24  double evaluate() const;
25 
26  std::string to_string() const;
27 
28 private:
29  node_impl(node_impl const& n) = delete;
30  node_impl& operator=(node_impl const& n) = delete;
31 
32  virtual void print_node(std::ostream& stream, int indent) const = 0;
33  virtual double evaluate_node() const = 0;
34  virtual std::string evaluate_string() const;
35 };
36 
40 class node_void : public node_impl {
41 public:
42  node_void();
43 private:
44  void print_node(std::ostream& stream, int indent) const override;
45  double evaluate_node() const override;
46  std::string evaluate_string() const override;
47 };
48 
52 class node_number : public node_impl {
53 public:
54  node_number(double value);
55  double value() const;
56 private:
57  void print_node(std::ostream& stream, int indent) const override;
58  double evaluate_node() const override;
59 
60  double value_;
61 };
62 
66 class node_identifier : public node_impl {
67 public:
68  node_identifier(std::string identifier);
69  std::string const& identifier() const;
70 private:
71  virtual void print_node(std::ostream& stream, int indent) const override;
72  virtual double evaluate_node() const override;
73  virtual std::string evaluate_string() const override;
74 
75  std::string identifier_;
76 };
77 
81 class node_assign : public node_impl {
82 public:
84  node identifier() const;
85  node value() const;
86 private:
87  std::string get_identifier() const;
88  double evaluate_value() const;
89 
90  virtual void print_node(std::ostream& stream, int indent) const override;
91  virtual double evaluate_node() const override;
92 
95 };
96 
98 class node_unary : public node_impl {
99 public:
101  node operand() const;
102 protected:
103  double evaluate_operand() const;
104 private:
105  virtual void print_node(std::ostream& stream, int indent) const override = 0;
106  virtual double evaluate_node() const override = 0;
107 
109 };
110 
112 class node_binary : public node_impl {
113 public:
115  node left() const;
116  node right() const;
117 protected:
118  double evaluate_left() const;
119  double evaluate_right() const;
120 private:
121  virtual void print_node(std::ostream& stream, int indent) const override = 0;
122  virtual double evaluate_node() const override = 0;
123 
126 };
127 
129 class node_negate : public node_unary {
130 public:
132 private:
133  virtual void print_node(std::ostream& stream, int indent) const override;
134  virtual double evaluate_node() const override;
135 };
136 
138 class node_add : public node_binary {
139 public:
141 private:
142  virtual void print_node(std::ostream& stream, int indent) const override;
143  virtual double evaluate_node() const override;
144 };
145 
147 class node_subtract : public node_binary {
148 public:
150 private:
151  virtual void print_node(std::ostream& stream, int indent) const override;
152  virtual double evaluate_node() const override;
153 };
154 
156 class node_multiply : public node_binary {
157 public:
159 private:
160  virtual void print_node(std::ostream& stream, int indent) const override;
161  virtual double evaluate_node() const override;
162 };
163 
165 class node_divide : public node_binary {
166 public:
168 private:
169  virtual void print_node(std::ostream& stream, int indent) const override;
170  virtual double evaluate_node() const override;
171 };
172 
173 #endif
virtual double evaluate_node() const override
Definition: node_impl.cpp:104
node_number(double value)
Definition: node_impl.cpp:65
node identifier_
Definition: node_impl.hpp:93
double evaluate_right() const
Definition: node_impl.cpp:200
node_unary(node operand)
Definition: node_impl.cpp:162
std::string evaluate_string() const override
Definition: node_impl.cpp:58
virtual std::string evaluate_string() const
Definition: node_impl.cpp:33
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:98
virtual double evaluate_node() const =0
node value() const
Definition: node_impl.cpp:127
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:286
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:248
double evaluate_left() const
Definition: node_impl.cpp:194
node_identifier(std::string identifier)
Definition: node_impl.cpp:88
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:145
double value_
Definition: node_impl.hpp:60
node_binary(node left, node right)
Definition: node_impl.cpp:178
virtual double evaluate_node() const override=0
Definition: node.hpp:16
virtual double evaluate_node() const override
Definition: node_impl.cpp:256
virtual double evaluate_node() const override
Definition: node_impl.cpp:275
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:46
virtual std::string evaluate_string() const override
Definition: node_impl.cpp:110
double evaluate_node() const override
Definition: node_impl.cpp:81
void print(std::ostream &stream, int indent) const
Definition: node_impl.cpp:15
node operand_
Definition: node_impl.hpp:108
node_assign(node identifier, node value)
Definition: node_impl.cpp:117
virtual double evaluate_node() const override=0
void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:75
virtual ~node_impl()
Definition: node_impl.cpp:12
node right() const
Definition: node_impl.cpp:188
double evaluate_operand() const
Definition: node_impl.cpp:172
std::string get_identifier() const
Definition: node_impl.cpp:133
std::string identifier_
Definition: node_impl.hpp:75
node_multiply(node left, node right)
Definition: node_impl.cpp:263
node_impl & operator=(node_impl const &n)=delete
double evaluate_value() const
Definition: node_impl.cpp:139
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:211
virtual void print_node(std::ostream &stream, int indent) const =0
virtual double evaluate_node() const override
Definition: node_impl.cpp:152
node_subtract(node left, node right)
Definition: node_impl.cpp:244
double evaluate() const
Definition: node_impl.cpp:21
node identifier() const
Definition: node_impl.cpp:121
double evaluate_node() const override
Definition: node_impl.cpp:52
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:229
virtual double evaluate_node() const override
Definition: node_impl.cpp:294
double value() const
Definition: node_impl.cpp:69
std::string to_string() const
Definition: node_impl.cpp:27
node operand() const
Definition: node_impl.cpp:166
node_negate(node operand)
Definition: node_impl.cpp:207
virtual void print_node(std::ostream &stream, int indent) const override
Definition: node_impl.cpp:267
virtual double evaluate_node() const override
Definition: node_impl.cpp:237
node_divide(node left, node right)
Definition: node_impl.cpp:282
virtual void print_node(std::ostream &stream, int indent) const override=0
node_add(node left, node right)
Definition: node_impl.cpp:225
node left() const
Definition: node_impl.cpp:182
virtual void print_node(std::ostream &stream, int indent) const override=0
virtual double evaluate_node() const override
Definition: node_impl.cpp:218
std::string const & identifier() const
Definition: node_impl.cpp:92