@@ -145,6 +145,39 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
145
145
this
146
146
}
147
147
148
+ case While (cond, stats) =>
149
+ this += " while ("
150
+ printTree(cond)
151
+ this += " ) "
152
+ stats match {
153
+ case stat :: Nil =>
154
+ printTree(stat)
155
+ case stats =>
156
+ this += " {"
157
+ indented {
158
+ this += lineBreak()
159
+ printTrees(stats, lineBreak())
160
+ }
161
+ this += lineBreak() += " }"
162
+ }
163
+
164
+ case DoWhile (stats, cond) =>
165
+ this += " do "
166
+ stats match {
167
+ case stat :: Nil =>
168
+ printTree(stat)
169
+ case stats =>
170
+ this += " {"
171
+ indented {
172
+ this += lineBreak()
173
+ printTrees(stats, lineBreak())
174
+ }
175
+ this += lineBreak() += " }"
176
+ }
177
+ this += " while ("
178
+ printTree(cond)
179
+ this += " )"
180
+
148
181
case ddef@ DefDef (name, targs, argss, tpt, rhs) =>
149
182
val flags = ddef.flags
150
183
if (flags.isOverride) sb.append(" override " )
@@ -225,7 +258,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
225
258
this += " = "
226
259
printTree(rhs)
227
260
228
- case Term .Block (stats, expr) =>
261
+ case Term .Block (stats0, expr) =>
262
+ def isLoopEntryPoint (tree : Tree ): Boolean = tree match {
263
+ case Term .Apply (Term .Ident (" while$" | " doWhile$" ), _) => true
264
+ case _ => false
265
+ }
266
+
267
+ val stats = stats0.filterNot(isLoopEntryPoint)
268
+
229
269
expr match {
230
270
case Term .Lambda (_, _) =>
231
271
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
@@ -235,31 +275,22 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
235
275
this += " => "
236
276
printTree(rhs)
237
277
this += " )"
238
-
239
- case Term .Apply (Term .Ident (" while$" ), _) =>
240
- val DefDef (" while$" , _, _, _, Some (Term .If (cond, Term .Block (body :: Nil , _), _))) = stats.head
241
- this += " while ("
242
- printTree(cond)
243
- this += " ) "
244
- printTree(body)
245
-
246
- case Term .Apply (Term .Ident (" doWhile$" ), _) =>
247
- val DefDef (" doWhile$" , _, _, _, Some (Term .Block (List (body), Term .If (cond, _, _)))) = stats.head
248
- this += " do "
249
- printTree(body)
250
- this += " while ("
251
- printTree(cond)
252
- this += " )"
253
-
278
+ case expr if isLoopEntryPoint(expr) && stats.size == 1 =>
279
+ // Print { def while$() = ...; while$() }
280
+ // as while (...) ...
281
+ // instead of { while (...) ... }
282
+ printTree(stats.head)
254
283
case _ =>
255
284
this += " {"
256
285
indented {
257
286
if (! stats.isEmpty) {
258
287
this += lineBreak()
259
288
printTrees(stats, lineBreak())
260
289
}
261
- this += lineBreak()
262
- printTree(expr)
290
+ if (! isLoopEntryPoint(expr)) {
291
+ this += lineBreak()
292
+ printTree(expr)
293
+ }
263
294
}
264
295
this += lineBreak() += " }"
265
296
}
@@ -719,6 +750,20 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
719
750
}
720
751
}
721
752
753
+ private object While {
754
+ def unapply (arg : Tree )(implicit ctx : Context ): Option [(Term , List [Statement ])] = arg match {
755
+ case DefDef (" while$" , _, _, _, Some (Term .If (cond, Term .Block (bodyStats, _), _))) => Some ((cond, bodyStats))
756
+ case _ => None
757
+ }
758
+ }
759
+
760
+ private object DoWhile {
761
+ def unapply (arg : Tree )(implicit ctx : Context ): Option [(List [Statement ], Term )] = arg match {
762
+ case DefDef (" doWhile$" , _, _, _, Some (Term .Block (body, Term .If (cond, _, _)))) => Some ((body, cond))
763
+ case _ => None
764
+ }
765
+ }
766
+
722
767
// TODO Provide some of these in scala.tasty.Tasty.scala and implement them using checks on symbols for performance
723
768
private object Types {
724
769
0 commit comments