Lolcode - Types


Advertisements


LOLCODE is designed to test the boundaries of the programming language design. It is an esoteric programming language inspired by the funny things on the Internet. This chapter gives you an understanding of LOLCODE types.

Types

Currently, the variable types in LOLCODE are −

  • strings (YARN)
  • integers (NUMBR)
  • floats (NUMBAR)
  • and booleans (TROOF)
  • Arrays (BUKKIT)

In LOLCODE the variable type is handled dynamically by the compiler. If a variable does not have an initial value, it is called untyped (known as NOOB in LOLCODE).

The syntax for declaring and using different types in LOLCODE is shown below −

To create a variable of any data type

I HAS A <VARIABLE> ITZ A <DATA TYPE>

To create a variable and assign a value to it

I HAS A <VARIABLE> ITZ <EXPRESSION<

To assign a value to an already created data type

<VARIABLE> R <EXPRESSION>

Untyped (NOOB)

The untyped data type (known as NOOB), cannot be converted into any other type except into a TROOF data type. The implicit casting of a NOOB into TROOF makes the variable FAIL. After that any operation on a NOOB results in an error.

Explicit casts of a NOOB data type (i.e. the types that are uninitialized and do not have any initial value) variable results to zero values for all other types.

To define an untyped variable, just declare a variable and assign a value as shown in this example −

Live Demo
HAI 1.2
I HAS A VAR3
VAR3 R "ANYVALUE"
VISIBLE VAR3

BTW Or declare in same line
I HAS A VAR4 ITZ 44
VISIBLE VAR4
KTHXBYE

When you run the above program, you will find the following result −

sh-
4.3$ lci main.lo 
ANYVALUE
44

Booleans (TROOFS)

In LOLCODE, the Boolean values are of two types. BOOLEAN generally have two values- true and false. But, in LOLCODE, the Boolean is known as TROOF, and the true/ false values are known as WIN/FAIL respectively. All the uninitialized values like an empty string (""), or an empty array will all cast to FAIL. All other initialized values evaluate to WIN.

Example

Live Demo
HAI 1.2
I HAS A VAR3 ITZ A TROOF
VAR3 R "FAIL"
   VISIBLE VAR3
KTHXBYE

You can see the following output when you execute the above code −

sh-4.3$ lci main.lo
FAIL

Numerical Types (NUMBR)

In LOLCODE, a NUMBR stands for an integer. Any sequence of digits is considered as a NUMBR, unless it has a decimal appearing anywhere in between the sequence. To make any number negative, it may be preceded by a hyphen (-) which signifies a negative number.

Example

Live Demo
HAI 1.2
I HAS A VAR3 ITZ A NUMBR
   VISIBLE VAR3
KTHXBYE

The above code shows you the following result when you run it−

sh- 
4.3$ lci main.lo
0

Similar to NUMBR, LOLCODE has another data type, which represents a decimal or a float in many programming languages. In LOLCODE, a NUMBAR is a float containing one decimal point. Casting a NUMBAR to a NUMBR truncates the decimal portion of the floating point number and returns it as a NUMBR, without any decimal.

Strings (YARN)

In LOLCODE, value containing strings, i.e. string literals (YARN) should start and end with double quotation marks ("”).

Anything may be written inside the string, like space, comma, full stop, exclamation or any other symbol. A string where any single quote is missing may cause an error. Colons are used as escape characters in LOLCODE, and any value following a colon gets a special meaning.

  • :) − A closing bracket following a colon represents a newline (\n)

  • :> − A closing angle bracket following a colon represents a tab (\t)

  • :o − A ‘o’ character following a colon represents a bell (beep) (\g)

  • :" − A “ following a colon represents a literal double quote (")

  • :: − A colon following a colon represents a single literal colon (:)

Example

Live Demo
HAI 1.2
I HAS A VAR3 ITZ A YARN
VAR3 R "XYZ"
   VISIBLE VAR3
KTHXBYE

The code given above produces the following output upon execution −

sh-
4.3$ lci main.lo 
XYZ

BUKKIT

This type represents an array. It has named slots, which can contain either variables or functions. A BUKKIT can be declared in the following way −

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

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.

Example

Live Demo
HAI 1.2
   I HAS A VAR6 ITZ A BUKKIT
   BTW DECLARING AN ARRAY
   VAR6 HAS A VAR7 ITZ "DOGE"
   
   BTW VAR7 IS A STRING VARIABLE THAT IS INSERTED  INTO ARRAY VAR6
   VISIBLE VAR6'Z VAR7
   BTW GET THE ELEMENT OF ARRAY
KTHXBYE

This is the output you will find when you run the code given above −

sh-
4.3$ lci main.lo 
DOGE


Advertisements
E-Books Store