-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlm.cc
382 lines (299 loc) · 8.68 KB
/
mlm.cc
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
string get_all_lines(string dir) {
ifstream file(dir);
string data;
char temp;
while (file.get(temp)) {
if (temp == '\n') {
data += ';';
continue;
}
data += temp;
}
if (data.back() != ';') {
data += ';';
}
return data;
}
bool is_token(char q) {
map<char, string> descriptors = {
{'=', "VAR_DECLARATION"},
{'+', "ADD_OPERATION"},
{'-', "SUB_OPERATION"},
{':', "CHAR_OUTPUT"},
{'<', "INT_OUTPUT"},
{'j', "JUMP_OPERATION"},
{';', "END_LINE"}
};
if (descriptors.find(q) != descriptors.end()) {
return true;
}
return false;
}
bool is_int(string s) {
try {
stoi(s);
return true;
}
catch (const exception& e) {
return false;
}
}
struct Variable {
string name;
int value;
};
struct Variables {
vector<Variable> variables;
};
struct Label {
int location;
string name;
};
struct Labels {
vector<Label> labels;
};
class Token {
public:
Token(string input) {
value = input;
tokenize(input);
}
string get_value() {
return value;
}
string get_desc() {
return descriptor;
}
void retokenize(string input) {
value = input;
tokenize(input);
}
private:
void tokenize(string input) {
map<string, string> descr =
{
{"=", "VAR_DECLARATION"},
{"+", "ADD_OPERATION"},
{"-", "SUB_OPERATION"},
{":", "CHAR_OUTPUT"},
{"<", "INT_OUTPUT"},
{"j", "JUMP_OPERATION"},
{";", "END_LINE"}
};
if (descr.find(input) != descr.end()) {
descriptor = descr.at(input);
}
else if (is_int(input)) {
descriptor = "INT_LITERAL";
}
else {
descriptor = "UNDEFINED";
}
}
string value;
string descriptor;
};
class Tokens {
public:
void add_token(Token &t) {
tokens.push_back(t);
}
void print_tokens() {
for (auto& a : tokens) {
cout << a.get_value() << " " << a.get_desc() << '\n';
}
}
int size() {
return tokens.size();
}
Token at(int index) {
return tokens[index];
}
Token& mod_at(int index) {
return tokens[index];
}
int get_line(int token_num) {
int line_count = 0;
for (int i = 0; i < tokens.size(); i++) {
if (tokens.at(i).get_desc() == "END_LINE")
line_count++;
if (i == token_num) {
return line_count;
}
}
return -1; // out of range of tokens
}
private:
vector<Token> tokens;
};
Label make_label(int loc, string naame) {
Label l;
l.location = loc;
l.name = naame;
return l;
}
Token make_token(char q = ' ', string s = "") {
if (s == "") {
string r = "";
r += q;
Token a(r);
return a;
}
else if (q == ' ') {
Token a(s);
return a;
}
return make_token(' ', "Error:-1");
}
Tokens tokenize_all_data(string &lines) {
Tokens tokens;
string current;
for (char &i : lines) {
current += i;
if (current == " ") {
current.clear();
}
if (is_token(i)) {
Token l = make_token(i);
current.pop_back();
// finding a token after some time, tells us that we have reached the end of a variable declaration
if (current != "" && current != " ") {
Token a = make_token(' ', current);
tokens.add_token(a);
current.clear();
}
// add l after the fact because that is the order it is in the source mlm file
tokens.add_token(l);
continue;
}
else if (i == ' ') {
if (current != "" && current != " ") {
if (current.back() == ' ') {
current.pop_back();
}
Token a = make_token(' ', current);
tokens.add_token(a);
current.clear();
}
}
}
return tokens;
}
void run_mlm(string dir) {
string lines = get_all_lines(dir);
Tokens tokens = tokenize_all_data(lines);
Variables vars;
Labels labls;
for (int q = 0; q < tokens.size(); q++) {
if (tokens.at(q).get_desc() == "UNDEFINED" && tokens.at(q + 1).get_desc() == "CHAR_OUTPUT") { // we know we are working with a label
labls.labels.push_back(make_label(q, tokens.at(q).get_value()));
continue;
}
if (tokens.at(q).get_desc() == "JUMP_OPERATION" && tokens.at(q + 3).get_desc() != "INT_LITERAL") {
int loc;
for (int i = 0; i < labls.labels.size(); i++) {
if (labls.labels[i].name == tokens.at(q + 3).get_value()) {
tokens.mod_at(q + 3).retokenize(to_string(labls.labels[i].location));
// cout << "once " << tokens.at(q + 3).get_value() << " " << labls.labels[i].location << '\n';
break;
}
}
}
}
// tokens.print_tokens();
// maybe add error handling here where you check that all the tokens make sense
for (int q = 0; q < tokens.size(); q++) {
if (tokens.at(q).get_desc() == "VAR_DECLARATION") {
Variable v;
v.name = tokens.at(q + 1).get_value();
v.value = 0;
vars.variables.push_back(v);
// increment q here because we have already sone what we need to with q + 1
q++;
continue;
}
else if (tokens.at(q).get_desc() == "ADD_OPERATION") {
int index = -1;
for (int i = 0; i < vars.variables.size(); i++) {
if (vars.variables[i].name == tokens.at(q + 1).get_value()) {
vars.variables[i].value++;
break;
}
}
q++;
continue;
}
else if (tokens.at(q).get_desc() == "SUB_OPERATION") {
for (int i = 0; i < vars.variables.size(); i++) {
if (vars.variables[i].name == tokens.at(q + 1).get_value()) {
vars.variables[i].value--;
break;
}
}
q++;
continue;
}
else if (tokens.at(q).get_desc() == "INT_OUTPUT") {
for (int i = 0; i < vars.variables.size(); i++) {
if (vars.variables[i].name == tokens.at(q + 1).get_value()) {
cout << vars.variables[i].value;
break;
}
}
q++;
continue;
}
else if (tokens.at(q).get_desc() == "CHAR_OUTPUT") {
for (int index = 0; index < vars.variables.size(); index++) {
if (vars.variables[index].name == tokens.at(q + 1).get_value()) {
cout << (vars.variables[index].value == 1 ? "a" : vars.variables[index].value == 2 ? "b" : vars.variables[index].value == 3 ? "c" : vars.variables[index].value == 4 ? "d" : vars.variables[index].value == 5 ? "e" : vars.variables[index].value == 6 ? "f" : vars.variables[index].value == 7 ? "g" : vars.variables[index].value == 8 ? "h" : vars.variables[index].value == 9 ? "i" : vars.variables[index].value == 10 ? "j" : vars.variables[index].value == 11 ? "k" : vars.variables[index].value == 12 ? "l" : vars.variables[index].value == 13 ? "m" : vars.variables[index].value == 14 ? "n" : vars.variables[index].value == 15 ? "o" : vars.variables[index].value == 16 ? "p" : vars.variables[index].value == 17 ? "q" : vars.variables[index].value == 18 ? "r" : vars.variables[index].value == 19 ? "s" : vars.variables[index].value == 20 ? "t" : vars.variables[index].value == 21 ? "u" : vars.variables[index].value == 22 ? "v" : vars.variables[index].value == 23 ? "w" : vars.variables[index].value == 24 ? "x" : vars.variables[index].value == 25 ? "y" : vars.variables[index].value == 26 ? "z" : vars.variables[index].value == 27 ? "\n" : vars.variables[index].value == 28 ? "?" : vars.variables[index].value == 29 ? "." : vars.variables[index].value == 30 ? " " : vars.variables[index].value == 31 ? ";" : vars.variables[index].value == 32 ? "=" : vars.variables[index].value == 33 ? "!" : vars.variables[index].value == 34 ? "{" : vars.variables[index].value == 35 ? "}" : vars.variables[index].value == 36 ? "(" : vars.variables[index].value == 37 ? ")" : vars.variables[index].value == 38 ? "[" : vars.variables[index].value == 39 ? "]" : vars.variables[index].value == 40 ? "<" : vars.variables[index].value == 41 ? ">" : "#");
break;
}
}
q++;
continue;
}
else if (tokens.at(q).get_desc() == "JUMP_OPERATION") {
string location = tokens.at(q + 3).get_value();
string comparison = tokens.at(q + 2).get_value();
Variable comparator;
for (int i = 0; i < vars.variables.size(); i++) {
if (vars.variables[i].name == tokens.at(q + 1).get_value()) {
comparator = vars.variables[i];
break;
}
}
// watch out for cases where comparator isn't valid
if (!is_int(comparison)) {
for (int i = 0; i < vars.variables.size(); i++) {
if (vars.variables[i].name == comparison) {
comparison = to_string(vars.variables[i].value);
break;
}
}
}
if (is_int(location)) {
int loc = stoi(location);
if (comparator.value != stoi(comparison)) {
q = loc;
continue;
}
}
q += 3;
}
}
}
int main(int argc, char *argv[]) {
string dir = "input.mlm";
if (argc == 2) {
dir = argv[1];
run_mlm(dir);
}
return 0;
}