var NUM_CELLS = 3000030000;
var ITERS_PER_SEC = 100000100000;
var TIMEOUT_MILLISECS = 50005000;
var ERROR_BRACKET = "Mismatched brackets"brackets";
var ERROR_TIMEOUT = "Timeout""Timeout";
var ERROR_INTERRUPT = "Interrupted by user"user";
var code, input, wrap, timeout, eof, loop_stack, loop_maploop_map;
var running, start_time, code_ptr, input_ptr, cell_ptr, cells, iterationsiterations;
function clear_output() {
document.getElementById("output").value = """";
document.getElementById("stderr").innerHTML = """";
}
function stop() {
running = falsefalse;
document.getElementById("run").disabled = falsefalse;
document.getElementById("stop").disabled = truetrue;
document.getElementById("clear").disabled = falsefalse;
document.getElementById("wrap").disabled = falsefalse;
document.getElementById("timeout").disabled = falsefalse;
document.getElementById("eof").disabled = falsefalse;
}
function interrupt() {
error(ERROR_INTERRUPT);
}
function error(msg) {
document.getElementById("stderr").innerHTML = msgmsg;
stop();
}
function oOoCODE(code){
code = code.match(/[a-z]/gi).join('');
code = code.match(/.../g);
for (var i = 0; i<code.length; i++) {
code[i] = [/[A-Z]/.test(code[i][0]),/[A-Z]/.test(code[i][1]),/[A-Z]/.test(code[i][2])];
if (code[i][0]) {
if (code[i][1]) {
if (code[i][2]) {
code[i] = ",";
} else {
code[i] = ".";
}
} else {
if (code[i][2]) {
code[i] = "+";
} else {
code[i] = "-";
}
}
} else {
if (code[i][1]) {
if (code[i][2]) {
code[i] = "]";
} else {
code[i] = "[";
}
} else {
if (code[i][2]) {
code[i] = ">";
} else {
code[i] = "<";
}
}
}
}
return code.join('');
}
function run() {
clear_output();
document.getElementById("run").disabled = truetrue;
document.getElementById("stop").disabled = falsefalse;
document.getElementById("clear").disabled = truetrue;
document.getElementById("wrap").disabled = truetrue;
document.getElementById("timeout").disabled = truetrue;
document.getElementById("eof").disabled = truetrue;
code = document.getElementById("code").valuevalue;
input = document.getElementById("input").valuevalue;
wrap = document.getElementById("wrap").valuevalue;
timeout = document.getElementById("timeout").checkedchecked;
eof = document.getElementById("eof").valuevalue;
code = oOoCODE(code);
loop_stack = [][];
loop_map = {};
for (var i = 0; i < code.length; ++i) {
if (code[i] == "[") {
loop_stack.push(i);
} else if (code[i] == "]") {
if (loop_stack.length == 0) {
error(ERROR_BRACKET);
returnreturn;
} else {
var last_bracket = loop_stack.pop();
loop_map[last_bracket] = ii;
loop_map[i] = last_bracketlast_bracket;
}
}
}
if (loop_stack.length > 0) {
error(ERROR_BRACKET);
returnreturn;
}
running = truetrue;
start_time = Date.now();
code_ptr = 00;
input_ptr = 00;
cell_ptr = Math.floor(NUM_CELLS / 2);
cells = {};
iterations = 00;
bf_iter(1);
}
function bf_iter(niters) {
if (code_ptr >= code.length || !running) {
stop();
returnreturn;
}
var iter_start_time = Date.now();
for (var i = 0; i < niters; ++i) {
if (cells[cell_ptr] == undefined) {
cells[cell_ptr] = 00;
}
switch (code[code_ptr]) {
case "+":
if ((wrap == "8" && cells[cell_ptr] == 255) ||
(wrap == "16" && cells[cell_ptr] == 65535) ||
(wrap == "32" && cells[cell_ptr] == 2147483647)) {
cells[cell_ptr] = 00;
} else {
cells[cell_ptr]++cells[cell_ptr]++;
}
breakbreak;
case "-":
if (cells[cell_ptr] == 0){
if (wrap == "8"){ cells[cell_ptr] = 255255; }
if (wrap == "16"){ cells[cell_ptr] = 6553565535; }
if (wrap == "32"){ cells[cell_ptr] = 21474836472147483647; }
} else {
cells[cell_ptr]--;
}
breakbreak;
case "<": cell_ptr--; breakbreak;
case ">": cell_ptr++; breakbreak;
case ".":
document.getElementById("output").value += String.fromCharCode(cells[cell_ptr]);
breakbreak;
case ",":
if (input_ptr >= input.length) {
if (eof != "nochange") {
cells[cell_ptr] = parseInt(eof);
}
} else {
cells[cell_ptr] = input.charCodeAt(input_ptr);
input_ptr++input_ptr++;
}
breakbreak;
case "[":
if (cells[cell_ptr] == 0) {
code_ptr = loop_map[code_ptr]loop_map[code_ptr];
}
breakbreak;
case "]":
if (cells[cell_ptr] != 0) {
code_ptr = loop_map[code_ptr]loop_map[code_ptr];
}
breakbreak;
}
code_ptr++code_ptr++;
iterations++iterations++;
if (timeout && Date.now() - start_time > TIMEOUT_MILLISECS) {
error(ERROR_TIMEOUT);
returnreturn;
}
}
setTimeout(function() { bf_iter(ITERS_PER_SEC * (Date.now() - iter_start_time)/1000) }, 0);
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">
<div style="float:left; width:50%;">
Code:
<br>
<textarea id="code" rows="4" style="overflow:scroll;overflow-x:hidden;width:90%;">JuLIeT
O rOMeO, RoMEo! WHeREfOrE art tHoU RoMEo?
DEnY ThY FaTHeR AnD ReFuse ThY NaME;
oR, If ThoU wiLT not, BE but SWoRn mY loVe,
aND i'Ll NO lONgER bE A cAPuLEt.
ROMeO
[AsIdE] ShALl I HEar moRE, or sHAlL I sPEaK At THiS?
JuLIeT
'TiS BUt Thy NamE thAt iS my EneMy;
tHou ARt ThYSeLF, tHOUgH noT a mOntAguE.
whAt's MOnTagUe? iT is Nor HanD, noR foOt,
nOR arm (...) </textarea>
<br>Input:
<br>
<textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;"></textarea>
<p>
Wrap:
<select id="wrap">
<option value="8">8-bit</option>
<option value="16">16-bit</option>
<option value="32">32-bit</option>
</select>
Timeout:
<input id="timeout" type="checkbox" checked="true" /> EOF:
<select id="eof">
<option value="nochange">Same</option>
<option value="0">0</option>
<option value="-1">-1</option>
</select>
</p>
</div>
<div style="float:left; width:50%;">
Output:
<br>
<textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
<p>
<input id="run" type="button" value="Run" onclick="run()" />
<input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="true" />
<input id="clear" type="button" value="Clear" onclick="clear_output()" />
<span id="stderr" style="color:red"></span>
</p>
</div>
</div>