Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does scala compiler store ASTs?

Tags:

scala

Compiler codebase is quite big, and I can't wrap my head around it all at once :)

Currently, I just want to get ASTs after "typer" phase from it. Is there a way to do it?

I run the compiler the following way:

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
like image 865
Rogach Avatar asked Sep 14 '12 16:09

Rogach


Video Answer


2 Answers

Use -Xprint:typer (to dump trees after typer) together with -Yshow-trees-compact (to dump the trees in raw AST format). If you also use -Yshow-trees-stringified, ASTs will be additionally dumped as pseudo Scala code (note: the last two options require 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))))))))
like image 148
Eugene Burmako Avatar answered Oct 06 '22 04:10

Eugene Burmako


Compiler codebase is quite big, and I can't wrap my head around it all at once :)

Except for the all important typer, most phases of the Scala compiler are described in detail at:

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

like image 42
Miguel Garcia Avatar answered Oct 06 '22 06:10

Miguel Garcia