-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.h
217 lines (172 loc) · 5.34 KB
/
operations.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cassert>
#include <map>
#include <cstdint>
#include <stack>
#include <string>
#ifdef ENABLE_GBI_ASSERTS
#define gbiAssert(condition, message) assert((condition) && message)
#else
#define gbiAssert(condition, message)
#endif
namespace gbi
{
typedef size_t SizeT;
typedef uint8_t CharT;
struct Token
{
virtual bool isNode() const
{
return false;
}
virtual bool isSignal() const
{
return false;
}
virtual ~Token() {}
};
struct Signal : Token
{
enum SignalType
{
AdditionStart,
FactorStart
};
bool isSignal() const override
{
return true;
}
Signal(SignalType type) :
m_Type(type)
{
}
SignalType m_Type;
};
struct Node : Token
{
enum class ExtraQualifier : CharT
{
None = 0x0,
Negative = 0x1,
Inverse = 0x2
};
Node():
m_Qualifiers(ExtraQualifier::None)
{
};
Node(ExtraQualifier qualifiers):
m_Qualifiers(qualifiers)
{
};
bool isNode() const override
{
return true;
}
void setInverse()
{
m_Qualifiers = static_cast<ExtraQualifier>(static_cast<CharT>(m_Qualifiers) | static_cast<CharT>(ExtraQualifier::Inverse));
}
void setNegative()
{
m_Qualifiers = static_cast<ExtraQualifier>(static_cast<CharT>(m_Qualifiers) | static_cast<CharT>(ExtraQualifier::Negative));
}
ExtraQualifier m_Qualifiers;
};
Node::ExtraQualifier operator&(const Node::ExtraQualifier& a, const Node::ExtraQualifier& b)
{
return static_cast<Node::ExtraQualifier>(static_cast<CharT>(a) & static_cast<CharT>(b));
}
Node::ExtraQualifier operator|(const Node::ExtraQualifier& a, const Node::ExtraQualifier& b)
{
return static_cast<Node::ExtraQualifier>(static_cast<CharT>(a) | static_cast<CharT>(b));
}
struct OperatorNode : Node
{
void push(std::shared_ptr<Node> node)
{
m_Terms.push_back(node);
}
std::vector<std::shared_ptr<Node>> m_Terms;
};
struct Addition : OperatorNode
{
static const Signal::SignalType AssociatedStartSignal = Signal::AdditionStart;
};
struct Factor : OperatorNode
{
static const Signal::SignalType AssociatedStartSignal = Signal::FactorStart;
};
struct Variable : Node
{
Variable(const std::string& variableName, ExtraQualifier qualifiers):
Node(qualifiers),
m_VariableName(variableName)
{
}
std::string m_VariableName;
};
struct Number : Node
{
Number(double value, ExtraQualifier qualifiers):
Node(qualifiers),
m_Value(value)
{
}
double m_Value;
};
class StateComputer
{
public:
void pushSignal(Signal::SignalType type)
{
m_Tokens.push(std::make_shared<Signal>(type));
}
void pushVariable(const std::string& variableName, bool negative)
{
std::shared_ptr<Variable> variable = std::make_shared<Variable>(
variableName, negative ? Node::ExtraQualifier::Negative : Node::ExtraQualifier::None);
m_Tokens.push(variable);
}
void pushNumber(double value, bool negative)
{
m_Tokens.push(std::make_shared<Number>(
value, negative ? Node::ExtraQualifier::Negative : Node::ExtraQualifier::None));
}
void setInverse()
{
gbiAssert(m_Tokens.top()->isNode(), "Only nodes can be set as division terms");
std::static_pointer_cast<Node>(m_Tokens.top())->setInverse();
}
void setNegative()
{
gbiAssert(m_Tokens.top()->isNode(), "Only nodes can be set as minus terms");
std::static_pointer_cast<Node>(m_Tokens.top())->setNegative();
}
template<typename T>
void close()
{
std::shared_ptr<T> operatorNode = std::make_shared<T>();
std::shared_ptr<Token> token = m_Tokens.top();
m_Tokens.pop();
while(!token->isSignal())
{
gbiAssert(token->isNode(), "Only nodes can be parts of an operatorNode node.");
gbiAssert(std::dynamic_pointer_cast<Node>(token), "Mismatched types, expecting Node, got something else.");
operatorNode->push(std::static_pointer_cast<Node>(token));
token = m_Tokens.top();
m_Tokens.pop();
}
gbiAssert(
std::dynamic_pointer_cast<Signal>(token) &&
(std::dynamic_pointer_cast<Signal>(token)->m_Type == T::AssociatedStartSignal),
"Expecting AdditionStart signal, got something else");
token = m_Tokens.top();
m_Tokens.pop();
gbiAssert(std::dynamic_pointer_cast<Node>(token), "Expecting Node, got something else");
operatorNode->push(std::static_pointer_cast<Node>(token));
m_Tokens.push(operatorNode);
}
private:
std::stack<std::shared_ptr<Token>> m_Tokens;
};
}
#undef gbiAssert