-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes.cpp
121 lines (85 loc) · 2.34 KB
/
nodes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <string>
#include <memory>
#include <sstream>
#include "nodes.h"
namespace Math_Interpreter {
NumberNode::NumberNode(double value)
: value(value)
{}
NodeKind NumberNode::kind() const {
return NodeKind::Number;
}
std::string NumberNode::str() const {
return std::to_string(value);
}
AddNode::AddNode(std::unique_ptr<Node> node_a, std::unique_ptr<Node> node_b)
: node_a(std::move(node_a)),
node_b(std::move(node_b))
{}
NodeKind AddNode::kind() const {
return NodeKind::Add;
}
std::string AddNode::str() const {
std::ostringstream oss;
oss << "(" << node_a->str() << "+" << node_b->str() << ")";
return oss.str();
}
SubtractNode::SubtractNode(std::unique_ptr<Node> node_a, std::unique_ptr<Node> node_b)
: node_a(std::move(node_a)),
node_b(std::move(node_b))
{}
NodeKind SubtractNode::kind() const {
return NodeKind::Subtract;
}
std::string SubtractNode::str() const {
std::ostringstream oss;
oss << "(" << node_a->str() << "-" << node_b->str() << ")";
return oss.str();
}
MultiplyNode::MultiplyNode(std::unique_ptr<Node> node_a, std::unique_ptr<Node> node_b)
: node_a(std::move(node_a)),
node_b(std::move(node_b))
{}
NodeKind MultiplyNode::kind() const {
return NodeKind::Multiply;
}
std::string MultiplyNode::str() const {
std::ostringstream oss;
oss << "(" << node_a->str() << "*" << node_b->str() << ")";
return oss.str();
}
DivideNode::DivideNode(std::unique_ptr<Node> node_a, std::unique_ptr<Node> node_b)
: node_a(std::move(node_a)),
node_b(std::move(node_b))
{}
NodeKind DivideNode::kind() const {
return NodeKind::Divide;
}
std::string DivideNode::str() const {
std::ostringstream oss;
oss << "(" << node_a->str() << "/" << node_b->str() << ")";
return oss.str();
}
PlusNode::PlusNode(std::unique_ptr<Node> node)
: node(std::move(node))
{}
NodeKind PlusNode::kind() const {
return NodeKind::Plus;
}
std::string PlusNode::str() const {
std::ostringstream oss;
oss << "(+" << node->str() << ")";
return oss.str();
}
MinusNode::MinusNode(std::unique_ptr<Node> node)
: node(std::move(node))
{}
NodeKind MinusNode::kind() const {
return NodeKind::Minus;
}
std::string MinusNode::str() const {
std::ostringstream oss;
oss << "(-" << node->str() << ")";
return oss.str();
}
} // namespace Math_Interpreter