Web 3.1415: Ajax, Ook! and Brainfuck
Yeah, April fools was yesterday, but I quite enjoyed Drupal’s April Fools Joke about porting Drupal to Ook!. 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.
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.
Cross-browser
To get started with application development in the OokJaXFuck framework, download and include OokJaXFuck library in your project.
Your first program
Creating an OokJaXFuck application in Brainfuck is as easy as this:
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!:
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:
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:
// 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.
Omnipresence: Ook! and Brainfuck using User JavaScript
Opera’s User JavaScript is quite powerful, since you can add support for custom scripting languages. Naturally, this means that we have to add support for Brainfuck and Ook! in Opera.
First, download the OokJaXFuck User JavaScript and install it. 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:
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().
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.
Comments
Comment from Jonny Axelsson on 2006-04-03 14:46
OokJaXFuck, eh? I wonder if I should take offence at that.
This discussion has been closed. No further comments may be added.