@@ -646,6 +646,17 @@ trait Printers
646646              printTree(body)
647647          }
648648
649+         case  IsDefDef (ddef @  DefDef (name, targs, argss, _, rhsOpt)) if  name.startsWith(" $anonfun" => 
650+           //  Decompile lambda definition
651+           assert(targs.isEmpty)
652+           val  args  ::  Nil  =  argss
653+           val  Some (rhs) =  rhsOpt
654+           inParens {
655+             printArgsDefs(args)
656+             this  +=  "  => " 
657+             printTree(rhs)
658+           }
659+ 
649660        case  IsDefDef (ddef @  DefDef (name, targs, argss, tpt, rhs)) => 
650661          printDefAnnotations(ddef)
651662
@@ -777,34 +788,13 @@ trait Printers
777788            case  IsValDef (tree) =>  ! tree.symbol.flags.isObject
778789            case  _ =>  true 
779790          }
791+           printFlatBlock(stats, expr)
780792
781-           expr match  {
782-             case  Term .Lambda (_, _) => 
783-               //  Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
784-               assert(stats.size ==  1 )
785-               val  DefDef (_, _, args ::  Nil , _, Some (rhs)) ::  Nil  =  stats
786-               inParens {
787-                 printArgsDefs(args)
788-                 this  +=  "  => " 
789-                 printTree(rhs)
790-               }
791-             case  _ => 
792-               this  +=  " {" 
793-               indented {
794-                 printStats(stats, expr)
795-               }
796-               this  +=  lineBreak() +=  " }" 
797-           }
798- 
799-         case  Term .Inlined (call, bindings, expansion) =>  //  FIXME: Don't print Inlined with empty calls?
800-           this  +=  " { // inlined" 
801-           indented {
802-             printStats(bindings, expansion)
803-           }
804-           this  +=  lineBreak() +=  " }" 
793+         case  Term .Inlined (_, bindings, expansion) => 
794+           printFlatBlock(bindings, expansion)
805795
806796        case  Term .Lambda (meth, tpt) => 
807-           //  Printed in Term.Block branch 
797+           //  Printed in by it's DefDef 
808798          this 
809799
810800        case  Term .If (cond, thenp, elsep) => 
@@ -847,15 +837,69 @@ trait Printers
847837
848838      }
849839
840+       def  flatBlock (stats : List [Statement ], expr : Term ):  (List [Statement ], Term ) =  {
841+         val  flatStats  =  List .newBuilder[Statement ]
842+         def  extractFlatStats (stat : Statement ):  Unit  =  stat match  {
843+           case  Term .Block (stats1, expr1) => 
844+             stats1.foreach(extractFlatStats)
845+             extractFlatStats(expr1)
846+           case  Term .Inlined (_, bindings, expansion) => 
847+             bindings.foreach(extractFlatStats)
848+             extractFlatStats(expansion)
849+           case  Term .Literal (Constant .Unit ()) =>  //  ignore
850+           case  stat =>  flatStats +=  stat
851+         }
852+         def  extractFlatExpr (term : Term ):  Term  =  term match  {
853+           case  Term .Block (stats1, expr1) => 
854+             stats1.foreach(extractFlatStats)
855+             extractFlatExpr(expr1)
856+           case  Term .Inlined (_, bindings, expansion) => 
857+             bindings.foreach(extractFlatStats)
858+             extractFlatExpr(expansion)
859+           case  term =>  term
860+         }
861+         stats.foreach(extractFlatStats)
862+         val  flatExpr  =  extractFlatExpr(expr)
863+         (flatStats.result(), flatExpr)
864+       }
865+       def  printFlatBlock (stats : List [Statement ], expr : Term ):  Buffer  =  {
866+         val  (stats1, expr1) =  flatBlock(stats, expr)
867+         //  Remove Term.Lambda nodes, lambdas are printed by their definition
868+         val  stats2  =  stats1.filter { case  Term .Lambda (_, _) =>  false ; case  _ =>  true  }
869+         val  (stats3, expr3) =  expr1 match  {
870+           case  Term .Lambda (_, _) => 
871+             val  init  :+  last  =  stats2
872+             (init, last)
873+           case  _ =>  (stats2, expr1)
874+         }
875+         if  (stats3.isEmpty) {
876+           printTree(expr3)
877+         } else  {
878+           this  +=  " {" 
879+           indented {
880+             printStats(stats3, expr3)
881+           }
882+           this  +=  lineBreak() +=  " }" 
883+         }
884+       }
885+ 
850886      def  printStats (stats : List [Tree ], expr : Tree ):  Unit  =  {
851887        def  printSeparator (next : Tree ):  Unit  =  {
852888          //  Avoid accidental application of opening `{` on next line with a double break
889+           def  rec (next : Tree ):  Unit  =  next match  {
890+             case  Term .Block (stats, _) if  stats.nonEmpty =>  this  +=  doubleLineBreak()
891+             case  Term .Inlined (_, bindings, _) if  bindings.nonEmpty =>  this  +=  doubleLineBreak()
892+             case  Term .Select (qual, _, _) =>  rec(qual)
893+             case  Term .Apply (fn, _) =>  rec(fn)
894+             case  Term .TypeApply (fn, _) =>  rec(fn)
895+             case  _ =>  this  +=  lineBreak()
896+           }
853897          next match  {
854-             case  Term . Block (_, _ ) =>   this   +=  doubleLineBreak() 
855-             case   Term . Inlined (_, _, _)  =>   this   +=  doubleLineBreak() 
856-             case  Term . Select (qual,  _, _) =>  printSeparator(qual )
857-             case  Term . Apply (fn, _ ) =>  printSeparator(fn )
858-             case   Term . TypeApply (fn, _)  =>  printSeparator(fn) 
898+             case  IsTerm (term ) => 
899+               flatBlock( Nil , term)  match  { 
900+                  case  (next  ::   _, _) =>  rec(next )
901+                  case  ( Nil , next ) =>  rec(next )
902+               } 
859903            case  _ =>  this  +=  lineBreak()
860904          }
861905        }
0 commit comments