Can a statically typed language be developed with the DLR?
I have to develop a compiler for a statically typed language for .NET I'm considering using the DLR for this instead of building all the parts (Lexer/Parser, Syntax, Semantic, Code Generation).
Would the DLR fit well for this scenario? Or it would be better to build the compiler开发者_开发知识库 directly for .NET and avoid all the dynamic calls.
EDIT: I did implemented the language without using any of the dynamic expressions that the DLR offers, instead I used the other expressions.
After that I can say that is much better when implementing a compiler to target the DLR instead of IL directly. The generated code still will be very clean.
If you want to see some code check: tigerconverters.codeplex.com/
The best thing the DLR has to offer you in this case is expression trees. These are the same expression trees introduced for LINQ but they've been extended to support generating full programs. It's much simpler to generate expression trees compared to IL and you get a bunch of useful checks when generating the tree instead of hard to diagnose failures when you generate invalid IL. So you should check out the System.Linq.Expressions. To compile and save to an assembly you'll want to use LambdaExpression.CompileToMethod.
The only thing dynamic when using these are the DynamicExpression nodes which you can completely avoid.
The DLR provides a lot of infrastructure that is useful for static languages as well. For example, it has default implementations for binding method calls and overload resolution. This is fine if the semantics of your language matches the default behaviour.
Error handling might be a bit tricky, however. If a method lookup fails, for example, the default binders will still return a valid expression, but it will be the code to throw an exception...
The DLR won't help you with parsing or lexing.
There are other options. For example, you may want to look at the Common Compiler Infrastructure project created by Herman Vitter of MSR. This may actually be a better overall match.
You by the way need to implement you own parser for you own DSL
(Domain Specific Language).
The compilation can be relay on CLR
, but not your parser. DLR
or CLR
will not make a difference in the sense of your actual question.
A useful link: Create a Language Compiler for .NET Framework
精彩评论