Thursday, October 06, 2005

"On Parade"

I had a piece published on LewRockwell.com today!

Monday, October 03, 2005

Nicer parser notation

Continuing our parser improvements, we can follow Dominus's lead and use operator overloading to cut down the syntactical clutter:

Operators added to Parser

 public static Parser operator -(Parser a, Parser b)
 {
   return new Concatenate(a, b);
 }

 public static Parser operator |(Parser a, Parser b)
 {
   return new Alternate(a, b);
 }

 public static Parser operator >(Parser p, ValuesTransform map)
 {
   return new T(p, map);
 }

 // C# forces us to overload < and > in pairs
 public static Parser operator <(Parser p, ValuesTransform map)
 {
   return new T(p, map);
 }

Add a few more static helpers, and we have the following much nicer looking generator.

 static Parser MakeParser()
 {
   ParserStub exprstub = new ParserStub(new StubToReal(GetExpression));
   ParserStub termstub = new ParserStub(new StubToReal(GetTerm));
   ParserStub factstub = new ParserStub(new StubToReal(GetFactor));

   expression =
     termstub - _(Tokens.Operator, "+") - exprstub > TOpFirst |
     termstub;

   term =
     factstub - _(Tokens.Operator, "*") - termstub > TOpFirst |
     factstub;

   Parser open  = _(Tokens.Operator, "(");
   Parser close = _(Tokens.Operator, ")");
   Parser comma = _(Tokens.Operator, ",");

   Parser arglist =
     open - exprstub - new Star(comma - exprstub) - close;

   factor =
     _(Tokens.Identifier) - (arglist | new Nothing()) > TTagVarOrFunction |
     open - exprstub - close > TStripParens |
     _(Tokens.Integer);

   return exprstub - new EndOfInput() > TOnlyExpression;
 }

In Higher-Order Perl, Dominus overloads right-shift to generate a transformation instead of greater-than as above, but C# requires the second operand of >> to be an int. Note also that C# requires overloading both the less-than and greater-than operators if either is defined.

Using binary operators changes the structure of the AST. This has the effect of beating OpFirst with an ugly stick:

 static private ArrayList OpFirst(ArrayList values)
 {
   ArrayList result = new ArrayList(3);
   result.Add(((ArrayList)values[0])[1]);  // ugh
   result.Add(((ArrayList)values[0])[0]);
   result.Add(values[1]);
   return result;
 }

Sunday, October 02, 2005

Poor Shula decision hurts Tide again

Alabama came up huge against Florida, only to have the great victory spoiled by a terrible injury to the gifted Tyrone Prothro.

Reminding us of Shula's stupid decision last year to leave Brodie Croyle in with a big lead over Western Carolina, which resulted in losing Croyle to a season-ending knee injury, now we've lost Prothro for the rest of the way. We had no business throwing at the end zone with the starters in the fourth quarter. You play more conservatively with a big lead because Tide tradition is winning with class -- we aren't out to embarrass other teams -- and so you don't risk injuries!

What a shame that Tyrone Prothro paid the price for Mike Shula's low-class move!