-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxbf.c
More file actions
602 lines (504 loc) · 18.3 KB
/
maxbf.c
File metadata and controls
602 lines (504 loc) · 18.3 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/**
* MaxBF: A Brainfuck interpreter.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cargs.h>
#ifndef PROJECT_VER
# define PROJECT_VER "unknown"
#endif
#define INITIAL_TAPE_SIZE 1000
#define INITIAL_JUMP_STACK_SIZE 100
#define TOK_RIGHT '>'
#define TOK_LEFT '<'
#define TOK_INCREMENT '+'
#define TOK_DECREMENT '-'
#define TOK_OUTPUT '.'
#define TOK_INPUT ','
#define TOK_JUMP_ZERO '['
#define TOK_JUMP_NZERO ']'
#define TOK_DEBUG '#' // Optional feature.
#define CELL_VALUE_EOF 0 // What the current cell is set to on EOF.
#define DEBUG_NUM_CELLS 3 // The number of cells on each side: 3 + current + 3.
#define OPTION_HELP 'h'
#define OPTION_VERSION 'v'
#define OPTION_INPUT 'i'
#define OPTION_OUTPUT 'o'
#define OPTION_DEBUG 'd'
/** Represent interpreter errors. */
typedef enum {
STATUS_OK, /** No errors. */
STATUS_ERR_ALLOC, /** The interpreter failed to dynamically allocate
memory. */
STATUS_ERR_LBOUND, /** The user went past the start of the tape. */
STATUS_ERR_NESTING, /** The user made an error when nesting brackets. */
} ExecutionStatus;
/**
* Represent the brainfuck tape which the program can manipulate.
*/
typedef struct {
unsigned char *data; /** The array of numbers representing the tape. */
size_t size; /** Array size (to check if more needs to be
allocated). */
unsigned char *pointer; /** Pointer to the current cell in the tape. */
} Tape;
/**
* The jump stack holds the file positions of all the jump-if-zero ([)
* instructions so that when the matching (]) instruction is found, it is
* possible to jump back.
*/
typedef struct {
fpos_t *data; /** The array of jump-if-zero instruction positions. */
size_t size; /** Array size, to check if more needs to be allocated. */
fpos_t *pos; /** The latest instruction in the array. */
fpos_t *skip_pos; /** Set to NULL by default. Set to an instruction if it is
a skip instruction so that we know when to ignore
nested braces. */
} JumpStack;
/** Command-line options for cargs. */
static struct cag_option options[] = {
{.identifier=OPTION_HELP,
.access_letters="h",
.access_name="help",
.value_name=NULL,
.description="Print a help message"},
{.identifier=OPTION_VERSION,
.access_letters="v",
.access_name="version",
.value_name=NULL,
.description="Print current MaxBF version"},
{.identifier=OPTION_INPUT,
.access_letters="i",
.access_name="input-file",
.value_name="FILE",
.description="Specify a file as input for the brainfuck program"},
{.identifier=OPTION_OUTPUT,
.access_letters="o",
.access_name="output-file",
.value_name="FILE",
.description="Specify a file as output for the brainfuck program"},
{.identifier=OPTION_DEBUG,
.access_letters="d",
.access_name="debug",
.value_name=NULL,
.description="Enable the # command for debugging"},
};
/** Configuration for the interpreter. */
struct interpreter_config {
const char *input_file;
const char *output_file;
bool debug_enabled;
};
/** Execute a brainfuck program from a FILE stream. */
ExecutionStatus execute_brainfuck_from_stream(FILE *fp, FILE *input_stream,
FILE *output_stream,
struct interpreter_config *config);
/** Given a Tape, allocate data and initialize all values. Return false on
allocation failure. */
bool init_tape(Tape *tape);
/** Deallocate Tape data. */
void destroy_tape(Tape *tape);
/** Given a JumpStack, allocate data and initialize all values. Return false on
allocation failure. */
bool init_jump_stack(JumpStack *jump_stack);
/** Deallocate JumpStack data. */
void destroy_jump_stack(JumpStack *jump_stack);
/** Move the tape pointer to the right, allocating more memory if necessary. */
ExecutionStatus tape_move_right(Tape *tape, JumpStack *jump_stack);
/** Move the tape pointer to the left, ensuring that the user does not go past
the start of the tape. */
ExecutionStatus tape_move_left(Tape *tape, JumpStack *jump_stack);
/** Increment the value currently pointed by the tape. */
ExecutionStatus tape_increment(Tape *tape, JumpStack *jump_stack);
/** Decrement the value currently pointed by the tape. */
ExecutionStatus tape_decrement(Tape *tape, JumpStack *jump_stack);
/** Print the value currently pointed by the tape. */
ExecutionStatus tape_output(Tape *tape, JumpStack *jump_stack, FILE *fp);
/** Read in a character and store it in the current cell. */
ExecutionStatus tape_input(Tape *tape, JumpStack *jump_stack, FILE *fp);
/** Add the current jump if zero to the jump stack and enable skipping if the
current cell is 0. */
ExecutionStatus tape_jump_if_zero(Tape *tape, JumpStack *jump_stack,
fpos_t *pos);
/** Push a value to the jump stack, allocating more memory if necessary. */
ExecutionStatus jump_stack_push(JumpStack *jump_stack, fpos_t *pos);
/** Remove the matching jump if zero command from the jump stack and jump if
current value is not zero. */
ExecutionStatus tape_jump_if_not_zero(Tape *tape, JumpStack *jump_stack,
FILE *fp);
/** Pop a value from the jump stack, returning STATUS_ERR_NESTING if there's
nothing to pop. */
ExecutionStatus jump_stack_pop(JumpStack *jump_stack, fpos_t *pos);
/** Print 5 cells in the tape, with the current cell in the middle, along with
the character set values they represent. */
ExecutionStatus tape_print_debug_info(Tape *tape);
static inline void exit_with_error(char *msg)
{
fprintf(stderr, "ERROR: %s\n", msg);
exit(EXIT_FAILURE);
}
static inline void warn(char *msg)
{
printf("WARNING: %s\n", msg);
}
#ifndef TESTING // An alternate main function will be used for tests.
int main(int argc, char *argv[])
{
char *error_msg = NULL;
cag_option_context context;
cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
// Parse command-line options using cargs.
struct interpreter_config config = { .input_file=NULL, .output_file=NULL };
while (cag_option_fetch(&context)) {
char identifier = cag_option_get(&context);
switch (identifier) {
case OPTION_HELP:
puts("Usage: maxbf [OPTIONS] FILE");
puts("A bulletproof interpreter for Brainfuck.");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
return EXIT_SUCCESS;
case OPTION_VERSION:
printf("MaxBF version %s\n", PROJECT_VER);
return EXIT_SUCCESS;
case OPTION_INPUT:
config.input_file = cag_option_get_value(&context);
case OPTION_OUTPUT:
config.output_file = cag_option_get_value(&context);
case OPTION_DEBUG:
config.debug_enabled = true;
}
}
// Set input/output streams.
FILE *input_stream, *output_stream;
if (config.input_file != NULL) {
input_stream = fopen(config.input_file, "r");
if (input_stream == NULL) {
error_msg = "Could not open input file.";
goto error;
}
} else {
input_stream = stdin;
}
if (config.output_file != NULL) {
output_stream = fopen(config.output_file, "w");
if (output_stream == NULL) {
error_msg = "Could not open output file.";
goto error;
}
} else {
output_stream = stdout;
}
// Parse file parameter.
int file_index = context.index;
if (file_index >= argc) {
error_msg = "Please specify brainfuck program file.";
goto error;
}
if (file_index < argc - 1) {
error_msg = "Too many file arguments specified.";
goto error;
}
FILE *fp = fopen(argv[file_index], "r");
if (fp == NULL) {
error_msg = "Could not open brainfuck program file.";
goto error;
}
switch (execute_brainfuck_from_stream(fp, input_stream, output_stream, &config)) {
case STATUS_OK:
break;
case STATUS_ERR_ALLOC:
fclose(fp);
exit_with_error("Error while allocating memory.");
break;
case STATUS_ERR_LBOUND:
fclose(fp);
exit_with_error("The program went past the start of the tape.");
break;
case STATUS_ERR_NESTING:
fclose(fp);
exit_with_error("Improperly nested jumps [ and ].");
break;
}
error: // Cleanup, regardless of error.
if (input_stream != NULL && input_stream != stdin) fclose(input_stream);
if (output_stream != NULL && output_stream != stdout) fclose(output_stream);
if (fp != NULL) fclose(fp);
if (error_msg == NULL) {
return EXIT_SUCCESS;
} else {
exit_with_error(error_msg);
}
}
#endif // ifndef TESTING
ExecutionStatus execute_brainfuck_from_stream(FILE *fp, FILE *input_stream,
FILE *output_stream,
struct interpreter_config *config)
{
// Initialize tape and jump stack.
Tape tape;
if (!init_tape(&tape)) {
return STATUS_ERR_ALLOC;
}
JumpStack jump_stack;
if (!init_jump_stack(&jump_stack)) {
destroy_tape(&tape);
return STATUS_ERR_ALLOC;
}
// Track the current position to make jumping back possible.
fpos_t pos;
fgetpos(fp, &pos);
// This will be set in case of an error.
ExecutionStatus status = STATUS_OK;
int ch;
while ((ch = fgetc(fp)) != EOF) {
switch (ch) {
case TOK_RIGHT:
status = tape_move_right(&tape, &jump_stack);
break;
case TOK_LEFT:
status = tape_move_left(&tape, &jump_stack);
break;
case TOK_INCREMENT:
status = tape_increment(&tape, &jump_stack);
break;
case TOK_DECREMENT:
status = tape_decrement(&tape, &jump_stack);
break;
case TOK_OUTPUT:
status = tape_output(&tape, &jump_stack, output_stream);
break;
case TOK_INPUT:
status = tape_input(&tape, &jump_stack, input_stream);
break;
case TOK_JUMP_ZERO:
status = tape_jump_if_zero(&tape, &jump_stack, &pos);
break;
case TOK_JUMP_NZERO:
status = tape_jump_if_not_zero(&tape, &jump_stack, fp);
break;
case TOK_DEBUG:
if (config->debug_enabled) {
status = tape_print_debug_info(&tape);
}
default:
break; // Ignore all other characters.
}
if (status != STATUS_OK) {
goto error;
}
// Update position in file.
fgetpos(fp, &pos);
}
// If there are any opening parentheses that weren't closed, we tell the
// user now.
if (jump_stack.pos != jump_stack.data) {
status = STATUS_ERR_NESTING;
goto error;
}
error:
// Deallocate memory and return the status code. This will happen whether or
// not there is an error.
destroy_tape(&tape);
destroy_jump_stack(&jump_stack);
return status;
}
bool init_tape(Tape *tape)
{
tape->data = calloc(INITIAL_TAPE_SIZE, 1);
if (tape->data == NULL) {
return false;
}
tape->size = INITIAL_TAPE_SIZE;
tape->pointer = tape->data;
return true;
}
void destroy_tape(Tape *tape)
{
free(tape->data);
}
bool init_jump_stack(JumpStack *jump_stack)
{
jump_stack->data = malloc(sizeof *jump_stack->data
* INITIAL_JUMP_STACK_SIZE);
if (jump_stack->data == NULL) {
return false;
}
jump_stack->size = INITIAL_JUMP_STACK_SIZE;
jump_stack->pos = jump_stack->data;
jump_stack->skip_pos = NULL;
return true;
}
void destroy_jump_stack(JumpStack *jump_stack)
{
free(jump_stack->data);
}
ExecutionStatus tape_move_right(Tape *tape, JumpStack *jump_stack)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
// If we have moved to the edge of the tape.
if (tape->pointer == tape->data + tape->size - 1) {
// Store the offset to account for the possibility that realloc may have
// moved the block of memory.
size_t pointer_offset = tape->pointer - tape->data;
size_t original_size = tape->size;
tape->size *= 2;
unsigned char *temp = realloc(tape->data, tape->size);
if (temp == NULL) return STATUS_ERR_ALLOC;
tape->data = temp;
// Initialize the new memory to 0.
memset(tape->data + original_size, 0, tape->size - original_size);
// Update the tape pointer, just in case realloc copied the memory into
// a different place.
tape->pointer = tape->data + pointer_offset;
}
tape->pointer++;
return STATUS_OK;
}
ExecutionStatus tape_move_left(Tape *tape, JumpStack *jump_stack)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
// Return an error if the user is trying to move past the start of the tape.
if (tape->pointer == tape->data) return STATUS_ERR_LBOUND;
tape->pointer--;
return STATUS_OK;
}
ExecutionStatus tape_increment(Tape *tape, JumpStack *jump_stack)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
(*tape->pointer)++;
return STATUS_OK;
}
ExecutionStatus tape_decrement(Tape *tape, JumpStack *jump_stack)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
(*tape->pointer)--;
return STATUS_OK;
}
ExecutionStatus tape_output(Tape *tape, JumpStack *jump_stack, FILE *fp)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
fputc((int)*tape->pointer, fp);
return STATUS_OK;
}
ExecutionStatus tape_input(Tape *tape, JumpStack *jump_stack, FILE *fp)
{
if (jump_stack->skip_pos != NULL) return STATUS_OK;
int ch;
if ((ch = fgetc(fp)) == EOF) {
ch = CELL_VALUE_EOF;
}
*tape->pointer = (char)ch;
return STATUS_OK;
}
ExecutionStatus tape_jump_if_zero(Tape *tape, JumpStack *jump_stack,
fpos_t *pos)
{
// Add current value to stack.
ExecutionStatus push_status = jump_stack_push(jump_stack, pos);
if (push_status != STATUS_OK) return push_status;
// Ignore current value if in skip mode.
if (jump_stack->skip_pos != NULL) return STATUS_OK;
// Jump to the matching ] if the current cell is 0.
if (*tape->pointer == 0) {
jump_stack->skip_pos = jump_stack->pos;
}
return STATUS_OK;
}
ExecutionStatus jump_stack_push(JumpStack *jump_stack, fpos_t *pos)
{
// If more memory needs to be allocated for the jump stack.
if (jump_stack->pos == jump_stack->data + jump_stack->size - 1) {
// Store offsets if realloc decided to move the block of memory.
size_t pos_offset = jump_stack->pos - jump_stack->data;
size_t skip_pos_offset = 0;
if (jump_stack->skip_pos != NULL) {
skip_pos_offset = jump_stack->skip_pos - jump_stack->data;
}
jump_stack->size *= 2;
fpos_t *temp = realloc(jump_stack->data,
sizeof(*temp) * jump_stack->size);
if (temp == NULL) return STATUS_ERR_ALLOC;
jump_stack->data = temp;
// Update the positions, just in case realloc copied the memory into a
// different place.
jump_stack->pos = jump_stack->data + pos_offset;
if (jump_stack->skip_pos != NULL) {
jump_stack->skip_pos = jump_stack->data + skip_pos_offset;
}
}
jump_stack->pos++;
*jump_stack->pos = *pos;
return STATUS_OK;
}
ExecutionStatus tape_jump_if_not_zero(Tape *tape, JumpStack *jump_stack,
FILE *fp)
{
// If we have finally matched the [ that initiated the skip.
if (jump_stack->skip_pos == jump_stack->pos) {
jump_stack->skip_pos = NULL;
}
fpos_t pos;
ExecutionStatus pop_status = jump_stack_pop(jump_stack, &pos);
if (pop_status != STATUS_OK) return pop_status;
// Jump if not zero.
if (*tape->pointer != 0) {
fsetpos(fp, &pos);
}
return STATUS_OK;
}
ExecutionStatus jump_stack_pop(JumpStack *jump_stack, fpos_t *pos)
{
// If we have nothing to pop, that means a [ could not be found to match the
// user's ].
if (jump_stack->pos == jump_stack->data) return STATUS_ERR_NESTING;
*pos = *jump_stack->pos;
jump_stack->pos--;
return STATUS_OK;
}
ExecutionStatus tape_print_debug_info(Tape *tape)
{
unsigned char *current_cell;
if (tape->pointer - tape->data < DEBUG_NUM_CELLS) {
current_cell = tape->data;
} else {
current_cell = tape->pointer - DEBUG_NUM_CELLS;
}
printf("\n");
for (int i = 0; i < DEBUG_NUM_CELLS * 2 + 1; i++, current_cell++) {
ptrdiff_t cell_index = current_cell - tape->data;
// Print a pointer to the current cell.
if (current_cell == tape->pointer) {
printf("|{->}");
}
if (cell_index < tape->size) {
if (isprint(*current_cell)) {
printf("| cell #%td = %d (%c) ", cell_index, *current_cell, *current_cell);
} else {
printf("| cell #%td = %d () ", cell_index, *current_cell);
}
} else {
// Even though this cell may not actually exist in memory yet, in an
// infinite tape, it will always be initialized to zero.
printf("| cell #%td = 0 () ", cell_index);
}
}
printf("|\n");
return STATUS_OK;
}