-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoop.cpp
More file actions
338 lines (302 loc) · 8.88 KB
/
Loop.cpp
File metadata and controls
338 lines (302 loc) · 8.88 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "header.h"
//input - array of data, which begin is where contition starts
//count - current position on array
//endofarray - point where array is end
int conditionLoop(char* input, int count, int sizeOfArray, int* currentnumberofoperation, int maxnumberofoperation, VarVector* variables)
{
int beginIndex = count;
count++;
int shiftOfCondition;
int shiftOfLoop;
int rightBracketCounter = 0;
int leftBracketCounter = 0;
int whileCount = count;
//
//Calculating range of loop (left and right bracket), subfunction also include internal loops and conditions
//the result is value, which indicate where is ends loop
while ((whileCount++ < sizeOfArray))
{
if (*(input + whileCount) == '{')
leftBracketCounter++;
else if (*(input + whileCount) == '}')
rightBracketCounter++;
if (leftBracketCounter != 0)
{
if (leftBracketCounter == rightBracketCounter)break;
}
}
whileCount++;
//
//if has another loop which ends when loop reach '{' bracket. Loop return symbol which can be null/!null symbol.
//
if (!condition(input, count, sizeOfArray, currentnumberofoperation, maxnumberofoperation, variables, &shiftOfCondition).isNull)
{
//shift necessary to avoid '{' bracket
shiftOfCondition++;
//main loop
condition(input, shiftOfCondition, sizeOfArray, currentnumberofoperation, maxnumberofoperation, variables, &shiftOfLoop);
}
//
//each check of contition incements operation counter
//
*currentnumberofoperation = *currentnumberofoperation + 1;
return whileCount - beginIndex;
}
//
//Function where loops are interpreted
//
int loop(char* input, int count, int sizeOfArray, int* currentnumberofoperation, int maxnumberofoperation, VarVector* variables)
{
int beginIndex = count;
count++;
int shiftOfCondition;
int shiftOfLoop;
int rightBracketCounter = 0;
int leftBracketCounter = 0;
int whileCount = count;
//
//counting begining and ending of loop
//
while ((whileCount++ < sizeOfArray))
{
if (*(input + whileCount) == '{')
leftBracketCounter++;
else if (*(input + whileCount) == '}')
rightBracketCounter++;
if (leftBracketCounter != 0)
{
if (leftBracketCounter == rightBracketCounter)break;
}
}
whileCount++;
//
//Get "safe" counter for all loops. Counter is not necessary for every loops
//
symbol loopConditionCount;
if (isDigit(*(input + whileCount)))
{
loopConditionCount = createDigitFromArray((input + whileCount), &whileCount);
whileCount++;
}
else
{
loopConditionCount.isNull = true;
}
while (!condition(input, count, sizeOfArray, currentnumberofoperation, maxnumberofoperation, variables, &shiftOfCondition).isNull)
{
if (!loopConditionCount.isNull)
{
if (loopConditionCount.value-- <= 0)
break;
}
*currentnumberofoperation = *currentnumberofoperation + 1;
shiftOfCondition++;
condition(input, shiftOfCondition, sizeOfArray, currentnumberofoperation, maxnumberofoperation, variables, &shiftOfLoop);
if (*currentnumberofoperation >= maxnumberofoperation)
break;
}
*currentnumberofoperation = *currentnumberofoperation + 1;
return whileCount - beginIndex;
}
//
//Main part of program, function interpreting each symbol from whole array.
//
symbol condition(char* data, int count, int sizeOfArray, int* numberOfOperation,
int maxnumberofoperation, VarVector* variables, int* shift)
{
bool isPossibleEndEquation = false;
queue equationQueue;
stack operatorStack;
symbol resultValue = symbol();
VarVector resultVar = VarVector();
symbol lastSymbol;
lastSymbol.isNull = true;
lastSymbol.strenght = 0;
lastSymbol._type = fobbidenS;
for (count; count < sizeOfArray; count++)
{
//
//skip next symbol if it is space of tab
//
if (*(data + count) == ' ' || *(data + count) == '\t')
{
isPossibleEndEquation = true;
continue;
}
//
//It happens when interpreter find whitesoace. Interpreter must find out, that it is end of equation, or just ordinary space
//
else if (isPossibleEndEquation)
{
symbol nextSymbol = createSymbol(data + count);
if (isMinusDigit((data + count), lastSymbol) || isDigit(*(data + count)) || isVar(*(data + count)) || *(data + count) == '(')
{
if (lastSymbol._type == digitS || lastSymbol._type == variableS || lastSymbol._type == rightBracketS)
{
resultValue = resultOfEquation(&equationQueue, &operatorStack, &resultVar, variables, numberOfOperation, maxnumberofoperation);
operatorStack = stack();
equationQueue = queue();
resultVar.clear();
if (*numberOfOperation > maxnumberofoperation)
{
break;
}
}
}
else if (isOperatorSymbol(nextSymbol) && isOperatorSymbol(lastSymbol))
{
if (!(nextSymbol._type == negationS || nextSymbol._type == minusS))
{
resultValue = resultOfEquation(&equationQueue, &operatorStack, &resultVar, variables, numberOfOperation, maxnumberofoperation);
operatorStack = stack();
equationQueue = queue();
resultVar.clear();
if (*numberOfOperation > maxnumberofoperation)
{
break;
}
}
}
}
if (*(data + count) == '?')
{
count += conditionLoop(data, count, sizeOfArray, numberOfOperation, maxnumberofoperation, variables);
continue;
}
else if (*(data + count) == '@')
{
count += loop(data, count, sizeOfArray, numberOfOperation, maxnumberofoperation, variables);
continue;
}
else if (*(data + count) == '{')
{
break;
}
else if (*(data + count) == '}')
{
count++;
break;
}
if (isMinusDigit((data + count), lastSymbol))
{
count++;
symbol newDigit = createDigitFromArray(data + count, &count);
newDigit.value = -newDigit.value;
equationQueue.push(newDigit);
lastSymbol = newDigit;
}
else if (isDigit(*(data + count)))
{
symbol newDigit = createDigitFromArray(data + count, &count);
equationQueue.push(newDigit);
lastSymbol = newDigit;
}
else if (isVar(*(data + count)))
{
char* name = createVarNameFromArray((data + count), &count);
symbol newVar = createEmptyVariable();
//
//check whether the variable is initalized before
//If it is true - new symbol/var gets past value
//
int index = variables->isInitalizedVariable(name);
if (index != -1)
{
newVar.index = getVariableID(name, variables);
newVar.isNull = variables->at(newVar.index).isNull;
newVar.value = variables->at(newVar.index).value;
}
else
{
newVar.index = variables->size();
var newVariable;
newVariable.isInitalized = false;
newVariable.isNull = true;
newVariable.name = name;
newVariable.value = 0;
variables->push_back(newVariable);
}
//
//when after variable is symbol '=', it indicates that this is the result of operation and it isn't included to equation
//
if (nextSymbol((data + count), 1) == '=' && nextSymbol((data + count), 2) != '=' && equationQueue.empty() && operatorStack.empty())
{
resultVar.push_back(variables->at(newVar.index));
count += nextSymbolShift((data + count), 1) - 1;
lastSymbol = createSymbol((data + count));
}
else
{
equationQueue.push(newVar);
lastSymbol = newVar;
}
}
else
{
symbol newSymbol;
if (isMinusVar(lastSymbol, data + count))
{
newSymbol = createMinusVarSymbol();
}
else
{
newSymbol = createSymbol(data + count);
}
if (newSymbol._type == equalS || newSymbol._type == notEqualS || newSymbol._type == lessEqualThenS
|| newSymbol._type == greatherEqualThenS)
{
count++;
}
//
//Special condition for one parameter operators
//
if (newSymbol._type == leftBracketS || newSymbol._type == minusVarS || newSymbol._type == negationS)
{
operatorStack.push(newSymbol);
}
//
//If interpreter finds right bracket then all operator on stack (until left bracket) are poped and pushed to queue.
//
else if (newSymbol._type == rightBracketS)
{
while (operatorStack.top()._type != leftBracketS)
{
symbol buffsymbol = operatorStack.top();
operatorStack.pop();
equationQueue.push(buffsymbol);
}
operatorStack.pop();
}
else if (!operatorStack.empty() && operatorStack.top().strenght >= newSymbol.strenght)
{
int size = operatorStack.size();
for (int a = 0; a < size; a++)
{
symbol buffsymbol = operatorStack.top();
if (buffsymbol._type == leftBracketS || (buffsymbol.strenght < newSymbol.strenght))
break;
operatorStack.pop();
equationQueue.push(buffsymbol);
}
operatorStack.push(newSymbol);
}
else
{
operatorStack.push(newSymbol);
}
lastSymbol = newSymbol;
}
}
*shift = count;
//
//Sometimes loop is ending and stack or queue isn't empty (brackets or end of size). Before return of function all equation must be done.
//
if (equationQueue.size() != 0)
{
resultValue = resultOfEquation(&equationQueue, &operatorStack, &resultVar, variables, numberOfOperation, maxnumberofoperation);
operatorStack = stack();
equationQueue = queue();
resultVar.clear();
}
return resultValue;
}