-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditional.cpp
More file actions
executable file
·165 lines (117 loc) · 3.91 KB
/
Conditional.cpp
File metadata and controls
executable file
·165 lines (117 loc) · 3.91 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
#include "Conditional.hpp"
#include "Parser.hpp"
Conditional::Conditional(ListIterator<std::string>* tokensIt, std::string* lines, int lineCount, int& lineNumber, VariableHashMap& vars, int scope) : ControlStructure(vars, scope, Statement::CONDITIONAL) {
std::string conditionStr = "";
tokensIt->next();
while(tokensIt->hasMore()) {
if((*tokensIt->getValue()).compare("then") == 0)
break;
conditionStr += *(tokensIt->getValue()) + " ";
tokensIt->next();
}
if((*tokensIt->getValue()).compare("then") != 0) {
throw RuntimeException("Expected token \"then\" on line " + std::to_string(lineNumber));
}
//std::cout << "Condition: " << conditionStr << std::endl;
condition = new Line(conditionStr, vars, scope + 1);
delete tokensIt;
lineNumber++;
Statement* stmt = nullptr;
do {
stmt = Parser::parse(lines, lineCount, lineNumber, vars, scope + 1);
statements.add(stmt);
} while( (stmt->getType() != Statement::CONTROLSTRUCTUREMOD) && (lineNumber < lineCount) );
if(lineNumber >= lineCount && stmt->getType() != Statement::CONTROLSTRUCTUREMOD) {
throw RuntimeException("End of file reached before conditional statement closed");
}
stmt = nullptr;
ControlStructureMod* close = (ControlStructureMod*)statements.getLast();
if(close->getModType() == ControlStructureMod::LOOP) {
throw RuntimeException("Expected token \"if\" on line " + std::to_string(lineNumber - 1));
} else if(close->getModType() == ControlStructureMod::ELSE) {
elseFlag = true;
//std::cout << "Else detected" << std::endl;
do {
stmt = Parser::parse(lines, lineCount, lineNumber, vars, scope + 1);
elseStatements.add(stmt);
} while( (stmt->getType() != Statement::CONTROLSTRUCTUREMOD) && (lineNumber < lineCount) );
if(lineNumber >= lineCount && stmt->getType() != Statement::CONTROLSTRUCTUREMOD) {
throw RuntimeException("End of file reached before conditional statement closed");
}
close = (ControlStructureMod*)(elseStatements.getLast());
if(close->getModType() != ControlStructureMod::CONDITIONAL) {
throw RuntimeException("Expected token \"if\" on line " + std::to_string(lineNumber - 1));
}
elseStatements.removeLast();
}
statements.removeLast();
close = nullptr;
}
void Conditional::execute() {
ListIterator<Statement>* it;
if(evaluateCondition()) { //Replace later with EvaluateCondition
it = statements.getIterator();
} else if(elseFlag == true) {
it = elseStatements.getIterator();
} else {
return;
}
while(it->hasMore()) {
it->getValue()->execute();
it->next();
}
delete it;
variables.removeWithScope(scope + 1);
}
void Conditional::print() {
for(int i = 0; i < scope; i++) {
std::cout << "\t";
}
std::cout << "if ";
condition->print();
ListIterator<Statement>* li = statements.getIterator();
while(li->hasMore()) {
li->getValue()->print();
li->next();
}
delete li;
if(elseFlag) {
for(int i = 0; i < scope; i++) {
std::cout << "\t";
}
std::cout << "else" << std::endl;
li = elseStatements.getIterator();
while(li->hasMore()) {
li->getValue()->print();
li->next();
}
delete li;
}
for(int i = 0; i < scope; i++) {
std::cout << "\t";
}
std::cout << "end if" << std::endl;
}
void Conditional::printSimple() {
std::cout << "if ";
condition->print();
if(elseFlag) {
std::cout << "\t" << "else";
}
std::cout << std::endl;
}
bool Conditional::evaluateCondition() {
Variable* result = condition->evaluate();
if(result->getType() != Types::BOOLEAN)
throw RuntimeException("Condition must evaluate to BOOLEAN");
bool* rtnBool = (bool*)(result->getValue());
bool rtn = *rtnBool;
//Dubious uncommenting
//delete rtnBool;
if(result->getScope() == -1)
delete result;
return rtn;
}
Conditional::~Conditional() {
delete condition;
}