This is a Brainfuck implementation in just 139 chars of JavaScript. Works only in Firefox. For a Cross-Browser compatible version see below.
Usage: f( string, callback)
string
is the brainfuck code you want to be executed
callback
is a reference to a function.
since JavaScript has no stdio the callback will be called on .
with the binary value of the current cell as only parameter. For the same reason ,
is unsupported.
Examples:
f("++.", alert) // alerts 2
f("+++.[->+<]>.", alert) // alerts 3 and 3
f("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++."+
"<<+++++++++++++++.>+++.------.--------.>+.>.+++.",console.log.bind(console))
// logs the ascii of "Hello World!\n"
function buffer(){
var a = [];
return {
add: function(b){
a.push(b);
},
flush: function(){
console.log( String.fromCharCode.apply(String,a));
},
clear: function(){
a=[];
}
};
}
var b=buffer();
f("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++."+
"<<+++++++++++++++.>.+++.------.--------.>+.>.+++.",b.add);
b.flush() // logs "Hello World!\n"
// compatible, 148 chars:
function f(a,b,c){return c?{"]":"}","[":"while(a[c]){",".":"b(a[c]);","<":"c--;",">":"c++;"}[a]||"a[c]=~~a[c]"+a+"1;":eval(a.replace(a=/./g,f,c=0))}
Fell free to fuck around and send me a note when you are done or have any suggestions.
mfG Hase
Very nice