Home

a LOLCODE to JavaScript translator based on the 1.2 spec

Show: Input JavaScript Syntax Highlighted LOLCODE About
Note: highlighting is not working with some browsers because the highlighting webservice is on a subdomain, and some browsers don't allow calls to subdomains. Firefox and Chrome seem to work, Opera and IE do not. I'll fix this at some point.

Input


Warning! There's no way I can protect you from executing an infinite loop, so if your code doesn't halt then you might make your browser hang! (If you use Google Chrome then only your current tab will hang)
BTW: error detection is pretty poor so if nothing seems to happen, it might be that your input was illegal and the interpreter crashed on it.

stdout


      

Translated JavaScript

None yet (run a program)

Prettified lolcode

None yet (run a program)

About

HAI

This page implements a rough interpreter for LOLCODE by way of translating the LOLCODE into JavaScript then letting your browser figure it out. It aims to implement the 1.2 specification. It's not complete (or perfect) but it does run a few demo programs. It won't run some code which can be found in various places online because their constructs don't seem to be defined in the spec. In fact most of the available example LOLCODE seems to be a proposal, and there often exist several variations on it.

OBTW

Some things to watch out for:

TLDR

If you want to make this implementation better/worse or just use it for LOLcat related hilarity on your website (it would be fun to expand it to allow basic DOM manipulation), the code is online and free here.

Note: largely untested on Internet Explorer, whose JavaScript engine is fussy at the best of times.

KTHXBYE

TODO list:
LOLCODE cheat sheet for loljs

Comments and Whitespace

Whitespace is largely ignored, but commands should generally be placed one per line. To put multiple commands on one line, separate them with a comma.

Comments can extend until the end of the line if they are opened with BTW:

HAI BTW this is a comment

Or they can be arbitrary length if opened with OBTW:

OBTW  
  this
  is
  a
  multi-line
  comment
TLDR

IO

Output can be printed to the screen by using
VISIBLE
. If the final argument of
VISIBLE
is an exclamation point (!) then a newline is not printed afterwards.

Input can be requested by using
GIMMEH VARNAME
which inserts the value (as string) the user provides into the variable called VARNAME

Obviously, being JavaScript, no file IO is available.

Variables and Types

Variables are declared as follows:
I HAS A NUMBER
and they are assigned by:
NUMBER R 12

They may also be assigned at declaration:
I HAS A NUMBER ITZ 12


LOLCODE recognises the types:
NUMBR
,
NUMBAR
,
TROOF
,
YARN
,
NOOB
and
BUKKIT
as float, int, bool, string, and null, and array respectively.

YARN literals are defined between " and ", but :" is an escape sequence. TROOF literals are
WIN
and
FAIL
(true and false).

Type casting works as follows:

I HAS A NUMBER ITZ 12
BTW x_ R MAEK y_ A TYPE is equivalent to x_ = (TYPE)y_
NUMBER R MAEK NUMBER A YARN   BTW number == "12"
BTW you can also use 'IS NOW A'
NUMBER IS NOW A TROOF BTW number == true
NUMBER IS NOW A NOOB  BTW number == null
NUMBER IS NOW A NUMBAR  BTW number == 0

Arrays

Array support is not defined in the LOLCODE 1.2 spec so I have taken a few liberties. An array is declared with:

I HAS A ARRAY ITZ GOT 1 AN 2 AN 3
BTW ARRAY = [1, 2, 3]
I HAS A NOTHERARRAY ITZ GOT NOTHING
BTW ARRAY = []
I HAS YETANOTHERARRAY ITZ GOT 1 AN NOTHING ELSE
BTW ARRAY = [1]

The last construct is redundant, NOTHING ELSE is not required. Assignment after declaration works almost as usual:

ARRAY R GOT 1 AN 2 AN 3
BTW ARRAY = [1, 2, 3]

Array access works with a single '!':

ARRAY R GOT 1 AN 2 AN 3
VISIBLE ARRAY!0
VISIBLE ARRAY!1

these can also be used for assignment directly into the given indices. This also allows for multi-dimensional arrays

I HAS A ARRAY ITZ GOT NOTHING
ARRAY!0 R GOT FAIL AN WIN
ARRAY!1 R GOT 0 AN 1
BTW ARRAY = [[false, true], [0, 1]]

Arrays and strings have the special 'LEN OF' operator available which returns the number of items they hold (at their first level)

Control

Conditional statements can be created with either
IZ VAR1 [BIGGR|SMALLR] THAN VAR2?  .... KTHX

or
VAR1, O RLY?, YA RLY ... OIC

An example follows:

I HAS A FOOD ITZ "CHEEZBURGER"
BOTH SAEM FOOD AN "CHEEZBURGER", O RLY?
  YA RLY
    VISIBLE "I LIKE CHEEZBURGER :D"
  MEBBE BOTH SAEM FOOD AN "COOKIE"
    VISIBLE "I LIKE COOKIE ::)"
  NO WAI
    VISIBLE "I WANT CHEEZBURGER :("
OIC

This could be re-written as a WTF? (switch) expression:

I HAS A FOOD ITZ "CHEEZBURGER"
FOOD, WTF?
  OMG "CHEEZBURGER"
    VISIBLE "I LIKE CHEEZBURGER :D"
    GTFO       BTW break
  OMG "COOKIE"  
    VISIBLE "I LIKE COOKIE ::)"
    GTFO
  OMGWTF    BTW default
    VISIBLE "I WANT CHEEZBURGER :("
OIC

Looping

Loops are simply defined as:
IM IN YR LOOP ... KTHX
where 'loop' may be any arbitrary sequence of letters. This creates an infinite loop which must be exited explicitly with
GTFO
. More complex loops can be created as follows:
IM IN YR LOOP UPPIN YR COUNTER WILE COUNTER SMALLR THAN 10
  VISIBLE COUNTER
IM OUTTA YR LOOP

or:

IM IN YR LOOP UPPIN YR COUNTER TIL BOTH SAEM COUNTER AN 10
  VISIBLE COUNTER
IM OUTTA YR LOOP
, where 'COUNTER' is just a variable identifier and anything after 'til' or 'wile' is an expression. If counter was not previously declared, it's set to integer 0.
If 'COUNTER' (or whatever) already exists and is set, you can set the loop action to decrement with
NERFIN
instead of
UPPIN

Operators

The language defined arithmetic operators are:
SUM OF
,
DIFF OF
,
PRODUKT OF
,
QUOSHUNT OF
,
MOD OF
,
BIGGR OF
,
SMALLR OF
which correspond to +, -, *, /, % (modulus), max and min.

Boolean (logic operators are):
BOTH SAEM
,
DIFFRINT
,
BOTH OF
,
EITHER OF
,
WON OF
,
NOT
,
ALL OF
,
ANY OF
, which correspond to ==, !=, &&, ||, exclusive or, not, nary && and nary ||.

Generally operators use prefix notation and are assembled as follows:

SUM OF 1 AN 2
, or more complex:
BOTH SAEM SUM OF 1 AN 2 AN 3 BTW this evaluates to WIN

The expressions' precedence is such that the expressions are evaluate left to right and may nest

SUM OF QUOSHUNT OF 1 AN 2 AN DIFF OF 3 AN 4
is equivalent to (1/2) + (3-4), i.e.
(SUM OF (QUOSHUNT OF 1 AN 2) AN (DIFF OF 3 AN 4))


These are all binary operators (i.e. they take two operands) except for
NOT
, which is unary (i.e. it takes only one operand), and
ALL OF
and
ANY OF
which are both nary (i.e. they take an arbitrary number of operands). Their list of operands is terminated by
MKAY
or the end of the line.

Greater than and less than can be achieved by combining the above operators:
BOTH SAEM X AN [BIGGR|SMALLR] OF X AN Y
, but there also exist
IZ X BIGGR THAN Y?
and
IZ X SMALLR THAN Y?
. These both create if statements with their condition and must be terminated with
KTHX


Finally, the concatenation operator is the nary
SMOOSH
and used as follows:
I HAS A VAR ITZ SMOOSH "STR1" AN "STR2" AN "STR" AN 3 MKAY
VISIBLE VAR BTW prints "str1str2str3"

Functions

Functions can be defined like this:

HOW DUZ I ADD YR NUM1 AN YR NUM2
  I HAS A SUM ITZ SUM OF NUM1 AN NUM2
  FOUND YR SUM  BTW returns sum
IF U SAY SO

This has defined a function 'add' with two arguments. It can be called like so:

I HAS A SUM ITZ ADD 1 2
VISIBLE SUM BTW prints 3

Functions are evaluated in the same way as operators, so the return value of a function may be nested inside an operator and vice versa, e.g.:

I HAS A NUM ITZ ADD 1 AN DIFF OF 1 AN 2
VISIBLE NUM  BTW prints NUM = 1 + (1-2) = 0

Further, if one defines a function which takes zero arguments it looks like a constant:

HOW DUZ I PI
  FOUND YR 3.14159
IF U SAY SO

VISIBLE QUOSHUNT OF PI AN 2 BTW prints 1.570795

Escape sequences

In LOLCODE escape sequences inside strings begin with a colon ':'. LOLCODE defines the following escape sequences for use inside strings:

":""    BTW An escaped quote
":)"    BTW newline
":>"    BTW tab
":o"    BTW bell (not implemented here)
"::"    BTW colon character.
":{VAR}"  BTW variable interpolation (inserts the string value of VAR)
":(HEX)"  BTW inserts a unicode character with the given hex code

Named unicode sequences are not yet supported.

Topics

Comments and Whitespace | IO | Variables and Types | Control and Loops | Operators | Functions | Escape Sequences

News and stuff

25/08/12 -- HEY YOUTUBE. What's up. My hosting deal isn't the best and you're putting my site under a lot of load. But it's also not the worst, and it seems to be dealing with it pretty well. The stress point is the syntax highlighting which uses a webservice to another site (on the same host). If you find that stops working, don't worry too much.

23/02/11 -- I notice this page has been surprisingly popular, so I've put in a bit of effort into making it easier for you to use. You can now open the cheatsheet in a new window instead of having to balance the small one. You need a _real browser_ to see this, but there's limited support on IE8 as well. Be warned that your back and forward buttons won't work within the window because JavaScript's handling of new windows is HORRIBLE. Also my source code is now truly a mess, so I hope you appreciate it.

OBTW if any of you are interested in PHP-based syntax highlighting for LOLCODE (or a bunch of other languages), check out Luminous (that's what's powering the highlighting on the `JavaScript' and `Syntax Highlighted LOLCODE' tabs). Also if anyone is interested in more useful JavaScript, I have a bunch of other stuff online, including a a KDE-4 style notification framework and a Pythonic utility library.