-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhileCommand.cpp
52 lines (43 loc) · 1.43 KB
/
WhileCommand.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
//
// Created by matan on 12/19/19.
//
#include "WhileCommand.h"
#include "helpFromEx1.h"
int WhileCommand::execute() {
protectSymbolTable.lock();
protectSymbolTable.unlock();
//calcuating the values of the expressions:
string varVals = tableToString();
Interpreter* i = new Interpreter();
i->setVariables(varVals);
double leftVal = (i->interpret(WhileCommand::leftSide))->calculate();
double rightVal = (i->interpret(WhileCommand::rightSide))->calculate();
//running the command:
while(conditionExist(leftVal, rightVal, WhileCommand::condition))
{
for(Command* comm : WhileCommand::listOfCommands)
{
comm->execute();
}
//calcuating the values of the expressions:
Interpreter* i1 = new Interpreter();
/////////////////////////////
protectSymbolTable.lock();
protectSymbolTable.unlock();
varVals = tableToString();
i1->setVariables(varVals);
leftVal = (i1->interpret(WhileCommand::leftSide))->calculate();
rightVal = (i1->interpret(WhileCommand::rightSide))->calculate();
delete i1;
}
delete i;
return WhileCommand::sizeOfScope;
}
//constructor:
WhileCommand::WhileCommand(string left, string condition1, string right, int sizeOfScope1, list<Command*> listOfCommands1) {
WhileCommand::leftSide = left;
WhileCommand::rightSide = right;
WhileCommand::condition = condition1;
WhileCommand::sizeOfScope = sizeOfScope1;
WhileCommand::listOfCommands = listOfCommands1;
}