-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIfCommand.cpp
More file actions
87 lines (73 loc) · 2.69 KB
/
IfCommand.cpp
File metadata and controls
87 lines (73 loc) · 2.69 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
#include "IfCommand.h"
#include "ConditionParser.h"
IfCommand::IfCommand(DataCommands *dataCommands, DataVars *dataVars) {
this->dataCommands = dataCommands;
this->dataVars = dataVars;
}
void IfCommand::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;
if (conditionParser->checkCondition()) {
index = oldIndex;
// set the new index of dataCommands
this->dataCommands->setIndex(oldIndex);
auto it1 = this->dataCommands->getSeparated().begin();
it1 += index;
Command *command;
int bracesCounter = 0;
do {
if (this->dataCommands->getSeparated().at(index).find('{') != string::npos) {
bracesCounter++;
index++;
it1++;
// set the new index of dataCommands
this->dataCommands->setIndex(index);
continue;
} else if (this->dataCommands->getSeparated().at(index).find('}') != string::npos) {
bracesCounter--;
index++;
// set the new index of dataCommands
this->dataCommands->setIndex(index);
continue;
}
auto it2 = stringsToCommands.find(*it1);
if (it2 == stringsToCommands.end()) {
it1++;
continue;
}
command = it2->second;
command->doCommand();
index = this->dataCommands->getIndex();
it1 = this->dataCommands->getSeparated().begin();
it1 += index;
} while (bracesCounter != 0);
} else {
//index++ until we get to the }
while (this->dataCommands->getSeparated().at(index).find('}')==string::npos){
index++;
}
//skip }
index++;
}
// set the new index of dataCommands
this->dataCommands->setIndex(index);
}