2012-09-14 84 views
7

編譯器代碼庫相當大,而且我無法一次把所有東西都包裹起來:)scala編譯器在哪裏存儲AST?

目前,我只是想在它從「typer」階段後獲得AST。有沒有辦法做到這一點?

我編譯運行方式如下:

val settings = new Settings 
settings.classpath.value = ... 
val compiler = new Global(settings, new ConsoleReporter(settings)) 
new compiler.Run() { 
    override def stopPhase(name: String) = name == "superaccessors" 
} compileSources files 

回答

6

使用-Xprint:typer連同-Yshow-trees-compact(傾倒在原料AST格式樹)(轉儲類型確定後樹)。如果您也使用-Yshow-trees-stringified,AST將被另外轉儲爲僞Scala代碼(注意:最後兩個選項需要2.10.0)。

C:\Projects\Kepler\sandbox @ ticket/6356>cat Test.scala 
class C { 
    def x = 2 
} 

C:\Projects\Kepler\sandbox @ ticket/6356>scalac -Xprint:typer -Yshow-trees-compact -Yshow-trees-stringified Test.scala 
[[syntax trees at end of typer]]// Scala source: Test.scala 
package <empty> { 
    class C extends scala.AnyRef { 
    def <init>(): C = { 
     C.super.<init>(); 
    () 
    }; 
    def x: Int = 2 
    } 
} 
PackageDef(
    Ident(<empty>), 
    List(
    ClassDef(Modifiers(), newTypeName("C"), List(), 
     Template(List(Select(Ident(scala), newTypeName("AnyRef"))), emptyValDef, 
     List(
     DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(newTypeName("C")), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), 
     DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), Literal(Constant(2)))))))) 
+0

但我的意思是,我怎麼AST我正在運行的程序裏面?編程? – Rogach

+0

呵呵。我懂了。讓我想想。 –

+4

使用'Run.units map(_.body)' –

1

編譯器代碼庫是相當大的,我不能一下子換我的頭周圍一切:)

除了所有重要的類型確定,Scala編譯器的大部分階段都在詳細描述:

http://lampwww.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/

+0

嗯,問題是我只對前四個階段感興趣 - 從解析器到typer,而那些在那裏都沒有描述:( – Rogach