Sunday, July 31, 2011

Esoteric Programming Languages

OK, so I learned to code in a Tandy TRS-80's BASIC around 1986 (my father's a natural born geek), then QBASIC, then Pascal, then PBASIC for Parallax's BASIC Stamp II, then Java, and then I met a real programming language: C, then C++, MIPS, s08, and so on.

The Tandy TRS-80, the first computer I remember coding for.

Esoteric Programming Languages are PLs made for the amusement of programmers, often intentionally difficult/fun/creepy to read.

I was completely ignorant of esoteric programming languages until 2002, when I ran into a piece of code that looked something like this:


I decided to read more about it and learned it was brainfuck code. Brainfuck is a programming language designed by Urban Müller with the objective of making the smallest compiler ever. It looks unintelligible and I guess that's part of the joke. But it's not useless: it's actually Turing Complete.

So how do I code in brainfuck?
The programmer's model is a huge byte memory and a pointer. You may modify the pointer but you can never be sure what address it's pointing to because you don't have access to it. This is just what a Turing Machine looks like. That's why it's Turing complete: because it's a Turing Tarpit.
There are only 8 instructions in brainfuck, each represented by a character.
Here they are:

Cmd     Meaning                                  C equivalent
Increment pointer                              p++; 

  Decrement pointer                              p--; 
  Increment pointed data                         *p++; 
  Decrement pointed data                         *p--; 
  Jump after matching ']' if pointed data is 0   while(*p){ 
  Go to matching '['                             } 
 ,  Get pointed byte from The input                *p=getchar(); 
 .  Send pointed byte to The output                putchar(*p);

Any other character is ignored and considered a comment (so comments must avoid the valid characters).

But it didn't stop there. I then learned there were tons of esoteric programming languages, such as Ook!, which is a parody of brainfuck (If that's possible). Ook! is actually the same as brainfuck, because it's isomorphic to it.
Ook! is supposed to be easily understood by orangutans :) Oh, and there's no need for comments because it's supposed to be clear enough for orangutans. Any character outside the proper Ook! language is a syntax error!
Ook! syntax consists of combinations of the symbols Ook. Ook! and Ook? in pairs, separated by single spaces, which match brainfuck characters. For example, "Ook. Ook?" means '>' and "Ook! Ook?" means '['.

      Quoting Wikipedia:
      "According to its designer, David Morgan-Mar, Ook! was meant to be easy for orangutans
      to read, and to prevent any mention of the word "monkey"".

      Quoting Dave Morgan's Ook! description:
      "Um, that's it. That's the whole language. What do you expect for something usable by

Ok, so this is how you say "hi" in Ook!:
    Ook. Ook? Ook! Ook? Ook! Ook! Ook? Ook! Ook. Ook? Ook! Ook? Ook! Ook! Ook? Ook! Ook? Ook. 
    Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
    Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. 
    Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. 
    Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook. Ook. Ook! Ook.
Get it? Of course not, you're not an orang-utan.

Visual Brainfuck
So I decided to start working with Borland C++ Builder in 2003 and I decided to make a brainfuck interpreter. I made Visual BF. Here's a screenshot of Visual Brainfuck running the Sierpinski triangle classic BF program.
Visual Brainfuck. Here's a dedicated post on it.

The "About" dialog had special thanks to my then boss for allowing me to be unproductive while learning.

Visual brainfuck had 2 extra tools:
  • A brainfuck text encoder, which generates a brainfuck program that prints the entered text.
  • An Ook! <-> brainfuck translator.
One esoteric PL that caught my attention is Whitespace. It uses only whitespace characters: tab, new line and space.

Befunge is a programming language that modifies the direction of code execution, by means of four arrow symbols (^,v,<, >). All instructions are stack operations with access to the source code for variables, constants and self-modification.

Here's a "Hello Wolrd" in Befunge:

    >25*"!dlrow ,olleH":v
                     >  ^

Here's the explanation of this code.

And guess what! Lolcat fans are encouraged to code in LOLCODE, a lolcat-syntax-based esoteric programming language. Its keywords are typical words found in lolcat pictures.

Here's a "Hai World" in LOLCODE:

And here's a program that prints a count (called "COUNT!!1"):
        UP VAR!!1

So here's a LOLCODE compiler for Parrot.

One-Instruction Set Computer
There's a lot of crazy stuff out there, and there's a computer architecture called a One Instruction Set Computer. Believe it or not, there are actual Turing complete OISCs.

This is a programming language intended for Pirates!!!
Hello World in R:
    Ahoy, mayteys!
    Aye, polly is a parrot that tells the crew what the Captain is thinkin'. Gar.
    Ahoy, polly says, "Hello World!"
    Dead men tell no tales.
Here's the explanation of this code. It's worth reading, trust me.

Writing this post I learned about Chef, another contribution of Dave Morgan. Chef is a programming language intented to make code look like an actual cooking recipe.

Here's a Hello World in Chef:

     Hello World Souffle.
     72 g haricot beans
     101 eggs
     108 g lard
     111 cups oil
     32 zucchinis
     119 ml water
     114 g red salmon
     100 g dijon mustard
     33 potatoes
     Put potatoes into the mixing bowl.
     Put dijon mustard into the mixing bowl.
     Put lard into the mixing bowl.
     Put red salmon into the mixing bowl.
     Put oil into the mixing bowl.
     Put water into the mixing bowl.
     Put zucchinis into the mixing bowl.
     Put oil into the mixing bowl.
     Put lard into the mixing bowl.
     Put lard into the mixing bowl.
     Put eggs into the mixing bowl.
     Put haricot beans into the mixing bowl.
     Liquefy contents of the mixing bowl.
     Pour contents of the mixing bowl into the baking dish.
     Serves 1.
So the mixing bowl is the stack and the baking dish is the standard output. Oh, and variables are ingredients and the quantities indicated are their values. n this example, note that their initials suggest the character they contain (e.g.: oil='o', water='w').

I could go on and on, really...
There's a LOT of esoteric programming languages out there (more than 600). Here are some of their names:
  • 1337,
  • Argh!, Aargh! (these are not pirate programming languages, they look a lot like Befunge), 
  • Zombie (by Dave Morgan, for necromancers), 
  • AAAAAAAAAAAAAA!!!! (I'm not getting into that), 
  • Brainfuck++ ("useful" brainfuck with file system and network support)
  • Brainfuck-- (downgrade of brainfuck, with only 5 instructions)
  • Whenever (the order of tasks is random).
Go to the Esoteric Programming Language Wiki for more fun!

No comments:

Post a Comment