-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForLine.cpp
More file actions
executable file
·78 lines (54 loc) · 1.82 KB
/
ForLine.cpp
File metadata and controls
executable file
·78 lines (54 loc) · 1.82 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
#include "ForLine.hpp"
#include <cstdlib>
#include <regex>
ForLine::ForLine(std::string ln, VariableHashMap& vars, int s) : Line("", vars, s) {
std::cmatch cm;
try {
std::regex_match(ln.c_str(), cm, forLineRegex);
} catch (std::exception&) {
throw RuntimeException("Invalid phrasing for for-style loop");
}
if(cm.size() != 4) {
throw RuntimeException("Invalid phrasing for for-style loop");
}
loopVarName = new VariableName(cm[1]);
initializationLine = new Line(*loopVarName + " = " + std::string(cm[2]), vars, scope);
line = *loopVarName + " <= " + std::string(cm[3]);
}
Variable* ForLine::evaluate() {
if(loopVar != nullptr)
variables.add(new VariableName(*loopVarName), loopVar);
return Line::evaluate();
}
void ForLine::print() {
}
void ForLine::step() {
(Line(*loopVarName + " = " + *loopVarName + " + 1", variables, scope)).execute(); //Execute an expression to step the variable
if(loopVar != nullptr) {
loopVar = new Variable(variables.get(loopVarName));
if(loopVar == nullptr) {
throw RuntimeException("Invalid Variable");
}
}
}
void ForLine::initialize() {
bool variableExists = true;
if(!variables.containsKey(loopVarName)) {
variables.add(new VariableName(*loopVarName), new Variable(new int(0),Types::INT,scope));
variableExists = false;
}
initializationLine->execute(); //Run line to initialize the loop variable
if(!variableExists)
loopVar = new Variable(variables.get(loopVarName));
}
void ForLine::printSimple() {
}
ForLine::~ForLine() {
delete loopVar;
delete loopVarName;
delete initializationLine;
}
void ForLine::execute() {
throw RuntimeException("For Lines should not be executed");
}
const std::regex ForLine::forLineRegex = std::regex("([A-Z]+)\\s+from\\s+(.*?)\\s+to\\s+(.*)");