Web 3.1415: Ajax, Ook! and Brainfuck

Yeah, April fools was yesterday, but I quite enjoyed “Drupal’s April Fools Joke”:http://drupal.org/node/56815 about porting Drupal to “Ook!”:http://en.wikipedia.org/wiki/Ook!_programming_language. Since I couldn’t find an Ook! implementation in ECMAScript, naturally, I had to write my own. And, while I was at it, I had to add support for Ook!’s parent/sibling language, “Brainfuck”:http://en.wikipedia.org/wiki/Brainfuck.
And, since working with Web 2.0 isn’t nearly hard enough, I’ve created OokJaXFuck in the form of a library that you can use to create your applications on top of.
*Update:* Now with unbroken links.


h3. Cross-browser
To get started with application development in the OokJaXFuck framework, download and include “OokJaXFuck library”:http://virtuelvis.com/download/2006/04/ookjaxfuck/ook.js in your project.
h4. Your first program
Creating an OokJaXFuck application in Brainfuck is as easy as this:
bc. var code = “,.” // Read character from input, then output it
// Create a new OokJaXFuck object
var app = new OokJaXFuck(code,”brainfuck”);
// Redirect output to the first paragraph on the page
app.output = document.getElementsByTagName(‘p’)[0];
app.run(); // run application
… It’s not much harder in Ook!:
bc. var code = “Ook. Ook! Ook! Ook.” // Read character from input, then output it
// Create a new OokJaXFuck object
var app = new OokJaXFuck(code,”ook”);
// Redirect output to the first paragraph on the page
app.output = document.getElementsByTagName(‘p’)[0];
app.run(); // run application
Simple enough, enjoy the examples:
* “Hello, World! in Brainfuck!”:http://virtuelvis.com/download/2006/04/ookjaxfuck/simple-bf.html
* “Hello, World! in Ook!”:http://virtuelvis.com/download/2006/04/ookjaxfuck/simple-ook.html
h4. Overriding host methods
In most cases, simply outputting text to a div isn’t what you’d want as a developer. Which is why most of the properties and methods of the OokJaXFuck class are public. A slightly more useful version would perhaps evaluate the output in some way, when the program has finished executing:
bc. // Create a Ook! instance
var web31415 = new OokJaXFuck(sourceText,”ook”);
web31415.output = “”; // Output is now a string
// Override the run method to add a callback using eval;
web31415.run = function(){
run_index = 0;
while (run_index < call_stack.length){ tokenMap[source_type][call_stack[run_index]](); run_index++ } eval(output); } // Also override putChar to work with strings web31415.putChar = function(){ output += String.fromCharCode((memory[mem_pointer])); } web31415.run(); We now have an OokJaXFuck application that evaluates it's own output as JavaScript. h3. Omnipresence: Ook! and Brainfuck using User JavaScript Opera's User JavaScript is quite powerful, since you can add support for "custom scripting languages":http://virtuelvis.com/archives/2005/03/plainscript. Naturally, this means that we have to add support for Brainfuck and Ook! in Opera. First, download the "OokJaXFuck User JavaScript":http://virtuelvis.com/download/2006/04/ookjaxfuck/ook-ujs.js and "install it":http://userjs.org/help/installation. If you want to serve OokJaXFuck applications to supporting clients, all you need to do now is to include your OokJaXFuck script as you would any other script, using the Content-Type "text/x-ook" for Ook! and "text/x-brainfuck" for Brainfuck: ==

<script type="text/x-ook" src="hello-world.ookjs"></script> or <script type="text/x-brainfuck" src="hello-world.bfjs"></script>

==
Including the scripts inline is of course also OK. Now, install the aforementioned User JavaScript and test these two examples:
* “OOk! script”:http://virtuelvis.com/download/2006/04/ookjaxfuck/ook-script.html
* “Brainfuck script”:http://virtuelvis.com/download/2006/04/ookjaxfuck/bf-script.html
h4. Authoring scripts
As with the example earlier, scripts parsed with the User JavaScript are first executed, and the final output is then evaluated using @eval()@.
h3. Closing remark
So, when you think that writing applications for Web 2.0 isn’t nearly hard enough, try Brainfuck or Ook! — you’ll learn to appreciate browser inconsistencies in an entirely new way.

1 Comment

  1. OokJaXFuck, eh? I wonder if I should take offence at that.