EmbeddedRelated.com
Forums

Brainf*ck in C

Started by tcfkat 1 month ago163 views
No, I do not want to insult you.
Yes, this has rather nothing to do with embedded.
Brainfuck is a so called esoteric programming language, created by the Swiss Urban Müller back in 1993. It implements a minimal Turing machine. Further information can be found on Wikipedia, I am not going to repeat myself.

On GitHub there are literally thousands of implementations.
Yes, writing another implementation is B... ok!
A simple "Hello World!" program looks like this in Brainfuck:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.+++.


Or even shorter:

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

My implementation below has the minor bug that multi-line is not allowed.
Enjoy! ;)

// Short implementation of programming language Brainfuck. tcfkat 20240324
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define TS 30000//Tape size
#define IS 10000//Input size
static uint8_t *t;//Tape
static int16_t tp;//Tape pointer
static char *i,*ip;//Input buffer and pointer
static void bs(char a,char b,uint8_t dir)//bracket search
{
uint8_t bc=0;//bracket counter
    while(1){
        if(*ip==a)bc++;
        if(*ip==b){bc--;if(!bc)return;}
        dir?ip++:ip--;
        if(ip<i||*ip==0){printf("No matching %c\n",b);free(t);free(i);exit(1);}
    }
}
int main(void)
{
    t=calloc(TS,1);
    ip=i=calloc(IS,1);
    if(!t||!i){if(t)free(t);if(i)free(i);printf("No mem\n");return 1;}
    fgets(i,IS-2,stdin);//Minor bug: \n terminates input, no multi-line input
    while(1){
        switch(*ip){
            case'>':if(++tp>=TS)tp=0;ip++;break;//Wrap around end of tape
            case'<':if(--tp<0)tp=TS-1;ip++;break;//Wrap around begin of tape
            case'+':++t[tp];ip++;break;
            case'-':--t[tp];ip++;break;
            case'.':putchar(t[tp]);ip++;break;
            case',':t[tp]=(uint8_t)*ip;ip++;break;
            case'[':if(!t[tp])bs('[',']',1);ip++;break;
            case']':bs(']','[',0);break;
            case 0:free(t);free(i);return 0;
            default:ip++;break;//Discard anything else
        }
    }
}