Skip to content

Commit ee98e8f

Browse files
committed
wip
1 parent b57943d commit ee98e8f

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

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)