Skip to content

Commit 833fadb

Browse files
committed
Add scripted test to test projects with quotes
1 parent 0918e04 commit 833fadb

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

compiler/test-resources/repl/i6007

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
scala> import quoted.Toolbox.Default._
2+
3+
scala> val v = '{ (if true then Some(1) else None).map(v => v+1) }
4+
val v: quoted.Expr[Option[Int]] = Expr(<pickled tasty>)
5+
6+
scala> v.show
7+
val res0: String = if (true) scala.Some.apply[scala.Int](1) else scala.None.map[scala.Int](((v: scala.Int) => v.+(1)))
8+
9+
scala> v.run
10+
val res1: Option[Int] = Some(2)

library/src/scala/quoted/Toolbox.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ object Toolbox {
1717

1818
def make(implicit settings: Settings): Toolbox = {
1919
val cl = getClass.getClassLoader
20+
println(">>>>>")
21+
cl.asInstanceOf[java.net.URLClassLoader].getURLs().toList.foreach(println)
22+
2023
try {
2124
val toolboxImplCls = cl.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
2225
val makeMeth = toolboxImplCls.getMethod("make", classOf[Settings])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
scalaVersion := sys.props("plugin.scalaVersion")
2+
3+
libraryDependencies ++= Seq(
4+
"ch.epfl.lamp" % "dotty_0.14" % scalaVersion.value,
5+
"ch.epfl.lamp" % "dotty_0.14" % scalaVersion.value % "test->runtime",
6+
"com.novocode" % "junit-interface" % "0.11" % "test"
7+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package hello
2+
3+
// Import Expr and some extension methods
4+
import scala.quoted._
5+
6+
object Main {
7+
8+
// Needed to run or show quotes
9+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
10+
11+
def main(args: Array[String]): Unit = {
12+
val square = stagedPower(2)
13+
println("3^2 = " + square(3))
14+
println("4.1^2 = " + square(4.1))
15+
println()
16+
val cube = stagedPower(3)
17+
println("2.4^3 = " + cube(2.4))
18+
println()
19+
20+
val toTheFourth = stagedPower(4)
21+
println("3^4 = " + toTheFourth(3))
22+
println()
23+
}
24+
25+
def stagedPower(n: Int): Double => Double = {
26+
// Code representing the labmda where the recursion is unrolled based on the value of n
27+
val code = '{ (x: Double) => ${powerCode(n, '{x})}}
28+
29+
println(s"staged power for n=" + n + ":")
30+
println(code.show)
31+
32+
// Evaluate the contents of the code and return it's value
33+
code.run
34+
}
35+
36+
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
37+
if (n == 0) '{1.0}
38+
else if (n == 1) x
39+
else if (n < 0) throw new Exception("Negative powers not implemented. Left as a small exercise. Dont be shy, try it out.")
40+
else if (n == 2) '{$x * $x}
41+
else if (n % 2 == 1) '{$x * ${powerCode(n - 1, x)}}
42+
else '{ val y = $x * $x; ${powerCode(n / 2, '{y})}}
43+
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> run

0 commit comments

Comments
 (0)