Can F # pattern matching be used as a solver / library for another language or DSL?

I am creating a toy language, I want to match the pattern. I could build it all myself (and don't know how), but because I will do it in F #, I wonder if I can put it all back to him.

So I have an interpreter and my own syntax. If I give AST, can I use F # to use it to solve pattern matching?

Another way to look at this, is it possible to use F # pattern matching with C # and other .NET languages?

For example, if the program (in the invented syntax is almost like F #):

case x with 
    ( 1 , 2 , Three):
        print("Found 1, 2, or 3!")
    else var1:
        print("%d" % var1)

      

Can be done

matched, error = F#MagicHere.Match(EvaluateMyAST)

      

+3


source to share


1 answer


I'm not entirely sure if I understand your question correctly, but I suspect you can use the F # Compiler Service to do what you need. Basically, the compiler service allows you to invoke some of the tasks that the compiler does (and this is a regular .NET library).

The first step would be to turn your AST into valid F # code - I think you could find a systematic way to do this (but that takes some thinking). Then you can use FCS to:



  • Enter a test expression that gives you warnings for overlapping cases and missing cases (for example, when you have an incomplete pattern match).

  • This gives you an AST of pattern matching, and I believe you can even get a decision tree that you can interpret to evaluate pattern matching.

  • Alternatively, you can use FCS to compile your code and run it (assuming you can translate your DSL to F # code)

+3


source







All Articles