Skip to content

Instantly share code, notes, and snippets.

@sharmaeklavya2
Last active October 9, 2024 18:03
Show Gist options
  • Save sharmaeklavya2/8a0e2581baf969be0f64 to your computer and use it in GitHub Desktop.
Save sharmaeklavya2/8a0e2581baf969be0f64 to your computer and use it in GitHub Desktop.
LOLCODE tutorial

LOLCODE

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.

Hello World

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

Comments:

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).

Adder

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

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:

  1. strings (YARN)
  2. integers (NUMBR)
  3. floats (NUMBAR)
  4. 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.

Input

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>.

Operators

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.

Greet a friend

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

Counting

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

Factorial

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

@pm10295
Copy link

pm10295 commented Oct 3, 2018

LOLCode programs have the file extensions of .lol and .lols

@jfacoustic
Copy link

LCI's future branch does have array'z.

@jfacoustic
Copy link

git clone https://github.com/justinmeza/lci --branch future

@OliverCordingl1
Copy link

What is the file extension when you save the lolcode program file??

.lol

@wsatchmo
Copy link

holy shit calm your ass

Agree

@birchtree02
Copy link

birchtree02 commented Sep 24, 2019

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?

@ashleyo
Copy link

ashleyo commented Apr 8, 2020

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?

@ashleyo
Copy link

ashleyo commented Apr 8, 2020

One I can answer! The source code of the compiler used by repl.it is here and a quick inspection reveals that "HOW IZ I" is actually implemented as "HOW DUZ I" (which makes more sense IMO).

Copy link

ghost commented Jul 17, 2021

Does anyone know how to add variables into other variables in LOLCODE? I used this code
I HAS A VAR1 ITZ "abc" I HAS A VAR2 ITZ "def" I HAS A VAR3 ITZ VAR1 VAR2*
And the error I get is:
Line 3: Expected: endline; Got: identifier(VAR2).
Since if you use the VISIBLE function with 2 variables after it it outputs the two variables together.
I assumed it would work the same way when creating a variable, but clearly not. Any ideas?
*Btw the lines were three separate ones.

@AlexDev404
Copy link

@fluffyfluffycake
Does anyone know how to add variables into other variables in LOLCODE? I used this code
I HAS A VAR1 ITZ "abc" I HAS A VAR2 ITZ "def" I HAS A VAR3 ITZ VAR1 VAR2*
And the error I get is:
Line 3: Expected: endline; Got: identifier(VAR2).
Since if you use the VISIBLE function with 2 variables after it it outputs the two variables together.
I assumed it would work the same way when creating a variable, but clearly not. Any ideas?
*Btw the lines were three separate ones.

You forgot to use SMOOSH to concat the strings together.
Use this

HAI 1.3
I HAS A VAR1 ITZ "abc" 
I HAS A VAR2 ITZ "def" 
I HAS A VAR3 ITZ SMOOSH VAR1 AN VAR2 MKAY   BTW - CONCATENATE STRINGS VAR1 AND VAR2 INTO ONE

VISIBLE VAR3

KTHXBYE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment