MGrammar Quick Challenge 2 – Unordered and optional multi-projections

Given this grammar:

module test
    language test
        syntax Main = OneTwo*;
        syntax OneTwo
            =  o:One t:Two "\r\n"
            => Result { One => true, Two => true }
            |  Two One "\r\n"
            => Result { One => true, Two => true }
            |  One "\r\n"
            => Result { One => true, Two => false }
            |  Two "\r\n"
            => Result { One => false, Two => true };
        token One = "one";
        token Two = "two";
        interleave Ignore = " ";

How could I refactor the syntax “OneTwo” to have only a single projection?

dynamic C# in unit tests

I’ve been writing some unit tests lately that require quite a bit of casting. This gets tiring pretty fast so I went ahead and decided to give the dynamic keyword a try. I’ll show the before and after examples and let you decide.

Here is the non-dynamic version.

public void InvokeWithAddPlusInvokeTest()
    string code = "f(1 + 2) + g()";
    var b1 = (BinaryExpression)this.pipeline.Compile(code);

    var f = (MethodInvokeExpression)b1.Left;
    var g = (MethodInvokeExpression)b1.Right;

    var b2 = (BinaryExpression)f.Parameters.Single();

    var one = (PrimitiveExpression)b2.Left;
    var two = (PrimitiveExpression)b2.Right;

    Assert.That(b1.Operator == BinaryOperator.Add);
    Assert.That(b2.Operator == BinaryOperator.Add);

    Assert.That(((ReferenceExpression)f.Target).Name == "f");
    Assert.That(((ReferenceExpression)g.Target).Name == "g");
    Assert.That(one.Value == "1");
    Assert.That(two.Value == "2");

In this test I am compiling an expression into an AST and digging around to verify that the correct nodes were created in the right places in the tree.

Here is the dynamic version.

public void InvokeWithAddPlusInvokeTest()
    string code = "f(1 + 2) + g()";
    dynamic b1 = this.pipeline.Compile(code);
    dynamic b2 = Enumerable.Single(b1.Left.Parameters);

    Assert.That(b1.Operator == BinaryOperator.Add);
    Assert.That(b2.Operator == BinaryOperator.Add);

    Assert.That(b1.Left.Target.Name == "f");
    Assert.That(b1.Right.Target.Name == "g");
    Assert.That(b2.Left.Value == "1");
    Assert.That(b2.Right.Value == "2");

A lot shorter that’s for sure. The only downside is that if I change the nodes I will no longer get compile time warnings… but I will get unit test errors so this shouldn’t theoretically matter. I also no longer get intellisense so I either have to just know the structure of the objects or use the debugger to figure it out. Still the simplicity is looking good.

Featured on

Tavis Hudson is a Tech Headhunter who first recruited me in 2005 to work for Magenic. Over the years we have kept in contact and run into each other at a few events here and there. Recently, Tavis contacted me about doing an over the phone interview (as in journalism not recruiting) for his blog.

In the first of a series of blog posts he is authoring called “Prodigy Developers” he did a write up based on the conversation we had. I’ll try not to let that title go to my head but thanks for the vote of confidence Tavis.

One of the things he touches on in his post is something that I’d like to elaborate on a little more. This is something I consciously say to myself from time to time:

One thing that I have learned in college and in my career so far is that there are lots of people who are smarter than me. I mean downright geniuses. I am not a genius. I am constantly awed by these people and their mathematical abilities or their ability to grasp incredibly complex topics or the speed with which they are able to get to conclusions. This is not my strong point even though I am not an idiot by any means. Yet somehow, in a field generally regarded as primarily mathematical, I seem to thrive. It’s not to say that intelligence is overrated but I think that passion and dedication are, perhaps, underrated.

Sometimes I like to say that I’m an artist who can’t paint. I know that sounds corny but what I’m trying to say is just that I have to rely more on my creative side than my intellectual side when programming. I’m more of a right brainer than a left brainer, if that makes sense. Maybe more people than I realize would be able to associate with this notion but I feel like this is what has helped me more than anything, not that it’s more important but maybe that it’s rare.

Sorry, enough self congratulation and introspection. Head on over and read Prodigy Developers – Justin Chase, by Tavis Hudson.

Expression Photoshop Importer – By Soma

In his latest blog post Soma did a feature preview of the new version of Expression Web and the Photoshop importer gets an honorable mention.

With Expression Web 3, we have significantly improved capabilities and workflow when working with Photoshop files. When you import a PSD file, you can choose just the layers you want to import in to your website. You can save the layers as JPG, PNG, or GIF and scale the image* before saving the individual layers in Expression Web 3. If the source file has changed, Expression Web 3 prompts you to update it.

*Actually, you can’t scale the imported image in this window but you can zoom in or out of the preview to view it scaled and you can adjust the quality if you select jpeg. Once it is imported you can scale the dimensions of the image with your image editor of choice.

One interesting thing about the Photoshop Importer is that it is an expression wide feature rather than simply for Blend. Specifically it will be a feature available in Blend, Web and Design for this release. In each application the importer will have slightly different behavior. For example in the window shown here you can see there are options for Encoding and Quality as well as a field displaying the estimated size. When you import a Photoshop file in Web you will get a single flattened image rather than multiple resources like in Blend and Design.

Designing the importer so that it could be extended to support different feature sets for each application was one of my main roles so far. It was a lot of fun and I learned a lot!