diff options
author | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2015-05-12 18:30:53 +0200 |
---|---|---|
committer | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2015-05-12 18:30:53 +0200 |
commit | 89bacb9c25a58454ff1878e67f7ea07ffc8c269f (patch) | |
tree | 51f1ff6c66aebe1b6109b1cffcc2bb8e4cf760a3 /tests/pending/run/reify_printf.scala | |
parent | a0fa33deafbea1bf53edc068c5ed9db5592822f9 (diff) | |
download | dotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.tar.gz dotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.tar.bz2 dotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.zip |
Run tests as they were in scala.
Diffstat (limited to 'tests/pending/run/reify_printf.scala')
-rw-r--r-- | tests/pending/run/reify_printf.scala | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/pending/run/reify_printf.scala b/tests/pending/run/reify_printf.scala new file mode 100644 index 000000000..c4ade7983 --- /dev/null +++ b/tests/pending/run/reify_printf.scala @@ -0,0 +1,71 @@ +import java.io.{ ByteArrayOutputStream, PrintStream } +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{universe => ru} +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.ToolBox +import scala.reflect.api._ +import scala.reflect.api.Trees +import scala.reflect.internal.Types +import scala.util.matching.Regex + +object Test extends App { + //val output = new ByteArrayOutputStream() + //Console.setOut(new PrintStream(output)) + val toolbox = cm.mkToolBox() + + val tree = tree_printf(reify("hello %s").tree, reify("world").tree) + val evaluated = toolbox.eval(tree) + //assert(output.toString() == "hello world", output.toString() +" == hello world") + + /* + // upd. Oh, good old times, our very-very first experiments with macros :) + macro def printf(format: String, params: Any*) : String = tree_printf(format: Tree, (params: Seq[Tree]): _*) + */ + + var i = 0 + def gensym(name: String) = { i += 1; TermName(name + i) } + + def createTempValDef( value : Tree, tpe : Type ) : (Option[Tree],Tree) = { + val local = gensym("temp") + ( + Some( + ValDef( + NoMods + , local + , TypeTree(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, typeOf[Int] ) + case "%s" => createTempValDef( paramsStack.pop, typeOf[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( TermName("scala") ) + , TermName("Predef") + ) + , TermName("print") + ) + , List(ref) + ): Tree + Block((evals ++ prints).toList, Literal(Constant(()))) + } +} |