This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.cpp
261 lines (243 loc) · 7.66 KB
/
Parser.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "Parser.h"
#include "Exceptions.h"
using namespace std;
// Note: could be implemented using some sort of lookup table
// data structure indexed by a pair of enums.
Mode Parser::determineMode(const Token t1, const Token t2) {
switch(t1) {
case LINEFEED:
return FLOWCONT;
case SPACE:
return STACKMANIP;
case TAB:
switch(t2) {
case LINEFEED:
return IO;
case SPACE:
return ARITH;
case TAB:
return HEAPACC;
default:
throw UnreachableTokenException();
}
default:
throw UnreachableTokenException();
}
}
vector<Token> Parser::tokenize(const string &program) {
vector<Token> tokens;
for(auto k = program.begin(); k != program.end(); k++) {
switch(*k) {
case '\n':
tokens.push_back(LINEFEED);
break;
case ' ':
tokens.push_back(SPACE);
break;
case '\t':
tokens.push_back(TAB);
break;
}
}
return tokens;
}
Program Parser::tokensToProgram(const vector<Token> &tokens) {
int amount = tokens.size();
if(amount < 3) {
exit(0); // Empty program
}
Mode m;
try {
m = determineMode(tokens[0], tokens[1]);
} catch (exception& e) { // Unreachable token exception
throw e;
}
Program p;
// FLOWCONT and STACKMANIP are only 1 token long, the rest is 2 tokens long
int start = ((m == FLOWCONT || m == STACKMANIP) ? 1 : 2);
for(int k = start; k < amount; k++) {
if(k == amount) {
cout << "k has reached max" << endl; // We might want to use an exception here...
}
if(m == STACKMANIP) {
processStackManip(tokens, p, k);
} else if(m == ARITH) {
processArith(tokens, p, k);
} else if(m == HEAPACC) {
processHeapAcc(tokens, p, k);
} else if(m == FLOWCONT) {
processFlowCont(tokens, p, k);
} else if(m == IO) {
processIO(tokens, p, k);
} else {
throw UnreachableTokenException();
}
k++; // Proceed to next instruction
if(k != amount) {
m = determineMode(tokens[k], tokens[k + 1]);
if(!(m == STACKMANIP || m == FLOWCONT)) {
k++;
}
}
}
return p;
}
// Side-effect: mutates the index from the for-loop in tokensToProgram
// Labels are also represented as numbers, so labels will be handled as well.
long Parser::tokensToNumber(const vector<Token> &tokens, int &index) {
vector<Token> binNum;
int amount = tokens.size();
int sign;
long sum = 0;
while(tokens[index] != LINEFEED) { // A number is terminated by a LINEFEED
binNum.push_back(tokens[index++]);
if(index == amount) {
throw PrematureEndException();
}
}
if(binNum.front() == SPACE) {
sign = 1;
} else if(binNum.front() == TAB) {
sign = -1;
} else {
throw UndefinedSignException();
}
binNum.erase(binNum.begin()); // Pop the sign bit
for(int k = binNum.size() - 1; k >= 0; k--) {
//cout << "Sum = " << sum << endl;
sum += ((binNum[k] == TAB) ? pow(2, k) : 0);
}
return sign * sum;
}
void Parser::parseNumber(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[++k] == LINEFEED) { // No label as argument
throw NoLabelArgumentException();
} else { // We're going to parse the label now
p.push_back(tokensToNumber(tokens, k));
}
}
void Parser::processStackManip(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[k] == SPACE) { // PUSH
p.push_back(PUSH);
parseNumber(tokens, p, k);
} else if(tokens[k] == TAB) {
k++;
if(tokens[k] == SPACE) { // COPY
p.push_back(COPY);
} else if(tokens[k] == LINEFEED) { // SLIDE
p.push_back(SLIDE);
} else {
throw UnreachableTokenException();
}
// COPY and SLIDE both require a numeric argument,
// so we shall try to parse a number now
parseNumber(tokens, p, k);
} else if(tokens[k] == LINEFEED) { // DUP, SWAP or DISCARD
// Could perhaps also be done more concisely
// with the ?:-operator, but I reckon something will go wrong
// in the exception branch
k++;
if(tokens[k] == SPACE) { // DUP
p.push_back(DUP);
} else if(tokens[k] == TAB) { // SWAP
p.push_back(SWAP);
} else if(tokens[k] == LINEFEED) { // DISCARD
p.push_back(DISCARD);
} else {
throw UnreachableTokenException();
}
} else {
throw UnreachableTokenException();
}
}
void Parser::processArith(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[k] == SPACE) {
k++;
if(tokens[k] == SPACE) { // ADD
p.push_back(ADD);
} else if(tokens[k] == TAB) { // SUB
p.push_back(SUB);
} else if(tokens[k] == LINEFEED) { // MUL
p.push_back(MUL);
} else {
throw UnreachableTokenException();
}
} else if(tokens[k] == TAB) {
k++;
if(tokens[k] == SPACE) { // DIV
p.push_back(DIV);
} else if(tokens[k] == TAB) { // MOD
p.push_back(MOD);
} else {
throw UnreachableTokenException();
}
} else {
throw UnreachableTokenException();
}
}
void Parser::processHeapAcc(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[k] == SPACE) { // STORE
p.push_back(STORE);
} else if(tokens[k] == TAB) { // RETRIEVE
p.push_back(RETRIEVE);
} else {
throw UnreachableTokenException();
}
}
void Parser::processFlowCont(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[k] == SPACE) {
k++;
if(tokens[k] == SPACE) { // MARK
p.push_back(MARK);
parseNumber(tokens, p, k);
} else if(tokens[k] == TAB) { // CALL
p.push_back(CALL);
parseNumber(tokens, p, k);
} else if(tokens[k] == LINEFEED) { // JUMP
p.push_back(JUMP);
parseNumber(tokens, p, k);
} else {
throw UnreachableTokenException();
}
} else if(tokens[k] == TAB) {
k++;
if(tokens[k] == SPACE) { // JUMPZERO
p.push_back(JUMPZERO);
parseNumber(tokens, p, k);
} else if(tokens[k] == TAB) { // JUMPNEG
p.push_back(JUMPNEG);
parseNumber(tokens, p, k);
} else if(tokens[k] == LINEFEED) { // ENDSUB
p.push_back(ENDSUB);
} else {
throw UnreachableTokenException();
}
} else if(tokens[k] == LINEFEED && tokens[++k] == LINEFEED) { // ENDPROG
p.push_back(ENDPROG);
} else {
throw UnreachableTokenException();
}
}
void Parser::processIO(const vector<Token> &tokens, Program &p, int &k) {
if(tokens[k] == SPACE) {
k++;
if(tokens[k] == SPACE) { // WRITEC
p.push_back(WRITEC);
} else if(tokens[k] == TAB) { // WRITEN
p.push_back(WRITEN);
} else {
throw UnreachableTokenException();
}
} else if(tokens[k] == TAB) {
k++;
if(tokens[k] == SPACE) { // READC
p.push_back(READC);
} else if(tokens[k] == TAB) { // READN
p.push_back(READN);
} else {
throw UnreachableTokenException();
}
} else {
throw UnreachableTokenException();
}
}