Delegates in Java - Why not? Some bad news and some good news
Mar 07

About a month ago Roman Strobl mentioned the Whirl programming language on his blog, along with some other eccentric programming languages by the same author, Sean.

Whirl is an odd programming language, reminding me a mutation of the accursed Enigma machine. It uses two wheel of 12 operation each, which the language’s operators rotate and execute. The language’s operators are only 1’s and 0’s. The language also allows for two registers and an infinite, non-RAM of memory.

Because of this, executing a command, reading an input or making a calculation changes the entire state of the Whirl machine, making even concurrent executions of the same operation different as the machine state changes with them.

I took it as a challenge to write a code generator for Whirl. The generator was written in Java and can perform basic calculus (addition, substraction, multiplication and division) and a basic string output operation. You can find the binary, the source and an example printing “Hello World” generated with it on the Whirl site.

Some notes about the code implementation:

  • It’s not pretty. It’s rough, mainly undocumented and is a result of staying up until 4am.
  • To implement the 24 Whirl commands I’ve used two enums (one for each wheel), each of them implementing the Command interface, which exposed one method of execute. This allowed me to write very short code (an enum name and a simple operation for execution, as they were all simple) and the compiler took care of generating 24 different classes for me. Then, the wheels could receive an array of the Command interface without knowing they are receiving an enum at all. Read back my tips with enums post for a refresh about them.
  • I’ve decided to pass around the Machine being operated upon as a parameter instead of using the Singleton design pattern. This is because using the Singleton design pattern to create system global variables is considered abusive and I try to refrain from doing that.
  • I made the order of commands on a wheel dynammic. This is because of a delusional idea I had back then way after mid-night of encrypting data using a Whirl machine initialised with a predefined and exchanged wheel structure. Obviously, when I woke up the day after I realised it’s not that great, but the code is still there.
  • The machine factory is creating Sean’s original Whirl machine. The factory is too related to my delusions for an encrypting Whirl machine, and could have produced a machine according to the aforemention wheel formation. That code, as well, remained.

Share/Save/Bookmark

Leave a Reply

Chaotic Java is Digg proof thanks to caching by WP Super Cache!