Skip to content

Commit 2a34f06

Browse files
committed
Add setting to Expr.show to allow printing raw tree
1 parent 432dcfc commit 2a34f06

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ 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-
val tree1 = (new TreeCleaner).transform(tree)(ctx)
47+
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
4848
printer.toText(tree1).mkString(pageWidth, false)
4949
}
5050
withTree(expr, show, settings)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ object Toolbox {
5353

5454
}
5555

56-
class Settings[T] private (val outDir: Option[String], val compilerArgs: List[String])
56+
class Settings[T] private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
5757

5858
object Settings {
5959

@@ -69,19 +69,22 @@ object Toolbox {
6969
): Settings[Run] = {
7070
var compilerArgs1 = compilerArgs
7171
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
72-
new Settings(outDir, compilerArgs1)
72+
new Settings(outDir, false, compilerArgs1)
7373
}
7474

7575
/** Quote show settings
76+
* @param color Print output with colors
77+
* @param rawTree Do not remove quote tree artifacts
7678
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
7779
*/
7880
def show(
7981
color: Boolean = false,
82+
rawTree: Boolean = false,
8083
compilerArgs: List[String] = Nil
8184
): Settings[Show] = {
8285
var compilerArgs1 = compilerArgs
8386
compilerArgs1 = s"-color:${if (color) "always" else "never"}" :: compilerArgs1
84-
new Settings(None, compilerArgs1)
87+
new Settings(None, rawTree, compilerArgs1)
8588
}
8689

8790
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
println(1)
3+
{
4+
println(2)
5+
{
6+
println(3)
7+
{
8+
println(4)
9+
{
10+
println(5)
11+
()
12+
}
13+
}
14+
}
15+
}
16+
}
17+
{
18+
{
19+
{
20+
{
21+
{
22+
()
23+
println(5)
24+
}
25+
println(4)
26+
}
27+
println(3)
28+
}
29+
println(2)
30+
}
31+
println(1)
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
implicit val settings = Settings.show(rawTree = true)
10+
11+
def a(n: Int, x: Expr[Unit]): Expr[Unit] =
12+
if (n == 0) x
13+
else a(n - 1, '{ println(~n.toExpr); ~x })
14+
15+
println(a(5, ()).show)
16+
17+
18+
def b(n: Int, x: Expr[Unit]): Expr[Unit] =
19+
if (n == 0) x
20+
else b(n - 1, '{ ~x; println(~n.toExpr) })
21+
22+
println(b(5, ()).show)
23+
}
24+
25+
}

0 commit comments

Comments
 (0)