-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck_interpreter.c
61 lines (51 loc) · 2.26 KB
/
brainfuck_interpreter.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
#include <stdio.h>
#define r_brainfuck_size 1001
#define r_loop_stack_size 124
static int r_brainfuck_interpreter_function(char* r_program_pointer, char* arguments_pointer) {
char brainfuck[r_brainfuck_size] = { 0 };
char* brainfuck_pointer = &brainfuck[r_brainfuck_size / 2];
int loop_stack[r_loop_stack_size];
int loop_index = 0;
for (unsigned int iterator = 0; r_program_pointer[iterator] != '\0'; ++iterator) {
if (r_program_pointer[iterator] == '>' || r_program_pointer[iterator] == '<') {
if (r_program_pointer[iterator] == '>') brainfuck_pointer++;
else brainfuck_pointer--;
continue;
}
if (r_program_pointer[iterator] == '+' || r_program_pointer[iterator] == '-') {
if (r_program_pointer[iterator] == '+') (*brainfuck_pointer)++;
else (*brainfuck_pointer)--;
continue;
}
if (r_program_pointer[iterator] == '.') { putchar(*brainfuck_pointer); continue; }
else if (r_program_pointer[iterator] == ',') { *brainfuck_pointer = *arguments_pointer++; continue; }
if (r_program_pointer[iterator] == '[' || r_program_pointer[iterator] == ']') {
if (r_program_pointer[iterator] == '[') {
if (*brainfuck_pointer == 0) {
unsigned int r_loop_count = 1u;
while (r_loop_count > 0) {
iterator++;
if (r_program_pointer[iterator] == '[') r_loop_count++;
if (r_program_pointer[iterator] == ']') r_loop_count--;
continue;
}
} else loop_stack[loop_index++] = iterator;
} else {
if (*brainfuck_pointer != 0) iterator = loop_stack[loop_index - 1];
else loop_index--;
}
continue;
}
continue;
} return 0;
}
int main(int argc, char *argv[]) {
if (argc <= 1) {
printf("Usage Error: BRAINFUCK_INTERPRETER <brainfuck_code> [<args>...](optional)\n");
return 1;
}
char* program_pointer = argv[1];
char* arguments_pointer = { 0 };
if (argc >= 3) arguments_pointer = argv[2];
return r_brainfuck_interpreter_function(program_pointer, arguments_pointer);
}