Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created December 1, 2011 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeno-by/1417984 to your computer and use it in GitHub Desktop.
Save xeno-by/1417984 to your computer and use it in GitHub Desktop.
compose the tree, equivalent to t5230, by hand
import scala.tools.nsc.reporters._
import scala.tools.nsc.Settings
import reflect.runtime.Mirror.ToolBox
object Test extends App {
// todo. compose the tree, equivalent to t5230, by hand
import scala.reflect.mirror._
import scala.reflect.api._
import scala.reflect.api.Modifier._
val enclosingClass = definitions.EmptyPackageClass
val classSym = enclosingClass.newClassSymbol(newTypeName("C"))
val ctor_tpt = TypeTree(classSym.typeOfThis)
val ctor_body = Block(List(Apply(Select(Super(This(newTypeName("")), newTypeName("")), newTermName("<init>")), List())), Literal(Constant(())))
val ctor = DefDef(Modifiers(), newTermName("<init>"), List(), List(List()), ctor_tpt, ctor_body)
ctor.setSymbol(classSym.newMethodSymbol(newTermName("<init>")))
ctor.symbol.setTypeSig(MethodType(List(), TypeRef(NoPrefix, classSym, List())))
ctor.setType(NoType)
val x_tpt = TypeTree(staticClass("scala.Int").tpe)
val x_body = Literal(Constant(2))
val x = DefDef(Modifiers(), newTermName("x"), List(), List(), x_tpt, x_body)
x.setSymbol(classSym.newMethodSymbol(newTermName("x")))
x.symbol.setTypeSig(NullaryMethodType(staticClass("scala.Int").tpe));
x.setType(NoType)
val self = ValDef(Modifiers(Set(`private`)), newTermName("_"), TypeTree(NoType), EmptyTree)
val tmpl = Template(List(TypeTree(staticClass("java.lang.Object").tpe), TypeTree(staticClass("scala.ScalaObject").tpe)), self, List(ctor, x))
val classDef = ClassDef(Modifiers(), newTypeName("C"), List(), tmpl)
classDef.setSymbol(classSym)
classSym.setTypeSig(ClassInfoType(List(staticClass("java.lang.Object").tpe, staticClass("scala.ScalaObject").tpe), newScopeWith(ctor.symbol, x.symbol), classSym))
val fn_println = Select(Select(Ident(staticModule("scala").moduleClass), newTermName("Predef")), newTermName("println"))
val expr_newc = Apply(Select(New(Ident(newTypeName("C"))), newTermName("<init>")), List())
val expr_println = Apply(fn_println, List(Select(expr_newc, newTermName("x"))))
val tree = Block(List(classDef), expr_println)
val reporter = new ConsoleReporter(new Settings)
val toolbox = new ToolBox(reporter)
val evaluated = toolbox.runExpr(tree)
println("evaluated = " + evaluated)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment