LOLcode is Turing Complete!

De meer studentikoze onderwerpen komen hier te staan.
Exhange students: Present yourself!

Moderator: Praesidium

User avatar
Foundation
Posts: 622

LOLcode is Turing Complete!

Post#1 » Wed Jan 23, 2008 3:36 pm

Dacht 'k effe stof te hebben voor ne schone paper, maar iemand was me een goei 6 maand geleden al voor :P

Het bewijs is echter prachtig: een BrainFuck interpreter in LOLcode!

Code: Select all


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
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
LSPTR R 0

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

BOTH SAEM IPTR IN MAH INSTRUCTIONS AN "[", O RLY?
YA RLY
LSPTR IN MAH LOOPSTACKZ R IPTR
LSPTR R SUM OF LSPTR AN 1
OIC

BOTH SAEM IPTR IN MAH INSTRUCTIONS AN "]", O RLY?
YA RLY
I HAS A STARTPTR
LSPTR R DIFF OF LSPTR AN 1
STARTPTR R LSPTR IN MAH LOOPSTACKZ
STARTPTR IN MAH LOOPZ R IPTR
IPTR IN MAH LOOPZ R STARTPTR
OIC

BOTH SAEM IPTR IN MAH INSTRUCTIONS AN "!", O RLY?
YA RLY
GTFO
NO WAI
IPTR R SUM OF IPTR AN 1
OIC
IM OUTTA YR CODE

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

BTW Reset instruction pointer to start
IPTR R 0

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

BTW Move tape head right
BOTH SAEM THING AN ">", O RLY?
YA RLY
LPTR IN MAH LTAPE R CELL
LPTR R SUM OF LPTR AN 1
BOTH SAEM RPTR AN 0, O RLY?
YA RLY
CELL R 0
NO WAI
RPTR R DIFF OF RPTR AN 1
CELL R RPTR IN MAH RTAPE
OIC
OIC

BTW Move tape head left
BOTH SAEM THING AN "<", O RLY?
YA RLY
RPTR IN MAH RTAPE R CELL
RPTR R SUM OF RPTR AN 1
BOTH SAEM LPTR AN 0, O RLY?
YA RLY
CELL R 0
NO WAI
LPTR R DIFF OF LPTR AN 1
CELL R LPTR IN MAH LTAPE
OIC
OIC

BTW Increment
BOTH SAEM THING AN "+", O RLY?
YA RLY
CELL R SUM OF CELL AN 1
OIC

BTW Decrement
BOTH SAEM THING AN "-", O RLY?
YA RLY
CELL R DIFF OF CELL AN 1
OIC

BTW Output produces numbers instead of ASCII characters
BOTH SAEM THING AN ".", O RLY?
YA RLY
VISIBLE CELL!
VISIBLE " "!
OIC

BTW Input doesn't work because we can't convert characters to integers
BTW Oh well, it doesn't stop it being turing complete

BTW Start of loop
BOTH OF BOTH SAEM THING AN "[" AN BOTH SAEM CELL AN 0, O RLY?
YA RLY
IPTR R IPTR IN MAH LOOPZ
OIC

BTW End of loop
BOTH OF BOTH SAEM THING AN "]" AN DIFFRINT CELL AN 0, O RLY?
YA RLY
IPTR R IPTR IN MAH LOOPZ
OIC

BTW End of program!
BOTH SAEM THING AN "!", O RLY?
YA RLY
GTFO
OIC

IPTR R SUM OF IPTR AN 1
IM OUTTA YR LOOP
KTHXBYE
bron: http://forum.lolcode.com/viewtopic.php?id=51

Return to “Algemeen”

Who is online

Users browsing this forum: No registered users and 85 guests