YALCP [Yet Another LOL Code Parser]

Sign in to queue

Description

Today's project takes us back a few years to when adding new languages to .Net was all the rage. Today's project is a LOLCode parser.

"Wait, isn't there one already?" you ask?

Yes, there is, http://code.google.com/p/lolcode-dot-net/, but today's implementation shows off a couple different things and it never hurts to take a new and unique look sometimes. No, you're not going to replace C# with this, but if you've ever wondered what it would take to create your own programming language, then this is a cool introduction.

Plus writing LOLCode is just kind of fun... Smiley

LOLCode Parser

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”…

image

image

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.

Let's look at the Solution;

image

A good bit of the magic happens via the parser generated by TinyPG;

image

Much of the rest of the magic is here;

private Action<IInputOutputProvider> Compile( string code )
{
    var parser = new Parser.Parser( new Parser.Scanner() );
    var tree = parser.Parse( code );

    if ( tree.Errors.Count > 0 ) throw new LOLCodeException( tree.Errors );

    var ioProviderParam = Expression.Parameter( typeof( IInputOutputProvider ), "ioParam" );

    // Build expression tree from parse tree
    var visitor = new Visitor
    {
        IOProvider = ioProviderParam
    };

    var expression = visitor.GetExpression( tree );

    // Compile and evaluate expression
    return Expression.Lambda<Action<IInputOutputProvider>>( expression, ioProviderParam ).Compile();
}

This block takes that raw text LOLCode and in the provides a compiled Lambda expression tree...

If you're looking for something fun to play with this weekend, want to amaze your friends and family with your new programming language or just have demanding kitties, LOLZ right over and get it... YOUZ CANZ HAS CDEZ

 

Page image, Geek cats, courtesy of the mad LOLscientist

Tag:

Coding4Fun

The Discussion

Add Your 2 Cents