-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.cpp
90 lines (78 loc) · 1.99 KB
/
program.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
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
#include "program.h"
Program::Program()
{
runtimeState = new RuntimeState();
}
Program::~Program()
{
delete runtimeState;
for (Statement *stmt : statementList) {
delete stmt;
}
}
void Program::appendStatement(Statement *stmt)
{
auto it = statementList.begin();
while (it != statementList.end() && (*it)->getStatementNum() < stmt->getStatementNum()) {
++it;
}
statementList.insert(it, stmt);
statementMap[stmt->getStatementNum()] = stmt;
}
void Program::removeStatement(int stmtNum)
{
auto it = statementMap.find(stmtNum);
if (it != statementMap.end()) {
statementList.remove(it->second);
statementMap.erase(it);
}
}
std::vector<std::string> Program::toString() const
{
std::vector<std::string> result;
for (Statement *stmt : statementList) {
result.push_back(stmt->toString());
}
return result;
}
Statement *Program::getStatement(int stmtNum)
{
auto it = statementMap.find(stmtNum);
if (it != statementMap.end()) {
return it->second;
}
return nullptr;
}
Statement *Program::getNextStatement(int stmtNum)
{
auto it = statementList.begin();
while (it != statementList.end() && (*it)->getStatementNum() <= stmtNum) {
++it;
}
if (it != statementList.end()) {
return *it;
}
return nullptr;
}
int Program::getNextStatementNum(int stmtNum) {
Statement *stmt = getNextStatement(stmtNum);
if (stmt != nullptr) {
return stmt->getStatementNum();
}
return -1;
}
Statement *Program::getFirstStatement() {
if (statementList.empty()) {
return nullptr;
}
return statementList.front();
}
void Program::run() {
int curline = getFirstStatement()->getStatementNum();
while (curline > 0) {
auto stmt = getStatement(curline);
runtimeState->PC = getNextStatementNum(curline); //endline's next line is -1
stmt->execute(*runtimeState);//could change PC and context
curline = runtimeState->PC;
}
}