Tuesday, May 01, 2012

Esoteric Programming Languages

So I've recently spent some time looking around at esoteric programming languages. These are languages that try to set things up so that it is difficult to do anything. This blog post is really just a ramble about interesting things I came across, so you've been warned. If you want to see the punchline, my LOLCODE program, just scroll to the end.

Befunge-93, the dialect I messed with a bit when I first came across these things. This is a 2 dimensional language where the program pointer moves around up, down, left, and right. Each character in a grid is a command. It was designed specifically so that writing a compiler would be difficult or impossible.

An example Befunge program follows:

2>:3g" "-!v\  g30          <
 |!`"O":+1_:.:03p>03g+:"O"`|
 @               ^  p3\" ":<
2 234567890123456789012345678901234567890123456789012345678901234567890123456789

This is the Sieve of Eratosthenes, which is a means of generating primes. If you want to see it work, go here. This has the Sieve as an example, and you can run it. If you use the "slow" button, it will run it a step at a time so you can see how Befunge works.

Befunge's Hello World program (a simple program that just prints "Hello World" to its output) looks like this:
"!dlrow olleH">:#,_@
Brainfuck is another one that implements a Turing Machine, which is a simple theoretical computing device. Brainfuck was designed to allow its author to make the smallest-possible compiler, which was 240 bytes on the Amiga. The language is Turing-Complete, which, roughly speaking, means it can solve every kind of calculation.

Brainfuck's Hello World program looks like this:
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<
+.+++++++..+++.>>>++++++++[<++++>-]
<.>>>++++++++++[<+++++++++>-]<---.
<<<<.+++.------.--------.>>+.

Another interesting one that was designed to make completely illegible programs is Whitespace. It only uses tabs, spaces and newlines for its commands. I won't reproduce its Hello World here, since it is very long. But here's a link. To "see" the code, drag-select the blank part of the page with your cursor. There's a special IDE you can get to make the programs visible.

Whenever is a weird one. There are no variables, and no flow control. Lines of code go into a "todo list" which it randomly picks from. If a line completes, it is removed, otherwise it goes back into the queue. Whenever's Hello World program is embarrassingly simple:

1 print("Hello world!");

At that Whenever page, the author says this regarding the Hello World program: "This is actually embarrassingly trivial in Whenever. Sort of a shame when you see it done in languages like Befunge and Malbolge."

Which led me to Malbolge. This language is named after one of the circles of Hell in Dante's Inferno.

Hello World in Malbolge is entirely cryptic:

('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"
 `CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@>

This language does a convultion on each command it executes, and so it scrambles its own program as it goes. Making something coherent out of this is a nightmare.

Quotes about Malbolge from Wikipedia:

"The peculiarity of Malbolge is that it was specifically designed to be impossible to write useful programs in. However, weaknesses in this design have been found that make it possible (though still very difficult) to write Malbolge programs in an organized fashion."

"Malbolge was so difficult to understand when it arrived that it took two years for the first Malbolge program to appear. The program was not even written by a human being: it was generated by a beam search algorithm designed by Andrew Cooke and implemented in Lisp."

"There is a more interesting discussion about whether one can implement sensible loops in Malbolge — it took many years before the first non-terminating one was introduced. A correct 99 Bottles of Beer program, which deals with non-trivial loops and conditions, was not announced for seven years; the first correct one was by Hisashi Iizawa in 2005"

My god. These people are insane.

Now, for the language that prompted me to make this post, LOLCODE. This is a language designed to work much like C, but to look like the language of the Lolcats at icanhascheesburger.com.

My first LOLCODE program was designed to be submitted to the now apparently-defunct 99 Bottles of Beer programming site. People can submit programs to print the lyrics to the song 99 Bottles of Beer in different languages. Good examples are supposed to illustrate features of the language it's written in. The LOLCODE examples don't use many language features, so I decided to write one.

LOLCODE is mainly interesting because it is so funny to read. Here are the features I used, and what they mean:

HAI / KTHXBYE - begin / end of program
OBTW / TLDR - beginning and end of long comments (if you didn't know, TLDR stands for "Too Long, Didn't Read", which is a rude response to a long post on internet forums)
HOW DUZ I <function> YR <variable> / IF U SAY SO - function definition with argument
GTFO - return, break
IM IN YR LOOP NERFIN YR <variable> / IM OUTTA YR LOOP - for loop with variable decrement
<expression> O RLY? / YA RLY / NO WAI / OIC - if-then-else-endif
<expression>, WTF? / OMG <const> / OMGWTF - switch-case-default
I HAS A <variable> - variable declaration

OBTW
    Almo's first LOLCODE program
    done according to LOLCODE v1.2 standard
    thanks to repl.it for the neat web IDE
TLDR

HAI

VISIBLE "O HAI!"

HOW DUZ I SHOWBEERZ YR NUMBEERZ
    DIFFRINT NUMBEERZ AN 69
    O RLY?
        YA RLY
            MOD OF NUMBEERZ AN 3, WTF?
                OMG 0
                    VISIBLE "Wahl haz " NUMBEERZ " botlez!!"
                    GTFO
                OMG 1
                    VISIBLE NUMBEERZ " botlez uv beer on teh wahl!"
                    GTFO
                OMGWTF
                    VISIBLE NUMBEERZ " botlez uv beer stihl on teh wahl!"
            OIC
        NO WAI
            VISIBLE "Da funy # 69 iz haow many botlez there iz!"
    OIC
    GTFO
IF U SAY SO

I HAS A BEERZVAR ITZ 99

IM IN YR LOOP NERFIN YR BEERZVAR TIL BOTH SAEM BEERZVAR AN 1
    VISIBLE "Therez " BEERZVAR " beerz on teh wahl..."
    VISIBLE "Takez wun daown, passez it around..."
    SHOWBEERZ DIFF OF BEERZVAR AN 1
IM OUTTA YR LOOP

VISIBLE "Jus wun botle uv beer on teh wahl!"
VISIBLE "Takez it daown, passez it around..."
VISIBLE "No moar botlez of beer on teh wahl!"
VISIBLE "Goez to stoar, getz sum moar..."
VISIBLE "99 botlez of beer is on teh wahl again, jus liek b4!"

VISIBLE "KTHXBYE"

KTHXBYE