Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.66 KB | None | 0 0
  1. val evals = ListBuffer[ValDef]()
  2. def precompute(value: Tree, tpe: Type): Ident = {
  3.   val freshName = TermName(c.fresh("eval$"))
  4.   evals += ValDef(Modifiers(), freshName, TypeTree(tpe), value)
  5.   Ident(freshName)
  6. }
  7. val paramsStack = Stack[Tree]((params map (_.tree)): _*)
  8. val refs = s_format.split("(?<=%[\\w%])|(?=%[\\w%])") map {
  9.   case "%d" => precompute(paramsStack.pop, typeOf[Int])
  10.   case "%s" => precompute(paramsStack.pop, typeOf[String])
  11.   case "%%" => Literal(Constant("%"))
  12.   case part => Literal(Constant(part))
  13. }
  14. val stats = evals ++ refs.map(ref => reify(print(c.Expr[Any](ref).splice)).tree)
  15. c.Expr[Unit](Block(stats.toList, Literal(Constant(()))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement