-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbefunge.c
291 lines (273 loc) · 6.03 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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STACK_TYPE long
#define STACK_FMT "%ld"
#define DEF_STACK_SIZE 1000
#define WIDTH 80
#define HEIGHT 25
// PROG_SPACE = WIDTH * HEIGHT
#define PROG_SPACE 2000
#define binary_op(__op_char__, __op__) \
case __op_char__: {\
STACK_TYPE __a__ = pop();\
STACK_TYPE __b__ = pop();\
push(__b__ __op__ __a__);\
break;\
}
#define direction_case(__case__, __dx__, __dy__) \
case __case__: {\
dx = __dx__;\
dy = __dy__;\
break;\
}
#define if_case(__op__, __change__, __stop__)\
case __op__: {\
STACK_TYPE a = pop();\
if (a) {__change__ = -1;} \
else {__change__ = 1;}\
__stop__ = 0;\
break;\
}
struct Program {
char valid;
size_t width;
size_t height;
char lines[PROG_SPACE];
};
struct Stack {
STACK_TYPE* arr;
STACK_TYPE* pointer;
int size;
};
struct Stack stack;
struct Program program;
char empty_stack() {
return stack.pointer < stack.arr;
}
STACK_TYPE head_with_info(bool empty) {
if (empty) {return 0;}
return *(stack.pointer);
}
// return head of list
STACK_TYPE head() {
return head_with_info(empty_stack());
}
STACK_TYPE pop() {
bool empty = empty_stack();
STACK_TYPE res = head_with_info(empty);
if (!empty) {stack.pointer--;}
return res;
}
// Add element to head of list
void push(int t) {
if (stack.pointer - stack.arr + 1 == stack.size) {
stack.size *= 2;
STACK_TYPE* old_p = stack.arr;
stack.arr = realloc(old_p, sizeof(STACK_TYPE) * stack.size);
stack.pointer = (stack.pointer - old_p) + stack.arr;
}
*(++(stack.pointer)) = t;
}
// "Classic Premature Optimisation" - Brian Whelan (BAmod pending)
// "Shut up I wanna" - me
struct Program* prog_from_file(char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Couldn't open file. Did you get the path right?\n");
program.valid = 0;
}
size_t bflines = 0;
size_t prog_size = WIDTH * HEIGHT;
size_t max_len = 0;
while (1) {
char* linep = program.lines + bflines * WIDTH;
int line_len = 0;
while (1) {
int curr = fgetc(file);
if (curr == '\n') {
bflines++;
max_len = max_len > line_len ? max_len : line_len;
for (char* i = linep + line_len; i < WIDTH + linep; i++) {
*i = ' ';
}
break;
} else if (curr == EOF) {
max_len = max_len > line_len ? max_len : line_len;
goto fileEnd;
} else {*(linep + line_len++) = (char) curr;}
}
}
fileEnd:
program.valid = 1;
program.width = max_len;
program.height = bflines;
fclose(file);
}
void print_prog_with_pointer(int width, int height, int x, int y) {
fprintf(stderr, "\x1b[2J");
for (int j = 0; j < height; j++) {
int linep = (WIDTH * j);
for (int i = 0; i < width; i++) {
if (y == j && x == i) {
fprintf(stderr, "\x1b[44mX\x1b[49m");
} else {
fprintf(stderr, "%c", *(program.lines + (linep + i)));
}
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n\x1b[34mStack: \x1b[39m");
for (STACK_TYPE* p = stack.arr; p <= stack.pointer; p++) {
fprintf(stderr, " " STACK_FMT, *p);
}
fprintf(stderr, "\n");
}
void out_of_bounds(int x, int y) {
printf("Program limits are 80 x 25, tried to access %d x %d", x, y);
}
void run() {
int width = program.width;
int height = program.height;
int x = 0;
int y = 0;
int dx = 1;
int dy = 0;
char string_mode = 0;
while(1) {
char curr = *(program.lines + (y * WIDTH + x));
#ifdef DEVEL
print_prog_with_pointer(width, height, x, y);
fprintf(stderr, "\n(press <return> to continue) ");
while (getchar() != '\n') {}
#endif
if (string_mode) {
if (curr == '"') {
string_mode = 0;
} else {
push(curr);
}
} else {
if (curr >= '0' && curr <= '9') {
push(curr - '0');
} else {
switch(curr) {
binary_op('+', +)
binary_op('-', -)
binary_op('*', *)
binary_op('/', /)
binary_op('%', %)
binary_op('`', >)
case '!': {
STACK_TYPE a = pop();
push(a == 0);
break;
}
direction_case('<', -1, 0)
direction_case('>', 1, 0)
direction_case('^', 0, -1)
direction_case('v', 0, 1)
case '?': {
int dir = rand() % 4;
switch(dir) {
direction_case(0, 0, 1)
direction_case(1, 0, -1)
direction_case(2, 1, 0)
direction_case(3, -1, 0)
}
break;
}
if_case('_', dx, dy)
if_case('|', dy, dx)
case '"': {
string_mode = !string_mode;
break;
}
case ':': {
push(head());
break;
}
case '\\': {
STACK_TYPE a = pop();
STACK_TYPE b = pop();
push(a);
push(b);
break;
}
case '$': {
pop();
break;
}
case '.': {
printf(STACK_FMT " ", pop());
break;
}
case ',': {
putchar(pop());
break;
}
case '#': {
x += dx;
y += dy;
break;
}
case 'p': {
STACK_TYPE yy = pop();
STACK_TYPE xx = pop();
STACK_TYPE v = pop();
if (xx >= WIDTH || xx < 0 || yy >= HEIGHT || yy < 0)
out_of_bounds(xx, yy);
if (width < xx) width = xx;
if (height < yy) height = yy;
*(program.lines + (yy * WIDTH + xx)) = v;
break;
}
case 'g': {
STACK_TYPE yy = pop();
STACK_TYPE xx = pop();
if (xx >= WIDTH || xx < 0 || yy >= HEIGHT || yy < 0)
out_of_bounds(xx, yy);
push((unsigned char) *(program.lines + (yy * WIDTH + xx)));
break;
}
case '&': {
STACK_TYPE num;
scanf(STACK_FMT, &num);
push(num);
break;
}
case '~': {
int a = getchar();
push(a);
break;
}
case '@': {
return;
}
}
}
}
x = (width + x + dx) % width;
y = (height + y + dy) % height;
}
}
void usage(char* launch) {
printf("Usage: %s [FILE]\nThis runs the befunge program in [FILE]\n", launch);
}
int main(int argc, char *argv[]) {
srand(time(0));
if (argc != 2) {
usage(argv[0]);
} else {
prog_from_file(argv[1]);
if (program.valid) {
stack.arr = malloc(sizeof(STACK_TYPE) * DEF_STACK_SIZE);
stack.pointer = stack.arr - 1;
stack.size = DEF_STACK_SIZE;
run();
free(stack.arr);
}
}
return 0;
}