Skip to content

Commit 8391c9e

Browse files
committed
Decompile [do] while loops
1 parent 4c8a2bd commit 8391c9e

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.printing.Texts._
77
import dotty.tools.dotc.core.Contexts._
88
import dotty.tools.dotc.core.Flags._
99
import dotty.tools.dotc.core.Symbols._
10+
import dotty.tools.dotc.core.StdNames._
1011

1112
import scala.language.implicitConversions
1213

@@ -16,7 +17,13 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1617
annots.filter(_.tpe != defn.SourceFileAnnotType)
1718

1819
override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
19-
super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
20+
trees match {
21+
case DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: y :: Nil if y.symbol.name == nme.WHILE_PREFIX =>
22+
keywordText("while") ~ " (" ~ toText(cond) ~ ")" ~ toText(body)
23+
case DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: y :: Nil if y.symbol.name == nme.DO_WHILE_PREFIX =>
24+
keywordText("do") ~ toText(body) ~ keywordText("while") ~ " (" ~ toText(cond) ~ ")"
25+
case _ => super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
26+
}
2027
}
2128

2229
override protected def packageDefText(tree: PackageDef): Text = {

tests/pos/simpleDoWhile.decompiled

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
================================================================================
2+
out/posTestFromTasty/pos/simpleDoWhile/Foo.class
3+
--------------------------------------------------------------------------------
4+
package <empty> {
5+
class Foo() extends Object() {
6+
def foo: Unit =
7+
{
8+
var i: Int = 1
9+
do
10+
{
11+
i = 0
12+
}
13+
while (i.!=(0))
14+
}
15+
}
16+
}
17+
--------------------------------------------------------------------------------

tests/pos/simpleDoWhile.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Foo {
2+
def foo = {
3+
var i = 1
4+
do {
5+
i = 0
6+
} while (i != 0)
7+
}
8+
}

tests/pos/simpleWhile.decompiled

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
================================================================================
2+
out/posTestFromTasty/pos/simpleWhile/Foo.class
3+
--------------------------------------------------------------------------------
4+
package <empty> {
5+
class Foo() extends Object() {
6+
def foo: Unit =
7+
{
8+
var i: Int = 1
9+
while (i.!=(0))
10+
{
11+
i = 0
12+
}
13+
}
14+
}
15+
}
16+
--------------------------------------------------------------------------------

tests/pos/simpleWhile.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Foo {
2+
def foo = {
3+
var i = 1
4+
while (i != 0) {
5+
i = 0
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)