Brainfuck

Simply compiles Brainfuck code to JavaScript and interprets the Brainfuck code by evaluating the JavaScript code.

Command line interface

Syntax

  1. $ brainfuck code [input]

Arguments

  • code – (string) The Brainfuck code to interpret.
  • input – (string, optional) Used as input to the Brainfuck program.

Example

Todo

  • Make options to interpret Brainfuck code from a file on the server instead of using direct input

brainfuck.js

  1. /**
  2. * Javascript brainfuck interpreter, inspired
  3. * by http://www.muppetlabs.com/~breadbox/bf/
  4. **/
  5. TinyShell.plugins.brainfuck = new Class({
  6. description: "Interpret brainfuck code",
  7. run : function(t, args) {
  8. if (!args.length) {
  9. t.print("brainfuck: usage: brainfuck code [input]");
  10. t.resume();
  11. return;
  12. }
  13. var commands = {
  14. '>' : "++p;",
  15. '<' : "--p;",
  16. '+' : "if(++array[p]>255)array[p]=0;", // modulus
  17. '-' : "if(--array[p]<0)array[p]=255;", // modulus
  18. '.' : "output.push(array[p]);",
  19. ',' : "array[p]=input.shift();",
  20. '[' : "while(array[p]){",
  21. ']' : "}"
  22. }
  23. var output = [];
  24. var input = args.length > 1 ? args[1].split("") : [];
  25. var js = "var array=[];for(var j=0;j<30000;j++){array[j]=0;};var p=0;";
  26. for(var i = 0; i < args[0].length; i++) if ($defined(commands[args[0].charAt(i)])) js += commands[args[0].charAt(i)];
  27. eval(js);
  28. var outstr = "";
  29. for(var i = 0; i < output.length; i++) outstr += String.fromCharCode(output[i]);
  30. t.print(outstr);
  31. t.resume();
  32. }
  33. });

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>