i.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <html><head><title>brainfuck interpreter</title><style><!--
  2. body,td,caption{background:#cccccc;color:#000000;font-family:monospace;font-size:12px}
  3. a:link,a:visited{color:#330099}a:hover,a:active{color:#ffffff}.t{border-bottom:1px dashed}
  4. --></style><script>
  5. function x(c){
  6. var y=document.f.i,z=c.length,v=32768,a=new Array(v),p=0,k=-1,e=new Array(z),l=new Array(z),s=new Array(z),m=127,n=-128
  7. if(document.f.q.checked){m=Number.MAX_VALUE;n=-(Number.MAX_VALUE)}
  8. for(j=0;j<v;j++){a[j]=0}
  9. for(j=0;j<z&&p>=0;j++){if(c.charAt(j)=='[')l[++p]=j;if(c.charAt(j)==']'){s[j]=l[p];e[l[p]]=j;p--}}
  10. if(p!=0){alert("Unbalanced brackets!");return}
  11. for(j=0;j<z;j++){switch(c.charAt(j)){
  12. case'#':if(document.f.d.checked){window.alert('Position within code: '+j+'\nPointer: '+p+'\nValue at pointer: '+a[p])};break
  13. case'<':p--;if(p<0)p=v-1;break
  14. case'>':p++;if(p>=v)p=0;break
  15. case'+':if((a[p]+1)>m)a[p]=n;else{a[p]++};break
  16. case'-':if((a[p]-1)<n)a[p]=m;else{a[p]--};break
  17. case']':j=s[j]
  18. case'[':if(a[p]==0)j=e[j];break
  19. case',':if(k+1>=y.value.length){if(!document.f.r.checked){a[p]=0;break}
  20. u=prompt("Input required. Press Cancel to halt program execution.","")
  21. if(u==''||u==null){j=z;break}
  22. else{y.value+=u}}a[p]=y.value.charCodeAt(++k);break
  23. case'.':document.f.o.value+=String.fromCharCode(a[p])}}
  24. if(document.f.w.checked){window.alert("Execution completed.")}
  25. }//</script></head><body><form name=f><table width="100%" border=0><tr><td width="40%" align=left valign=top>
  26. <table border=1 cellspacing=0 cellpadding=5><caption><b>brainfuck commands</b>
  27. <tr><td>&gt;</td><td>increment pointer
  28. <tr><td>&lt;</td><td>decrement pointer
  29. <tr><td>+</td><td>increment value at pointer
  30. <tr><td>-</td><td>decrement value at pointer
  31. <tr><td>[</td><td>begin loop (continues while value at pointer is non-zero)
  32. <tr><td>]</td><td>end loop
  33. <tr><td>,</td><td>read one character from input into value at pointer
  34. <tr><td>.</td><td>print value at pointer to output as a character
  35. <tr><td>#</td><td>display debugging info (in debug mode)
  36. <tr><td colspan=2 align=center>Any other characters are ignored.</table>
  37. <p>Learn more about the Brainfuck programming language:<br>&nbsp;
  38. <a href="http://www.catseye.mb.ca/esoteric/bf/">Source</a> |
  39. <a href="http://www.muppetlabs.com/~breadbox/bf/">@ Muppetlabs</a> |
  40. <a href="http://esoteric.sange.fi/brainfuck/">Code archive</a>
  41. <td width="60%" align=center valign=top><table width="1%" border=0><tr><td align=left valign=bottom>
  42. <b><span class=t title="Warning: clicking these links will replace the existing code with a new program.">programs:</span></b>
  43. <a href="javascript:void(0)" onClick="b.value='+++++++++[>++++++++<-]>. H\n<+++[>+++++++++++<-]>. i\n[-]<+++[>+++++++++++<-]>. !'">hello</a>
  44. <a href="javascript:void(0)" onClick="b.value='+[,.]'">echo</a>
  45. <a href="javascript:void(0)" onClick="b.value='+[>,]<-[+.<-]'">rev</a>
  46. <a href="javascript:void(0)" onClick="b.value='->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++>++>>+>>+>++>++>+>>>>+++>+>>>>++>++>>>>+>>++>+>+++>>>++>>++++++>>+>>++>+>>>>+++>>+++++>>+>+++>>>++>>++>>+>>++>+>+++>>>++>>+++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>++>+>>>>+++>>+++++>>>>++>>>>+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+>+++>+>++>++>>>>>>++>+>+++>>>>>+++>>>++>+>+++>+>+>++>>>>>>++>>>+>>>++>+>>>>+++>+>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++++>>>+>++>>+++>+>+>++>+>+++>>>>>+++>>>+>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>>>+++>>+++>>>+[[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>-.------------------->-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>++++++++++++++<]>+++<]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<]'">quine</a>
  47. <p><b><span class=t title="Click to append code to your program. Review the code before you use it! It may make assumptions about the values at or near the pointer.">functions:</span></b>
  48. <a href="javascript:void(0)" onClick="b.value+=' ([<+>-]<) '">add</a>
  49. <a href="javascript:void(0)" onClick="b.value+=' ([>+>+<<-]>>[<<+>>-]) '">dup</a>
  50. <a href="javascript:void(0)" onClick="b.value+=' ([>+<-]<[>+<-]>>[<<+>>-]<) '">swap</a>
  51. <a href="javascript:void(0)" onClick="b.value+=' (>[-]>[-]<< <[>[>+>+<<-] >[<+>-] <<-] >>>[<<<+>>>-]<<<) '">mul</a>
  52. <a href="javascript:void(0)" onClick="b.value+=' ({condition}[{conditional code}[-]]) '">if</a>
  53. </td><td align=right>Debug&nbsp;mode:<input type=checkbox name=d><br>Large variables:<input type=checkbox name=q><br>
  54. Prompt for input:<input type=checkbox name=r><br>Alert when finished:<input type=checkbox name=w><tr><td colspan=3>
  55. <textarea name=b cols=50 rows=9></textarea><tr><td><b>code ^</b></td>
  56. <td align=right><input type=button value=execute onClick="x(b.value)"> <input type=button value=clear onClick="b.value=''"></table>
  57. <br><b>input:</b><input type=text name=i size=44> <input type=button value=clear onClick="i.value=''">
  58. <table width="1%" border=0><tr><td colspan=2><textarea name=o cols=50 rows=4></textarea><tr><td><b>output ^</b></td>
  59. <td align=right><input type=button value=clear onClick="o.value=''"></table></table></form></body></html>