Created
May 17, 2013 06:43
-
-
Save halcat0x15a/5597342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bits | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
object Macros { | |
def bits(n: Long) = macro impl | |
def impl(c: Context)(n: c.Expr[Long]): c.Tree = { | |
import c.universe._ | |
def bit(n: Long): c.Tree = | |
if (n == 0) | |
reify(H).tree | |
else if (n % 2 == 0) | |
Apply(reify(O).tree, List(bit(n / 2))) | |
else | |
Apply(reify(I).tree, List(bit(n / 2))) | |
n match { | |
case Expr(Literal(Constant(x: Int))) => bit(x) | |
case Expr(Literal(Constant(x: Long))) => bit(x) | |
} | |
} | |
} |
Author
halcat0x15a
commented
May 17, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment