Граф объектов цитат F#

В C# я мог создать строковое представление графа объектов довольно легко с деревьями выражений.

public static string GetGraph<TModel, T>(TModel model, Expression<Func<TModel, T>> action) where TModel : class
{
            var method = action.Body as MethodCallExpression;
            var body = method != null ? method.Object != null ? method.Object as MemberExpression : method.Arguments.Any() ? method.Arguments.First() as MemberExpression : null : action.Body as MemberExpression;
            if (body != null)
            {
                string graph = GetObjectGraph(body, typeof(TModel))
                return graph;
            }
            throw new Exception("Could not create object graph");
}

В F# я смотрел на Цитаты, чтобы попытаться сделать то же самое и не могу вполне понять это. Я делал попытку преобразования цитаты в Выражение, пользующееся библиотеками PowerPack, но не имел никакой удачи до сих пор, и информация в Интернете кажется довольно редкой по этой теме.

Если вход:

let result = getGraph myObject <@ myObject.MyProperty @>

вывод должен быть "myobject. MyProperty"

5
задан Stephen Swensen 28 February 2011 в 00:13
поделиться

1 ответ

You can see what you get from quotation expression in fsi session:

> let v = "abc"
> <@ v.Length @>;;
val it : Expr<int>
= PropGet (Some (PropGet (None, System.String v, [])), Int32 Length, [])

> <@ "abc".Length @>;;
val it : Expr<int>
= PropGet (Some (Value ("abc")), Int32 Length, [])

You can find description of all active patterns available to parse qoutations into

manual\FSharp.Core\Microsoft.FSharp.Quotations.Patterns.html

under your F# installation directory or at msdn site

There is nice Chris Smith's book "Programming F#" with chapter named "Quotations" :)

So, after all, just try to write simple parser:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Quotations.DerivedPatterns

let rec getGraph (expr: Expr) =
  let parse args =
    List.fold_left (fun acc v -> acc ^ (if acc.Length > 0 then "," else "") ^ getGraph v) "" args
  let descr s = function
    | Some v -> "(* instance " ^ s ^ "*) " ^ getGraph v
    | _ -> "(* static " ^ s ^ "*)"
  match expr with
  | Int32 i -> string i
  | String s -> sprintf "\"%s\"" s
  | Value (o,t) -> sprintf "%A" o
  | Call (e, methodInfo, av) ->
    sprintf "%s.%s(%s)" (descr "method" e) methodInfo.Name (parse av)
  | PropGet(e, methodInfo, av) ->
    sprintf "%s.%s(%s)" (descr "property" e) methodInfo.Name (parse av)
  | _ -> failwithf "I'm don't understand such expression's form yet: %A" expr

P.S. And of course you will need some code to translate AST to human readable format.

5
ответ дан 14 December 2019 в 04:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: