Can a statically typed language be developed with DLR?

I need to develop a compiler for a statically typed language for .NET. I am considering using DLR for this instead of creating all the parts (Lexer / Parser, Syntax, Semantic, Code Generation).

Will DLR fit this scenario? Or it would be better to build a compiler directly for .NET and avoid all dynamic calls.

EDIT: I implemented the language without using the dynamic expressions that DLR offers, I used other expressions instead.

After that, I can say that it is much better if you implement a compiler to directly target DLR instead of IL. The generated code will still be very clean.

If you want to see some code check: tigerconverters.codeplex.com/

+6
source share
3 answers

The best that DLR can offer you in this case is expression trees. These are the same expression trees that were introduced for LINQ, but they have been extended to support the creation of complete programs. It is much easier to generate expression trees compared to IL, and you get a bunch of useful checks when generating a tree, rather than hard to diagnose failures when you generate an invalid IL. Therefore, you should check out System.Linq.Expressions. To compile and save the assembly, you will want to use LambdaExpression.CompileToMethod.

The only thing dynamic in use is the DynamicExpression nodes, which you can completely avoid.

+4
source

DLR provides a lot of infrastructure, which is also useful for static languages. For example, it has default implementations for bind method calls and overload resolution. This is normal if the semantics of your language match the default behavior.

Error handling can be a bit complicated. If the method is not searched, for example, the default binders will still return the correct expression, but this will be the code to throw an exception ...

DLR will not help you with parsing or vocabulary.

There are other options. For example, you can see the project Common Compiler Infrastructure , created by Herman Witter from MSR. In fact, this may be a better match.

+3
source

By the way, you need to implement your own parser for your own DSL (Domain Specific Language). Compilation may be a relay on the CLR , but not on your parser. DLR or CLR will not affect the meaning of your actual question.

Useful link: Create a language compiler for the .NET Framework

+2
source

Source: https://habr.com/ru/post/898700/


All Articles