Nicer Linq
A few days ago I posted about Ugly Linq. Ever since then, I kept thinking about how ugly it is to handle this by hand. Suddenly, it hit me that I don't have to do it that way.
Boo already has the facilities to take a compiler AST and translate that into the code that would recreate this AST. In particular, this makes the code we previously had to write to this:
public class ConditionMacro : AbstractAstMacro { public override Statement Expand(MacroStatement macro) { Expression serialize = new CodeSerializer().Serialize(macro.Arguments[0]); var body = new Block(); body.Statements.Add(new ReturnStatement(macro.Arguments[0])); return new ExpressionStatement( new MethodInvocationExpression( AstUtil.CreateReferenceExpression(typeof(Condition).FullName), new BlockExpression(body), serialize ) ); } }
And what that means is that given this code:
condition a > 10
We can get this result:
And that is it.
You get both the actual compiled expression and the AST that describes this. This is critically important because you can now take this piece of AST and do transformations / views on it.
And that is important if you want to have reliable graphical representation on top of a textual DSL, which is what my chapter 10 is going to cover.
Damn, this is simple! Thanks Rodrigo!
Comments
So does condition become a keyword? How would I put this into use? sorry but I am kinda lost :)
This is just a sample, and it is just showing how you can use a macro.
A macro is a user defined keyword.
Wow so that's all it take to define a keyword? The reason I asked is that I've work with parsing engine before and it wasn't all that clear cut... if it's all it take to define a keyword then I definitely need to invest sometimes in Boo. Is there a code sample that I can dl and play with?
Check the Boo repository, and the Boo site
Comment preview