Skip to content
Browse files

Centralize the anti bakery-of-doom device

  • Loading branch information...
1 parent 95a0505 commit f0197ab2bab30d21200fd0cb74298a5902e9353d @retronym committed Feb 19, 2012
View
75 src/main/scala/com/github/retronym/macrocosm/Macrocosm.scala
@@ -2,18 +2,20 @@ package com.github.retronym.macrocosm
object Macrocosm {
def macro showTree(a: Any): String = {
+ val util = Util(_context); import util._
+
val s = show(a: Tree)
- Literal(Constant(s))
+ stringLit(s)
}
def macro assert1(c: Boolean) = {
- implicit val __context: _context.type = _context
+ val util = Util(_context); import util._
Apply(predefAssert, List[Tree](c, stringLit(show(c))))
}
def macro assert2(c: Boolean) = {
- implicit val __context: _context.type = _context
+ val util = Util(_context); import util._
val cTree: Tree = c
val pos = c.pos.asInstanceOf[scala.tools.nsc.util.OffsetPosition]
@@ -28,44 +30,59 @@ object Macrocosm {
def macro trace(c: Any) = {
- implicit val __context: _context.type = _context
+ val util = Util(_context); import util._
object tracingTransformer extends Transformer {
- override def transform(tree: Tree): Tree = tree match {
- case a @ Apply(qual, args) =>
- val tempValName = newTermName(nextName)
- val sub = Apply(transform(qual), args.map(a => transform(a)))
- Block (
- List(
- ValDef(Modifiers(), tempValName, TypeTree(sub.tpe), sub),
- Apply(predefPrint, List(stringLit(show(a) + " = "))),
- Apply(predefPrintln, List(Ident(tempValName)))
- ),
- Ident(tempValName)
- )
- case _ => super.transform(tree)
+ //val symTree = mutable.buffer[SymTree]
+ override def transform(tree: Tree): Tree = {
+ tree match {
+ case a @ Apply(qual, args) =>
+ val tempValName = newTermName(nextName)
+ //val tempValSymbol = newValue(tempValName)
+ val sub = Apply(transform(qual), args.map(a => transform(a)))
+ Block(
+ List(
+ new _context.ValDef(Modifiers(), tempValName, TypeTree(sub.tpe), sub),
+ Apply(predefPrint, List(stringLit(show(a) + " = "))),
+ Apply(predefPrintln, List(Ident(tempValName)))
+ ),
+ Ident(tempValName)
+ )
+ case _ => super.transform(tree)
+ }
}
}
val t = tracingTransformer.transform(c)
//println("t = " + t)
t
}
- private[this] def predefAssert(implicit context: scala.reflect.macro.Context): context.Tree =
- predefSelect("assert")
- private[this] def predefPrintln(implicit context: scala.reflect.macro.Context): context.Tree =
- predefSelect("println")
+ import scala.reflect.macro.Context
- private[this] def predefPrint(implicit context: scala.reflect.macro.Context): context.Tree =
- predefSelect("print")
+ implicit def Util(context: Context) = new Util[context.type](context)
- private[this] def predefSelect(name: String)(implicit context: scala.reflect.macro.Context): context.Tree = {
+ class Util[C <: Context with Singleton](val context: C) {
import context._
- Select(Select(Ident(newTermName("scala")), newTermName("Predef")), newTermName(name))
- }
+
+ def id(a: Tree): Tree = a
+
+ def predefAssert: Tree =
+ predefSelect("assert")
- private[this] def stringLit(s: String)(implicit context: scala.reflect.macro.Context): context.Tree = {
- import context._
- Literal(Constant(s))
+ def predefPrintln: Tree =
+ predefSelect("println")
+
+ def predefPrint: Tree =
+ predefSelect("print")
+
+ def predefSelect(name: String): Tree = {
+ import context._
+ Select(Select(Ident(newTermName("scala")), newTermName("Predef")), newTermName(name))
+ }
+
+ def stringLit(s: String): Tree = {
+ import context._
+ Literal(Constant(s))
+ }
}
}
View
3 src/test/scala/com/github/retronym/macrocosm/MacrocosmTest.scala
@@ -22,5 +22,6 @@ object MacrocosmTest extends App {
//println(showTree(plus(1, 2)))
val i: Int = trace(plus(1, plus(2, 3)))
- //trace("foo".toString.toString)
+
+ //trace("foo".toString.toString)
}

0 comments on commit f0197ab

Please sign in to comment.
Something went wrong with that request. Please try again.