JSFiddle - React, Tailwind, and code Playground
by Conor O'Brien
HTML
<h1>Flummery</h1><textarea id="code"></textarea><button id="b">go →</button><div id="out"></div><div id="codeT"></div>
CSS
*{font-family:Consolas;}#codeT{white-space:pre;}
JavaScript
function Flummery(code,options){
options = options || {
cellSize: 255, // the maximal value which a cell can have
order: "[<>+-]", // the "tape" on which the commands remain
origin: 0, // the starting index for the command tape
identity: ".,", // brainfuck commands that can be accessed
compile: true, // optimization by compiling to javascript
};
code = code.replace(/\s/g,"");
// transpile to BF
var transpile = "";
for(var i=0;i<code.length;i++){
if(code[i] === ">")
options.origin++;
else if(code[i] === "<")
options.origin--;
//console.log(options.order[options.origin]);
if(options.identity.indexOf(code[i]) >= 0)
transpile += code[i];
else
transpile += options.order[options.origin] || "";
}
console.log(transpile);
if(options.compile){
var next = compile(transpile,options);
console.log(next);
return [transpile,next];
} else if(this instanceof Flummery){ // used with "new" keyword?
// TODO
} else {
// TODO
}
}
function compile(transpile,options){
var toEx = "var tape = [0], ptr = 0;\n", tabLevel = 0;
transpile = transpile.match(/([+-><.])\1*|[[,\]]/g);
transpile.forEach(function(x){
switch(x[0]){
case "+":
case "-":
toEx += "\t".repeat(tabLevel)+"tape[ptr] "+x[0]+"= "+x.length+";\n";
toEx += "\t".repeat(tabLevel)+(x[0]==="+"?"if(tape[ptr] > "+options.cellSize+")\n":"if(tape[ptr] < 0)\n")+"\t".repeat(tabLevel+1)+"tape[ptr] %= "+(options.cellSize+1)+";\n";
break;
case ">":
case "<":
toEx += "\t".repeat(tabLevel)+"ptr "+(x[0]==="<"?"-":"+")+"= "+x.length+";\n"+"\t".repeat(tabLevel)+"tape[ptr] = tape[ptr] || 0;\n";
break;
case ".":
toEx += "\t".repeat(tabLevel)+"document.getElementById(\"out\").innerHTML += String.fromCharCode(tape[ptr])"+(x.length===1?"":".repeat("+x.length+")")+";\n";
break;
case ",":
toEx += "\t".repeat(tabLevel)+"tape[ptr] =...