Skip to content

Instantly share code, notes, and snippets.

@burke
Created July 5, 2011 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burke/1065680 to your computer and use it in GitHub Desktop.
Save burke/1065680 to your computer and use it in GitHub Desktop.
function brainfuck(x){var p='23\n',o='',s=x.length,i=0,n=0,c=0,d=0,m={},l={},u={
},t=[];for(c=0; c<s; c++)if(x[c]=='[')t.push(c);else if(x[c]==']')u[l[c]=t.pop()
]=c;for(c=0; c<s&&i<100000; c++,i++){switch(x[c]){case'>':d++;break;case'<':d--;
break;case '+':m[d]=((m[d] || 0)+1)&255;break;case '-':m[d]=((m[d] || 0)-1)&255;
break;case'.':o+=String.fromCharCode(m[d]); break; case',':m[d]=p.charCodeAt(n++
)||0;break; case '[': m[d]||(c=u[c]);break;case ']':c=l[c]-1;break;}};return o;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment