-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.c
237 lines (194 loc) · 4.64 KB
/
bf.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
/*
* Brainfuck Interpreter in C99
* Balduin Dettling, 2015-07-15
*
* *** Usage ***
*
* - compile: $ gcc -o bf bf.c
*
* - If you have a text file containing a bf program, specify the
* file as 1st command line argument. For example:
* $ ./bf program.bf
* $ ./bf programs/brainfuck/program.bf
*
* - If no file is specified, you will be asked to enter a program
* into the standard input.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#define MEMORY_SIZE 256 // Memory size in bytes
#define PROGRAM_SIZE 80000 // Max. program length in chars
// Print a hexdump-like representation of the memory block
// [start, end)
void
print_memory(unsigned char* start, unsigned char* end)
{
int i = 0;
unsigned char* it = start;
while (it < end) {
// Formatting - extra \n after 8 lines
if (i % 128 == 0 && i > 0) {
putchar('\n');
if (i % 256 == 0) {
putchar('\n');
}
}
// Print memory positions at the start of each line
if (i % 16 == 0) {
printf("\n0x%04x: ", i);
}
// Print 2 bytes of memory
printf("%02x%02x ", *(it), *(it + 1));
i += 2;
it += 2;
}
printf("\n");
}
// Run the program with the given memory range
void
interpret(char* const program_start, char* const program_end,
unsigned char* const memory_start, unsigned char* const memory_end)
{
char* instruction_ptr = program_start;
unsigned char* data_ptr = memory_start;
while (instruction_ptr < program_end) {
switch (*instruction_ptr) {
// Simple instructions
case '>':
data_ptr++;
break;
case '<':
data_ptr--;
break;
case '+':
(*data_ptr)++;
break;
case '-':
(*data_ptr)--;
break;
// I/O instructions
case '.':
putchar(*(data_ptr));
break;
case ',':
printf("\nEnter char to save in cell %ld: ",
(data_ptr - memory_start));
*(data_ptr) = getchar();
// scanf("%c\n", memory + ptr);
break;
// Loop/branch instructions
case '[':
if (*(data_ptr) == 0) {
// Jump forward to matching ]
int bracket_counter = 1;
while (bracket_counter) {
instruction_ptr++;
if (instruction_ptr >= program_end) {
goto out_of_program;
}
if (*instruction_ptr == '[') {
bracket_counter++;
}
if (*instruction_ptr == ']') {
bracket_counter--;
}
}
instruction_ptr--;
}
break;
case ']':
// Jump back to matching [
if (*(data_ptr) != 0) {
int bracket_counter = 1;
while (bracket_counter) {
if (instruction_ptr < program_start) {
goto out_of_program;
}
instruction_ptr--;
if (*instruction_ptr == ']') {
bracket_counter++;
}
if (*instruction_ptr == '[') {
bracket_counter--;
}
}
}
break;
case 'p':
print_memory(memory_start, memory_end);
break;
out_of_program:
fprintf(stderr, "Invalid data pointer address\n");
exit(1);
}
instruction_ptr++;
}
}
int
main(int argc, char** argv)
{
char* program = calloc(PROGRAM_SIZE, sizeof(char));
// Parse command line options
int opt;
int opt_print_memory;
int opt_print_time;
while ((opt = getopt(argc, argv, "mt")) != -1) {
switch (opt) {
case 'm':
opt_print_memory = 1;
break;
case 't':
opt_print_time = 1;
break;
default:
printf("Usage: %s [-mt] [<file>]\n", argv[0]);
printf(" -m : Print memory contents left behind by program\n");
printf(" -t : Print elapsed time\n");
exit(1);
}
}
// Adjust arguments
argc -= optind;
argv += optind;
size_t program_length;
// Program input
if (argc) {
// If the user has specified a file, read it
FILE* input_file = fopen(argv[0], "r");
if (input_file == NULL) {
printf("The file \"%s\" could not be opened. Does it exist?\n",
argv[0]);
return (1);
}
program_length = fread(program, 1, PROGRAM_SIZE, input_file);
}
else {
// If no argument was passed, ask the user to enter some code
fread(program, 1, PROGRAM_SIZE, stdin);
}
unsigned char* memory = calloc(MEMORY_SIZE, sizeof(char));
// Start time measurement
struct timeval t1, t2;
if (opt_print_time) {
gettimeofday(&t1, NULL);
}
// Do the magic
interpret(program, program + program_length, memory, memory + MEMORY_SIZE);
if (opt_print_time) {
gettimeofday(&t2, NULL);
int usec = t2.tv_usec - t1.tv_usec;
int sec = t2.tv_sec - t1.tv_sec;
float total = sec + ((float) usec) / 10E6;
printf("\nElapsed time: %f seconds\n", total);
}
// For debugging
if (opt_print_memory) {
print_memory(memory, memory + MEMORY_SIZE);
}
free(memory);
free(program);
return 0;
}