-
-
Save lifthrasiir/596667 to your computer and use it in GitHub Desktop.
/* The world's smallest Brainfuck interpreter in C, by Kang Seonghoon | |
* http://j.mearie.org/post/1181041789/brainfuck-interpreter-in-2-lines-of-c */ | |
s[99],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++%93;)for(b=c&2,b=c%7?a&&(c&17 | |
?c&1?(*r+=b-1):(r+=b-1):syscall(4-!b,b,r,1),0):v;b&&c|a**r;v=d)main(!c,&a);d=v;} |
To any newcomers:
Compile using gcc with the flag "-m32"
You may need to install the gcc-multilib package in order to target an x86 binary
After you have the right libraries installed, you should be able to compile like this:
gcc -m32 -o bf bf.c
good stuff.
Here is the clarified version. I used gpt-4 to reconstruct the code, meaning I just wanted to share the clarified version for learning purposes. I don't know C language, but I was curious.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define TAPE_SIZE 30000
// Declare an array to act as the tape for Brainfuck. Initialize all to zero.
unsigned char s[TAPE_SIZE] = {0};
// This will be the data pointer used in Brainfuck
unsigned char* ptr = s;
// Forward declaration of the main function to make it recursive
int main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
char* v;
// Ensure that a Brainfuck program is provided as an argument
if (argc != 2) {
fprintf(stderr, "Usage: %s 'brainfuck_code'\n", argv[0]);
return 1;
}
v = argv[1];
while (*v) {
switch (*v) {
case '>':
ptr++;
// Boundary check to prevent going beyond the tape
if (ptr >= s + TAPE_SIZE) {
fprintf(stderr, "Error: Pointer out of bounds\n");
return 1;
}
break;
case '<':
// Boundary check to prevent going before the tape
if (ptr <= s) {
fprintf(stderr, "Error: Pointer out of bounds\n");
return 1;
}
ptr--;
break;
case '+':
(*ptr)++;
break;
case '-':
(*ptr)--;
break;
case '.':
putchar(*ptr);
break;
case ',':
*ptr = (unsigned char)getchar();
break;
case '[':
if (!*ptr) {
int loop = 1;
while (loop > 0) {
v++;
if (*v == '[') loop++;
if (*v == ']') loop--;
}
}
break;
case ']':
if (*ptr) {
int loop = 1;
while (loop > 0) {
v--;
if (*v == '[') loop--;
if (*v == ']') loop++;
}
}
break;
}
v++;
}
return 0;
}
How to compile?
gcc brainfuck.c
How to run?
./a.out "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
"
This should dispay
Hello world
Here is the clarified version. I used gpt-4 to reconstruct the code, meaning I just wanted to share the clarified version for learning purposes. I don't know C language, but I was curious.
Honestly looks like chat gpt just spat out a generic bf interpreter, it doesn't share many characteristics with the minimal interpreter.
Here is the clarified version. I used gpt-4 to reconstruct the code, meaning I just wanted to share the clarified version for learning purposes. I don't know C language, but I was curious.
Honestly looks like chat gpt just spat out a generic bf interpreter, it doesn't share many characteristics with the minimal interpreter.
Yeah, it doesn't even call main recursively, it just iterates through the string. For some reason, ChatGPT has been upsetting me more and more recently (
How does this work? :O