<= Home
Use AST for Maintainable MSIL Code.
Hammett, creator of the Castle framework, made a good point and that it is to make an abstract syntax tree (AST) intermediate representation instead of emitting IL directly.
Abstract Syntax Tree (via wikipedia) :
“In computer science, abstract syntax tree (AST) is a data structure representing something which has been parsed, often used as a compiler or interpreter's internal representation of a computer program while it is being optimized and from which code generation is performed. The range of all possible such structures is described by the abstract syntax.“
Why use an intermediate representation for emitting MSIL in .NET?
- Breaks the MSIL into manageable pieces.
- Easier code generation
- Level of abstraction
There are other benefits discussed thoroughly here but most are not as relevant as the above when dealing with MSIL code. (By the way MSIL stands for Microsoft Intermediate Language, which is what your C# or VB.NET code gets compiled to.)
Using an AST subset ensures your system will scale (manageable) as the codebase grows. To see an example of AST in C# I recommend downloading the Castle.DynamicProxy. The Castle AST implementation works very similar to the System.CodeDom classes.