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

0 commit comments

Comments
 (0)