InputExamples:
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 |
NUMBR,
NUMBAR,
TROOF,
YARN,
NOOB, as these are types (float, int, bool, string, null).
BOTH SAEM 1 AN WIN, O RLY?, use the ',' comma to separate them.
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.
MAEK PRODUKT OF 1 AN 2 A YARN(i.e. (String)(1*2)) (did I do this? I think I did)
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
VISIBLE. If the final argument of
VISIBLEis an exclamation point (!) then a newline is not printed afterwards.
GIMMEH VARNAMEwhich inserts the value (as string) the user provides into the variable called VARNAME
Obviously, being JavaScript, no file IO is available.
I HAS A NUMBERand they are assigned by:
NUMBER R 12
I HAS A NUMBER ITZ 12
NUMBR,
NUMBAR,
TROOF,
YARN,
NOOBand
BUKKITas float, int, bool, string, and null, and array respectively.
WINand
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
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)
IZ VAR1 [BIGGR|SMALLR] THAN VAR2? .... KTHX
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
IM IN YR LOOP ... KTHXwhere '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.
NERFINinstead of
UPPIN
SUM OF,
DIFF OF,
PRODUKT OF,
QUOSHUNT OF,
MOD OF,
BIGGR OF,
SMALLR OFwhich correspond to +, -, *, /, % (modulus), max and min.
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 4is equivalent to (1/2) + (3-4), i.e.
(SUM OF (QUOSHUNT OF 1 AN 2) AN (DIFF OF 3 AN 4))
NOT, which is unary (i.e. it takes only one operand), and
ALL OFand
ANY OFwhich are both nary (i.e. they take an arbitrary number of operands). Their list of operands is terminated by
MKAYor the end of the line.
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
SMOOSHand used as follows:
I HAS A VAR ITZ SMOOSH "STR1" AN "STR2" AN "STR" AN 3 MKAY VISIBLE VAR BTW prints "str1str2str3"
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
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.
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.