-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
374 lines (320 loc) · 8.54 KB
/
interpreter.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
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
typedef void(*voidfuncptr_t)();
typedef struct __parsed_data_t {
voidfuncptr_t *fs;
int *ltofpos;
} parsed_data_t;
void read_options(int argc, char **argv);
FILE* validate_file(char *path);
wchar_t* read_file(FILE *fp);
parsed_data_t* parse(wchar_t *str);
void run();
void __read();
void __write();
void __push();
void __pop();
void __enqueue();
void __dequeue();
void __move_line();
void __next_line();
void __tmp_inc();
void __tmp_dec();
void __tmp_reset();
parsed_data_t *data = NULL;
char *path = NULL;
int tmp = 0;
int pos = 0;
int line = 0;
int *__stack = NULL;
int *__queue = NULL;
int __stack_top = 0;
int __queue_rear = 0;
int __queue_front = 0;
int __stack_size = 0;
int __queue_size = 0;
#ifdef DEBUG
int debugfd;
#endif
void debug(char* s) {
#ifdef DEBUG
printf("%s", s);
#endif
}
int main(int argc, char** argv) {
char *path = NULL;
for (int i = 1; i < argc; i++) {
if (!strcmp("-s", argv[i])) {
__stack_size = atoi(argv[++i]);
} else if (!strcmp("-q", argv[i])) {
__queue_size = atoi(argv[++i]);
} else {
path = argv[i];
}
}
if (path == NULL) {
fprintf(stderr, "파일안줘서정신나갈것같애\n");
exit(1);
}
__stack_size = __stack_size ? __stack_size : 256;
__queue_size = __queue_size ? __queue_size + 1 : 256 + 1;
__stack = calloc(__stack_size, sizeof(int));
__queue = calloc(__queue_size, sizeof(int));
#ifdef DEBUG
printf("Using stack size: %d, queue size: %d\n", __stack_size, __queue_size);
printf("v %p\n", __push);
printf("^ %p\n", __pop);
printf("< %p\n", __enqueue);
printf("> %p\n", __dequeue);
printf("l %p\n", __move_line);
printf("\\ %p\n", __next_line);
printf("+ %p\n", __tmp_inc);
printf("- %p\n", __tmp_dec);
printf(". %p\n", __tmp_reset);
#endif
setlocale(LC_ALL, "");
FILE *fp = validate_file(path);
wchar_t *str = read_file(fp);
fclose(fp);
#ifdef DEBUG
puts("");
for (int i = 0; ~str[i];) {
printf("%d ", str[i++]);
}
printf("-1\n");
#endif
data = parse(str);
#ifdef DEBUG
for (int i=0;data->fs[i] != NULL;i++) {
printf("%p ", data->fs[i]);
}
puts("");
for (int i=0;data->ltofpos[i] || !i;i++) {
printf("%d ", data->ltofpos[i]);
}
puts("");
#endif
run();
return 0;
}
void __write() {
#ifdef DEBUG
printf("write %d -> ", tmp);
#endif
if (tmp > 127)
printf("%c%c%c", 0xe0 + ((tmp & 0xf000) >> 12), 0x80 + ((tmp & 0x0fc0) >> 6), 0x80 + (tmp & 0x003f));
else
printf("%c", tmp);
fflush(stdout);
}
void __read() {
#ifdef DEBUG
printf("read");
#endif
tmp = fgetwc(stdin);
}
void __push() {
#ifdef DEBUG
printf("push");
#endif
__stack[__stack_top++] = tmp;
}
void __pop() {
#ifdef DEBUG
printf("pop");
#endif
tmp = __stack[--__stack_top];
}
void __enqueue() {
#ifdef DEBUG
printf("enqueue");
#endif
__queue[__queue_rear++] = tmp;
__queue_rear %= __queue_size;
}
void __dequeue() {
#ifdef DEBUG
printf("dequeue");
#endif
tmp = __queue[__queue_front++];
__queue_front %= __queue_size;
}
void __tmp_inc() {
#ifdef DEBUG
printf("inc");
#endif
tmp++;
}
void __tmp_dec() {
#ifdef DEBUG
printf("dec");
#endif
tmp--;
}
void __tmp_reset() {
#ifdef DEBUG
printf("reset");
#endif
tmp = 0;
}
void __next_line() {
#ifdef DEBUG
printf("next line");
#endif
line++;
}
void __move_line() {
pos = data->ltofpos[line += __stack[--__stack_top]];
#ifdef DEBUG
printf("move line %d\n", line);
#endif
}
FILE* validate_file(char *path) {
FILE *fp = NULL;
if ((fp = fopen(path, "rt")) == NULL) {
fprintf(stderr, "파일이없어정신나갈것같애\n");
exit(1);
}
return fp;
}
wchar_t* read_file(FILE *fp) {
wchar_t *wcs = calloc(512, sizeof(wchar_t));
long long idx = 0, alloced = 512;
errno = 0;
while (WEOF != (wcs[idx++] = fgetwc(fp))) {
if (idx >= alloced) {
wcs = (wchar_t *)realloc(wcs, (alloced += 512) * sizeof(wchar_t));
}
}
if (errno == EILSEQ) {
fprintf(stderr, "파일읽는데정신나갈것같애: got illegal sequence (EILSEQ) while reading file.\n");
exit(1);
}
return wcs;
}
parsed_data_t* parse(wchar_t *str) {
int i = 0;
int line = 0;
int pos = 0;
int tmp = 0;
parsed_data_t *data = calloc(1, sizeof(parsed_data_t));
data->fs = calloc(512, sizeof(voidfuncptr_t));
data->ltofpos = calloc(512, sizeof(int));
data->ltofpos[0] = 0;
while (~str[i] || tmp > 0) {
#ifdef DEBUG
printf("i = %6d, str[i] = %2lc, tmp = %07x, line = %3d, pos = %4d\n",
i, str[i] == 10 ? '\\' : str[i], tmp, line, pos);
#endif
if (!wcsncmp(str+i, L"정신", 2) && (0xf & tmp) == 0) {
tmp |= 0x1000000;
i += 2;
} else if (!wcsncmp(str+i, L"점심", 2) && (0xf & tmp) == 0) {
tmp |= 0x2000000;
i += 2;
} else if (!wcsncmp(str+i, L"나갈", 2) && (0xf & tmp) == 1) {
tmp |= 0x0100000;
i += 2;
} else if (!wcsncmp(str+i, L"나가서먹을", 5) && (0xf & tmp) == 1) {
tmp |= 0x0200000;
i += 5;
} else if (!wcsncmp(str+i, L"것", 1) && (0xf & tmp) == 2) {
tmp |= 0x0010000;
i += 1;
} else if (!wcsncmp(str+i, L"거", 1) && (0xf & tmp) == 2) {
tmp |= 0x0020000;
i += 1;
} else if (!wcsncmp(str+i, L"같애", 2) && (0xf & tmp) == 3) {
tmp |= 0x0001000;
i += 2;
} else if (!wcsncmp(str+i, L"같아", 2) && (0xf & tmp) == 3) {
tmp |= 0x0002000;
i += 2;
} else if (!wcsncmp(str+i, L"?", 1) && (0xf & tmp) >= 4 && !(0x0000f00 & tmp)) {
tmp |= (tmp & 0xf) << 8;
i += 1;
} else if (!wcsncmp(str+i, L".", 1) && (0xf & tmp) >= 4 && !(0x00000f0 & tmp)) {
tmp |= (tmp & 0xf) << 4;
i += 1;
} else if ((0xf & tmp) >= 4) {
// switch-case
switch (tmp >> 16) {
case 0x111:
data->fs[pos++] = __push;
break;
case 0x121:
data->fs[pos++] = __pop;
break;
case 0x211:
data->fs[pos++] = __enqueue;
break;
case 0x221:
data->fs[pos++] = __dequeue;
break;
case 0x112:
data->fs[pos++] = __read;
break;
case 0x122:
data->fs[pos++] = __write;
break;
case 0x212:
data->fs[pos++] = __tmp_dec;
break;
case 0x222:
data->fs[pos++] = __tmp_inc;
break;
default:
fprintf(stderr, "E: Invalid syntax at %d: %lc.\n", i, str[i]);
exit(1);
}
if ((0x000f000 & tmp) >> 12 == 2)
data->fs[pos++] = __move_line;
int t = (tmp >> 4) & 0x0000ff;
if ((t & 0xf0) == 4);
else if ((t & 0x0f) == 4) {
data->fs[pos++] = __tmp_reset;
}
if ((t & 0xf0) == 5);
else if ((t & 0x0f) == 5) {
data->fs[pos++] = __tmp_reset;
}
if (!wcsncmp(str+i, L"\n", 1)) {
data->fs[pos++] = __next_line;
data->ltofpos[++line] = pos;
i++;
}
tmp = -1;
} else {
fprintf(stderr, "E: Invalid syntax at %d: %lc.\n", i, str[i]);
exit(1);
}
tmp++;
}
data->fs[pos] = NULL;
return data;
}
void run() {
for (;data->fs[pos] != NULL;) {
(*data->fs[pos++])();
#ifdef DEBUG
printf("\n%d\nstack", pos);
for (int i=0;i<__stack_top;i++) {
printf(" %d", __stack[i]);
}
printf("\nqueue");
if (__queue_front <= __queue_rear)
for (int i=__queue_front;i<__queue_rear;i++) {
printf(" %d", __queue[i]);
}
else
for (int i=__queue_front;i<__queue_rear + __queue_size;i++) {
printf(" %d", __queue[(i + __queue_size) % __queue_size]);
}
printf("\ntmp %d\n\n", tmp);
#endif
}
}