Friday, October 17, 2008

LOLCODE

LOLCODE is an esoteric programming language inspired by the language expressed in examples of the LOLCAT Internet meme.[1] The language was created in 2007 by Adam Lindsay, researcher at the Computing Department of Lancaster University.[2]

The language is not clearly defined in terms of operator priorities and correct syntax, but several functioning interpreters and compilers already exist. The language has been proven Turing-complete by the implementation of a Brainfuck compiler.[3]

Example:
HAI
CAN HAS STDIO?
VISIBLE "HAI WORLD!"
KTHXBYE [4]

Code Comment
HAI In all LOLCODE programs, HAI (an internet slang misspelling of "hi") introduces the program.
CAN HAS [FILE]? In many programming languages, one of the first statements will be a library#include [stdio standing for standard input/output library]. This command is a tongue in cheek corruption of that, asking if a file is obtainable, obtaining it if possible, and raising an exception if not[5]. It is there primarily for authenticity — in fact, it is ignored in current implementations of LOLCODE. inclusion for common functions such as input and output. Typically this is included by a call such as
VISIBLE [MESSAGE] prints a message to the screen.
KTHXBYE HAI introduces the program, so KTHXBYE (meaning "Okay — thanks — bye!") terminates it.

Example 2:

HAI
BTW This is a BrainFuck interpreter written in LOLCode
BTW It accepts as input a BF program, followed by a "!", followed by any input to the BF program.
BTW Since BrainFuck is turing-complete, this proves that LOLCode is too

I HAS A INSTRUCTIONS BTW Array for BF instructions
I HAS A IPTR BTW Pointer to first empty element in INSTRUCTIONS
LOL IPTR R 0
I HAS A LOOPZ BTW Array of loop start/end addresses
I HAS A LOOPSTACKZ BTW Loop stack for building the above two
I HAS A LSPTR BTW Pointer to first empty element of LOOPSTACKZ
LOL LSPTR R 0

BTW Read in BF instructions, terminated with "!"
IM IN YR CODE
GIMMEH LETTAR IPTR IN MAH INSTRUCTIONS

IZ IPTR IN MAH INSTRUCTIONS LIEK "["?
LOL LSPTR IN MAH LOOPSTACKZ R IPTR
UPZ LSPTR!!
KTHX

IZ IPTR IN MAH INSTRUCTIONS LIEK "]"?
I HAS A STARTPTR
NERFZ LSPTR!!
LOL STARTPTR R LSPTR IN MAH LOOPSTACKZ
LOL STARTPTR IN MAH LOOPZ R IPTR
LOL IPTR IN MAH LOOPZ R STARTPTR
KTHX

IZ IPTR IN MAH INSTRUCTIONS LIEK "!"?
GTFO
NOWAI
UPZ IPTR!!
KTHX
KTHX

BTW Variables for BF's tape
I HAS A LTAPE
I HAS A RTAPE
I HAS A LPTR
LOL LPTR R 0
I HAS A RPTR
LOL RPTR R 0
I HAS A CELL
LOL CELL R 0

BTW Reset instruction pointer to start
LOL IPTR R 0

BTW Start interpreting
IM IN YR LOOP
I HAS A THING
LOL THING R IPTR IN MAH INSTRUCTIONS

BTW Move tape head right
IZ THING LIEK ">"?
LOL LPTR IN MAH LTAPE R CELL
UPZ LPTR!!
IZ RPTR LIEK 0?
LOL CELL R 0
NOWAI
NERFZ RPTR!!
LOL CELL R RPTR IN MAH RTAPE
KTHX
KTHX

BTW Move tape head left
IZ THING LIEK "<"?
LOL RPTR IN MAH RTAPE R CELL
UPZ RPTR!!
IZ LPTR LIEK 0?

No comments: