-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbefunge.c
449 lines (371 loc) · 8.41 KB
/
befunge.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// http://catseye.tc/projects/befunge93/doc/website_befunge93.html
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define WIDTH 80
#define HEIGHT 25
typedef enum {
OP_ADD = '+', // add top 2
OP_SUB = '-', // sub top 2
OP_MULT = '*', // mult top 2
OP_DIV = '/', // divide top 2
OP_MOD = '%', // divide top 2 modulus
OP_DUP = ':', // duplicate top stack value
OP_SWAP = '\\', // swap top 2
OP_POP = '$', // pop top 1
OP_NOT = '!', // if top 1 is 0, then 1, else 0.
OP_GREATERTHAN = '`', // compare val1,val2, 1 if val1>val2, else 0.
//
OP_DIR_RIGHT = '>',
OP_DIR_LEFT = '<',
OP_DIR_UP = '^',
OP_DIR_DOWN = 'v',
OP_DIR_RANDOM = '?',
OP_IF_HORIZ = '_', // if pop1 true (!= 0), left, else right
OP_IF_VERT = '|', // if pop1 true (!= 0), up, else down
OP_STRINGMODE = '"',
OP_OUT_INT = '.',
OP_OUT_CHAR = ',',
OP_IN_INT = '&',
OP_IN_CHAR = '~',
OP_BRIDGE = '#', // jump over next command (used for crossings?)
OP_CODE_GET = 'g', // get value at code into stack (push 1)
OP_CODE_PUT = 'p', // put value from stack at code
OP_END = '@', // end program
OP_BLANK = ' ',
} operation;
typedef enum {
DIR_RIGHT = 0,
DIR_DOWN = 1,
DIR_LEFT = 2,
DIR_UP = 3
} direction;
typedef struct stack_entry {
int value;
struct stack_entry * next;
} stack_entry;
typedef struct runner {
char * memory;
int x, y;
direction dir;
stack_entry * stack;
int stringmode;
int ended;
} runner;
int trim(char * line) {
int len;
int trimmed = 0;
while ((len = strlen(line)) &&
(line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[len - 1] = '\0';
++ trimmed;
}
return trimmed;
}
runner * init_runner() {
runner * result = malloc(sizeof(runner));
result->memory = (char *) malloc(WIDTH * HEIGHT);
memset(result->memory, OP_BLANK, WIDTH * HEIGHT);
result->x = 0;
result->y = 0;
result->dir = DIR_RIGHT;
result->stack = NULL;
result->stringmode = 0;
result->ended = 0;
return result;
}
int read_file_into_memory(runner * run, FILE * in) {
int result = 0;
char line[WIDTH + 1];
int line_no = 1;
while (fgets(line, sizeof(line), in) != NULL) {
if ((line_no - 1) > HEIGHT) {
fprintf(stderr, "line %d: line is beyond maximum height %d\n",
line_no, HEIGHT);
return -1;
}
trim(line);
int line_length = strlen(line);
if (line_length > WIDTH) {
fprintf(stderr,
"line %d: line has length %d but must be no longer than %d\n",
line_no, line_length, WIDTH);
result = -1;
}
else {
unsigned memory_offset = (line_no - 1) * WIDTH;
memcpy(run->memory + memory_offset, line, line_length);
}
++ line_no;
}
return result;
}
// befunge-93 says popping off an empty stack returns 0, not
// underflow.
int pop_int(runner * run) {
int result;
if (run->stack == NULL) {
result = 0;
}
else {
result = run->stack->value;
stack_entry * next = run->stack->next;
free(run->stack);
run->stack = next;
}
return result;
}
void push_int(runner * run, int val) {
stack_entry * new_entry = (stack_entry *) malloc(sizeof(stack_entry));
new_entry->value = val;
new_entry->next = run->stack;
run->stack = new_entry;
}
int val_at_xy(runner * run, int x, int y) {
return run->memory[y * WIDTH + x];
}
int val_at_pc(runner * run) {
return val_at_xy(run, run->x, run->y);
}
int set_val_at_xy(runner * run, int x, int y, int val) {
return (run->memory[y * WIDTH + x] = val);
}
void move(runner * run) {
switch (run->dir) {
case DIR_LEFT:
run->x -= 1;
break;
case DIR_UP:
run->y -= 1;
break;
case DIR_RIGHT:
run->x += 1;
break;
case DIR_DOWN:
run->y += 1;
break;
default:
fprintf(stderr, "direction set to non-sensical value %d; abort!\n",
run->dir);
exit(1);
}
// wrap the x and y after moving.
if (run->x < 0) {
run->x += WIDTH;
}
run->x %= WIDTH;
if (run->y < 0) {
run->y += HEIGHT;
}
run->y %= HEIGHT;
}
/* execute the instruction at the current PC (x,y) and move the cursor
* depending on the direction after executing the current op.
*/
int step(runner * run) {
int result = 0;
operation op = run->memory[run->y * WIDTH + run->x];
#if 0
char stack_str[512] = "[ ";
stack_entry * e = run->stack;
while (e != NULL) {
sprintf(stack_str, "%s%d ", stack_str, e->value);
e = e->next;
}
strcat(stack_str, "]");
printf("(%d, %d): '%c' (%d) :: step :: stack = %s\n",
run->x, run->y,
op, op,
stack_str);
#endif
if (run->stringmode) {
if (op == OP_STRINGMODE) {
run->stringmode = 0;
}
else {
push_int(run, val_at_pc(run));
}
move(run);
return 0;
}
switch (op) {
case OP_ADD:
push_int(run, pop_int(run) + pop_int(run));
break;
case OP_SUB: {
int val1 = pop_int(run);
int val2 = pop_int(run);
push_int(run, val2 - val1);
break;
}
case OP_MULT:
push_int(run, pop_int(run) * pop_int(run));
break;
case OP_DIV: {
int val1 = pop_int(run);
int val2 = pop_int(run);
push_int(run, val2 / val1);
break;
}
case OP_MOD: {
int val1 = pop_int(run);
int val2 = pop_int(run);
push_int(run, val2 % val1);
break;
}
case OP_DUP: {
int in = pop_int(run);
push_int(run, in);
push_int(run, in);
break;
}
case OP_SWAP: {
int val1 = pop_int(run), val2 = pop_int(run);
push_int(run, val1);
push_int(run, val2);
break;
}
case OP_POP:
pop_int(run);
break;
case OP_NOT:
push_int(run, (pop_int(run) == 0) ? 1 : 0);
break;
case OP_GREATERTHAN: {
int val1 = pop_int(run);
int val2 = pop_int(run);
push_int(run, val2 > val1 ? 1 : 0);
break;
}
/* directions.. */
case OP_DIR_RIGHT:
run->dir = DIR_RIGHT;
break;
case OP_DIR_DOWN:
run->dir = DIR_DOWN;
break;
case OP_DIR_LEFT:
run->dir = DIR_LEFT;
break;
case OP_DIR_UP:
run->dir = DIR_UP;
break;
case OP_DIR_RANDOM: {
int r = rand() % 4;
run->dir = r;
break;
}
case OP_IF_HORIZ:
if (pop_int(run) != 0)
run->dir = DIR_LEFT;
else
run->dir = DIR_RIGHT;
break;
case OP_IF_VERT:
if (pop_int(run) != 0)
run->dir = DIR_UP;
else
run->dir = DIR_DOWN;
break;
case OP_STRINGMODE:
run->stringmode = 1;
break;
case OP_OUT_INT: {
char buf[512];
sprintf(buf, "%d ", pop_int(run));
write(1, buf, strlen(buf));
break;
}
case OP_OUT_CHAR:
{
char buf[1];
buf[0] = pop_int(run);
write(1, buf, 1);
}
break;
case OP_IN_INT: {
int val;
if (scanf("%d ", &val) < 1) {
fprintf(stderr, "error at (%d, %d): could not read an int from stdin!\n",
run->x, run->y);
return -1;
}
push_int(run, val);
break;
}
case OP_IN_CHAR: {
char buf[1];
if (read(0, buf, 1) < 1) {
fprintf(stderr, "error at (%d, %d): could not read a byte from stdin!\n",
run->x, run->y);
return -1;
}
push_int(run, (int) buf[0]);
break;
}
case OP_BRIDGE:
// extra move; will end up past the thing after the bridge
move(run);
break;
case OP_CODE_GET: {
int y = pop_int(run);
int x = pop_int(run);
int val = val_at_xy(run, x, y);
push_int(run, val);
break;
}
case OP_CODE_PUT: {
int y = pop_int(run);
int x = pop_int(run);
int val = pop_int(run);
set_val_at_xy(run, x, y, val);
break;
}
case OP_END:
run->ended = 1;
break;
case OP_BLANK:
// do nothing.
break;
default:
if (op >= '0' && op <= '9') {
push_int(run, op - '0');
}
else {
fprintf(stderr,
"error at (%d, %d): unknown befunge instruction '%c' (%d)\n",
run->x, run->y, op, op);
return -1;
}
}
// move the PC based on current direction.
move(run);
return result;
}
int execute(runner * run) {
int result = 0;
while (!run->ended && result == 0) {
result = step(run);
}
return result;
}
int main(int argc, char ** argv) {
FILE * in;
if (argc > 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
perror("fopen");
exit(1);
}
}
else {
in = stdin;
}
runner * run = init_runner();
if (read_file_into_memory(run, in) == -1) {
exit(1);
}
int ret = execute(run);
printf("\n\n>> ret is %d\n", ret);
}