Lably
Skip to content

LOLCODE

Esoteric programming language

  • 4 min read
  • 3 chapters

Related on Lably

Through Exception handling and JavaScript

The language is not clearly defined in terms of operator priorities and correct syntax, but several functioning interpreters and compilers exist. One interpretation of the language has been proven Turing-complete.

Language structure and examples

LOLCODE's keywords are drawn from the heavily compressed (shortened) patois of the lolcat Internet meme. Here follow a "Hello, World!" program and a simple program to output a file to a monitor. Similar code was printed in the Houston Chronicle.

  • :) represents a newline (\n)
  • :> represents a tab (\t)
  • :o represents a bell character (\a)
  • :" represents a literal double quote (")
  • :: represents a single literal colon (:)
  • :(<hex>) converts a single hexadecimal Unicode code point to local environment encoding (for example, UTF-8)
  • :{<variable>} interpolates the value of the enclosed variable, cast as a string
  • :[<character name>] converts normative name of a single Unicode character to local environment encoding

Example 1

<span id="mwSA"></span><span class="linenos" data-line="1" id="mwSQ"></span>HAI 1.2 <span class="linenos" data-line="2" id="mwSg"></span>CAN HAS STDIO? <span class="linenos" data-line="3" id="mwSw"></span>VISIBLE "HAI WORLD!" <span class="linenos" data-line="4" id="mwTA"></span>KTHXBYE

Example 2

<span id="mwfw"></span><span class="linenos" data-line="1" id="mwgA"></span>HAI 1.2 <span class="linenos" data-line="2" id="mwgQ"></span>CAN HAS STDIO? <span class="linenos" data-line="3" id="mwgg"></span>PLZ OPEN FILE "LOLCATS.TXT"? <span class="linenos" data-line="4" id="mwgw"></span> AWSUM THX <span class="linenos" data-line="5" id="mwhA"></span> VISIBLE FILE <span class="linenos" data-line="6" id="mwhQ"></span> O NOES <span class="linenos" data-line="7" id="mwhg"></span> INVISIBLE "ERROR!" <span class="linenos" data-line="8" id="mwhw"></span>KTHXBYE

In this example, commands to open a file (PLZ OPEN FILE "NAME"?—"Please open this file?"), and error handling (AWSUM THX—"Awesome, thanks!", and O NOES—"Oh no!") are introduced.

Example 3

Other commands include I HAS A variable for declaring variables, variable R value ("variable [is/are/being] value") for assigning them, sending error messages to the front end via INVISIBLE instead of VISIBLE, and BTW ("by the way") to denote a comment, making the parser ignore the rest of the line.

Loops are created with IM IN YR label (inspired by the "Im in ur noun, verbing yr related noun" LOLcat meme), and ended with IM OUTTA YR label. Loops can be broken with the keyword ENUF ("enough"), or in older versions, GTFO. Loops can also be ended with the conditional IZ command, as demonstrated in the next example.

<span id="mwrw"></span><span class="linenos" data-line="1" id="mwsA"></span>HAI 1.0 <span class="linenos" data-line="2" id="mwsQ"></span>CAN HAS STDIO? <span class="linenos" data-line="3" id="mwsg"></span>I HAS A VAR <span class="linenos" data-line="4" id="mwsw"></span>IM IN YR LOOP <span class="linenos" data-line="5" id="mwtA"></span> UP VAR!!1 <span class="linenos" data-line="6" id="mwtQ"></span> VISIBLE VAR <span class="linenos" data-line="7" id="mwtg"></span> IZ VAR BIGGER THAN 10? KTHX <span class="linenos" data-line="8" id="mwtw"></span>IM OUTTA YR LOOP <span class="linenos" data-line="9" id="mwuA"></span>KTHXBYE

This simple program displays the numbers 1–11 and terminates (as of specification 1.0). The same program as of specification 1.2 is (assuming VAR starts at 0):

<span id="mwwQ"></span><span class="linenos" data-line="1" id="mwwg"></span>HAI 1.2 <span class="linenos" data-line="2" id="mwww"></span>CAN HAS STDIO? <span class="linenos" data-line="3" id="mwxA"></span>IM IN YR LOOP UPPIN YR VAR TIL BOTH SAEM VAR AN 10 <span class="linenos" data-line="4" id="mwxQ"></span> VISIBLE SUM OF VAR AN 1 <span class="linenos" data-line="5" id="mwxg"></span>IM OUTTA YR LOOP <span class="linenos" data-line="6" id="mwxw"></span>KTHXBYE

Example 4

<span id="mwyw"></span><span class="linenos" data-line="1" id="mwzA"></span>HAI 1.0 <span class="linenos" data-line="2" id="mwzQ"></span>CAN HAS STDIO? <span class="linenos" data-line="3" id="mwzg"></span>VISIBLE "U SEE THIS" <span class="linenos" data-line="4" id="mwzw"></span> <span class="linenos" data-line="5" id="mw0A"></span>BTW VISIBLE "U SEE NOTHING" <span class="linenos" data-line="6" id="mw0Q"></span> <span class="linenos" data-line="7" id="mw0g"></span>OBTW <span class="linenos" data-line="8" id="mw0w"></span>VISIBLE "U SEE NOTHIN" <span class="linenos" data-line="9" id="mw1A"></span>VISIBLE "U STIL SEE NOTHIN" <span class="linenos" data-line="10" id="mw1Q"></span>TLDR <span class="linenos" data-line="11" id="mw1g"></span> <span class="linenos" data-line="12" id="mw1w"></span>VISIBLE "U SEE THIS" <span class="linenos" data-line="13" id="mw2A"></span>KTHXBYE

The above example will return the following:

<span id="mw3A"></span><span class="go" id="mw3Q">U SEE THIS</span> <span class="go" id="mw3g">U SEE THIS</span>

This is because line 3 outputs U SEE THIS but line 5 is ignored due to the fact that it is commented out by the BTW keyword. Lines 8 and 9 aren't run because they are in a multiline comment that starts in line 7, and ends on line 10. Line 12 outputs U SEE THIS and line 13 terminates the program.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication