-2

I am currently making a brainfuck and have encountered a problem with loops.

I followed some advice from this but I can't seem to get it working.

Here is my code so far:

<html>
<body>
<font face="consolas">
<script>
var brPos = 0;
var k = 0;
var loop = [];
var printtape = "";
var out = "";
var i = 0;
var pointer = 0;
var tape = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var source = prompt("Code").split("");
while (i<source.length+1){
    if (source[i] == "<"){
        pointer--;
    } else if (source[i] == ">"){
        pointer++;
    } else if (source[i] == "+"){
        tape[pointer]++;
    } else if (source[i] == "-"){
        tape[pointer]--;
    } else if (source[i] == ","){
        tape[pointer] = prompt("Input").charCodeAt(0);
    } else if (source[i] == "."){
        out += String.fromCharCode(tape[pointer]);
    } else if (source[i] == "["){
        loop.push(pointer);
        if (tape[pointer] == 0){
            brPos = i;
            while (k >= 0) {
                if (source[brPos] == "[") {
                    k++;
                } else if (source[brPos] == "]") {
                    k--;
                }
            }
            i = brPos;
            brPos = 0;
            loop.pop();
        }
    } else if (source[i] == "]"){
        i=loop[loop.length-1];
    }
    i++;
    for (j=0;j<tape.length;j++) {
        if (tape[j] > 255) {
            tape[j] = 0;
        } else if (tape[j] < 0) {
            tape[j] = 255;
        }
    }
    console.log(tape);
    console.log(loop);
}
printtape = "";
printtape += "|";
for (i=0;i<tape.length;i++) {
    if (tape[i]<10) {
        printtape += "00"+tape[i]+"|";
    }
    if (tape[i]>=10&&tape[i]<100) {
        printtape += "0"+tape[i]+"|";
    }
    if (tape[i]>=100) {
        printtape += tape[i]+"|";
    }
}
printtape += "<br>";
printtape += "  ";
for (i=0;i<pointer;i++) {
    printtape += "    ";
}
printtape += "^";
document.write(printtape);
alert(out);
</script>
</font>
</body>
</html>

This is the offending code (I think):

} else if (source[i] == "["){
    loop.push(pointer);
    if (tape[pointer] == 0){
        brPos = i;
        while (k >= 0) {
            if (source[brPos] == "[") {
                k++;
            } else if (source[brPos] == "]") {
                k--;
            }
        }
        i = brPos;
        brPos = 0;
        loop.pop();
    }
} else if (source[i] == "]"){
    i=loop[loop.length-1];
}

When I run the code (in IE) with a brainf*ck loop in it, it doesnt end the while loop and eventually crashes and I don't know why.

P.S. I know someone in the comments is going to say that the <font> tag is invalid HTML, and I should use CSS, but it works, it's quicker than CSS and I really don't care.

Zoe
  • 27,060
  • 21
  • 118
  • 148
FinW
  • 111
  • 1
  • 6

1 Answers1

1

There's nothing I know about Brainfuck parsing, but the code logic fails and you get infinite loop because brPos is a constant inside the loop. You make the same comparations and get to the k++ line over and over again. brPos has to be changed if you ever wanna get out of there.

Rápli András
  • 3,869
  • 1
  • 35
  • 55