-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaintem.c
416 lines (355 loc) · 9.06 KB
/
maintem.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
#include <stdio.h>
#include <stdlib.h>
#ifndef MAX_STACK_SIZE
# define MAX_STACK_SIZE 14096
#endif
#define STACK_CELL_COUNT 33
#ifndef MAX_QUEUE_SIZE
# define MAX_QUEUE_SIZE 14096
#endif
typedef long long cell_t;
typedef int unichar_t;
/* Helper macros */
#define HEX__(n) 0x##n##LU
#define B8__(x) ((x&0x0000000FLU)?1:0) \
+((x&0x000000F0LU)?2:0) \
+((x&0x00000F00LU)?4:0) \
+((x&0x0000F000LU)?8:0) \
+((x&0x000F0000LU)?16:0) \
+((x&0x00F00000LU)?32:0) \
+((x&0x0F000000LU)?64:0) \
+((x&0xF0000000LU)?128:0)
/* User macros */
#define B8(d) ((unsigned char)B8__(HEX__(d)))
static void illegal_utf8(const char* msg) {
fprintf(stderr, "Illegal UTF-8 character: %s\n", msg);
abort();
}
static inline void _printchar (unichar_t num) {
int size;
int header_bits;
unsigned char header_magic;
if (num <= 0x007F) {
header_magic = B8(0);
header_bits = 1;
size = 0;
} else if (num <= 0x07FF) {
header_magic = B8(110);
header_bits = 3;
size = 1;
} else if (num <= 0xFFFF) {
header_magic = B8(1110);
header_bits = 4;
size = 2;
} else if (num <= 0x1FFFFF) {
header_magic = B8(11110);
header_bits = 5;
size = 3;
} else if (num <= 0x3FFFFFF) {
header_magic = B8(111110);
header_bits = 6;
size = 4;
} else if (num <= 0x7FFFFFFF) {
header_magic = B8(1111110);
header_bits = 7;
size = 5;
} else {
putchar('?');
return;
}
char result[6];
int idx;
for (idx = size; idx > 0; idx--) {
result[idx] = B8(10000000) + (num & B8(111111));
num >>= 6;
}
result[0] = num + (header_magic << (8 - header_bits));
putchar((int)result[0]);
for (idx = 1; idx <= size; idx++) {
putchar((int)result[idx]);
}
}
static inline void _printnum (cell_t c) {
printf("%lld", c);
}
#define CHECK_EOF(c) { \
if (c == EOF) { \
fprintf(stderr, "met EOF while reading characters\n"); \
abort(); \
} \
}
static inline unichar_t _getchar () {
int i_c1 = getchar();
unsigned char c1 = (unsigned char)i_c1;
unichar_t result;
if (i_c1 == EOF) {
result = -1;
} else if (c1 >> 7) {
int size = 0;
if ((c1 >> 5) == B8(110)) {
// 110xxxxx
result = c1 & B8(11111);
size = 1;
} else if ((c1 >> 4) == B8(1110)) {
// 1110xxxx
result = c1 & B8(1111);
size = 2;
} else if ((c1 >> 3) == B8(11110)) {
// 11110xxx
result = c1 & B8(111);
size = 3;
} else if ((c1 >> 2) == B8(111110)) {
// 111110xx
result = c1 & B8(11);
size = 4;
} else if ((c1 >> 1) == B8(1111110)) {
result = c1 & B8(1);
size = 5;
} else {
char s[100];
sprintf(s, "Invalid character number: %x", c1);
illegal_utf8(s);
}
int idx = 0;
for (idx = 0; idx < size; idx++) {
int i_nc = getchar();
CHECK_EOF(i_nc);
unsigned char nc = (unsigned char)i_nc;
if ((nc >> 6) != B8(10))
illegal_utf8("Following characters after first character in utf-8 encoding should start with 10(binary)");
result = (result << 6) + (nc & B8(111111));
}
} else {
result = (unichar_t)c1;
}
// fprintf(stderr, "Input char ord: %d\n", result);
return result;
}
static inline cell_t _getnum() {
cell_t c;
scanf("%lld", &c);
return c;
}
cell_t *queue;
cell_t *queue_front, *queue_back;
int _is_queuemode = 0;
static inline void _enterqueuemode() {
_is_queuemode = 1;
}
static inline void _leavequeuemode() {
_is_queuemode = 0;
}
static inline cell_t _queue_len() {
if (queue_front <= queue_back) {
return queue_back - queue_front;
} else {
return queue_back + MAX_QUEUE_SIZE - queue_front;
}
}
#define QUEUE_CHECK_STUB(check_overflow) { \
if (check_overflow) { \
cell_t* head_1; \
head_1 = queue_back + 1; \
if ((head_1 - queue) >= MAX_QUEUE_SIZE) \
head_1 = queue; \
if (head_1 == queue_front) { \
fprintf(stderr, "Queue Overflow\n"); \
abort(); \
} \
} else { \
if (queue_back == queue_front) { \
fprintf(stderr, "Queue Underflow\n"); \
abort(); \
} \
} \
}
static inline void _enqueue(cell_t c) {
#ifdef CHECK_STACK
QUEUE_CHECK_STUB(1);
#endif
*queue_back = c;
queue_back++;
if (queue_back - queue >= MAX_QUEUE_SIZE) queue_back = queue;
}
static inline cell_t _dequeue() {
#ifdef CHECK_STACK
QUEUE_CHECK_STUB(0);
#endif
cell_t c = *queue_front;
queue_front++;
if (queue_front - queue >= MAX_QUEUE_SIZE) queue_front = queue;
return c;
}
static inline cell_t _peekqueue() {
#ifdef CHECK_STACK
QUEUE_CHECK_STUB(0);
#endif
return *queue_front;
}
static inline void _dupqueue() {
#ifdef CHECK_STACK
QUEUE_CHECK_STUB(0);
QUEUE_CHECK_STUB(1);
#endif
cell_t c;
c = *queue_front;
queue_front--;
if (queue_front < queue)
queue_front += MAX_QUEUE_SIZE;
*queue_front = c;
}
cell_t *stack_bots[STACK_CELL_COUNT];
cell_t *stack_tops[STACK_CELL_COUNT];
cell_t** p_cur_stack_top;
cell_t** p_base_stack_top;
static inline cell_t _stack_no_len(int stackno) {
return stack_tops[stackno] - stack_bots[stackno];
}
static inline cell_t _stack_len() {
int stackno = p_cur_stack_top - stack_tops;
return _stack_no_len(stackno);
}
static inline cell_t _base_stack_len() {
int stackno = p_base_stack_top - stack_tops;
return _stack_no_len(stackno);
}
#define STACK_CHECK_STUB(len_invocation, check_overflow) { \
cell_t len = len_invocation; \
if (check_overflow) { \
if (len >= MAX_STACK_SIZE) { \
fprintf(stderr, "Stack Overflow\n"); \
abort(); \
} \
} else { \
if (len <= 0) { \
fprintf(stderr, "Stack Underflow\n"); \
abort(); \
} \
} \
}
static inline void _push_stack(cell_t c) {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_len(), 1);
#endif
**p_cur_stack_top = c;
(*p_cur_stack_top)++;
}
static inline void _dupstack() {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_len(), 1);
STACK_CHECK_STUB(_stack_len(), 0);
#endif
cell_t c = *((*p_cur_stack_top) - 1);
**p_cur_stack_top = c;
(*p_cur_stack_top)++;
}
static inline void _push(cell_t c) {
if (_is_queuemode)
_enqueue(c);
else
_push_stack(c);
}
static inline void _push_stack_no(int stackno, cell_t c) {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_no_len(stackno), 1);
#endif
cell_t** t;
t = &stack_tops[stackno];
**t = c;
(*t)++;
}
static inline void _push_base_stack(cell_t c) {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_base_stack_len(), 1);
#endif
**p_base_stack_top = c;
(*p_base_stack_top)++;
}
static inline void _dup() {
if (_is_queuemode) {
_dupqueue();
} else {
_dupstack();
}
}
static inline void _stack_sel(int stackno) {
p_cur_stack_top = &stack_tops[stackno];
}
static inline void _set_base_stack() {
p_base_stack_top = p_cur_stack_top;
}
static inline cell_t _peek_stack() {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_len(), 0);
#endif
return *((*p_cur_stack_top) - 1);
}
static inline cell_t _peek() {
if (_is_queuemode)
return _peekqueue();
else
return _peek_stack();
}
static inline cell_t _peek_stack_no(int stackno) {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_no_len(stackno), 0);
#endif
return *(stack_tops[stackno] - 1);
}
static inline cell_t _peek_base_stack() {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_base_stack_len(), 0);
#endif
return *((*p_base_stack_top) - 1);
}
static inline cell_t _pop_stack() {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_len(), 0);
#endif
(*p_cur_stack_top)--;
return **p_cur_stack_top;
}
static inline cell_t _pop() {
if (_is_queuemode)
return _dequeue();
else
return _pop_stack();
}
static inline cell_t _pop_stack_no(int stackno) {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_stack_no_len(stackno), 0);
#endif
stack_tops[stackno]--;
return *stack_tops[stackno];
}
static inline cell_t _pop_base_stack() {
#ifdef CHECK_STACK
STACK_CHECK_STUB(_base_stack_len(), 0);
#endif
(*p_base_stack_top)--;
return **p_base_stack_top;
}
static inline cell_t _storage_len() {
if (_is_queuemode)
return _queue_len();
else
return _stack_len();
}
void setup_stack() {
int idx;
for (idx = 0; idx < STACK_CELL_COUNT; idx++) {
stack_bots[idx] = stack_tops[idx] = malloc(MAX_STACK_SIZE * sizeof(cell_t));
}
queue = queue_back = queue_front = malloc(MAX_QUEUE_SIZE * sizeof(cell_t));
p_cur_stack_top = p_base_stack_top = &stack_tops[0];
}
void main_program() {
//{_CC_MAIN}
}
int main () {
setup_stack();
main_program();
if (_stack_len() > 0)
return (int)_pop_stack();
return 0;
}