LOLCODE

From Esolang
Jump to navigation Jump to search

LOLCODE is a general-purpose programming language being developed to resemble the kitty pidgin natlang of the lolcat phenomenon. It was initially created in 2007, one year after LOLCATS became an Internet meme and development continued for some time before the creator lost interest (or something like that).

Keywords

These are the main keywords. All LOLCODE keywords are written in uppercase.

Code Comment
HAI [version] In all LOLCODE programs, HAI introduces the program and specifies the version (although it isn't used yet).
BTW Comment Introduces a single line comment.
OBTW Comment

TLDR

Introduces a multi line comment.
CAN HAS [library]? Includes a library. Currently (version 1.4) the following libraries are available: STDIO, STRING, SOCKS and STDLIB.
VISIBLE [string] Prints STRING to the standard output stream. If an exclamation mark (!) is added outside the string, no newline will be printed.
GIMMEH [var] Reads a string from the standard input stream into the variable.
I HAS A [var] Declares a variable without a value. Its type will be NOOB.
I HAS A [var] ITZ [value] Declares a variable and assigns a value to it.
I HAS A [var] ITZ A [type] Declares a variable of the specified type and assigns the initial value of that type to it. (See Types)
I HAS A [var] ITZ LIEK A [other var] Declares a variable and copies the contents of the other variable into the newly created variable. This is only valid if the other variable is a BUKKIT.
[var] R [value] Assigns a value to a variable.
BOTH SAEM [expression] AN [expression] Compares two expressions (a variable, a value or another comparison). Returns WIN if both expressions have the same value.
DIFFRINT [expression] AN [expression] Compares two expressions (a variable, a value or another comparison). Returns WIN if both expressions have different values.
BIGGR OF [expression] AN [expression] Returns the bigger of the two given expressions.
SMALLR OF [expression] AN [expression] Returns the smaller of the two given expressions.
SUM OF [expression] AN [expression] Calculates [expression1] + [expression2]
DIFF OF [expression] AN [expression] Calculates [expression1] - [expression2]
PRODUKT OF [expression] AN [expression] Calculates [expression1] * [expression2]
QUOSHUNT OF [expression] AN [expression] Calculates [expression1] / [expression2]
MOD OF [expression] AN [expression] Calculates [expression1] modulo [expression2]
SMOOSH [argument1] AN [argument2] (AN [argument3] (AN [argument4] ...)) MKAY Concatenates the given YARNs.
MAEK [expression] A [type] Explicitly casts the expression to the given type. If the expression is a variable it is not changed. To also change a variable use [var] R MAEK [var] A [type].
[variable] IS NOW A [type] Explicitly casts a variable to the given type. The value of the variable is changed to the new type. This is equivalent to [var] R MAEK [var] A [type].
SRS [var] Interprets a YARN variable as an identifier.
[expression], O RLY?
	YA RLY
		...SOMECODE
	NO WAI
		...SOMECODE
	OIC
An if statement. If the expression can be evaluated to WIN (equivalent of true) the YA RLY branch is executed, otherwise the NO WAI branch is executed.
[expression], WTF?
	OMG [value]
		[code]
	OMG [value]
		[code]
	OMGWTF
		[code]
	OIC
The WTF?-statement compares the expression to the values of the OMG-statements. The values must be distinct and literal, i.e. they mustn't contain any variables or expressions which have to be evaluated at runtime. The OMG block can be followed by any number of statements and can be terminated with GTFO. If an OMG-block is not terminated with GTFO the next OMG will be executed too, regardless of the comparison value. The default case (OMGWTF) is executed when no value matches the expression.
IM IN YR [label] (UPPIN|NERFIN YR [var] (TIL|WILE [expression]))
	...SOMECODE
IM OUTTA YR [label]
Runs the code in the loop. If UPPIN|NERFIN YR [var] is included the loop iterates over the variable increasing (UPPIN) or decreasing (NERFIN) it. If TIL|WILE [expression] is included (may only be included, if an iteration variable is specified) the loop executes until (TIL) or while (WILE) the expression is WIN. The keyword GTFO exits any loop immediately.
KTHXBYE KTHXBYE terminates the program.

Functions

Functions can be declared with HOW IZ I [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...))) [code block] IF U SAY SO

A function can return with one of the following statements:

  • FOUND YR [expression] returns the value of the expression
  • GTFO return without value (NOOB)

A function can be called with I IZ [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...))) MKAY. If the arguments are expressions, they are evaluated before the function is called.

Types

There are currently 6 types available: YARN (string), NUMBR (integer), NUMBAR (float), TROOF (boolean), BUKKIT (array) and NOOB.

NOOB

This is the untyped type. All declared variables without a value are of this type and their value is also NOOB. This type can implicitly only be cast to TROOF and returns FAIL. If explicitly cast to other types it will return the default value for the new type.

TROOF

This is the equivalent of a boolean variable. It can have one of two values: WIN (true) and FAIL (false). When types are cast to TROOF, an empty string (""), an empty array and numerical zero return FAIL. All other values are cast to WIN.

Numerical types

A NUMBR is an integer as specified in the host implementation/architecture. Any contiguous sequence of digits outside of a quoted YARN and not containing a decimal point (.) is considered a NUMBR. A NUMBR may have a leading hyphen (-) to signify a negative number.

A NUMBAR is a float as specified in the host implementation/architecture. It is represented as a contiguous string of digits containing exactly one decimal point. Casting a NUMBAR to a NUMBR truncates the decimal portion of the floating point number. Casting a NUMBAR to a YARN (by printing it, for example), truncates the output to a default of two decimal places. A NUMBAR may have a leading hyphen (-) to signify a negative number.

Casting of a string to a numerical type parses the string as if it were not in quotes. If there are any non-numerical, non-hyphen, non-period characters, then it results in an error. Casting WIN to a numerical type results in "1" or "1.0"; casting FAIL results in a numerical zero.

YARN

YARNs are demarked with double quotation marks ("). A YARN without a closing quote will cause an error. All characters inside a string represent their own value except the colon (:), which is being used as the escape character. The following escape sequences are available:

  • :) is a newline (\n)
  • :> is a tab (\t)
  • :O is a beep (\a)
  • :" is a literal double quote (")
  • :: is a literal colon (:)
  • :([hex]) resolves the hex number into the corresponding Unicode code point.
  • :{[var]} interpolates the current value of the enclosed variable, cast as a string.
  • :[[char name]] resolves the [char name] in capital letters to the corresponding Unicode normative name.

BUKKIT

This type represents an array. It has named slots, which can contain either variables or functions. A BUKKIT can be declared in one of two ways:

BTW declaration of the BUKKIT
I HAS A [object] ITZ A BUKKIT

BTW creating a variable in a slots
[object] HAS A [var] ITZ [value]

BTW creating a function inside the BUKKIT
HOW IZ [object] [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...)))
	[function code]
IF U SAY SO

The other way is:

BTW declaration of the BUKKIT
O HAI IM [object]
	BTW creating a variable in a slot
	I HAS A [var] ITZ [value]

	BTW creating a function inside the BUKKIT
	HOW IZ I [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...)))
		[function code]
	IF U SAY SO
KTHX

A variable inside a BUKKIT can be used with [object]'Z [var]. A function inside a BUKKIT can be called with [object] IZ [function] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...))) MKAY or with I IZ [object]'Z [function] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...))) MKAY

A function inside a BUKKIT may also access variables and other functions of the BUKKIT by using ME'Z [var] or ME IZ [function name] (YR [argument1] (AN YR [argument2] (AN YR [argument3] ...))) MKAY

Examples

Hello World

This program prints "Hai world!" to the standard output.

HAI 1.3
VISIBLE "Hai world!"
KTHXBYE

Loops

This program counts from 0 to 9 and prints the numbers on a single line to the standard output.

HAI 1.3
IM IN YR loop UPPIN YR var TIL BOTH SAEM var AN 10
	VISIBLE SMOOSH var AN " " MKAY!
IM OUTTA YR loop
KTHXBYE

Stack

This program implements a stack-like structure in LOLcode and demonstrates its use.

HAI 1.3
O HAI IM pile
	I HAS A length ITZ 0
	I HAS A max ITZ -1
	
	HOW IZ I pushin YR item
		DIFFRINT ME'Z max AN BIGGR OF ME'Z max AN ME'Z length, O RLY?
			YA RLY, ME HAS A SRS ME'Z length ITZ item, ME'Z max R SUM OF ME'Z max AN 1
			NO WAI, ME'Z SRS ME'Z length R item
		OIC
		ME'Z length R SUM OF ME'Z length AN 1
	IF U SAY SO
	
	HOW IZ I popin
		DIFFRINT ME'Z length AN 0, O RLY?
		YA RLY
			ME'Z length R DIFF OF ME'Z length AN 1
			I HAS A item ITZ ME'Z SRS ME'Z length
			ME'Z SRS ME'Z length R NOOB
			FOUND YR item
		OIC
	IF U SAY SO
	
	HOW IZ I gettinLen
		FOUND YR ME'Z length
	IF U SAY SO
KTHX

I HAS A stack ITZ LIEK A pile

stack IZ pushin YR "testvalue" MKAY
stack IZ pushin YR "value2" MKAY
VISIBLE stack IZ popin MKAY
stack IZ pushin YR "lolcat" MKAY
stack IZ pushin YR "longcat" MKAY
VISIBLE stack IZ popin MKAY
VISIBLE stack IZ popin MKAY
VISIBLE stack IZ popin MKAY

KTHXBYE

Output:

value2
longcat
lolcat
testvalue

Truth-Machine

A truth-machine asks for input, then checks if it equals 1. If true, then output 1s indefinitely, otherwise, output a 0 and terminate the program

HAI 1.3
BTW since LOLCODE doesn't have input, we just use this variable as input
I HAS A input ITZ A TROOF
input R [input]
input, O RLY?
	YA RLY
		IM IN YR loop
			VISIBLE "1"!
		IM OUTTA YR loop
	NO WAI
		VISIBLE "0"
	OIC
KTHXBYE

Criticism

LOLCODE is often criticized for not being Esoteric enough. By design, LOLCODE is actually a normal procedural language behind its lulzy syntax. This is a stark contrast from "True" Esolangs like Befunge, which features a two-dimensional, almost game board-like syntax. For this reason, LOLCODE is technically categorized as a Weirdlang.

An example of how LOLCODE is just a normal language with weird syntax is shown here. This is an identical program to the above "Stack" program, but with nothing changed except the syntax:

BEGIN program VERSION 1.3
CLASS stack{
	ATTR VAR length = 0
	ATTR VAR max = -1
	FUNCTION push item{
		IF THIS.max != (bigger(THIS.max, THIS.length){
			THIS[THIS.length] = item
			THIS.max+1
		} ELSE {
			THIS[THIS.length] = item	
		}
		THIS.length+=1
	}

	FUNCTION pop{
		IF THIS.length != 0{
			length-=1
			item = THIS[THIS.length]
			THIS[THIS.length] = null
			RETURN item
		}
	}
	FUNCTION getLen{
		RETURN THIS.length
	}
}

stack s = new stack

s.push("testvalue")
s.push("value2")
PRINT stack.pop()
s.push("normal human being")
s.push("Basketball player")
PRINT s.pop()
PRINT s.pop()
PRINT s.pop()

END program

Output:

value2
Basketball player
normal human being
testvalue

External resources