Repository navigation
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
184 lines (144 loc) · 4.39 KB
/
Copy pathinterpreter.cpp
File metadata and controls
184 lines (144 loc) · 4.39 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <unordered_map>
#include <queue>
#include "interpreter.h"
#include "storytokenizer.h"
#include "goto.h"
#include "ifs.h"
#include "link.h"
#include "set.h"
#include "text.h"
#include "section.h"
using namespace std;
Interpreter::Interpreter(StoryTokenizer st)
{
//loops through all passages to add PassageTokens to the map and vector
while(st.hasNextPassage())
{
PassageToken ptok = st.nextPassage();
pass_index.emplace(ptok.getName(),passages.size());
passages.push_back(ptok);
}
hitLink = 0;
}
void Interpreter::run()
{
int finished = 0; //allows 1 more pass through once at the end of the game
while(finished == 0) //loops while through passages while remaining within bounds
{
hitLink = 0;
hitGoto = 0;
links.clear();
queue<Ifs> ifBlocks;//queue used to store each if statement block. queue in order to follow first in first out
PassageTokenizer ptok(passages[pos].getText()); //uses the passage vector for the tokenizer
while(ptok.hasNextSection())
{
SectionToken stok(ptok.nextSection());
//checks if the current section is an if/elseif/else and finds its block
if (stok.getType() == IF || stok.getType() == ELSEIF || stok.getType() == ELSE)
{
string copy = stok.getText();
stok = ptok.nextSection();
Ifs ifs(copy, stok.getText());
ifBlocks.push(ifs);
stok = ptok.nextSection();
//if there are any elseif/else related to the previous if statement, that gets added to the queue as well
while (stok.getType() == ELSE || stok.getType() == ELSEIF)
{
copy = stok.getText();
stok = ptok.nextSection();
Ifs extra(copy, stok.getText());
ifBlocks.push(extra);
}
while(!ifBlocks.empty())
{
ifBlocks.front().execute(this);//executes the front if statement
if(ifBlocks.front().getComplete() == 1)//if that executes properly, removes the other elseifs or else from the queue
{
while(!ifBlocks.empty())
ifBlocks.pop();
}
else
{
ifBlocks.pop();
}
}
if (stok.getType() == SET)
{
Set st(stok.getText());
st.execute(this);
}
else if (stok.getType() == TEXT)
{
Text tx(stok.getText());
tx.execute(this);
}
else if (stok.getType() == LINK)
{
Link* link = new Link(stok.getText());
cout << link->getText();
links.push_back(link);
hitLink = 1;
}
else if (stok.getType() == GOTO)
{
Goto gt(stok.getText());
gt.execute(this);
hitGoto = 1;
break;
}
if (hitGoto == 1)
break;
}
else if(stok.getType() == SET)
{
Set st(stok.getText());
st.execute(this);
}
else if(stok.getType() == TEXT)
{
Text tx(stok.getText());
tx.execute(this);
}
else if(stok.getType() == LINK)
{
Link* link = new Link(stok.getText());
cout << link->getText();
links.push_back(link);
hitLink = 1;
}
else if(stok.getType() == GOTO)
{
Goto gt(stok.getText());
gt.execute(this);
hitGoto = 1;
break;
}
if (hitGoto == 1)
break;
}
//Ends Program if passage with no Links or Gotos is ran
if ((hitLink == 0) && (hitGoto == 0))
finished = 1;
if(hitLink == 1)
{
int choicecount = 0;
cout << "\n"<< endl;
for (int i = 0; i < links.size(); i++) //prints ll the link choices
{
cout << i + 1 << ". " << links[i]->getText() << endl;
choicecount++;
}
int choice;
cin >> choice;
while (!cin || choice < 1 || choice > choicecount) //takes user input for the passage they want to go to
{
cout << "Invalid Choice, Re-enter a number between 1 and " << choicecount << ": ";
cin.clear();
cin.ignore(10000, '\n');
cin >> choice;
}
links[choice - 1]->execute(this);//moves to the passage that the user selected
}
}
}