Sml 从Poly/ML中的源代码字符串获取解析树

Sml 从Poly/ML中的源代码字符串获取解析树,sml,polyml,Sml,Polyml,我正在尝试编译一个源代码字符串,并使用Poly/ML打印解析树。以下代码已编译,但解析树为空: fun main () = let val stream = TextIO.openString "let val a = \"abc\"; val b = \"def\"; val c = a ^ b in print c end"; val _ = PolyML.compiler (fn () => TextIO.input1 stream, []);

我正在尝试编译一个源代码字符串,并使用Poly/ML打印解析树。以下代码已编译,但解析树为空:

fun main () =
    let
        val stream = TextIO.openString "let val a = \"abc\"; val b = \"def\"; val c = a ^ b in print c end";
        val _ = PolyML.compiler (fn () => TextIO.input1 stream, []);
        val (_, parseTree) = !PolyML.IDEInterface.parseTree
    in
        PolyML.print (parseTree);
        PolyML.print (List.length parseTree);
        List.map PolyML.print (parseTree);
        ()
    end
运行此:

$ ./a.out
[...]
0
$
我需要做什么才能从编译器中获得解析树?我还使用
CPCompilerResultFun
编译器参数尝试了一个变体。但这也不起作用:

fun main () =
    let
        fun useTree (NONE, _) () =
            (PolyML.print "not parsed"; ())
          | useTree (SOME parseTree, _) () =
            (PolyML.print "parsed"; PolyML.print parseTree; ());

        val stream = TextIO.openString "let val a = \"abc\"; val b = \"def\"; val c = a ^ b in print c end";
        val _ = PolyML.compiler (fn () => TextIO.input1 stream, [PolyML.Compiler.CPCompilerResultFun useTree]);
    in
        ()
    end

运行此命令不会产生任何输出。

我可以通过提供
PolyML.Compiler.CPCompilerResultFun
编译器选项来获得它。它让你可以。然而,我不能说太多关于解析树实际上是如何表示的。有一些文档(我的网站已经关闭了),但是我还不能对它有太多的理解

val resultTrees : PolyML.parseTree list ref = ref [];

fun compilerResultFun (parsetree, codeOpt) =
  let
    val _ =
      case parsetree of
        SOME pt => resultTrees := !resultTrees @ [pt]
      | NONE => ()
  in
    fn () => raise Fail "not implemented"
  end;

val stream = TextIO.openString "val a = 1";

val _ = PolyML.compiler (fn () => TextIO.input1 stream, [
  PolyML.Compiler.CPCompilerResultFun compilerResultFun
]);

val [(a, [PolyML.PTfirstChild b])] = !resultTrees;
val (_, [PolyML.PTfirstChild c, PolyML.PTparent d, PolyML.PTprint e]) = b ();

你能演示一下解析树的值吗?我使用了您的代码并尝试打印(a、b、c等),但不管怎样它都打印了“fn”。@eatonphil我不知道如何遍历树。今晚我会更深入地看一看。我所能说的就是不要
PolyML。打印
它们,在REPL中运行脚本,它提供了更好的漂亮打印。实际上,我知道我做错了什么。这些值是函数,其漂亮的打印值为“fn”。我给他们打了电话,能够更清楚地了解我的期望。