summaryrefslogtreecommitdiff
path: root/test/files/run/reify_printf.scala
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2011-12-05 00:05:57 +0100
committerEugene Burmako <xeno.by@gmail.com>2011-12-05 00:05:57 +0100
commit7193d21a9be25bf8a705492493971f3267687098 (patch)
tree0f03cd92cc5898cb7c809019e475005f3bc3603a /test/files/run/reify_printf.scala
parent3e9e4ecf360e6eda5c26f798abfcb9bb882cf772 (diff)
downloadscala-7193d21a9be25bf8a705492493971f3267687098.tar.gz
scala-7193d21a9be25bf8a705492493971f3267687098.tar.bz2
scala-7193d21a9be25bf8a705492493971f3267687098.zip
Test pack for various flavors of reflection.
Diffstat (limited to 'test/files/run/reify_printf.scala')
-rw-r--r--test/files/run/reify_printf.scala75
1 files changed, 75 insertions, 0 deletions
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(())))
+ }
+}