LOLCODE is an esoteric programming language inspired by lolcat memes on the internet. The language was created in 2007 by Adam Lindsay, researcher at Lancaster University's Computing Department.
Here I have explained basics of LOLCODE with complete examples. However, you may have questions even after reading them because I have not included all details. For an exhaustive reference, read the official documentation.
HAI 1.2
BTW This is the famous 'Hello World' program
VISIBLE "Hello World"
KTHXBYE
This is a hello world program in LOLCODE.
All programs begin with the line HAI <lolcode language specification version number>
. Currently we're using LOLCODE 1.2, so all programs should begin with HAI 1.2
. All programs end with KTHXBYE
(Short for "Okay, Thanks, Bye")
To print something, write VISIBLE <whatever you want to print>
Indentation and spaces are irrelevant in LOLCODE. LOLCODE is case-sensitive. All keywords in this language are uppercase.
You can put multiple statements on a single line by using commas
HAI 1.2, VISIBLE "Hello World", KTHXBYE
Single-line comments: Anything after BTW
till the end of the line is treated as a comment (like //
in C, C++, Java).
Multiple line comments: Anything between OBTW
and TLDR
is treated as a comment (like /*
and */
in C, C++, Java).
HAI 1.2
BTW computes the sum of 2 numbers
I HAS A x
I HAS A y
I HAS A s
GIMMEH x
GIMMEH y
x IS NOW A NUMBR
y IS NOW A NUMBR
s R SUM OF x AN Y
VISIBLE s
KTHXBYE
Variables are declared using the syntax I HAS A <variable_name>
. To declare and assign in the same step, syntax is I HAS A <variable_name> ITZ <value>
. For eg, I HAS A x ITZ 3
declares a variable x
with value 3.
To assign to a variable, the syntax is <variable> R <value>
. For eg, x R 3
assigns the value 3 to x
.
LOLCODE is a dynamically typed language, so variables don't have fixed types. A variable's type is the same as the type of the value it is currently storing. The variable types that LOLCODE currently recognizes are:
- strings (
YARN
) - integers (
NUMBR
) - floats (
NUMBAR
) - booleans (
TROOF
)
Until a variable is given an initial value, it is untyped (NOOB
). TROOF
variables can have only 2 values, WIN
and FAIL
.
Unfortunately, arrays are not supported by LOLCODE as of now.
GIMMEH <variable_name>
takes input from the user (stdin) as a YARN and stores it in a variable. To convert it to some other type write <variable> IS NOW A <new_type>
.
Mathematical operators and functions rely on prefix notation.
Calling unary operators then has the following syntax <operator> <expression1>
The AN
keyword can optionally be used to separate arguments, so a binary operator expression has the following syntax, <operator> <expression1> [AN] <expression2>
The basic math operators are binary prefix operators.
SUM OF <x> AN <y> BTW +
DIFF OF <x> AN <y> BTW -
PRODUKT OF <x> AN <y> BTW *
QUOSHUNT OF <x> AN <y> BTW /
MOD OF <x> AN <y> BTW modulo
BIGGR OF <x> AN <y> BTW max
SMALLR OF <x> AN <y> BTW min
<x>
and <y>
may each be expressions in the above, so mathematical operators can be nested and grouped indefinitely.
Boolean operators working on TROOFs:
BOTH OF <x> [AN] <y> BTW and: WIN iff x=WIN, y=WIN
EITHER OF <x> [AN] <y> BTW or: FAIL iff x=FAIL, y=FAIL
WON OF <x> [AN] <y> BTW xor: FAIL if x=y
NOT <x> BTW unary negation: WIN if x=FAIL
Comparison operators:
BOTH SAEM <x> [AN] <y> BTW WIN iff x == y
DIFFRINT <x> [AN] <y> BTW WIN iff x != y
There are (currently) no special numerical comparison operators. Greater-than and similar comparisons are done using the minimum and maximum operators.
BOTH SAEM <x> AN BIGGR OF <x> AN <y> BTW x >= y
BOTH SAEM <x> AN SMALLR OF <x> AN <y> BTW x <= y
DIFFRINT <x> AN SMALLR OF <x> AN <y> BTW x > y
DIFFRINT <x> AN BIGGR OF <x> AN <y> BTW x < y
A bare expression without any assignment, is a legal statement in LOLCODE. Aside from any side-effects from the expression when evaluated, the final value is placed in the temporary variable IT
. IT
's value remains in local scope and exists until the next time it is replaced with a bare expression.
HAI 1.2
BTW Greets a friend
I HAS A animal
GIMMEH animal
BOTH SAEM animal AN "cat"
O RLY?
YA RLY
VISIBLE "Hello cat"
VISIBLE "Nice to meet you"
MEBBE BOTH SAEM animal AN "mouse"
VISIBLE "Hello mouse"
VISIBLE "Nice to eat you"
NO WAI
VISIBLE "Hello stranger"
OIC
KTHXBYE
Syntax for conditional branching (known as if-else in most languages) in LOLCODE is
<expression>
O RLY?
YA RLY
<code block>
[MEBBE <expression>
<code block>]
[MEBBE <expression>
<code block>]
...
[NO WAI
<code block>]
OIC
Syntax for looping
IM IN YR LOOP [UPPIN|NERFIN YR <variable> [TIL|WILE <expression>]]
<code block>
IM OUTTA YR LOOP
You can explicitly break out of loops using the keyword GTFO
(short for Get The F*** Out)
For loop example:
HAI 1.2
I HAS A n
GIMMEH n
n IS NOW A NUMBR
IM IN YR LOOP UPPIN YR i TIL BOTH SAEM i AN n
VISIBLE SUM OF i AN 1
IM OUTTA YR LOOP
KTHXBYE
Here i
is a temporary variable which starts at 0
while loop example:
HAI 1.2
I HAS A i ITZ 1
I HAS A n
GIMMEH n
n IS NOW A NUMBR
IM IN YR LOOP
VISIBLE i
BOTH SAEM i AN n
O RLY?, YA RLY, GTFO, OIC
i R SUM OF i AN 1
IM OUTTA YR LOOP
KTHXBYE
HAI 1.2
HOW IZ I factorial YR n
I HAS A prod ITZ 1
IM IN YR LOOP UPPIN YR i TIL BOTH SAEM i AN n
prod R PRODUKT OF prod AN SUM OF i AN 1
IM OUTTA YR LOOP
FOUND YR prod
IF U SAY SO
I HAS A n
GIMMEH n
n IS NOW A NUMBR
VISIBLE I IZ factorial YR n MKAY
KTHXBYE
For more info, read up the official documentation
I haven't managed to get functions to work on repl.it
This is the code I am using:
HAI 1.2
HOW IZ I yeet
VISIBLE "HAI"
IF U SAY SO
KTHXBYE
I get the error
Line 3: Expected: endline; Got: identifier(IZ).
Any ideas why it's not working?