Brain Puff Interpreter

Based on a crazy idea by JuJu2143: https://github.com/juju2143/flufflepuff

Code


Input


Output



Source Code

 
function toBrainFuck(code) {
    var bf = "";
    var i = 0;
    while(i < code.length) {
        if(code[i] == 'b') {
            if(code[i+1] == 'l') {
                bf += '-';
                i +=2 ;
            } else {
                bf += '>';
                i++;
            }
        } else if(code[i] == 't') {
            bf += '<';
            i++
        } else if(code[i] == '!') {
            bf += '.';
            i++;
        } else if(code[i] == '?') {
            bf += ',';
            i++;
        } else if(code[i] == 'p') {
            if(code[i+1] == 'f') {
                bf += '+';
                i += 2;
            } else {
                alert("Compilation error near " + i)
                return;
            }
        } else if(code[i] == '*') {
            if(code.substring(i, i+6) == '*gasp*') {
                bf += '[';
                i += 6;
            } else  if(code.substring(i, i+6) == '*pomf*') {
                bf += ']';
                i +=6;
            } else {
                alert("Compilation error near " + i)
                return;
            }
        } else {
            alert("Compilation error near " + i)
            return;
        }
    }

    return bf;
}

function execute(bf) {
    var memory = [0]
    
    var input = document.getElementById("inputarea").value;
    var output = document.getElementById("outputarea").value;

    var memptr = 0;
    var inptr = 0;
    var i = 0;

    while(i < bf.length) {
        if(bf[i] == '+')
            memory[memptr]++;
        else if(bf[i] == '-')
            memory[memptr]--;
        else if(bf[i] == '>') {
            memptr++;
            if(memory.length <= memptr)
                memory[memptr] = 0;
        } else if(bf[i] == '<') {
            memptr--;
        } else if(bf[i] == '.')
            document.getElementById("outputarea").value += String.fromCharCode(memory[memptr]);
        else if(bf[i] == ',') {
            if(inptr >= input.length)
                 memory[memptr] = -1;    
            else
                memory[memptr] = input.charCodeAt(inptr++);
        } else if(bf[i] == '[') {
            if(memory[memptr] == 0) {
                var s = 1;
                while(s > 0) {
                    i++;
                    if(bf[i] == '[')
                        s++;
                    else if(bf[i] == ']')
                        s--;
                }
            }
        } else if(bf[i] == ']') {
            if(memory[memptr] != 0) {
                var s = 1;
                while(s > 0) {
                    i--;
                    if(bf[i] == '[')
                        s--;
                    else if(bf[i] == ']')
                        s++;
                }
            }
        }

        i++;
    }
}

function magic() {
    var bf = toBrainFuck(document.getElementById("codearea").value);
    execute(bf);
}


(ↄ) Tommy Savaria, 2017 : https://newlunarfire.github.io