diff options
author | Paul Phillips <paulp@improving.org> | 2011-12-04 15:59:05 -0800 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-12-04 15:59:05 -0800 |
commit | a17908694b89115026f587bcb8a8ed6d16768cad (patch) | |
tree | 8f1f985102a5546025aa1857b0224397453e8418 /test/files/run | |
parent | da35106f81a5c24e78ff51c95e10052ad4f23b18 (diff) | |
parent | 7193d21a9be25bf8a705492493971f3267687098 (diff) | |
download | scala-a17908694b89115026f587bcb8a8ed6d16768cad.tar.gz scala-a17908694b89115026f587bcb8a8ed6d16768cad.tar.bz2 scala-a17908694b89115026f587bcb8a8ed6d16768cad.zip |
Merge remote-tracking branch 'kepler/topic/reifytests'
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/reify_anonymous.check | 1 | ||||
-rw-r--r-- | test/files/run/reify_anonymous.scala | 14 | ||||
-rw-r--r-- | test/files/run/reify_generic.check | 1 | ||||
-rw-r--r-- | test/files/run/reify_generic.scala | 15 | ||||
-rw-r--r-- | test/files/run/reify_inheritance.check | 1 | ||||
-rw-r--r-- | test/files/run/reify_inheritance.scala | 23 | ||||
-rw-r--r-- | test/files/run/reify_printf.check | 0 | ||||
-rw-r--r-- | test/files/run/reify_printf.scala | 75 |
8 files changed, 130 insertions, 0 deletions
diff --git a/test/files/run/reify_anonymous.check b/test/files/run/reify_anonymous.check new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/test/files/run/reify_anonymous.check @@ -0,0 +1 @@ +4 diff --git a/test/files/run/reify_anonymous.scala b/test/files/run/reify_anonymous.scala new file mode 100644 index 0000000000..1e7f3fe856 --- /dev/null +++ b/test/files/run/reify_anonymous.scala @@ -0,0 +1,14 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + println(new {def x = 2; def y = x * x}.y) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_generic.check b/test/files/run/reify_generic.check new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/test/files/run/reify_generic.check @@ -0,0 +1 @@ +4 diff --git a/test/files/run/reify_generic.scala b/test/files/run/reify_generic.scala new file mode 100644 index 0000000000..aef038b2d8 --- /dev/null +++ b/test/files/run/reify_generic.scala @@ -0,0 +1,15 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + val product = List(1, 2, 3).head * List[Any](4, 2, 0).head.asInstanceOf[Int] + println(product) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_inheritance.check b/test/files/run/reify_inheritance.check new file mode 100644 index 0000000000..25bf17fc5a --- /dev/null +++ b/test/files/run/reify_inheritance.check @@ -0,0 +1 @@ +18
\ No newline at end of file diff --git a/test/files/run/reify_inheritance.scala b/test/files/run/reify_inheritance.scala new file mode 100644 index 0000000000..2a1b5f764f --- /dev/null +++ b/test/files/run/reify_inheritance.scala @@ -0,0 +1,23 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + class C { + def x = 2 + def y = x * x + } + + class D extends C { + override def x = 3 + } + + println(new D().y * new C().x) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_printf.check b/test/files/run/reify_printf.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/run/reify_printf.check diff --git a/test/files/run/reify_printf.scala b/test/files/run/reify_printf.scala new file mode 100644 index 0000000000..30901b98c2 --- /dev/null +++ b/test/files/run/reify_printf.scala @@ -0,0 +1,75 @@ +import java.io.{ ByteArrayOutputStream, PrintStream } +import scala.reflect.Code +import scala.reflect.mirror._ +import scala.reflect.api._ +import scala.reflect.api.Trees +import scala.reflect.internal.Types +import reflect.runtime.Mirror.ToolBox +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import scala.util.matching.Regex + +object Test extends App { + val tree = tree_printf(Code.lift("hello %s").tree, Code.lift("world").tree) + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter, args mkString " ") + val ttree = toolbox.typeCheck(tree) + + val output = new ByteArrayOutputStream() + Console.setOut(new PrintStream(output)) + val evaluated = toolbox.runExpr(ttree) + + assert(output.toString() == "hello world", output.toString() +" == hello world") + + /* + macro def printf(format: String, params: Any*) : String = tree_printf(format: Tree, (params: Seq[Tree]): _*) + */ + + var i = 0 + def gensym(name: String) = { i += 1; newTermName(name + i) } + + def createTempValDef( value : Tree, tpe : Type ) : (Option[Tree],Tree) = { + val local = gensym("temp") + ( + Some( + ValDef( + Modifiers() + , local + , TypeTree().setType(tpe) + , value + ) + ) + , Ident(local) + ) + } + + def tree_printf(format: Tree, params: Tree*) = { + val Literal(Constant(s_format: String)) = format + val paramsStack = scala.collection.mutable.Stack(params: _*) + val parsed = s_format.split("(?<=%[\\w%])|(?=%[\\w%])") map { + case "%d" => createTempValDef( paramsStack.pop, classToType(classOf[Int]) ) + case "%s" => createTempValDef( paramsStack.pop, classToType(classOf[String]) ) + case "%%" => { + (None:Option[Tree], Literal(Constant("%"))) + } + case part => { + (None:Option[Tree], Literal(Constant(part))) + } + } + + val evals = for ((Some(eval), _) <- parsed if eval != None) yield (eval: Tree) + val prints = for ((_, ref) <- parsed) yield + Apply( + Select( + Select( + Ident( newTermName("scala") ) + , newTermName("Predef") + ) + , newTermName("print") + ) + , List(ref) + ): Tree + Block((evals ++ prints).toList, Literal(Constant(()))) + } +} |