-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhileCommand.cpp
More file actions
97 lines (88 loc) · 3.23 KB
/
WhileCommand.cpp
File metadata and controls
97 lines (88 loc) · 3.23 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
#include <cstring>
#include "WhileCommand.h"
#include "DataCommands.h"
#include "DataVars.h"
#include "ConditionParser.h"
WhileCommand::WhileCommand(DataCommands *dataCommands, DataVars *dataVars) {
this->dataCommands = dataCommands;
this->dataVars = dataVars;
}
void WhileCommand::doCommand() {
// get the index from dataCommands
unsigned long index = this->dataCommands->getIndex();
// skip the command
index++;
// get the left from the vector in dataCommands
string left = this->dataCommands->getSeparated().at(index);
// skip the left
index++;
// get the operator from the vector in dataCommands
string oper = this->dataCommands->getSeparated().at(index);
// skip the oper
index++;
// get the right from the vector in dataCommands
string right = this->dataCommands->getSeparated().at(index);
// skip the right
index++;
ConditionParser *conditionParser;
conditionParser = new ConditionParser(left, oper, right, this->dataVars);
this->deathMap.push_back(conditionParser);
unordered_map<string, Command *> stringsToCommands = dataCommands->getStringsToCommands();
unsigned long oldIndex = index;
bool isEntered = false;
while (conditionParser->checkCondition()) {
isEntered = true;
index = oldIndex;
// set the new index of dataCommands
this->dataCommands->setIndex(oldIndex);
vector<string> separetedCopy=this->dataCommands->getSeparated();
auto it1 = separetedCopy.begin();
it1 += index;
Command *command;
int bracesCounter = 0;
do {
string currentComStr=separetedCopy.at(index);
if (currentComStr.find('{') != string::npos) {
bracesCounter++;
index++;
it1++;
// set the new index of dataCommands
this->dataCommands->setIndex(index);
continue;
} else if (currentComStr.find('}') != string::npos) {
bracesCounter--;
index++;
// set the new index of dataCommands
this->dataCommands->setIndex(index);
continue;
}
if(stringsToCommands.count(*it1)>=1) {
command = stringsToCommands.at(*it1);
command->doCommand();
}
else{
it1++;
continue;
}
index = this->dataCommands->getIndex();
it1 = separetedCopy.begin();
it1 += index;
} while (bracesCounter != 0);
conditionParser = new ConditionParser(left, oper, right, this->dataVars);
this->deathMap.push_back(conditionParser);
}
if (!isEntered) {
//right now at '{'. index++ until we get to the last '}'
int bracesSkipCount = 0;
do{
if (this->dataCommands->getSeparated().at(index).find('{') != string::npos) {
bracesSkipCount++;
}else if (this->dataCommands->getSeparated().at(index).find('}') != string::npos) {
bracesSkipCount--;
}
index++;
}while (bracesSkipCount > 0);
}
// set the new index of dataCommands
this->dataCommands->setIndex(index);
}