simply, 425 bytes
This big boy defines an anonymous function which takes an input string and returns the output of the Brainfuck program.
This anonymous function has to be stored into a variable, and receives the code as the 1st argument ($C
), and the input as the 2nd argument ($T
).
This is a language being interpreted by JavaScript, which then interprets Brainfuck to generate JavaScript code. 🤯
fn($C$I){if run&empty($I)$I=''$T=&json_decode(<<<J
{".":"O+=String.fromCharCode(T[P]);",",":"T[P]=I[0]?I.shift().charCodeAt(0):0;","<":"P=P?P-1:255;",">":"P=(P+1)%256;","-":"T[P]=T[P]?T[P]-1:255;","+":"T[P]=T[P]?(T[P]+1)%256:1;","[":"while(T[P]){","]":"}"}
J)$N=''each$X in$C if$T[$X]$N=&str_concat($N$T[$X])$F=run$argv->map->constructor('X'run&str_concat("var I=Array.from(X+''),O='',T=[],P=0;"$N"return O"))send run$F($I);}
It is an absolute mess!
Ungolfed - to plain English
Set $fn to the anonymous function($code, $input) {
If call &empty($input) then {
Set $input to "".
}
Set $tokens to the result of calling &json_decode(<<<JSON
{
".": "output += String.fromCharCode(tape[pointer]);",
",": "tape[pointer] = input.length ? input.shift().charCodeAt(0) : 0;",
"<": "pointer = pointer ? pointer - 1 : 255;",
">": "pointer = (pointer + 1) % 256;",
"-": "tape[pointer] = tape[pointer] ? tape[pointer] - 1 : 255;",
"+": "tape[pointer] = tape[pointer] ? (tape[pointer] + 1) % 256 : 1;",
"[": "while(tape[pointer]){",
"]": "}"
}
JSON);
Set $inner_code to "".
Foreach $char in $code {
If $tokens[$char] then {
Set $inner_code to the result of calling &str_concat($inner_code, $tokens[$char], "\n").
}
}
Set $js_code to the result of calling &str_concat(
"input = Array.from(input + '');\nvar output = '';\nvar tape = [];\nvar pointer = 0;\n",
$inner_code,
"return output;"
).
Set $fn to the result of calling $argv["map"]["constructor"]("input", $js_code).
Return the result of calling $fn($input).
}
Now it is a lot more readable!
How does it work?
This will simply read the input code, character by character, and replace the valid tokens into JavaScript code.
Using the array["filter"]["constructor"](code)
(but using "map"
to save bytes) trick in JSFuck, I can receive a JavaScript function which I can execute normally!
This is FAAAAAAAAAAAAR from the intended way of doing things, but ... it works!
Example output
This is the JavaScript code generated for the test cases.
Hello world: +[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--.
var fn = function anonymous(X
) {
var I=Array.from(X+''),O='',T=[],P=0;T[P]=T[P]?(T[P]+1)%256:1;while(T[P]){T[P]=T[P]?(T[P]+1)%256:1;while(T[P]){P=P?P-1:255;P=P?P-1:255;P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;P=(P+1)%256;P=(P+1)%256;P=(P+1)%256;P=(P+1)%256;}T[P]=T[P]?(T[P]+1)%256:1;P=P?P-1:255;T[P]=T[P]?T[P]-1:255;P=P?P-1:255;T[P]=T[P]?T[P]-1:255;P=P?P-1:255;P=P?P-1:255;P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;}P=P?P-1:255;P=P?P-1:255;O+=String.fromCharCode(T[P]);P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;O+=String.fromCharCode(T[P]);P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;O+=String.fromCharCode(T[P]);O+=String.fromCharCode(T[P]);T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;O+=String.fromCharCode(T[P]);P=P?P-1:255;P=P?P-1:255;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;O+=String.fromCharCode(T[P]);P=P?P-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;O+=String.fromCharCode(T[P]);P=(P+1)%256;P=(P+1)%256;O+=String.fromCharCode(T[P]);P=(P+1)%256;O+=String.fromCharCode(T[P]);T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;T[P]=T[P]?(T[P]+1)%256:1;O+=String.fromCharCode(T[P]);T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;O+=String.fromCharCode(T[P]);P=(P+1)%256;T[P]=T[P]?T[P]-1:255;O+=String.fromCharCode(T[P]);P=(P+1)%256;P=(P+1)%256;T[P]=T[P]?T[P]-1:255;T[P]=T[P]?T[P]-1:255;O+=String.fromCharCode(T[P]);return O
};
console.log(fn());
Cat program: ,[.,]
var fn = function anonymous(X
) {
var I=Array.from(X+''),O='',T=[],P=0;T[P]=I[0]?I.shift().charCodeAt(0):0;while(T[P]){O+=String.fromCharCode(T[P]);T[P]=I[0]?I.shift().charCodeAt(0):0;}return O
};
console.log(fn(prompt("What does the cat say?", "")));