Brainfuck Interp & Eval

By sizzlemctwizzle Last update Nov 21, 2011 — Installed 748 times.

Script Summary: Interprets Brainfuck source code and evalutates its javascript equivalent



Version: 1.0.5

Description

It is with great joy that I present Userscripts.org the ground-breaking Brainfuck development environment for Greasemonkey. If you are anything like me, when a great idea for a new program pops into your head, Brainfuck is your first language of choice to implement your amazing idea. Unfortunately, most current compilers are crude and inconvenient. However, with a interpreter in Greasemonkey, Brainfuck heaven is just a click away. And with a language like Brainfuck, you'll need ALL the extra time you can get.

How to use

To enter Brainfuck code just click on the Greasemonkey icon and select the "User Script Commands..." submenu and click "Run Brainfuck code". A prompt will popup for you to enter your code. After you click "Ok" you will be prompted for an input. Finally, once you click "Ok" again, your code will be interpreted and evaluated and your output will be displayed(providing your code works). Next the javascript equivalent of your code will be displayed, along with an error if one has occurred.

Development features as rich as language itself

  • Enter source - enter your Brainfuck code into the beautiful GUI
  • Input - Give your code the text it can read
  • Output - The text returned by your code
  • Javascript Equivalence - Most Brainfuck interpreters written in Javascript directly interpret Brainfuck code, instead my script first converts all Brainfuck code to Javascript using regular expression. Once the code has been converted it is then evaluated. The benifit of this is that you can learn more about Brainfuck by seeing your code in its equivalent javascript form. I do many things to try and keep the resulting code slightly less redundant.

Current Limitations

Freezed by very large complex scrips. There will always be limitations to a interpreter written in an already interpreted language. If you want to do anything really serious(with Brainfuck?) then I suggest you find a more optimized compiler. This is intended to be a simple, light-hearted, learning tool.

Command support

My interpreter supports all 8 of the Brainfuck commands:
  • ">" = increase data point by one
  • "<" = decrease data point by one
  • "+" = increase the value at the current data point by one
  • "-" = decrease the value at the current data point by one
  • "." = output the value at the current data point
  • "," = replace the value of the current data point with an input value
  • "[" = jump forward past the matching "]" if the value of the current data point is zero
  • "]" = jump backward to the matching "[" if the value of the current data point is not zero

Simple Examples

Reverse the input
+[>,]<-[+.<-] This BF code becomes this JavaScript:
// Initialize values
var input = "Hello, World!",
    ipoint = 0,
    output = "",
    cells = [],
    dpoint = 0;
for (var i = 0; i < 30000; ++i) {
    cells[i] = 0;
}

// Actual BF code begins here
++cells[dpoint];
while (cells[dpoint]) {
    ++dpoint;
    cells[dpoint] = input.charCodeAt(ipoint++);
}--dpoint;
--cells[dpoint];
while (cells[dpoint]) {
    ++cells[dpoint];
    output += String.fromCharCode(cells[dpoint]);
    --dpoint;
    --cells[dpoint];
}
Repeat the input
,[.,]
Output "Hello World!"
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
ROT13(okay so it's really not simple, but I like to show off that I can run it!)
+[,+[-[>+>+<<-]>[<+>-]+>>++++++++[<-------->-]<-[<[-]>>>+[<+<+>>-]<[>+<-]<[<++>>>+[<+<->>-]<[>+<-]]>[<]<]>>[-]<<<[[-]<[>>+>+<<<-]>>[<<+>>-]>>++++++++[<-------->-]<->>++++[<++++++++>-]<-<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<<<<+>>>>++++[<++++++++>-]>-]<<-<-]>[<<<<[-]>>>>[<<<<->>>>-]]<<++++[<<++++++++>>-]<<-[>>+>+<<<-]>>[<<+>>-]+>>+++++[<----->-]<-[<[-]>>>+[<+<->>-]<[>+<-]<[<++>>>+[<+<+>>-]<[>+<-]]>[<]<]>>[-]<<<[[-]<<[>>+>+<<<-]>>[<<+>>-]+>------------[<[-]>>>+[<+<->>-]<[>+<-]<[<++>>>+[<+<+>>-]<[>+<-]]>[<]<]>>[-]<<<<<------------->>[[-]+++++[<<+++++>>-]<<+>>]<[>++++[<<++++++++>>-]<-]>]<[-]++++++++[<++++++++>-]<+>]<.[-]+>>+<]>[[-]<]<]

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.