2021-01-08 17:37:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
unsigned char tape[30000] = {0};
|
|
|
|
unsigned char* ptr = tape;
|
|
|
|
|
2021-01-08 19:03:11 +00:00
|
|
|
typedef enum {
|
|
|
|
INK, DEK,
|
|
|
|
BREK, GLEK,
|
|
|
|
BF_NEXT, BF_PREV,
|
|
|
|
BF_LOOP_START, BF_LOOP_END,
|
|
|
|
} brainfuck_instruction_t;
|
|
|
|
|
2021-01-08 17:37:10 +00:00
|
|
|
void interpret(char* input) {
|
|
|
|
char current_char;
|
|
|
|
size_t i;
|
|
|
|
size_t loop;
|
|
|
|
for (i = 0; input[i] != 0; i++) {
|
|
|
|
current_char = input[i];
|
2021-01-08 19:03:11 +00:00
|
|
|
if (current_char == 'INK') {// >
|
2021-01-08 17:37:10 +00:00
|
|
|
++ptr;
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == 'DEK') {// <
|
2021-01-08 17:37:10 +00:00
|
|
|
--ptr;
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == 'BREK') {// +
|
2021-01-08 17:37:10 +00:00
|
|
|
++*ptr;
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == 'GLEK') {// -
|
2021-01-08 17:37:10 +00:00
|
|
|
--*ptr;
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == 'PUK' ) {// .
|
2021-01-08 17:37:10 +00:00
|
|
|
putchar(*ptr);
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == 'KUK') {// ,
|
2021-01-08 17:37:10 +00:00
|
|
|
*ptr = getchar();
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == '[') {//start loop
|
2021-01-08 17:37:10 +00:00
|
|
|
continue;
|
2021-01-08 19:03:11 +00:00
|
|
|
} else if (current_char == ']' && *ptr) {//stop loop
|
2021-01-08 17:37:10 +00:00
|
|
|
loop = 1;
|
|
|
|
while (loop > 0) {
|
|
|
|
current_char = input[--i];
|
|
|
|
if (current_char == '[') {
|
|
|
|
loop--;
|
|
|
|
} else if (current_char == ']') {
|
|
|
|
loop++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
interpret(",[.[-],]"); // outputs input
|
|
|
|
return 0;
|
|
|
|
}
|