From ad92037d3437a902dff30c2239607a237c28a910 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Tue, 18 Sep 2012 14:01:19 -0400 Subject: [PATCH] sample usage of separate macro configuration --- .../compiler-project/macro-config/build.sbt | 26 +++++++++++++++++++ .../macro-config/src/macro/scala/Def.scala | 16 ++++++++++++ .../macro-config/src/main/scala/Use.scala | 8 ++++++ .../compiler-project/macro-config/test | 2 ++ 4 files changed, 52 insertions(+) create mode 100644 sbt/src/sbt-test/compiler-project/macro-config/build.sbt create mode 100644 sbt/src/sbt-test/compiler-project/macro-config/src/macro/scala/Def.scala create mode 100644 sbt/src/sbt-test/compiler-project/macro-config/src/main/scala/Use.scala create mode 100644 sbt/src/sbt-test/compiler-project/macro-config/test diff --git a/sbt/src/sbt-test/compiler-project/macro-config/build.sbt b/sbt/src/sbt-test/compiler-project/macro-config/build.sbt new file mode 100644 index 000000000..2d0ecc8cb --- /dev/null +++ b/sbt/src/sbt-test/compiler-project/macro-config/build.sbt @@ -0,0 +1,26 @@ +// Adds a "macro" configuration for macro dependencies. +// By default, this includes the dependencies of the normal sources. +// Drop the `extend(Compile)` to include no dependencies (not even scala-library) by default. +ivyConfigurations += config("macro").hide.extend(Compile) + +// add the compiler as a dependency for src/macro/ +libraryDependencies <+= + scalaVersion("org.scala-lang" % "scala-compiler" % _ % "macro") + +// adds standard compile, console, package tasks for src/macro/ +inConfig(config("macro"))(Defaults.configSettings) + +// puts the compiled macro on the classpath for the main sources +unmanagedClasspath in Compile <++= + fullClasspath in config("macro") + +// includes sources in src/macro/ in the main source package +mappings in (Compile, packageSrc) <++= + mappings in (config("macro"), packageSrc) + +// Includes classes compiled from src/macro/ in the main binary +// This can be omitted if the classes in src/macro/ aren't used at runtime +mappings in (Compile, packageBin) <++= + mappings in (config("macro"), packageBin) + +scalaVersion := "2.10.0-M7" \ No newline at end of file diff --git a/sbt/src/sbt-test/compiler-project/macro-config/src/macro/scala/Def.scala b/sbt/src/sbt-test/compiler-project/macro-config/src/macro/scala/Def.scala new file mode 100644 index 000000000..59e22ecb9 --- /dev/null +++ b/sbt/src/sbt-test/compiler-project/macro-config/src/macro/scala/Def.scala @@ -0,0 +1,16 @@ +import scala.reflect.macros.Context +import language.experimental.macros + +object Def +{ + def desugar(a: Any): String = macro desugarImpl + + def desugarImpl(c: Context)(a: c.Expr[Any]) = { + import c.universe._ + + val s = show(a.tree) + c.Expr( + Literal(Constant(s)) + ) + } +} \ No newline at end of file diff --git a/sbt/src/sbt-test/compiler-project/macro-config/src/main/scala/Use.scala b/sbt/src/sbt-test/compiler-project/macro-config/src/main/scala/Use.scala new file mode 100644 index 000000000..c2be57a1a --- /dev/null +++ b/sbt/src/sbt-test/compiler-project/macro-config/src/main/scala/Use.scala @@ -0,0 +1,8 @@ +object Use +{ + def main(args: Array[String]) + { + val str = Def.desugar(3 + 5) + assert(str == args(0), s"Expected '${args(0)}', got '$str'") + } +} \ No newline at end of file diff --git a/sbt/src/sbt-test/compiler-project/macro-config/test b/sbt/src/sbt-test/compiler-project/macro-config/test new file mode 100644 index 000000000..973515cd6 --- /dev/null +++ b/sbt/src/sbt-test/compiler-project/macro-config/test @@ -0,0 +1,2 @@ +> run 8 +-> run 9