On the second Thursday of the month at 5pm, the Rochester, MN .NET User Group meets at Mann Hall. You’re very welcome to join us if you’re in the area.
We often have someone interesting presenting something exciting, but occasionally that doesn’t work out and I’ll present something instead This is where the LOLCode Parser was born…
First you should try it out here. It’ll ask you to write some LOLCode and optionally give it some inputs to pass in. Let’s start with “Hai World”…
HAI CAN HAS STDIO? VISIBLE "HAI WORLD!" N " I SLEEPIN" KTHXBYE
Paste that and click the “PLZ RUN THE CODE 4 ME” button. This demonstrates the VISIBLE statement and the N concatenation operator.
Let’s step it up a bit and add some conditions (to see this work its magic, you’ll need an input value in the second textbox):
HAI CAN HAS STDIO? I HAS A VAR VISIBLE "GIMMEH INPUTZ!..." GIMMEH VAR VISIBLE "U SEZ " N VAR IZ VAR BIGR THAN 10? YARLY BTW this is true VISIBLE VAR N " IZ BIG NUMBER!" NOWAI BTW this is false VISIBLE VAR N " IZ LITTLE NUMBER!" KTHX VISIBLE "I IZ DONE" KTHXBYE
Here we’ve added some user input (GIMMEH VAR will read a line from the input source to the VAR variable). The IZ..YARLY..NOWAI..KTHX construct lets us specify a condition based on its value. The BTW statement begins a comment.
Cats won’t usually chase their tails like dogs will, but they use loops to remind their owners it’s feeding time. This one just wants to count to 10 between meows:
HAI CAN HAS STDIO? VISIBLE "HAI WORLD!" I HAS A VAR IM IN YR KITCHN UPZ VAR!!1 VISIBLE VAR IZ VAR BIGR THAN 9? GTFO. KTHX KTHX VISIBLE "FEED MEOW!" KTHXBYE
If you’re thinking this isn’t very impressive, you’re right. Still, it does have some cool points:
- It’s using a parser generated by TinyPG (awesome tool on CodeProject by Herre Kuijpers).
- The LOLCode is being parsed into a LINQ expression tree (similar to this old post, but no DLR and much simpler).
- Because it’s an expression tree, it can be compiled and cached on the server. This should run as fast as C#, so your cat will be happy. For this demo, if you don’t change the code between requests it’ll just run the cached, pre-compiled code. Anything in the Inputs box is passed as a parameter.
- This isn’t just an expression returning a value; it’s building a full LOLCode program (with conditions, loops etc).
- It allows your program to run for up to two seconds; if it overstays its welcome, it will be brutally terminated (putting your cat in an infinite loop is animal cruelty).
You’re unlikely to want LOLCode in your own apps, but some of the ideas could be useful. For example, you could use it to provide a custom scripting language for clients to automate aspects of your apps.
You can find the source code here. It’s pretty easy to use TinyPG to define your own parser (see the Cjc.LOLCode.tpg file for sample syntax), then extend the ParseTreeVisitor / Visitor classes to build an expression tree.
Finally, here’s some bonus LOLCode:
HAI CAN HAS STDIO? VISIBLE "HAI WORLD!" I HAS A VAR I HAS A INPUTZ IM IN YR KITCHN UPZ VAR!!1 GIMMEH INPUTZ VISIBLE VAR N ", " N INPUTZ IZ VAR BIGR THAN 9? GTFO. KTHX KTHX VISIBLE "FEED MEOW!" KTHXBYE
And some inputs to go with it:
10 9 8 7 6 5 4 3 2 1
You might also want to take a look at a LOLQL parser (source here); it allows your cat to query a database through Entity Framework!