Overview

Here's a really quick and nasty PHP LOLCODE interpreter. It does the job of a 'hello world' example but it's no where near finished and should not be taken as beautiful code. More over, if you base your entire compiler on this method of implementation, your children will suffer. Feel free to play around with it though. :-)

Supported Commands

The following commands are currently recognised and parsed correctly (on a line by line basis.):

  • HAI
  • VISIBLE <string|var>
  • I HAS A <var>
  • I IZ SLEEPIN[!!]<seconds>
  • <var> IZ <value>
  • UP <var>[!!int]
  • NERF <var>[!!int]
  • BTW [comment]
  • KTHXBAI

Source Code

<?php
function lol($b) {
   $d=explode("\n",$b); $n=count($d); $e="% OH NOES! "; $a=array();
   if($n<1) { print($e."NOT ENUF DATA\n"); exit; }
   if($d[0]!="HAI") { print($e."MISSIN HAI\n"); exit; }

   for($i=1;$i<$n;$i++) { $d[$i]=trim($d[$i]); if(substr($d[$i],0,3)!="BTW") {
      if(substr($d[$i],0,3)=="HAI") { print($e."TOO MANY HAI\n"); break; }
      if(substr($d[$i],0,7)=="KTHXBAI") { print("% KTHXBYE.\n"); break; }
      if(substr($d[$i],0,7)=="VISIBLE") {
         if(!ereg("(\")+",$d[$i])) { print($a[trim(substr($d[$i],8))]."\n"); }
         else { print(trim(substr($d[$i],9,-1))."\n"); }
      }
      if(substr($d[$i],0,12)=="I IZ SLEEPIN") { $d[$i]=ereg_replace("[A-Za-z!]+","",$d[$i]); sleep(trim($d[$i])); }
      if(substr($d[$i],0,7)=="I HAS A") { $a[trim(substr($d[$i],8))]="null"; }
      if(strstr($d[$i],' IZ ')) { $v=explode(" IZ ",$d[$i]); $a[$v[0]]=$v[1]; }
      if(substr($d[$i],0,2)=="UP") { if(strstr($d[$i],'!!')) { $v=explode("!!",substr($d[$i],3)); $a[$v[0]]=$a[$v[0]]+$v[1]; } else { $a[substr($d[$i],3)]++; } }
      if(substr($d[$i],0,2)=="NERF") { if(strstr($d[$i],'!!')) { $v=explode("!!",substr($d[$i],3)); $a[$v[0]]=$a[$v[0]]-$v[1]; } else { $a[substr($d[$i],3)]--; } }
   } }
}
?>

Example Usage

<? $foo = <<<END
HAI
   BTW comments work now, ja?
   VISIBLE "I HAS A FIVE SECONDZ COUNTER!"
   I HAS A cookie
   I IZ SLEEPIN!!1
   cookie IZ 1
   VISIBLE cookie
   I IZ SLEEPIN!!1
   UP cookie!!1
   VISIBLE cookie
   I IZ SLEEPIN!!1
   UP cookie!!1
   VISIBLE cookie
   I IZ SLEEPIN!!1
   UP cookie!!1
   VISIBLE cookie
   I IZ SLEEPIN!!1
   UP cookie!!1
   VISIBLE cookie
KTHXBAI
END;
lol($foo); ?>
contributions/lolphp.txt · Last modified: 2007/05/30 17:25 by shox
Recent changes RSS feed Creative Commons License Donate Driven by DokuWiki