Crazy Programming Languages Challenge - Part 2 - LOLCODE

For your enjoyment and my own, I am trying out some crazy programming languages:

  1. Brainfuck
  2. LOLCODE <- You are here
  3. Whitespace
  4. Piet
  5. MalbolgeZombie

Last week I tried Brainfuck which was as bad as I thought. This week, LOLCODE.

Overview

Unlike Brainfuck, LOLCODE is a little bit more like traditional programming languages in the way that it is written.

The full spec can be found here. I am not going to cover everything written there here as does have a lot of substance.

The basic premise is that all of the keywords of the language are written like "Internet slang". So for example:

A new variable can be created like:

I HAS A VAR

A comment can be written like:

BTW this is a comment

An if-else statement could be written likes so:

BOTH SAEM A [AN] B
O RLY?
    YA RLY
       VISIBLE "Hello"
    NO WAI
        VISIBLE "Goodbye"
OIC

It is actually quite funny seeing of the language constructs recreated in this language. As I said, I can't cover anything so even if you are not going to try the language, you may find it amusing to read the spec.

Installing and Running

Installing LOLCODE is actually very straightforward and the instructions for doing so are on the official website:

git clone https://github.com/justinmeza/lci.git
cd lci
cmake .
make && make install

I had only 2 minor issues. I had to install cmake but that was simply apt-get install cmake. The other problem is that you have to be a sudoer to run make install otherwise you will get an error.

Once you have done this, that is it. You simply run lci and the name of the file (which doesn't have to have any particular extension) and it instantly compiles and runs.

Building "Hello, World!"

Like last week, I was going to build the basic "Hello, World!" program however the website gave away the answer:

HAI 1.2
    CAN HAS STDIO?
    VISIBLE "HAI WORLD!!!1!"
KTHXBYE

As you can see, it is also very simple. It runs like so:

So I decided to try a more complex program: A program to convert between Celsius and Fahrenheit.

It would work by taking an input from the user to decide the mode and then a number that they want to convert. It would then return an answer.

I start the code the same way as the "Hello, World" example above and then I have some variable declarations:

  I HAS A conversion_type
  I HAS A conversion_label
  I HAS A opposite_conversion_label
  I HAS A temperature

These will all come into play later.

Next, I want the user to enter C to convert to Celsius or F to convert to Farenheit. I do not want the program to continue unless one of these two options have been entered.

So, I create a while loop. In LOLCODE, a while loop looks like this:

IM IN YR label
    BTW code goes here
IM OUTTA YR label

So you specify a name for the loop and use the same name to close it. It is possible to set this to a condition, however I simply break out of it when I am done, so this is the equivalent of:

while (true) { //code goes here }

Inside my while loop, I print two lines and wait for an input:

VISIBLE "Press C to convert from Farenheit to Celsius"
VISIBLE "Press F to convert from Celsius to Farenheit"
GIMMEH conversion_type

Note the GIMMEH command here. This waits for the user to enter an input and then assigns that value into the variable that you give, in this case, conversion_type.

And so now I want to check this input and break out of the loop if it is a valid one. Otherwise, it will just continue asking and receiving.

And now we have the slightly confusing syntax of the conditional statements.

It goes like this:

  BOTH SAEM conversion_type AN "C"
  O RLY?
  YA RLY
      conversion_label R "Farenheit"
      opposite_conversion_label R "Celsius"
      GTFO
  MEBBE BOTH SAEM conversion_type AN "F"
      conversion_label R "Celsius"
      opposite_conversion_label R "Farenheit"            
      GTFO
  OIC 

Lots of language constructs going on here.

The if statement starts of with a true or false expression. BOTH SAEM takes two things separated with an AN and returns true if they are equal. In this case I am checking if conversion_type is equal to C.

If-statements, start of with O RLY? and end with OIC.

The code within the YA RLY block is what is run if the previous expression is true. So in this case if conversion_type is C.

After this block, I another condition, this time starts with MEBBE and I am now checking if conversion_type is equal to F.

To sum-up the PHP equivilent to this statement would be:

if ($conversion_type === "C") {

} elseif ($conversion_type === "F") {

}

With code within each block.

The code within each block is simply assigning the variables are initialised earlier with values using the R keyword.

Finally, conditional block ends with GTFO which is what breaks out of the loop that we are in.

Next, I want to get the value that the user wants to convert. This time, I am not going to perform any validation (although I wanted to check if it as a number but was unable to) so there is no need for a loop:

  VISIBLE SMOOSH "Your choice: " conversion_type MKAY
  VISIBLE SMOOSH "Please enter a temperature in " opposite_conversion_label ": " MKAY
  GIMMEH temperature

Again, I am using VISIBLE and GIMMEH to perform output and input commands. You will also see SMOOSH and MKAY. This is basically string concatenation. I am providing a list of space separated strings that are getting "smooshed" together.

Now that I have a value, I want to run a function. So I have another if statement:

I HAS A answer
BOTH SAEM conversion_type AN "C"
O RLY?
   YA RLY
       answer R I IZ farenheitToCelsius YR temperature MKAY
  MEBBE BOTH SAEM conversion_type AN "F"
       answer R I IZ celsiusToFarenheit YR temperature MKAY
OIC

Above, I first create another variable for to store the answer and I also have another conditional statement identical to the one earlier.

Then you can see this line:

answer R I IZ farenheitToCelsius YR temperature MKAY

The first part is easy, answer R is storing whatever is about to happen into the answer variable. Next, we see how a function is called:

I IZ functionName YR param1 param2 MKAY

You can have as many parameters as you like, we only have the one in our case. This works exactly as you would expect functions to work in terms of how they scope variables and how you return them.

One thing to note is that you have to have the function created BEFORE you used it, unlike languages like PHP where the function only has to be within the scope and the order doesn't matter.

So I have the function written near the beginning of the function:

HOW IZ I farenheitToCelsius YR temperature
    BTW C=(F-32)x5/9
    temperature IS NOW A NUMBAR
    I HAS A answer ITZ QUOSHUNT OF PRODUKT OF 5 AN DIFF OF temperature AN 32 AN 9
    FOUND YR answer
IF U SAY SO

Functions start with declaring the name and the parameters as you see here and end with IF U SAY SO.

Inside the function itself, you can see I have a little comment of the formula that I need using the syntax for comments, BTW.

I then do temperature IS NOW A NUMBAR. I had to do this to make sure that the parameter is treated as a floating point decimal. Otherwise, if the user gives a whole number, it would be cast as an integer and the calculation would round it.

Next I create a variable and assign it a value in one go: I HAS A answer ITZ, whatever comes after ITZ would be assigned to answer.

Then I use the mathematical functions made available to me to perform the calculations (these are all outlined in the documentation). There is no brackets and the functions tend to be structured like:

 F x AN y

So,

QUOSHUNT OF PRODUKT OF 5 AN DIFF OF temperature AN 32 AN 9

written in English pseudocode is:

division (multiply(5, subtract(temperature, 32)), 9)

It is a little confusing at first to structure it in this slightly different way.

Finally, the function returns the answer with the line:

FOUND YR answer

The celsiusToFarenheit counterpart is structured in the exact same way just with a different formula.

Now I have returned from my function, I output the answer:

VISIBLE SMOOSH "The temperature in " opposite_conversion_label " is " answer ". " MKAY

And thats it!

So if I run the program, this is how it looks:

Summary

There we have it. LOLCODE was actually a lot easier than Brainfuck last week because it was actually a lot closer to the sort of programming that I am used to (albeit with an unusual syntax) and therefore building a more complex program that actually does something useful, was easy.

Again, documentation for this can be found here. Next week, I fear it will not be as easy as we go onto Whitespace!


© 2012-2017