LOLCODE

Appeared in:
2007
Paradigm:
Typing discipline:
File extensions:
*.lol
Versions and implementations (Collapse all | Expand all):
Programming language

LOLCODE is an esoteric programming language created under the influence of a meme lolcat.

The language was created in 2007 by Adam Lindsay. The original description of the language was rather vague, so the existing implementations (quite numerous, by the way) differ a lot. Several implementations of the language, like LOLCode.NET and loljs, are proven to be Turing-complete by creating a Brainfuck interpreter in it.

The language is based on elements of Internet slang, so anybody who knows it can figure out what the programs do even without deep knowledge of the language.

Language constructs:

  • The program is enclosed in keywords HAI and KTHXBYE. HAI can be followed by the version of language specification implemented, but it’s unclear how the interpreter should process it.

  • Comments are provided as BTW (a comment till the end of line) and OBTW ... TLDR (a multi-line comment).

  • I HAS A <varname> declares a variable. LOLCODE is dynamically typed, so there’s no need to declare the variable type explicitly.

  • <varname> R <value> assigns a value to a variable. LOLCODE supports the following data types: NOOB (untyped — this value is help by a variable before it is initialized), NUMBR (integer), NUMBAR (floating-point), YARN (string) и TROOF (boolean). BUKKIT type is reserved for arrays. Types can be cast implicitly.

  • functions and operators use prefix notation. If the number of function arguments can vary, their list must be delimited with MKAY keyword. Idividual arguments can be separated with an optional AN keyword.

  • math operators (work with NUMBR and NUMBAR data types, if needed, can convert from YARN):

    SUM OF — addition
    DIFF OF — subtraction
    PRODUKT OF — multiplication
    QUOSHUNT OF — division
    MOD OF — division remainder
    BIGGR OF — maximum
    SMALLR OF — minimum
    
  • boolean operators (work with TROOF data type which can have values WIN and FAIL):

    BOTH OF — logical "and"
    EITHER OF — logical "or"
    WON OF — xor
    NOT — logical "not"
    ALL OF ... MKAY — logical "and" for arbitrary number of arguments
    ANY OF ... MKAY — logical "or" for arbitrary number of arguments
    
  • comparison operators: BOTH SAEM for equality and DIFFRINT for inaquality. The current version of language specification provides no support for other types of comparison, so they have to be constructed using BIGGR OF and SMALLR OF.

  • SMOOSH … MKAY is string concatenation.

  • MAEK <expression> [A] <type> converts the value of the given expression to the given data type. To change type of a variable, use <variable> IS NOW A <type>.

  • VISIBLE prints an arbitrary number of arguments, which are converted to YARN data type and concatenated. By default each output ends with a newline; to avoid this, one has to add ! to the end of the list of arguments.

  • GIMMEH reads a string from input stream.

  • implicit variable IT is used to store the value of the last expression mentioned and to process it in flow control statements.

  • basic if-then-else construct looks as follows:

    <expression>
    O RLY?
      YA RLY
        <code block>
      NO WAI
        <code block>
    OIC
    
  • case construct looks like this:

    <expression>
    WTF?
      OMG <value literal>
        <code block>
     [OMG <value literal>
        <code block> …]
     [OMGWTF
        <code block>]
    OIC
    

    Comparisons use the IT variable and a constant. OMG block can contain any number of commands and might end with GTFO. If there is no GTFO command, the next comparison is done, and so on.

  • infinite loop:

    IM IN YR <label>
      <code block>
    IM OUTTA YR <label>
    
  • while loop:

    IM IN YR <label> <operation> YR <variable> [TIL|WILE <expression>]
      <code block>
    IM OUTTA YR <label>
    

    The loop uses a temporary local variable and any unary operation that changes it, including UPPIN and NERFIN (increment and decrement). The expression is used as a condition of end of loop: TIL stops as soon as it becomes true (WIN), WILE — false (FAIL).

  • function declaration:

    HOW DUZ I <function name> [YR <argument1> [AN YR <argument2> …]]
      <code block>
    IF U SAY SO
    

    There are three ways to return from a function. FOUND YR <expression> returns the value of this expression. GTFO returns NOOB. If the execution flow reached the end of the function without returning, the return value is the value of IT variable.

  • GTFO is an equivalent of break in other languages; it is used to break out from loops, case statement and functions.

Elements of syntax:

Inline comments BTW
Non-nestable comments OBTW ... TLDR
Case-sensitivity yes
Variable identifier regexp [a-zA-Z]*
Variable assignment <varname> R <value>
Variable declaration I HAS A <varname>
Physical (shallow) equality BOTH SAEM
Physical (shallow) inequality DIFFRINT
If - then <expression> O RLY? YA RLY <code block> OIC
If - then - else <expression> O RLY? YA RLY <code block> NO WAI <code block> OIC
Loop forever IM IN YR <label> ... IM OUTTA YR

Examples:

Hello, World!:

Example for versions loljs 1.1
HAI
  CAN HAS STDIO?
  VISIBLE "Hello, World!"
KTHXBYE

Factorial:

Example for versions loljs 1.1

This example uses iterative factorial definition.

HAI
  I HAS A N ITZ 0
  I HAS A FACTORIAL ITZ 1
  IM IN YR LOOP UPPIN YR N WILE N SMALLR THAN 17
    VISIBLE SMOOSH N "! = " FACTORIAL
    FACTORIAL R PRODUKT OF FACTORIAL AN SUM OF N AN 1
  IM OUTTA YR LOOP
KTHXBYE

Factorial:

Example for versions loljs 1.1

This example uses recursive factorial calculation.

HAI
  HOW DUZ I FACTORIAL N
    BOTH SAEM 0 AN N
    O RLY?
      YA RLY
        FOUND YR 1
      NO WAI
        FOUND YR PRODUKT OF N AN FACTORIAL DIFF OF N AN 1
    OIC
  IF U SAY SO
  
  I HAS A N ITZ 0
  IM IN YR LOOP UPPIN YR N WILE N SMALLR THAN 17
    VISIBLE SMOOSH N "! = " FACTORIAL N
  IM OUTTA YR LOOP
KTHXBYE

Fibonacci numbers:

Example for versions loljs 1.1
HAI
  I HAS A I ITS 0
  I HAS A FIB1 ITS 0
  I HAS A FIB2 ITS 1
  IM IN YR LOOP UPPIN YR I TIL BOTH SAEM I AN 16
    VISIBLE SMOOSH FIB2 ", "!
    I HAS A FIB3 ITS SUM OF FIB1 AN FIB2
    FIB1 R FIB2
    FIB2 R FIB3
  IM OUTTA YR LOOP
  VISIBLE "..."
KTHXBYE

Fibonacci numbers:

Example for versions loljs 1.1
HAI
  HOW DUZ I FIBONACCI N
    BOTH SAEM 1 AN BIGGR OF N AN 1, O RLY?
      YA RLY
        FOUND YR 1
      NO WAI
        FOUND YR SUM OF FIBONACCI DIFF OF N AN 1 AN FIBONACCI DIFF OF N AN 2
    OIC
  IF U SAY SO
  
  I HAS A N ITZ 0
  IM IN YR LOOP UPPIN YR N WILE N SMALLR THAN 16
    VISIBLE SMOOSH FIBONACCI N ", "!
  IM OUTTA YR LOOP
  VISIBLE "..."
KTHXBYE

Quadratic equation:

Example for versions loljs 1.1
HAI
  HOW DUZ I SQRT X
    I HAS A X_N ITZ 10
    I HAS A LIMIT ITZ 100
    I HAS A COUNTER ITZ 0
    IM IN YR LOOP UPPIN YR COUNTER WILE COUNTER SMALLR THAN LIMIT
      I HAS A TERM ITZ QUOSHUNT OF X AN X_N
      TERM R SUM OF X_N AN TERM
      TERM R QUOSHUNT OF TERM AN 2
      X_N R TERM
    IM OUTTA YR LOOP
    FOUND YR X_N
  IF U SAY SO

  I HAS A AC
  GIMMEH AC
  AC IS NOW A NUMBR
  BOTH SAEM AC AN 0, O RLY?
    YA RLY
      VISIBLE "Not a quadratic equation."
    NO WAI
      I HAS A BC
      GIMMEH BC
      BC IS NOW A NUMBR
      I HAS A CC
      GIMMEH CC
      CC IS NOW A NUMBR
      I HAS A D ITZ DIFF OF PRODUKT OF BC AN BC  AN PRODUKT OF 4 AN PRODUKT OF AC AN CC
      BOTH SAEM D AN 0, O RLY?
        YA RLY
          VISIBLE SMOOSH "x = " QUOSHUNT OF BC AN PRODUKT OF -2 AN AC
        NO WAI
          BOTH SAEM 0 AN SMALLR OF 0 AN D, O RLY?
            YA RLY
              VISIBLE SMOOSH "x1 = " QUOSHUNT OF SUM OF BC AN SQRT D AN PRODUKT OF -2 AN AC
              VISIBLE SMOOSH "x2 = " QUOSHUNT OF DIFF OF BC AN SQRT D AN PRODUKT OF -2 AN AC
            NO WAI
              D R PRODUKT OF D AN -1
              VISIBLE SMOOSH "x1 = (" QUOSHUNT OF BC AN PRODUKT OF -2 AN AC ", " QUOSHUNT OF SQRT D AN PRODUKT OF -2 AN AC ")"
              VISIBLE SMOOSH "x2 = (" QUOSHUNT OF BC AN PRODUKT OF -2 AN AC ", " QUOSHUNT OF SQRT D AN PRODUKT OF 2 AN AC ")"
          OIC
      OIC
  OIC
KTHXBYE

CamelCase:

Example for versions loljs 1.1

Note that LOLCODE has no character processing functions, except for concatenation, so you can’t compare characters or get their ASCII-codes. This means that to figure out whether a character is a letter you need to create a “dictionary” of all letters and just look it up.

HAI
  I HAS A UPP ITZ "QWERTYUIOPASDFGHJKLZXCVBNM"
  I HAS A LOW ITZ "qwertyuiopasdfghjklzxcvbnm"

  HOW DUZ I LOWER CHAR
    I HAS A I ITZ 0
    IM IN YR LOOP UPPIN YR I TIL BOTH SAEM I AN LEN OF UPP
      BOTH SAEM UPP!I AN CHAR, O RLY?
        YA RLY
          FOUND YR LOW!I
      OIC
    IM OUTTA YR LOOP
    FOUND YR CHAR
  IF U SAY SO

  HOW DUZ I UPPER CHAR
    I HAS A I ITZ 0
    IM IN YR LOOP UPPIN YR I TIL BOTH SAEM I AN LEN OF UPP
      BOTH SAEM LOW!I AN CHAR, O RLY?
        YA RLY
          FOUND YR UPP!I
      OIC
    IM OUTTA YR LOOP
    FOUND YR CHAR
  IF U SAY SO

  HOW DUZ I ISLOWER CHAR
    I HAS A I ITZ 0
    IM IN YR LOOP UPPIN YR I TIL BOTH SAEM I AN LEN OF UPP
      BOTH SAEM LOW!I AN CHAR, O RLY?
        YA RLY
          FOUND YR WIN
      OIC
    IM OUTTA YR LOOP
    FOUND YR FAIL
  IF U SAY SO


  I HAS A TEXT
  GIMMEH TEXT
  I HAS A CAMELCASE ITZ ""
  I HAS A I ITZ 0
  I HAS A SPACE ITZ WIN
  IM IN YR LOOP UPPIN YR I TIL BOTH SAEM I AN LEN OF TEXT
    I HAS A CHAR ITZ LOWER TEXT!I
    ISLOWER CHAR, O RLY?
      YA RLY
        BTW this is a letter (already lowercase), modify depending on SPACE
        SPACE, O RLY?
          YA RLY
            CHAR R UPPER CHAR
        OIC
        CAMELCASE R SMOOSH CAMELCASE CHAR
        SPACE R FAIL
      NO WAI
        BTW this is space - mark it
        SPACE R WIN
    OIC
  IM OUTTA YR LOOP
  VISIBLE CAMELCASE
KTHXBYE