File tree 19 files changed +244
-75
lines changed
compiler/src/dotty/tools/dotc/quoted 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
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
+ // n is a lifted constant
9
+ val n = 2
10
+ println(power(n, 4.0 ).show)
11
+ println(power(n, 4.0 ).run)
12
+ }
13
+
14
+ def power (n : Expr [Int ], x : Expr [Double ]): Expr [Double ] = {
15
+ n match {
16
+ case Constant (n1) => powerCode(n1, x)
17
+ case _ => ' { dynamicPower(~ n, ~ x) }
18
+ }
19
+ }
20
+
21
+ private def powerCode (n : Int , x : Expr [Double ]): Expr [Double ] =
22
+ if (n == 0 ) '(1.0)
23
+ else if (n == 1 ) x
24
+ else if (n % 2 == 0 ) ' { { val y = ~ x * ~ x; ~ powerCode(n / 2 , '(y)) } }
25
+ else ' { ~ x * ~ powerCode(n - 1 , x) }
26
+
27
+ def dynamicPower (n : Int , x : Double ): Double =
28
+ if (n == 0 ) 1.0
29
+ else if (n % 2 == 0 ) dynamicPower(n / 2 , x * x)
30
+ else x * dynamicPower(n - 1 , x)
31
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ val y: Double = 5.0.*(5.0)
3
+ y
4
+ }
5
+ 25.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
+ // n is a constant in a quote
9
+ println(power('(2), 5.0).show)
10
+ println(power('(2), 5.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
+ Test.dynamicPower(
2
+ {
3
+ println("foo")
4
+ 2
5
+ }
6
+ , 6.0)
7
+ foo
8
+ 36.0
Original file line number Diff line number Diff line change @@ -5,27 +5,6 @@ import dotty.tools.dotc.quoted.Toolbox._
5
5
object Test {
6
6
7
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
-
16
- // 2 is a lifted constant
17
- println(power(2 , 3.0 ).show)
18
- println(power(2 , 3.0 ).run)
19
-
20
- // n is a lifted constant
21
- val n = 2
22
- println(power(n, 4.0 ).show)
23
- println(power(n, 4.0 ).run)
24
-
25
- // n is a constant in a quote
26
- println(power('(2), 5.0).show)
27
- println(power('(2), 5.0).run)
28
-
29
8
// n2 is clearly not a constant
30
9
val n2 = ' { println(" foo" ); 2 }
31
10
println(power(n2, 6.0 ).show)
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
{
2
- {
3
- (x: Int) =>
4
- {
5
- 2.+(x).+(4)
6
- }
7
- }
2
+ (x: Int) => 2.+(x).+(4)
8
3
}
9
4
6
10
5
8
Original file line number Diff line number Diff line change
1
+ {
2
+ println(1)
3
+ println(2)
4
+ println(3)
5
+ println(4)
6
+ println(5)
7
+ ()
8
+ }
9
+ {
10
+ println(5)
11
+ println(4)
12
+ println(3)
13
+ println(2)
14
+ println(1)
15
+ }
Original file line number Diff line number Diff line change
1
+
2
+ import dotty .tools .dotc .quoted .Toolbox ._
3
+
4
+ import scala .quoted ._
5
+ import scala .quoted .Liftable ._
6
+
7
+ object Test {
8
+ def main (args : Array [String ]): Unit = {
9
+
10
+ def a (n : Int , x : Expr [Unit ]): Expr [Unit ] =
11
+ if (n == 0 ) x
12
+ else a(n - 1 , ' { println(~ n.toExpr); ~ x })
13
+
14
+ println(a(5 , ()).show)
15
+
16
+
17
+ def b (n : Int , x : Expr [Unit ]): Expr [Unit ] =
18
+ if (n == 0 ) x
19
+ else b(n - 1 , ' { ~ x; println(~ n.toExpr) })
20
+
21
+ println(b(5 , ()).show)
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments