📜 ⬆️ ⬇️

160 byte Brainfuck interpreter

After reading about the IP stack twIP , which fits in the size of a tweet and responds to pings, the Korean programmer Kang Seonghon decided to create something as tiny and workable. And he created the smallest C Brainfuck interpreter with a size of only 160 bytes.

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;}

The interpreter is able to execute any program on Brainfuck.

$ cc bf.c -o bf
$ ./bf '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'
Hello World!


Of course, because of the desire to squeeze into 160 bytes, many things had to be shifted to the environment. A more portable and environment-independent version of the interpreter takes 170 bytes.
')
s[999],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++%93;)for(b=c%7?a&&(c&17?c&1?(*r-=c-44):(r+=c-61):c&2?putchar(*r):(*r=getchar()),0):v;b&&c|a**r;v=d)main(!c,&b-1);d=v;}

If you add a normal tab, the code will look like this:

 //   s[999], *r=s, *d, c; main(a, b) { char *v=1[d=b]; for(;c = *v++ % 93;) for(b = c%7 ? a && (c & 17 ? c & 1 ? (*r -= c - 44) :(r += c - 61) :c & 2 ? putchar(*r) :(*r = getchar()) ,0) :v; b&&c | a * *r; v=d) main(!c,&b-1); d = v; } 

Source: https://habr.com/ru/post/105028/


All Articles