CodeIgniter Forums

Full Version: LOLCODE 1.2 Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Adam Griffiths[/eluser]
Ok so here is the LOLCODE 1.2 Library for CI.

libraries/lolcode.php
Code:
<?php

class Lolcode
{
    
    function lol_parse($file)
    {
        if(file_exists(APPPATH."lol/".$file.".lol"))
        {
            $file2 = file_get_contents(APPPATH."lol/".$file.".lol");
        }
        else
        {
            echo("File ".APPPATH."lol/".$file." does not exist!");
        }
        
        $code =  $this->_lol_replace($file2);
        eval('?>'.$code.'');
        return $code;
    }
    
    function _lol_replace($lol_code)
    {
        $array = array(
            '/^HAI 1.2$/','<?php',

            '/^KTHXBYE$/','?>',

            '/^OBTW$/','/*',

            '/^TLDR$/','*/',

            '/^[\s]*BTW (.*)$/','//$1',

            '/^[\s]*I HAS A ([a-zA-Z0-9_-]+)$/','\$$1 = null;',

            '/^[\s]*I HAS A ([a-zA-Z0-9_-]+) ITZ (BUCKET)$/','\$$1 = array(',

            '/^[\s]*I HAS A ([a-zA-Z0-9_-]+) ITZ ([a-zA-Z0-9]+)$/','\$$1 = \'$2\';',

            '/^[\s]*I HAS A ([a-zA-Z0-9_-]+) ITZ (.*)$/','\$$1 = $2;',

            '/^[\s]*VISIBLE ([a-zA-Z0-9-]+)$/','echo \($$1);',

            '/^[\s]*VISIBLE (.*)$/','echo ($1);',

            '/^[\s]*CAN HAS ([a-zA-Z0-9-]+)$/','include(\'$1.php\');',

            '/^[\s]*MUST HAS ([a-zA-Z0-9-]+)$/','require(\'$1.php\');',

            '/^[\s]*([a-zA-Z0-9_-]+) UPPIN$/','\$$1++;',

            '/^[\s]*([a-zA-Z0-9_-]+) NERFIN$/','\$$1--;',

            '/^[\s]*SUM OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\$var = $$1 + $$2; echo($var);',

            '/^[\s]*DIFF OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\$var = $$1 - $$2; echo($var);',

            '/^[\s]*PRODUKT OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\$var = $$1 * $$2; echo($var);',

            '/^[\s]*QUOSHUNT OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\$var = $$1 / $$2; echo($var);',

            '/^[\s]*BOTH SAEM ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 === $$2) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*DIFFRINT ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 !== $$2) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*BOTH SAEM ([a-zA-Z0-9_-]+) AN BIGGR OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 >= $$3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*BOTH SAEM ([a-zA-Z0-9_-]+) AN SMALLR OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 <= $$3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*DIFFRINT ([a-zA-Z0-9_-]+) AN BIGGR OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 > $$3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*DIFFRINT ([a-zA-Z0-9_-]+) AN SMALLR OF ([a-zA-Z0-9_-]+) AN ([a-zA-Z0-9_-]+)$/','\if($$1 < $$3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*BOTH SAEM "(.*)" AN BIGGR OF "(.*)" AN "(.*)"$/','\if($1 >= $3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*BOTH SAEM "(.*)" AN SMALLR OF "(.*)" AN "(.*)"$/','\if($1 <= $3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*DIFFRINT "(.*)" AN BIGGR OF "(.*)" AN "(.*)"$/','\if($1 > $3) { echo("WIN"); } else { echo("FAIL"); };',

            '/^[\s]*DIFFRINT "(.*)" AN SMALLR OF "(.*)" AN "(.*)"$/','\if($1 < $3) { echo("WIN"); } else { echo("FAIL"); };'

        );
        $search = array();
        $replace = array();
        $lines = explode("\n",$lol_code);

        foreach($array as $key=>$var){
            if(1 & $key)    {
                $replace[] = $var;
            } else {
                $search[] = $var;
            }
        }
        $lines = preg_replace($search,$replace,$lines);
        $lol_code = implode("\n",$lines);
        return $lol_code;
    }
}

?&gt;

Usage
Firstly create a new folder in your application/ folder called lol/.

Loading and parsing lolcode files is easy, just use:-

Code:
$this->load->library('lolcode');
$this->lolcode->lol_parse('test');

All lolcode files must end with .lol

The LOLCODE

All lolcode files must begin with HAI 1.2 and end in KTHXBYE. You can print messagesto the screen using VISIBLE, and assing values to variables using I HAS A variable ITZ value. Comments are BTW, and multiline comments are OBTW and end with TLDR.

Example.

Code:
HAI 1.2
I HAS A PIZZA ITZ CHEESE
VISIBLE PIZZA
BTW this is a comment :P
OBTW
This won't be read, as is stated in the end comment command.
TLDR

You can also do math comparisons:-

Code:
SUM OF <x> AN <y>       BTW  +
DIFF OF <x> AN <y>      BTW  -
PRODUKT OF <x> AN <y>   BTW  *
QUOSHUNT OF <x> AN <y>  BTW  /

and

Code:
BOTH SAEM <x> [AN] <y>   BTW  WIN iff x == y
DIFFRINT <x> [AN] <y>    BTW  WIN iff x != y

and

Code:
BOTH SAEM <x> AN BIGGR OF <x> AN <y>   BTW x >= y
BOTH SAEM <x> AN SMALLR OF <x> AN <y>  BTW x <= y
DIFFRINT <x> AN SMALLR OF <x> AN <y>   BTW x > y
DIFFRINT <x> AN BIGGR OF <x> AN <y>    BTW x < y

You can ++ and -- variables using:-

Code:
UPPIN VAR    BTW UPPIN = VAR++
NERFIN VAR   BTW NERFIN = VAR--

This library will support loops and functions as soon as I have more spare time.

Code:
HAI 1.2
VISIBLE "CAN I HAS CHEEZBURGERZ?"
KTHXBYE

Let the fun time begin! Tongue

El Forum

[eluser]andyr[/eluser]
I dont get LOLCODE, really i dont - some developers find it fun. But what is the point of intentionally obfuscating your code to the point of insantity? It just goes against all principles of software maintenance.

It really grinds my gears when developers abbreviate every variable, function and method to the point where the resulting code is so unreadable, its just quicker to refactor line by line in order to have something remotely maintainable.

El Forum

[eluser]Adam Griffiths[/eluser]
[quote author="andyr" date="1220460531"]I dont get LOLCODE, really i dont - some developers find it fun. But what is the point of intentionally obfuscating your code to the point of insantity? It just goes against all principles of software maintenance.

It really grinds my gears when developers abbreviate every variable, function and method to the point where the resulting code is so unreadable, its just quicker to refactor line by line in order to have something remotely maintainable.[/quote]

This isn't intended for use in any real life applications*. It's just something I put together to pass the time. Smile

*other than...

Code:
HAI
VISIBLE "HAI WURLD!"
KTHXBYE

Big Grin

El Forum

[eluser]m4rw3r[/eluser]
Here is a LOLCODE example from LOLPython (dunno if it works with PHP, but it uses yield so I guess not):
Code:
IN MAI datetime GIMME date LIKE DATE

SO IM LIKE FIBBING WIT N OK?
    LOL ITERATE FIBONACCI TERMS LESS THAN N /LOL
    SO GOOD N BIG LIKE EASTERBUNNY
    BTW, FIBONACCI LIKE BUNNIES! LOL
    U BORROW CHEEZBURGER
    U BORROW CHEEZBURGER
    I CAN HAZ CHEEZBURGER
    HE CAN HAZ CHEEZBURGER
    WHILE I CUTE?
        I AND HE CAN HAZ HE AND I ALONG WITH HE
        IZ HE BIG LIKE N?
            KTHXBYE
        U BORROW HE

IZ __name__ KINDA LIKE "__main__"?
    COMPLAIN "NOW IZ" AND DATE OWN today THING
    IZ BIGNESS ARGZ OK KINDA LIKE 1?
        N CAN HAS 100
    NOPE?
        N CAN HAS NUMBR ARGZ LOOK AT 1!!
    GIMME EACH I IN UR FIBBING WIT N OK?
        VISIBLE I
It's the fibonacci numbers Smile

El Forum

[eluser]Adam Griffiths[/eluser]
That isn't up to the 1.2 spec! Half of t is made up. Tongue

El Forum

[eluser]frenzal[/eluser]
nice work Smile
KTHXBYE

El Forum

[eluser]Bramme[/eluser]
I can understand why some people find it funny, but the whole lol thing has been so so so exhausted. I can't believe people actually made code from it Confused