Home Posts Gitlab Mastodon

Write Brainfuck-like programs using just C++’s overloaded operators:

BrainFuck ++C Meaning
> ++ Increment the data pointer by one (to point to the next cell to the right).
< Decrement the data pointer by one (to point to the next cell to the left).
+ + Increment the byte at the data pointer by one.
- - Decrement the byte at the data pointer by one.
. * Output the byte at the data pointer.
, & Accept one byte of input, storing its value in the byte at the data pointer.
[ ~ If the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
] ! If the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.

You can get rid of all C++ complexities by using this subset, writing in the brainfuck way.

C++ code needed

#include <cstdio>

#define SIZE (1024*8)

struct C {
    enum {INCP=1,DECP=2,INC=3,DEC=4,PUT=5,GET=6,BEG=7,END=8};
    char memory[SIZE] = {0};
    int cursor = 0;
    char program[SIZE] = {};
    int ps = 0; // Program size
    int pc = 0; // Program counter/cursor
    int curr_inst = 0;
    int nest = 0;

    // Increment the data pointer by one (to point to the next cell to the right). 
    inline C &operator++() {program[ps++]=INCP;return R();}
    // Decrement the data pointer by one (to point to the next cell to the left).
    inline C &operator--() {program[ps++]=DECP;return R();}
    // Increment the byte at the data pointer by one. 
    inline C &operator+() {program[ps++]=INC;return R();}
    // Decrement the byte at the data pointer by one.
    inline C &operator-() {program[ps++]=DEC;return R();}
    // Output the byte at the data pointer. 
    inline C &operator*() {program[ps++]=PUT;return R();}
    // Accept one byte of input, storing its value in the byte at the data pointer 
    C &operator&() {program[ps++]=GET;return R();}
    // If the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ! command. 
    C &operator~() {program[ps++]=BEG;return R();}
    // If the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching ~ command. 
    C &operator!() {program[ps++]=END;return R();}

    private :
    C &R(){
        curr_inst = curr_inst ? curr_inst : program[pc];
        while(curr_inst) {
            switch(curr_inst) {
                case INCP: cursor = (cursor+1)%SIZE;break;
                case DECP: cursor = (cursor+SIZE-1)%SIZE;break;
                case INC: memory[cursor]+=1;break;
                case DEC: memory[cursor]-=1;break;
                case PUT: putchar(memory[cursor]);break;
                case GET: memory[cursor]=getchar();break;
                case BEG:
                    if(!memory[cursor]) {
                        while(true) {
                            pc+=1;
                            int i = program[pc];
                            if (!i){pc-=1;return*this;} // We have to decrement PC again in order to have the correct behavior if the function is called again
                            nest += (i==BEG);
                            nest -= (i==END);
                            if (nest < 0) break;
                        }
                    }
                    break;
                case END:
                    if(memory[cursor]) {
                        while(true) {
                            pc-=1;
                            int i = program[pc];
                            if (!i)return*this;
                            nest += (i == END);
                            nest -= (i == BEG);
                            if (nest < 0)break;
                        }
                        break;
                    }
                    break;
            }
            nest = 0;
            pc+=1;
            curr_inst = program[pc];
        }
        return*this;
    }
} C;

Note that the programs have to be written in REVERSE order.

BrainFuck to ++C conversor

Input

Output

Examples:

Hello world

From https://sange.fi/esoteric/brainfuck/bf-source/prog/HELLOBF.BF

https://godbolt.org/z/z8T139oYd

int
main() {
    * + + + + + + + + + + ! - ~ * + -- ! - ++ + + + + -- ~ + +
    + + + + + + ++ ! - ~ * - - - - - - - - * - - - - - - * + +
    + * -- ! - ++ + + + -- ~ + + + + + + + + ++ * -- ! - ++ +
    + + + + -- ~ + + + + + + + + + + + ++ * -- ! - ++ + + + +
    -- ~ + + + + + + + + ++ ! - ~ * + + + * * + + + + + + + *
    + -- ! - ++ + + + + -- ~ + + + + + + + ++ * -- ! - ++ + +
    + + + + + + -- ~ + + + + + + + + + ++C;
    return 0;
}

Triangle

From https://sange.fi/esoteric/brainfuck/bf-source/prog/triangle.bf

https://godbolt.org/z/468MG3GK6

int
main() {
    + + + + + ! -- ! - ~ * + + + * + + + + + + + + + + ! -- -- -- -- ! -- -- -- ! ++ - -- - ~ ++
    + + ! -- + ++ - ~ - ++ ++ ++ + -- - ~ ++ + ! -- + ++ - ~ - ~ -- -- -- + ! ! ++ ! - ~ * ++ !
    ++ + -- -- + + + + + + + + + + ++ - ~ ++ + ! -- + ++ - ~ -- -- * ++ ! - -- + + + + ++ ~ + +
    + + + + + + ++ - ~ ~ ++ ++ ++ ! -- -- * ++ ++ ++ + -- - ~ ++ ! -- + ++ - ~ - ~ -- -- -- --
    -- -- -- -- -- -- + ++ ++ ++ + ++ ++ ++ + + ++ ++ ! - -- + + + + ++ ~ + + + + + + + + ++ ! -
    ++ + + + + + + + + -- ~ + + + + ++ ! * - ~C;
    return 0;
}

99 Bottles

From https://sange.fi/esoteric/brainfuck/bf-source/prog/99botles.bf

https://godbolt.org/z/v68nhre5W

int
main() {
    ! -- -- -- ! - ~ * - - - * + + + * - - - * - - - - - -- ! - ++ - - - - - -
    - - - - -- ~ + + + + + + + + + ++ * * + + + + + + + + + + + * -- ! - ++ -
    - - - - - - - - - - -- ~ + + ++ * - -- ! - ++ + + + + + + + + + + + -- ~ +
    + + + + + + + ++ * + -- ! - ++ - - - - - - - - - - -- ~ + + + + + + + ++ *
    - - - * - - - - - - - - - - - - * + + + + -- ! - ++ + + + + + + + + + + --
    ~ + + + + + + + + ++ * + + -- ! - ++ - - - - - - - - - - -- ~ + + + + + +
    + + ++ * - * - - -- ! - ++ + + + + + + + + + -- ~ + + + + + + + + + ++ * -
    - -- ! - ++ - - - - - - - - - - -- ~ + + + + + + + + ++ * + + + + + + + +
    + + + + + * * + + + * -- ! - ++ + + + + + + + + + + + -- ~ + + + + + + ++
    * -- ! - ++ - - - - - - - - - - -- ~ + + + + + + + ++ * - - - - - - - - -
    * - - -- ! - ++ + + + + + + + + + -- ~ + + + + + + + + + ++ * -- ! - ++ +
    + + + + + + + -- ~ + + + + ++ ! - ~ -- -- -- -- ! - ++ ++ ++ ++ * + + + +
    + + + + + + + + + + -- -- -- -- ~ -- ! - ++ ++ ++ + -- -- -- ~ ++ ++ ++ !
    - -- -- -- + ++ + ++ ++ ~ ++ ++ * - - - - - - - * - - - - - - - - * * + +
    + + + -- ++ * -- ! - ++ + + + + + + + + + -- ~ + + + + + ++ * + + -- ! - ++
    + + + + + + + + -- ~ + + + + ++ * -- ! - ++ + + + + + + + + -- ~ + + + + !
    - ~ ++ ! - ~ ++ ++ * -- -- -- -- -- -- ! - ++ ++ ++ ++ ++ * -- -- -- -- --
    ~ -- ! - ++ ++ + -- -- ~ ++ ++ ! - -- -- + ++ + ++ ~ ++ ++ ++ ! - ++ ++ +
    + + + + + -- + + + + + + -- ~ + + + + + + + + -- ! - -- ! - ~ ++ ~ -- + ++
    ++ ! ! - ~ -- ! - ~ ++ ~ - -- -- ! - ++ + -- ~ ++ ++ -- ! - ++ ++ ++ + --
    -- -- ~ ++ ++ ++ ! - -- -- -- + ++ + ++ ++ ~ -- -- -- ! - -- -- - ++ + ++ ~
    -- -- + ! - ~ ++ ! - ++ ++ ++ + -- -- ! - ~ -- ~ ++ + ++ ++ ! - ++ - -- ~
    ++ + + + + + + + + + ! - -- - -- -- ! - -- + ++ ++ ++ + + + + + + + + + +
    -- -- ~ ++ ! - ++ + -- ~ ++ ! - -- -- ! - ~ ++ + ++ ~ -- -- + ++ ++ ++ ~
    -- + + + + + + + + + -- -- -- ! - ~ ++ ++ ++ ! - ++ ++ + -- -- ~ ++ ++ ! -
    -- -- + ++ + ++ ~ -- -- ! - ~ ++ ! - ~ ++ - -- -- -- ! - ~ * - - - * + --
    ! - ++ - - - - - - - - - - - -- ~ + + + + + + + + ++ * - - - - - - - - - -
    * - - - - - - - * + + + + + + * - - - * - -- ! - ++ + + + + + + -- ~ + + +
    ++ * -- ! - ++ + + + + + + + + + + + + + -- ~ + + + + + ++ * + + + + -- ! -
    ++ - - - - - - - - - - - -- ~ + + + + + + + + ++ * + + + + + + + + + + + *
    + + + -- ! - ++ + + + + + + + + + + -- ~ + + + + + + + ++ * - - -- ! - ++
    - - - - - - - - - -- ~ + + + + + + + + + ++ * * -- ! - ++ + + + + + + -- ~
    + + + ++ * -- ! - ++ - - - - - -- ~ + + + ++ * -- ! - ++ + + + + + + + + +
    + -- ~ + + + + + + + + ++ * + + -- ! - ++ - - - - - - - - - - -- ~ + + + +
    + + + ++ * - - - - - - - - - - * + + + + + + + + + + + + + * -- ! - ++ + +
    + + + + + + + + + + + -- ~ + + + + + ++ * + + -- ! - ++ - - - - - - - - -
    - -- ~ + + + + + + + + ++ * - - - - - - - - - * + + + + + + + + * + + + +
    + + + + + + + * - - -- ! - ++ + + + + + + + + + + -- ~ + + + + + + + ++ *
    + -- ! - ++ - - - - - - - - - - -- ~ + + + + + + + ++ * - - - - - - - - -
    * - * - -- ! - ++ + + + + + + + + + + -- ~ + + + + + + + + ++ * + -- ! -
    ++ - - - - - - - - - - -- ~ + + + + + + + ++ * - - - - - - * + + + + + + +
    + + + * + + + + + + + + + + + + + * + + + + -- ! - ++ + + + + + + + + + +
    -- ~ + + + + + + + ++ * - - - * - -- ! - ++ - - - - - - - - - - -- ~ + + +
    + + + + + + + ++ * + + + + + + + + + + + + + * * + + + * -- ! - ++ + + + +
    + + + + + + + -- ~ + + + + + + ++ * -- ! - ++ - - - - - - - - - - -- ~ + +
    + + + + + ++ * - - - - - - - - - * - - -- ! - ++ + + + + + + + + + -- ~ +
    + + + + + + + + ++ * -- ! - ++ + + + + + + + + -- ~ + + + + ++ ! - ~ -- --
    -- -- ! - ++ ++ ++ ++ * + + + + + + + + + + + + + + -- -- -- -- ~ -- ! - ++
    ++ ++ + -- -- -- ~ ++ ++ ++ ! - -- -- -- + ++ + ++ ++ ~ ++ ++ * - - - - - -
    - * - - - - - - - - * * + + + + + -- ++ * -- ! - ++ + + + + + + + + + -- ~
    + + + + + ++ * + + -- ! - ++ + + + + + + + + -- ~ + + + + ++ * - - -- ! - ++
    + + + + + + -- ~ + + + + ++ ++ ++ * -- -- -- -- -- -- ! - ++ ++ ++ ++ ++ *
    -- -- -- -- -- ~ -- ! - ++ ++ + -- -- ~ ++ ++ ! - -- -- + ++ + ++ ~ ++ ++
    ++ * - - - * - - - - - -- ! - ++ - - - - - - - - - - -- ~ + + + + + + + + +
    ++ * * + + + + + + + + + + + * -- ! - ++ - - - - - - - - - - - -- ~ + + ++ *
    - -- ! - ++ + + + + + + + + + + + -- ~ + + + + + + + + ++ * + -- ! - ++ - -
    - - - - - - - - -- ~ + + + + + + + ++ * - - - * - - - - - - - - - - - - * +
    + + + -- ! - ++ + + + + + + + + + + -- ~ + + + + + + + + ++ * + + -- ! - ++
    - - - - - - - - - - -- ~ + + + + + + + + ++ * - * - - -- ! - ++ + + + + + +
    + + + -- ~ + + + + + + + + + ++ * - - -- ! - ++ - - - - - - - - - - -- ~ + +
    + + + + + + ++ * + + + + + + + + + + + + + * * + + + * -- ! - ++ + + + + + +
    + + + + + -- ~ + + + + + + ++ * -- ! - ++ - - - - - - - - - - -- ~ + + + + +
    + + ++ * - - - - - - - - - * - - -- ! - ++ + + + + + + + + + -- ~ + + + + +
    + + + + ++ * -- ! - ++ + + + + + + + + -- ~ + + + + ++ ! - ~ -- -- -- -- ! -
    ++ ++ ++ ++ * + + + + + + + + + + + + + + -- -- -- -- ~ -- ! - ++ ++ ++ + --
    -- -- ~ ++ ++ ++ ! - -- -- -- + ++ + ++ ++ ~ ++ ++ * - - - - - - - * - - - -
    - - - - * * + + + + + -- ++ * -- ! - ++ + + + + + + + + + -- ~ + + + + + ++
    * + + -- ! - ++ + + + + + + + + -- ~ + + + + ++ * -- ! - ++ + + + + + + + +
    -- ~ + + + + ! - ~ ++ ! - ~ ++ ++ * -- -- -- -- -- -- ! - ++ ++ ++ ++ ++ *
    -- -- -- -- -- ~ -- ! - ++ ++ + -- -- ~ ++ ++ ! - -- -- + ++ + ++ ~ ++ ++ ++
    ! - ++ ++ + + + + + + -- + + + + + + -- ~ + + + + + + + + -- ! - -- ! - ~ ++
    ~ -- + ++ ++ ! ! - ~ -- ! - ~ ++ ~ - -- -- ! - ++ + -- ~ ++ ! - ++ ++ ++ +
    -- -- -- ~ ++ ++ ++ ! - -- -- -- + ++ + ++ ++ ~ -- -- -- ! - -- -- - ++ + ++
    ~ -- -- + ! - ~ ++ ! - ++ ++ ++ + -- -- ! - ~ -- ~ ++ + ++ ++ ! - ++ - -- ~
    ++ + + + + + + + + + ! - -- - -- -- ! - -- + ++ ++ ++ + + + + + + + + + + --
    -- ~ ++ ! - ++ + -- ~ ++ ! - -- -- ! - ~ ++ + ++ ~ -- -- + ++ ++ ++ ~ -- +
    + + + + + + + + -- -- -- ! - ~ ++ ++ ++ ! - ++ ++ + -- -- ~ ++ ++ ! - -- --
    + ++ + ++ ~ -- -- ! - ~ ++ ! - ~ ++ ~ -- ! - ++ + + + + + + + + + + + -- ~ +
    + + + + + + + + ++C;
    return 0;
}

Further reading


Publish date:

Modified date: