-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforked.c
229 lines (198 loc) · 6.53 KB
/
forked.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
#include <stdio.h>
#include <string.h>
#include "forked.h"
int stack[30000];
int size;
int memory;
struct { int x, y, d; } jumps[300];
int jumpnum;
enum { UNDEF, NORTH, EAST, SOUTH, WEST };
struct { int direction, x, y; } ips[500];
int ip_top, ip_count;
#define ip ips[ip_top]
int swap(int *, int *);
int interp(void)
{
ip.direction = EAST;
while (1) { for (ip_top = 0; ip_top <= ip_count; ip_top++) {
if (parse(buf[ip.y][ip.x]) == -1)
return 0;
switch (ip.direction) {
case NORTH: ip.y--; break;
case EAST: ip.x++; break;
case SOUTH: ip.y++; break;
case WEST: ip.x--; break;
}
if (ip.x < 0) {
if (ip.direction == WEST)
while (buf[ip.y][++(ip.x)]);
else
return 1;
}
else if (ip.y < 0) {
if (ip.direction == NORTH)
while (buf[++(ip.y)][ip.x]);
else
return 1;
}
else if (ip.y >= 1000) {
return 2;
}
else if (buf[ip.y][ip.x] <= 0) {
if (ip.direction == EAST)
while (buf[ip.y][--(ip.x)]);
else if (ip.direction == SOUTH)
while (buf[--(ip.y)][ip.x]);
else
return 2;
}
}}
}
void mirror(char);
void do_fork(void);
int parse(char command)
{
if (display_code)
putchar(command);
switch (command) {
case '&': return -1;
/* directionals */
case '^': ip.direction = NORTH; break;
case 'v': ip.direction = SOUTH; break;
case '>': ip.direction = EAST; break;
case '<': ip.direction = WEST; break;
/* mirrors */
case '\\': case '/': mirror(command); break;
/* input */
case '$': scanf("%d",&stack[size++]); break;
case '~': stack[size++] = getchar(); break;
/* output */
case '%': printf("%d",size ? stack[size-1] : 0); break;
case '@': size && putchar(stack[size-1]); break;
case '?': printf("%d",size ? stack[--size] : 0); break;
case '!': size && putchar(stack[--size]); break;
/* stack */
case 'i': size > 0 && (stack[size-1]++); break;
case 'd': size > 0 && (stack[size-1]--); break;
case '+': size > 1 && (stack[size-2] += stack[size-1]); size > 1 && (size--); break;
case '\'': size > 1 && (stack[size-2] -= stack[size-1]); size > 1 && (size--); break;
case '*': size > 1 && (stack[size-2] *= stack[size-1]); size > 1 && (size--); break;
case '_': size > 1 && (stack[size-2] /= stack[size-1]); size > 1 && (size--); break;
case 'm': size > 1 && (stack[size-2] %= stack[size-1]); size > 1 && (size--); break;
case 's': size > 1 && swap(&stack[size-2],&stack[size-1]); break;
case '=': size > 1 && (stack[size-2] = (stack[size-2] == stack[size-1]));
size > 1 && (size--);
break;
case 'l': size > 1 && (stack[size-2] < (stack[size-2] == stack[size-1]));
size > 1 && (size--);
break;
case 'g': size > 1 && (stack[size-2] > (stack[size-2] == stack[size-1]));
size > 1 && (size--);
break;
case 'p': size && (size--); break;
case '"': size && (stack[size] = stack[size-1]); size && size++; break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
stack[size++] = command - '0';
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
stack[size++] = command - 'A' + 10;
break;
/* memory */
case 'P': size && (memory = stack[size-1]); size && (size--); break;
case 'S': size && (memory = stack[size-1]); break;
case 'U': stack[size++] = memory; break;
case 'O': memory = 0; break;
/* fork */
case ':': do_fork(); break;
}
return 0;
}
void mirror(char m)
{
if (m == '\\') {
switch (ip.direction) {
case EAST: ip.direction = SOUTH; break;
case SOUTH: ip.direction = WEST; break;
case WEST: ip.direction = NORTH; break;
case NORTH: ip.direction = EAST; break;
}
}
else if (m == '/') {
switch (ip.direction) {
case EAST: ip.direction = NORTH; break;
case NORTH: ip.direction = EAST; break;
case WEST: ip.direction = SOUTH; break;
case SOUTH: ip.direction = WEST; break;
}
}
}
void do_fork(void)
{
int case1, case2, case3;
case1 = case2 = case3 = 0;
if (ip.direction == NORTH) {
if (buf[ip.y+1][ip.x] == '|')
case1 = 1;
if (buf[ip.y][ip.x-1] == '-')
case2 = 1;
if (buf[ip.y][ip.x+1] == '-')
case3 = 1;
}
else if (ip.direction == SOUTH) {
if (buf[ip.y-1][ip.x] == '|')
case1 = 1;
if (buf[ip.y][ip.x-1] == '-')
case2 = 1;
if (buf[ip.y][ip.x+1] == '-')
case3 = 1;
}
else if (ip.direction == WEST) {
if (buf[ip.y][ip.x+1] == '-')
case1 = 1;
if (buf[ip.y-1][ip.x] == '|')
case2 = 1;
if (buf[ip.y+1][ip.x] == '|')
case3 = 1;
}
else if (ip.direction == EAST) {
if (buf[ip.y][ip.x-1] == '-')
case1 = 1;
if (buf[ip.y-1][ip.x] == '|')
case2 = 1;
if (buf[ip.y+1][ip.x] == '|')
case3 = 1;
}
if (case1 && case2 && case3) {
if (ip.direction == WEST) {
if (stack[size-1] > 0)
ip.direction = NORTH;
else
ip.direction = SOUTH;
}
else if (ip.direction == NORTH) {
if (stack[size-1] > 0)
ip.direction = EAST;
else
ip.direction = WEST;
}
else if (ip.direction == EAST) {
if (stack[size-1] > 0)
ip.direction = SOUTH;
else
ip.direction = NORTH;
}
else if (ip.direction == SOUTH) {
if (stack[size-1] > 0)
ip.direction = WEST;
else
ip.direction = EAST;
}
}
//printf("\ndirection = %d, case1 = %d, case2 = %d, case3 = %d\n",direction,case1,case2,case3);
}
int swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}