Execute Brain**** is an implementation of Brainf***.
Other implementations of Brainf***.
RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages.

Below are links to each of the versions of RCBF.

An implementation need only properly implement the following instructions:



Command

Description


 > 
 Move the pointer to the right


 < 
 Move the pointer to the left


 + 
 Increment the memory cell under the pointer


 - 
 Decrement the memory cell under the pointer


 . 
 Output the character signified by the cell at the pointer


 , 
 Input a character and store it in the cell at the pointer


 [ 
 Jump past the matching ] if the cell under the pointer is 0


 ] 
 Jump back to the matching [ if the cell under the pointer is nonzero

Any cell size is allowed,   EOF   (End-O-File)   support is optional, as is whether you have bounded or unbounded memory.




 
record BFI
{
    cmd : char;
    next : BFI;
    jmp : BFI;
}
 
record MEM
{
    val : int;
    next : MEM;
    prev : MEM;
}
 
func compile(prog : string) -> BFI
{
    var i = 0;
    var n = BFI;
    var p = BFI;
    var j = BFI;
    var pgm = BFI;
 
    for (i = 0; i < length(prog); i = i + 1) {
        n = BFI('0', nil, nil);
 
        if (p != nil) {
            p.next = n
        } else {
            pgm = n
        };
 
        n.cmd = prog[i];
        p = n;
 
        if (prog[i] == '[') {
            n.jmp = j;
            j = n;
            0
        } else if (prog[i] == ']') {
            n.jmp = j;
            j = j.jmp;
            n.jmp.jmp = n;
            0
        } else {
            0
        }
    };
 
    pgm
}
 
func exec(pgm : BFI) -> int
{
    var m = MEM(0, nil, nil);
    var n = BFI;
 
    for (n = pgm; n != nil; n = n.next) {
        if (n.cmd == '+') {
            m.val = m.val + 1
        } else if (n.cmd == '-') {
            m.val = m.val - 1
        } else if (n.cmd == '.') {
            printc(chr(m.val));
            0
        } else if (n.cmd == ',') {
            m.val = read()
        } else if (n.cmd == '[') {
            if (m.val == 0) {
                n = n.jmp;
                0
            } else {
                0
            }
        } else if (n.cmd == ']') {
            if (m.val != 0) {
                n = n.jmp;
                0
            } else {
                0
            }
        } else if (n.cmd == '<') {
            m = m.prev;
            0
        } else if (n.cmd == '>') {
            if (m.next == nil) {
                m.next = MEM(0, nil, nil);
                m.next.prev = m;
                0
            } else {
                0
            };
 
            m = m.next;
            0
        } else {
            0
        }
    };
 
    0
}
 
func run(prog : string) -> int
{
    var pgm = BFI;
 
    pgm = compile(prog);
    exec(pgm);
 
    0
}
 
func main() -> int
{
    /* Hello World! */
    run("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.");
    0
}
 