Advertisement
Guest User

Untitled

a guest
May 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.80 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>A brainfuck interpreter in Javascript</title>
  4. <script type='text/javascript'>
  5. //first we create the brainfuck environment
  6.  
  7. //empty array of 30000 elements
  8. var ar=new Array(30000);
  9. for(var n=0;n<30000;n++)
  10. {
  11.     ar[n]=0;
  12. }
  13. //current location in array
  14. var pointer=0;
  15. //the text of the program we're running (for parsing)
  16. var prog="";
  17. //the current character in program we're running
  18. var run_index=0;
  19.  
  20. //these are internal functions for some dynamic content using divs and such
  21.  
  22. //input returns the entered text
  23. function input()
  24. {
  25.     text=document.getElementById("input_area").value.substr(0,1);
  26.     document.getElementById("input_area").value=text.substr(1);
  27.     //return the ascii value of the text
  28.     return text.charCodeAt();
  29. }
  30. function output(str)
  31. {
  32.     document.getElementById("output_area").value+=str;
  33. }
  34.  
  35. //okay, so these are functions for each of brainfuck's commands
  36. function inc_point() //>
  37. {
  38.     if(pointer<29999)
  39.     {
  40.         pointer=pointer+1;
  41.     }
  42.     else
  43.     {
  44.         //I suppose I should give an error message
  45.         output("Err: Pointer past end of array");
  46.         run_index=prog.length; //stop program execution, this was a fatal error
  47.         return;
  48.     }
  49. }
  50. function dec_point() //<
  51. {
  52.     if(pointer>0)
  53.     {
  54.         pointer=pointer-1;
  55.     }
  56.     else
  57.     {
  58.         //same deal as before
  59.         output("Err: Pointer before start of array");
  60.         run_index=prog.length; //stop program execution, this was a fatal error
  61.         return;
  62.     }
  63. }
  64. function inc_byte() //+
  65. {
  66.     ar[pointer]=ar[pointer]+1;
  67. }
  68. function dec_byte() //-
  69. {
  70.     ar[pointer]=ar[pointer]-1;
  71. }
  72. function out_byte() //.
  73. {
  74.     output(String.fromCharCode(ar[pointer]));
  75. }
  76. function in_byte() //,
  77. {
  78.     //input is made to accept one byte only
  79.     ar[pointer]=input();
  80. }
  81. function jmp_f() //[
  82. {
  83.     if(ar[pointer]==0)
  84.     {
  85.         while(prog.charAt(run_index)!="]")
  86.         {
  87.             if(run_index<prog.length)
  88.             {
  89.                 run_index++;
  90.             }
  91.             else
  92.             {
  93.                 output("Err: Program ends within a conditional");
  94.                 run_index=prog.length; //stop program execution, this was a fatal error
  95.                 return;
  96.             }
  97.         }
  98.         //we didn't return, so we found the end of the conditional, handle that
  99.         run_index++;
  100.     }
  101.     else
  102.     {
  103.         //else continue normal execution
  104.         run_index++;
  105.     }
  106. }
  107. function jmp_b() //]
  108. {
  109.     if(ar[pointer]!=0)
  110.     {
  111.         while(prog.charAt(run_index)!="[")
  112.         {
  113.             if(run_index>0)
  114.             {
  115.                 run_index--;
  116.             }
  117.             else
  118.             {
  119.                 output("Err: No matching [ for a given ]");
  120.                 run_index=prog.length; //stop program execution, this was a fatal error
  121.                 return;
  122.             }
  123.         }
  124.         //we didn't return, so we found the start of the conditional, handle that
  125.         run_index++;
  126.     }
  127.     else
  128.     {
  129.         //else continue normal execution
  130.         run_index++;
  131.     }
  132. }
  133.  
  134. //okay, now the parser
  135. function bf_eval(text)
  136. {
  137.     //resets all defaults to ignore all previous programs' environments
  138.     run_index=0;
  139.     pointer=0;
  140.     for(var n=0;n<30000;n++)
  141.     {
  142.         ar[n]=0;
  143.     }
  144.    
  145.     prog=text;
  146.     document.getElementById("output_area").value="";
  147.     document.getElementById("input_area").value="";
  148.     document.getElementById("code_area").value="";
  149.    
  150.     while(run_index<prog.length)
  151.     {
  152. //      alert(prog.charAt(run_index)); //debug
  153.         switch(prog.charAt(run_index))
  154.         {
  155.             case ">":
  156.                 inc_point();
  157.                 run_index++;
  158.                 break;
  159.             case "<":
  160.                 dec_point();
  161.                 run_index++;
  162.                 break;
  163.             case "+":
  164.                 inc_byte();
  165.                 run_index++;
  166.                 break;
  167.             case "-":
  168.                 dec_byte();
  169.                 run_index++;
  170.                 break;
  171.             case ".":
  172.                 out_byte();
  173.                 run_index++;
  174.                 break;
  175.             case ",":
  176.                 in_byte();
  177.                 run_index++;
  178.                 break;
  179.             case "[":
  180.                 jmp_f();
  181.                 break;
  182.             case "]":
  183.                 jmp_b();
  184.                 break;
  185.         }
  186.     }
  187. }
  188. </script>
  189. </head>
  190. <body>
  191. <form>
  192.     <textarea rows="10" cols="80" id="code_area" ondblclick="bf_eval(this.value);"></textarea><br>
  193.     <input id="input_area"></input><br>
  194.     <textarea rows="10" cols="80" id="output_area"></textarea>
  195. </form>
  196. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement