-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopCommand.cpp
More file actions
139 lines (130 loc) · 5.7 KB
/
LoopCommand.cpp
File metadata and controls
139 lines (130 loc) · 5.7 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
//
// Created by duni on 22/12/2019.
//
#include "LoopCommand.h"
#include "ExpressionCalculate.h"
#include "globalVariables.h"
#include "SetCommand.h"
#include <algorithm>
#include <iostream>
int LoopCommand::execute(vector<string> params) {
int index = 0;
int returnCount = 0;
int currCell = 0;
Command *c;
vector<string> block;
auto first = params.begin();
auto last = params.begin();
advance(last, 4);
// Insert the condition of the if statement into a new vector for evaluating it later on
vector<string> conditionVec(first, last);
vector<string>::iterator iteratorBegin, iteratorEnd = params.begin();
iteratorBegin = params.begin();
advance(iteratorEnd, 5);
// Delete all the cells that are part of the condition of the if statement from the original vector
params.erase(iteratorBegin, iteratorEnd);
// Add the cells of the condition in the if statement into the return value
returnCount += 5;
Interpreter *i = new Interpreter();
Expression *left = nullptr;
Expression *right = nullptr;
try {
// Remove spaces from the left side of the condition
string::iterator end_pos = remove(conditionVec[1].begin(), conditionVec[1].end(), ' ');
conditionVec[1].erase(end_pos, conditionVec[1].end());
// Remove tabs from the left side of the condition
end_pos = remove(conditionVec[1].begin(), conditionVec[1].end(), '\t');
conditionVec[1].erase(end_pos, conditionVec[1].end());
// Calculate the left side of the condition
left = i->interpret(conditionVec[1]);
double answerLeft = left->calculate();
// Remove spaces from the right side of the condition
end_pos = remove(conditionVec[3].begin(), conditionVec[3].end(), ' ');
conditionVec[3].erase(end_pos, conditionVec[3].end());
// Remove tabs from the right side of the condition
end_pos = remove(conditionVec[3].begin(), conditionVec[3].end(), '\t');
conditionVec[3].erase(end_pos, conditionVec[3].end());
// Calculate the right side of the condition
right = i->interpret(conditionVec[3]);
double answerRight = right->calculate();
// While the condition of the while statement is true - execute the commands of the while block
while (resultCondition(answerLeft, answerRight, conditionVec[2])) {
currCell = 0;
/* Go through all of the commands in the block of the while, and insert them into a new vector that contains
the commands to be executed in the current iteration (because the values of the different variables are
being updated in every new iteration of the while command) */
while (params[currCell].compare("}") != 0) {
block.emplace_back(params[currCell]);
currCell++;
}
index = 0;
// Go through all of the commands in the if statement block
while (block.size() > 0) {
index = 0;
bool isCSetCommand = false;
// Remove spaces
string::iterator end_pos = remove(block[index].begin(), block[index].end(), ' ');
block[index].erase(end_pos, block[index].end());
// Remove tabs
end_pos = remove(block[index].begin(), block[index].end(), '\t');
block[index].erase(end_pos, block[index].end());
/* Check if the current command is one of the commands from the map of commands or it is a command that
assigns a specific variable */
if (globalVariables::CommandMap.find(block[index]) == globalVariables::CommandMap.end()) {
c = new SetCommand();
isCSetCommand = true;
} else {
c = globalVariables::CommandMap.at(block[index]);
}
// Execute the current command and add it's number of cells to the return value
if (c != nullptr) {
index += c->execute(block);
if (isCSetCommand) {
delete c;
}
}
// Delete the current command from the block commands vector
iteratorEnd = block.begin();
iteratorBegin = block.begin();
advance(iteratorEnd, index);
block.erase(iteratorBegin, iteratorEnd);
}
// Calculate both of the sides of the while condition
left = i->interpret(conditionVec[1]);
answerLeft = left->calculate();
right = i->interpret(conditionVec[3]);
answerRight = right->calculate();
}
index = 0;
// add the if block cells to the return value
while (params[index].compare("}") != 0) {
index++;
}
returnCount += index + 1;
}
// Delete memory
catch (const char *e) {
if (e != nullptr) {
cout << "no" << endl;
delete e;
delete i;
}
}
return returnCount;
}
// This function is evaluating the condition of the while command
bool LoopCommand::resultCondition(double answerLeft, double answerRight, string op) {
if (op.compare("<") == 0) {
return (answerLeft < answerRight);
} else if (op.compare(">") == 0) {
return (answerLeft > answerRight);
} else if (op.compare("<=") == 0) {
return (answerLeft <= answerRight);
} else if (op.compare(">=") == 0) {
return (answerLeft >= answerRight);
} else if (op.compare("==") == 0) {
return (abs(answerLeft - answerRight) < 0.0001);
} else {
return (abs(answerLeft - answerRight) >= 0.0001);
}
}