-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
415 lines (389 loc) · 8.47 KB
/
parser.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <stdio.h>
#include <stdlib.h>
#include "eli.h"
#include "input.h"
#include "parser.h"
// Enum for input symbol types.
typedef enum {
LETTER, NUMBER, WHITE_SPACE, END, NO_TYPE
} InputCharType;
// Enum for all supported esoteric languages.
typedef enum {
OOK, COMPRESSED_OOK, BRAINFUCK, UNKNOWN
} LanguageType;
// Parse function for each supported esoteric language.
Instruction* parse_ook(int size);
Instruction* parse_compressed_ook(int size);
Instruction* parse_brainfuck(int size);
LanguageType get_language(void);
static int character; // input character
static InputCharType input; // input symbol
// Sets input symbol type of loaded symbol.
void getInputType(void) {
if ((character >= 'A' && character <= 'Z') ||
(character >= 'a' && character <= 'z'))
input = LETTER;
else if (character >= '0' && character <= '9')
input = NUMBER;
else if (character == EOF)
input = END;
else if (character <= ' ')
input = WHITE_SPACE;
else
input = NO_TYPE;
}
// Read one symbol from the input and get its symbol type.
void readInput(void) {
character = getChar();
getInputType();
}
// Try to match given character and current symbol.
// Error and file termination is caused if match is not successfull.
void match(char a) {
if (a == EOF)
error("Unexpected end of file.");
if (a != character)
error("Unexpected symbol.");
readInput();
}
// Parse file and return array of instructions.
Instruction* parseFile(char* fileName) {
int size;
LanguageType lang;
size = initInput(fileName);
lang = get_language();
if (r_lang_dump) {
switch (lang) {
case OOK:
printf("== File is being interpreted as Ook! language ==\n");
break;
case COMPRESSED_OOK:
printf("== File is being interpreted as compressed Ook! language ==\n");
break;
case BRAINFUCK:
printf("== File is being interpreted as Brainfuck language ==\n");
break;
default:
printf("Unknown language.");
}
}
resetFilePtr();
switch (lang) {
case OOK:
return parse_ook(size);
case COMPRESSED_OOK:
return parse_compressed_ook(size);
case BRAINFUCK:
return parse_brainfuck(size);
default:
error("Unknown language.");
}
return NULL; // not reachable
}
// Get language based on input file.
// If language cannot be determined or is not supported,
// error and program termination occures.
LanguageType get_language(void) {
readInput();
q0:
switch (input) {
case WHITE_SPACE:
readInput();
goto q0;
case LETTER:
if (character == 'O')
return OOK;
default:
goto q1;
}
q1:
switch (input) {
case WHITE_SPACE:
readInput();
goto q1;
default:
switch (character) {
case '!':
case '?':
return COMPRESSED_OOK;
case '<':
case '>':
case '+':
case '-':
case '[':
case ']':
case ',':
return BRAINFUCK;
case '.':
readInput();
goto q1;
default:
error("Cannot determine input language.");
}
}
return UNKNOWN;
}
Instruction* parse_ook(int size) {
int last = 0;
Instruction* inst = (Instruction*) malloc (((int)(size/4) + 1) * sizeof(Instruction));
readInput();
a0:
switch (input) {
case END:
inst[last] = EXIT;
return inst;
case WHITE_SPACE:
readInput();
goto a0;
case LETTER:
match('O');
match('o');
match('k');
switch (character) {
case '.':
readInput();
goto a1;
case '!':
readInput();
goto a2;
case '?':
readInput();
goto a3;
}
default:
error("Unexpected symbol.");
}
a1:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto a1;
case LETTER:
match('O');
match('o');
match('k');
switch (character) {
case '.':
inst[last++] = INC;
readInput();
goto a0;
case '!':
inst[last++] = SCANC;
readInput();
goto a0;
case '?':
inst[last++] = FWD;
readInput();
goto a0;
}
default:
error("Unexpected symbol.");
}
a2:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto a2;
case LETTER:
match('O');
match('o');
match('k');
switch (character) {
case '.':
inst[last++] = PRINTC;
readInput();
goto a0;
case '!':
inst[last++] = DEC;
readInput();
goto a0;
case '?':
inst[last++] = BRZ;
readInput();
goto a0;
}
default:
error("Unexpected symbol.");
}
a3:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto a3;
case LETTER:
match('O');
match('o');
match('k');
switch (character) {
case '.':
inst[last++] = BWD;
readInput();
goto a0;
case '!':
inst[last++] = BNZ;
readInput();
goto a0;
}
default:
error("Unexpected symbol.");
}
return inst; // not reachable
}
Instruction* parse_compressed_ook(int size) {
int last = 0;
Instruction* inst = (Instruction*) malloc ((size+1) * sizeof(Instruction));
readInput();
b0:
switch (input) {
case END:
inst[last] = EXIT;
return inst;
case WHITE_SPACE:
readInput();
goto b0;
default:
switch (character) {
case '.':
readInput();
goto b1;
case '!':
readInput();
goto b2;
case '?':
readInput();
goto b3;
default:
error("Unexpected symbol.");
}
}
b1:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto b1;
default:
switch (character) {
case '.':
inst[last++] = INC;
readInput();
goto b0;
case '!':
inst[last++] = SCANC;
readInput();
goto b0;
case '?':
inst[last++] = FWD;
readInput();
goto b0;
default:
error("Unexpected symbol.");
}
}
b2:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto b2;
default:
switch (character) {
case '.':
inst[last++] = PRINTC;
readInput();
goto b0;
case '!':
inst[last++] = DEC;
readInput();
goto b0;
case '?':
inst[last++] = BRZ;
readInput();
goto b0;
default:
error("Unexpected symbol.");
}
}
b3:
switch (input) {
case END:
error("Unexpected end of file.");
case WHITE_SPACE:
readInput();
goto b3;
default:
switch (character) {
case '.':
inst[last++] = BWD;
readInput();
goto b0;
case '!':
inst[last++] = BNZ;
readInput();
goto b0;
default:
error("Unexpected symbol.");
}
}
return inst; // not reachable
}
Instruction* parse_brainfuck(int size) {
int last = 0;
Instruction* inst = (Instruction*) malloc ((size+1) * sizeof(Instruction));
readInput();
c0:
switch (input) {
case END:
inst[last] = EXIT;
return inst;
case WHITE_SPACE:
readInput();
goto c0;
default:
switch (character) {
case '>':
inst[last++] = FWD;
readInput();
goto c0;
case '<':
inst[last++] = BWD;
readInput();
goto c0;
case '+':
inst[last++] = INC;
readInput();
goto c0;
case '-':
inst[last++] = DEC;
readInput();
goto c0;
case '.':
inst[last++] = PRINTC;
readInput();
goto c0;
case ',':
inst[last++] = SCANC;
readInput();
goto c0;
case '[':
inst[last++] = BRZ;
readInput();
goto c0;
case ']':
inst[last++] = BNZ;
readInput();
goto c0;
default:
error("Unexpected symbol.");
}
}
return inst; // not reachable
}