Skip to content

Instantly share code, notes, and snippets.

@youz
Created January 9, 2009 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youz/45041 to your computer and use it in GitHub Desktop.
Save youz/45041 to your computer and use it in GitHub Desktop.
brainf*ck interpreter for Javascript
/*@cc_on@*//*@if (@_jscript) var print=function(s){WScript.Echo(s)};@*//*@end@*/
var prompt=function(x,y){return'a'};
var alert = print;
(function(bf){j=s=[];o=p='';b=[m=0];bf=bf.split('');
for(i in bf)(c=bf[i])=='['?s.push(i):c==']'?j[j[i]=s.pop()]=i:0;
for(i=0;c=bf[i],i<bf.length;++i)
c=='>'?b[++m]=b[m]||0:
c=='<'?--m:c=='+'?++b[m]:
c=='-'?--b[m]:c=='['?b[m]||(i=j[i]):
c==']'?b[m]&&(i=j[i]):
c==','?(b[m]=((p=prompt('>',''))&&p.length)?p.charCodeAt(0):0):
c=='.'?o+=String.fromCharCode(b[m]):0;
alert(o)
})('++++++++[>++++++++<-]>+.');
//})((S=(D=document).selection)?D.S.createRange().text:D.getSelection());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment