File tree Expand file tree Collapse file tree 19 files changed +244
-75
lines changed
compiler/src/dotty/tools/dotc/quoted Expand file tree Collapse file tree 19 files changed +244
-75
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,8 @@ class QuoteDriver extends Driver {
44
44
def show (tree : Tree , ctx : Context ): String = {
45
45
val printer = new DecompilerPrinter (ctx)
46
46
val pageWidth = ctx.settings.pageWidth.value(ctx)
47
- printer.toText(tree).mkString(pageWidth, false )
47
+ val tree1 = (new TreeCleaner ).transform(tree)(ctx)
48
+ printer.toText(tree1).mkString(pageWidth, false )
48
49
}
49
50
withTree(expr, show, settings)
50
51
}
Original file line number Diff line number Diff line change
1
+ package dotty .tools .dotc .quoted
2
+
3
+ import dotty .tools .dotc .ast .Trees ._
4
+ import dotty .tools .dotc .ast .tpd
5
+ import dotty .tools .dotc .core .Contexts ._
6
+ import dotty .tools .dotc .core .Constants ._
7
+
8
+ /** Clean up quote artifacts from the tree to make it simpler to read.
9
+ * - Flattens block and remove blocks with not statements
10
+ */
11
+ class TreeCleaner extends tpd.TreeMap {
12
+ import tpd ._
13
+
14
+ override def transform (tree : Tree )(implicit ctx : Context ): Tree = super .transform(tree) match {
15
+ case Block (Nil , expr1) => expr1
16
+ case Block (stats1, expr1) =>
17
+ val flatStats = stats1.flatMap {
18
+ case Block (stats2, expr2) => stats2 ::: expr2 :: Nil
19
+ case Literal (Constant (())) => Nil
20
+ case stat => stat :: Nil
21
+ }
22
+ expr1 match {
23
+ case Block (stats3, expr3) => Block (flatStats ::: stats3, expr3)
24
+ case expr3 => Block (flatStats, expr3)
25
+ }
26
+ case tree1 => tree1
27
+ }
28
+ }
Original file line number Diff line number Diff line change 1
1
1.0
2
2
5.0
3
3
{
4
+ val y: Double = 5.0.*(5.0)
5
+ y
6
+ }
7
+ 5.0.*(
4
8
{
5
9
val y: Double = 5.0.*(5.0)
6
10
y
7
11
}
8
- }
9
- {
10
- 5.0.*(
11
- {
12
- {
13
- val y: Double = 5.0.*(5.0)
14
- y
15
- }
16
- }
17
- )
18
- }
12
+ )
Original file line number Diff line number Diff line change
1
+ 3
2
+ 4
3
+ abc
4
+ null
5
+ OK
Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+
3
+ import dotty .tools .dotc .quoted .Toolbox ._
4
+
5
+ object Test {
6
+
7
+ def main (args : Array [String ]): Unit = {
8
+ (3 : Expr [Int ]) match { case Constant (n) => println(n) }
9
+ '(4) match { case Constant(n) => println(n) }
10
+ '("abc") match { case Constant(n) => println(n) }
11
+ '(null) match { case Constant(n) => println(n) }
12
+
13
+ '(new Object) match { case Constant(n) => println(n); case _ => println("OK") }
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ val y: Double = 3.0.*(3.0)
3
+ y
4
+ }
5
+ 9.0
Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+
3
+ import dotty .tools .dotc .quoted .Toolbox ._
4
+
5
+ object Test {
6
+
7
+ def main (args : Array [String ]): Unit = {
8
+ // 2 is a lifted constant
9
+ println(power(2 , 3.0 ).show)
10
+ println(power(2 , 3.0 ).run)
11
+ }
12
+
13
+ def power (n : Expr [Int ], x : Expr [Double ]): Expr [Double ] = {
14
+ n match {
15
+ case Constant (n1) => powerCode(n1, x)
16
+ case _ => ' { dynamicPower(~ n, ~ x) }
17
+ }
18
+ }
19
+
20
+ private def powerCode (n : Int , x : Expr [Double ]): Expr [Double ] =
21
+ if (n == 0 ) '(1.0)
22
+ else if (n == 1 ) x
23
+ else if (n % 2 == 0 ) ' { { val y = ~ x * ~ x; ~ powerCode(n / 2 , '(y)) } }
24
+ else ' { ~ x * ~ powerCode(n - 1 , x) }
25
+
26
+ def dynamicPower (n : Int , x : Double ): Double =
27
+ if (n == 0 ) 1.0
28
+ else if (n % 2 == 0 ) dynamicPower(n / 2 , x * x)
29
+ else x * dynamicPower(n - 1 , x)
30
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ val y: Double = 3.0.*(3.0)
3
+ y
4
+ }
5
+ 9.0
Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+
3
+ import dotty .tools .dotc .quoted .Toolbox ._
4
+
5
+ object Test {
6
+
7
+ def main (args : Array [String ]): Unit = {
8
+ // 2 is a lifted constant
9
+ println(power(2 , 3.0 ).show)
10
+ println(power(2 , 3.0 ).run)
11
+ }
12
+
13
+ def power (n : Expr [Int ], x : Expr [Double ]): Expr [Double ] = {
14
+ n match {
15
+ case Constant (n1) => powerCode(n1, x)
16
+ case _ => ' { dynamicPower(~ n, ~ x) }
17
+ }
18
+ }
19
+
20
+ private def powerCode (n : Int , x : Expr [Double ]): Expr [Double ] =
21
+ if (n == 0 ) '(1.0)
22
+ else if (n == 1 ) x
23
+ else if (n % 2 == 0 ) ' { { val y = ~ x * ~ x; ~ powerCode(n / 2 , '(y)) } }
24
+ else ' { ~ x * ~ powerCode(n - 1 , x) }
25
+
26
+ def dynamicPower (n : Int , x : Double ): Double =
27
+ if (n == 0 ) 1.0
28
+ else if (n % 2 == 0 ) dynamicPower(n / 2 , x * x)
29
+ else x * dynamicPower(n - 1 , x)
30
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ val y: Double = 4.0.*(4.0)
3
+ y
4
+ }
5
+ 16.0
You can’t perform that action at this time.
0 commit comments