Skip to content

Instantly share code, notes, and snippets.

@Kambfhase
Forked from 140bytes/LICENSE.txt
Last active September 25, 2015 22:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Kambfhase/995255 to your computer and use it in GitHub Desktop.
Save Kambfhase/995255 to your computer and use it in GitHub Desktop.
Brainfuck139

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

function f( // NFE for self passing
a, // the brainfuck source code to convert
b, // the callback
c // will be truthy when called via replace
) c ? // note the firefox only function expression
{ // micromode: a is a single char
"]": "}", // maps the brainfuck instructions to JS statements
"[": "while(a[c]){",
".": "b(a[c]);",
"<": "c--;",
">": "c++;"
}[a] ||
"a[c]=~~a[c]"+// ~~a[c] converts a[c] into an integer ie. undefined -> 0
a + "1;" // if the current char is + or - map them to `a[c]=~~a[c]+1;`.
: // macromode: a is the brainfuck source
eval( // eval the compiled code in the current scope!!!
a.replace( // take the source and replace it char by char
a=/./g, // 1) replace one char at a time
// 2) use the regex as an object for the array
f, // change to micro mode for the individual char
c=0 // this has no effect for a.replace() its just a good place to set c to 0.
)
// execution mode: a is the "array"
// c is the pointer and b the callback
)
function f(a,b,c)c?{"]":"}","[":"while(a[c]){",".":"b(a[c]);","<":"c--;",">":"c++;"}[a]||"a[c]=~~a[c]"+a+"1;":eval(a.replace(a=/./g,f,c=0))
//ES6, 130chars
(a,b,c)=>eval(a.replace(a=/./g,a=>(["a[c]=~~a[c]"+a+"1;","}","while(a[c]){","b(a[c]);","c--;","c++;"]["][.<>".indexOf(a)+1]),c=0))
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Kambfhase
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "brainfuck139",
"description": "brainfuck in 139 bytes",
"keywords": [
"139bytes",
"brainfuck",
"firefox only"
]
}
@Inviz
Copy link

Inviz commented Mar 12, 2013

Very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment