-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.c
151 lines (140 loc) · 4.38 KB
/
brainfuck.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
/*
* A simple interpreter for the brainfuck language.
*
* TODO:
* - Remove possible integer overflow bugs
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "stack.h"
#define INITIAL_TAPE_LENGTH 512
typedef struct {
FILE *code_stream;
stack_t *positions;
int *tape;
unsigned long tape_len;
unsigned long tape_pos;
} program_t;
void skip_loop(program_t *program);
void push_loop_start(program_t *program);
/* Returns zero on success. */
int resize_tape(program_t *program);
int main(int argc, char **argv) {
int return_code = EXIT_FAILURE;
if (argc != 2) {
printf("Usage: ./brainfuck <program>\n");
return EXIT_FAILURE;
}
program_t program = {
fopen(argv[1], "r"),
stack_create(),
calloc(INITIAL_TAPE_LENGTH, 1),
INITIAL_TAPE_LENGTH
};
if (!program.code_stream) {
fprintf(stderr, "Unable to open file %s\n", argv[1]);
stack_destroy(program.positions);
return EXIT_FAILURE;
}
if (!program.tape) {
fprintf(stderr, "Unable to allocate a tape\n");
fclose(program.code_stream);
stack_destroy(program.positions);
return EXIT_FAILURE;
}
char c, temp;
fpos_t *loop_start;
while ((c = fgetc(program.code_stream)) != EOF) {
switch (c) {
case '[': // Jump to the matching ']' if the current cell is equal to 0
if (!program.tape[program.tape_pos]) {
skip_loop(&program);
} else {
push_loop_start(&program);
}
break;
case ']': // Jump to the matching open bracket
loop_start = (fpos_t *)stack_peek(program.positions);
if (!loop_start) {
fprintf(stderr, "Error: unmatched ']'!\n");
goto error;
}
if (program.tape[program.tape_pos]) {
fsetpos(program.code_stream, loop_start);
} else {
free(stack_pop(program.positions));
}
break;
case '+': // Increment
program.tape[program.tape_pos]++;
break;
case '-': // Decrement
program.tape[program.tape_pos]--;
break;
case '<': // Move left
if (program.tape_pos < 0) {
fprintf(stderr, "Error: Cannot move beyond beginning of tape!\n");
goto error;
}
program.tape_pos--;
break;
case '>': // Move right
program.tape_pos++;
if (program.tape_pos >= program.tape_len) {
resize_tape(&program);
if (resize_tape(&program)) {
goto error;
}
}
break;
case '.': // Output
putchar(program.tape[program.tape_pos]);
break;
case ',': // Input
if ((temp = getchar()) != EOF) {
program.tape[program.tape_pos] = temp;
}
break;
default: // Everything which isn't a command is considered a comment
break;
}
}
return_code = EXIT_SUCCESS;
error:
fclose(program.code_stream);
stack_destroy_with_elements(program.positions);
free(program.tape);
return return_code;
}
void skip_loop(program_t *program) {
int unclosed_scopes_count = 1;
while (unclosed_scopes_count > 0) {
char c = fgetc(program->code_stream);
if (c == '[') {
unclosed_scopes_count++;
} else if(c == ']') {
unclosed_scopes_count--;
}
}
fgetc(program->code_stream);
}
void push_loop_start(program_t *program) {
fpos_t *loop_start = malloc(sizeof(fpos_t));
fgetpos(program->code_stream, loop_start);
stack_push(program->positions, loop_start);
}
int resize_tape(program_t *program) {
if (program->tape_pos >= program->tape_len) {
program->tape_len *= 2;
program->tape = realloc(program->tape, program->tape_len);
// Initialize the new part of the tape to zero
memset(program->tape + program->tape_len/2, 0, program->tape_len/2);
}
if (!program->tape) {
fprintf(stderr, "Out of memory\n");
return -1;
}
return 0;
}