Skip to content

Commit 432dcfc

Browse files
committed
Flatten bocks in Expr.show
1 parent 8bd1625 commit 432dcfc

19 files changed

+244
-75
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class QuoteDriver extends Driver {
4444
def show(tree: Tree, ctx: Context): String = {
4545
val printer = new DecompilerPrinter(ctx)
4646
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)
4849
}
4950
withTree(expr, show, settings)
5051
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
1.0
22
5.0
33
{
4+
val y: Double = 5.0.*(5.0)
5+
y
6+
}
7+
5.0.*(
48
{
59
val y: Double = 5.0.*(5.0)
610
y
711
}
8-
}
9-
{
10-
5.0.*(
11-
{
12-
{
13-
val y: Double = 5.0.*(5.0)
14-
y
15-
}
16-
}
17-
)
18-
}
12+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
3
2+
4
3+
abc
4+
null
5+
OK
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
val y: Double = 3.0.*(3.0)
3+
y
4+
}
5+
9.0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
val y: Double = 3.0.*(3.0)
3+
y
4+
}
5+
9.0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
val y: Double = 4.0.*(4.0)
3+
y
4+
}
5+
16.0
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
val y: Double = 5.0.*(5.0)
3+
y
4+
}
5+
25.0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Test.dynamicPower(
2+
{
3+
println("foo")
4+
2
5+
}
6+
, 6.0)
7+
foo
8+
36.0

tests/pending/run-with-compiler/quote-run-constants-extract.scala renamed to tests/run-with-compiler/quote-run-constants-extract-6.scala

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,6 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
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-
298
// n2 is clearly not a constant
309
val n2 = '{ println("foo"); 2 }
3110
println(power(n2, 6.0).show)

tests/run-with-compiler/quote-run-constants-extract.check

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/run-with-compiler/quote-run-staged-interpreter.check

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
{
3-
(x: Int) =>
4-
{
5-
2.+(x).+(4)
6-
}
7-
}
2+
(x: Int) => 2.+(x).+(4)
83
}
94
6
105
8
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)