-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFormula.cpp
214 lines (159 loc) · 5.49 KB
/
Formula.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
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
#include "Pch.h"
#include "Formula.h"
unsigned Formula::s_evaluationCounter = 0;
Formula::Formula ()
: m_termCount(0)
{
}
Formula::Formula (Formula && other)
: m_termCount(other.m_termCount)
{
for (unsigned i = 0; i < m_termCount; ++i)
m_termBuffer[i] = other.m_termBuffer[i];
}
Formula::Formula (const Formula & other)
: m_termCount(other.m_termCount)
{
for (unsigned i = 0; i < m_termCount; ++i)
m_termBuffer[i] = other.m_termBuffer[i];
}
Formula & Formula::operator= (Formula && other) {
m_termCount = other.m_termCount;
for (unsigned i = 0; i < m_termCount; ++i)
m_termBuffer[i] = other.m_termBuffer[i];
return *this;
}
Formula & Formula::operator= (const Formula & other) {
m_termCount = other.m_termCount;
for (unsigned i = 0; i < m_termCount; ++i)
m_termBuffer[i] = other.m_termBuffer[i];
return *this;
}
Result Formula::Evaluate (const IFormulaContext * context) const {
Result ret;
++s_evaluationCounter;
ret.code = RESULT_CODE_OK;
if (!m_termCount) {
ret.payload.num.value = 0.0;
return ret;
}
unsigned index = m_termCount - 1;
ret = EvaluateSubexpression(context, &index);
assert(index == 0);
return ret;
}
Result Formula::EvaluateSubexpression (const IFormulaContext * context, unsigned * pindex) const {
Result ret;
const Term & term = m_termBuffer[*pindex];
switch (term.type) {
case Term::TERM_TYPE_TOKEN:
{
auto newContext = context->ResolveContext(term.payload.scopedToken.scope);
if (!newContext)
newContext = context;
ret = newContext->ResolveNumber(*newContext, 0, term.payload.scopedToken.token);
}
break;
case Term::TERM_TYPE_LITERAL:
{
ret.type = RESULT_TYPE_SCALAR;
ret.code = RESULT_CODE_OK;
ret.payload.num.value = term.payload.literalValue;
}
break;
case Term::TERM_TYPE_EVALUATOR:
ret = term.payload.evaluator(context, *this, pindex);
break;
case Term::TERM_TYPE_OPERATOR:
{
Operator op = term.payload.op;
--(*pindex);
Result right = EvaluateSubexpression(context, pindex);
--(*pindex);
Result left = EvaluateSubexpression(context, pindex);
if (left.type != right.type) {
ret.code = RESULT_CODE_TYPE_ERROR;
if (left.type == RESULT_TYPE_SCALAR && right.type == RESULT_TYPE_VECTOR2) {
if (op == OPERATOR_MULTIPLY) {
ret.type = RESULT_TYPE_VECTOR2;
ret.code = RESULT_CODE_OK;
ret.payload.num.value = left.payload.num.value * right.payload.num.value;
ret.payload.num.value2 = left.payload.num.value * right.payload.num.value2;
}
}
else if (left.type == RESULT_TYPE_VECTOR2 && right.type == RESULT_TYPE_SCALAR) {
if (op == OPERATOR_MULTIPLY) {
ret.type = RESULT_TYPE_VECTOR2;
ret.code = RESULT_CODE_OK;
ret.payload.num.value = left.payload.num.value * right.payload.num.value;
ret.payload.num.value2 = left.payload.num.value2 * right.payload.num.value;
}
else if (op == OPERATOR_DIVIDE) {
ret.type = RESULT_TYPE_VECTOR2;
ret.code = RESULT_CODE_OK;
ret.payload.num.value = left.payload.num.value / right.payload.num.value;
ret.payload.num.value2 = left.payload.num.value2 / right.payload.num.value;
}
}
}
else {
ret.code = RESULT_CODE_OK;
ret.type = left.type;
switch (op) {
case OPERATOR_ADD: ret.payload.num.value = left.payload.num.value + right.payload.num.value; break;
case OPERATOR_SUBTRACT: ret.payload.num.value = left.payload.num.value - right.payload.num.value; break;
case OPERATOR_MULTIPLY: ret.payload.num.value = left.payload.num.value * right.payload.num.value; break;
case OPERATOR_DIVIDE: ret.payload.num.value = left.payload.num.value / right.payload.num.value; break;
default:
ret.code = RESULT_CODE_SYNTAX_ERROR;
ret.payload.num.value = 0.0;
break;
}
if (ret.code == RESULT_CODE_OK && ret.type == RESULT_TYPE_VECTOR2) {
switch (op) {
case OPERATOR_ADD: ret.payload.num.value2 = left.payload.num.value2 + right.payload.num.value2; break;
case OPERATOR_SUBTRACT: ret.payload.num.value2 = left.payload.num.value2 - right.payload.num.value2; break;
case OPERATOR_MULTIPLY:
case OPERATOR_DIVIDE:
ret.code = RESULT_CODE_TYPE_ERROR;
break;
}
}
}
}
break;
}
return ret;
}
bool Formula::EvaluateScopedToken (unsigned index, unsigned * outScope, unsigned * outToken) const {
const Term & term = m_termBuffer[index];
if (term.type != Term::TERM_TYPE_TOKEN)
return false;
*outScope = term.payload.scopedToken.scope;
*outToken = term.payload.scopedToken.token;
return true;
}
void Formula::Push (ValueT literalValue) {
m_termBuffer[m_termCount].type = Term::TERM_TYPE_LITERAL;
m_termBuffer[m_termCount].payload.literalValue = literalValue;
++m_termCount; // TODO - ensure that the Push() overloads don't overflow the buffer
}
void Formula::Push (FTerminalEvaluator evaluator) {
m_termBuffer[m_termCount].type = Term::TERM_TYPE_EVALUATOR;
m_termBuffer[m_termCount].payload.evaluator = evaluator;
++m_termCount;
}
void Formula::Push (Operator op) {
m_termBuffer[m_termCount].type = Term::TERM_TYPE_OPERATOR;
m_termBuffer[m_termCount].payload.op = op;
++m_termCount;
}
void Formula::Push (unsigned scope, unsigned token) {
m_termBuffer[m_termCount].type = Term::TERM_TYPE_TOKEN;
m_termBuffer[m_termCount].payload.scopedToken.scope = scope;
m_termBuffer[m_termCount].payload.scopedToken.token = token;
++m_termCount;
}
unsigned Formula::GetEvaluationCounter () {
return s_evaluationCounter;
}