@@ -149,6 +149,39 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
149
149
this
150
150
}
151
151
152
+ case While (cond, stats) =>
153
+ this += " while ("
154
+ printTree(cond)
155
+ this += " ) "
156
+ stats match {
157
+ case stat :: Nil =>
158
+ printTree(stat)
159
+ case stats =>
160
+ this += " {"
161
+ indented {
162
+ this += lineBreak()
163
+ printTrees(stats, lineBreak())
164
+ }
165
+ this += lineBreak() += " }"
166
+ }
167
+
168
+ case DoWhile (stats, cond) =>
169
+ this += " do "
170
+ stats match {
171
+ case stat :: Nil =>
172
+ printTree(stat)
173
+ case stats =>
174
+ this += " {"
175
+ indented {
176
+ this += lineBreak()
177
+ printTrees(stats, lineBreak())
178
+ }
179
+ this += lineBreak() += " }"
180
+ }
181
+ this += " while ("
182
+ printTree(cond)
183
+ this += " )"
184
+
152
185
case ddef@ DefDef (name, targs, argss, tpt, rhs) =>
153
186
val flags = ddef.flags
154
187
if (flags.isOverride) sb.append(" override " )
@@ -229,7 +262,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
229
262
this += " = "
230
263
printTree(rhs)
231
264
232
- case Term .Block (stats, expr) =>
265
+ case Term .Block (stats0, expr) =>
266
+ def isLoopEntryPoint (tree : Tree ): Boolean = tree match {
267
+ case Term .Apply (Term .Ident (" while$" | " doWhile$" ), _) => true
268
+ case _ => false
269
+ }
270
+
271
+ val stats = stats0.filterNot(isLoopEntryPoint)
272
+
233
273
expr match {
234
274
case Term .Lambda (_, _) =>
235
275
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
@@ -239,31 +279,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
239
279
this += " => "
240
280
printTree(rhs)
241
281
this += " )"
242
-
243
- case Term .Apply (Term .Ident (" while$" ), _) =>
244
- val DefDef (" while$" , _, _, _, Some (Term .If (cond, Term .Block (body :: Nil , _), _))) = stats.head
245
- this += " while ("
246
- printTree(cond)
247
- this += " ) "
248
- printTree(body)
249
-
250
- case Term .Apply (Term .Ident (" doWhile$" ), _) =>
251
- val DefDef (" doWhile$" , _, _, _, Some (Term .Block (List (body), Term .If (cond, _, _)))) = stats.head
252
- this += " do "
253
- printTree(body)
254
- this += " while ("
255
- printTree(cond)
256
- this += " )"
257
-
258
282
case _ =>
259
283
this += " {"
260
284
indented {
261
285
if (! stats.isEmpty) {
262
286
this += lineBreak()
263
287
printTrees(stats, lineBreak())
264
288
}
265
- this += lineBreak()
266
- printTree(expr)
289
+ if (! isLoopEntryPoint(expr)) {
290
+ this += lineBreak()
291
+ printTree(expr)
292
+ }
267
293
}
268
294
this += lineBreak() += " }"
269
295
}
@@ -739,6 +765,22 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
739
765
}
740
766
}
741
767
768
+ private object While {
769
+ def unapply (arg : Tree )(implicit ctx : Context ): Option [(Term , List [Statement ])] = arg match {
770
+ case DefDef (" while$" , _, _, _, Some (Term .If (cond, Term .Block (bodyStats, _), _))) => Some ((cond, bodyStats))
771
+ case Term .Block (List (tree), _) => unapply(tree)
772
+ case _ => None
773
+ }
774
+ }
775
+
776
+ private object DoWhile {
777
+ def unapply (arg : Tree )(implicit ctx : Context ): Option [(List [Statement ], Term )] = arg match {
778
+ case DefDef (" doWhile$" , _, _, _, Some (Term .Block (body, Term .If (cond, _, _)))) => Some ((body, cond))
779
+ case Term .Block (List (tree), _) => unapply(tree)
780
+ case _ => None
781
+ }
782
+ }
783
+
742
784
// TODO Provide some of these in scala.tasty.Tasty.scala and implement them using checks on symbols for performance
743
785
private object Types {
744
786
0 commit comments