aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
blob: 65c64f708cc6faf6d5e0a10914c756eef16af86b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
package dotty.tools
package dotc
package repl

import java.io.{
  File, PrintWriter, PrintStream, StringWriter, Writer, OutputStream,
  ByteArrayOutputStream => ByteOutputStream
}
import java.lang.{Class, ClassLoader, Thread, System, StringBuffer}
import java.net.{URL, URLClassLoader}

import scala.collection.immutable.ListSet
import scala.collection.mutable
import scala.collection.mutable.{ListBuffer, HashSet, ArrayBuffer}

//import ast.parser.SyntaxAnalyzer
import io.{PlainFile, VirtualDirectory}
import scala.reflect.io.{PlainDirectory, Directory}
import reporting.{ConsoleReporter, Reporter}
import core.Flags
import util.{SourceFile, NameTransformer}
import io.ClassPath
import ast.Trees._
import parsing.Parsers._
import core._
import dotty.tools.backend.jvm.GenBCode
import Symbols._, Types._, Contexts._, StdNames._, Names._, NameOps._
import Decorators._
import scala.util.control.NonFatal
import printing.SyntaxHighlighting

/** An interpreter for Scala code which is based on the `dotc` compiler.
 *
 *  The overall approach is based on compiling the requested code and then
 *  using a Java classloader and Java reflection to run the code
 *  and access its results.
 *
 *  In more detail, a single compiler instance is used
 *  to accumulate all successfully compiled or interpreted Scala code.  To
 *  "interpret" a line of code, the compiler generates a fresh object that
 *  includes the line of code and which has public definition(s) to export
 *  all variables defined by that code.  To extract the result of an
 *  interpreted line to show the user, a second "result object" is created
 *  which imports the variables exported by the above object and then
 *  exports a single definition named "result".  To accommodate user expressions
 *  that read from variables or methods defined in previous statements, "import"
 *  statements are used.
 *
 * This interpreter shares the strengths and weaknesses of using the
 *  full compiler-to-Java.  The main strength is that interpreted code
 *  behaves exactly as does compiled code, including running at full speed.
 *  The main weakness is that redefining classes and methods is not handled
 *  properly, because rebinding at the Java level is technically difficult.
 *
 * @author Moez A. Abdel-Gawad
 * @author Lex Spoon
 * @author Martin Odersky
 *
 * @param out   The output to use for diagnostics
 * @param ictx  The context to use for initialization of the interpreter,
 *              needed to access the current classpath.
 */
class CompilingInterpreter(
  out: PrintWriter,
  ictx: Context,
  parentClassLoader: Option[ClassLoader]
) extends Compiler with Interpreter {
  import ast.untpd._
  import CompilingInterpreter._

  ictx.base.initialize()(ictx)

  /** directory to save .class files to */
  val virtualDirectory =
    if (ictx.settings.d.isDefault(ictx)) new VirtualDirectory("(memory)", None)
    else new PlainDirectory(new Directory(new java.io.File(ictx.settings.d.value(ictx)))) // for now, to help debugging

  /** A GenBCode phase that uses `virtualDirectory` for its output */
  private class REPLGenBCode extends GenBCode {
    override def outputDir(implicit ctx: Context) = virtualDirectory
  }

  /** Phases of this compiler use `REPLGenBCode` instead of `GenBCode`. */
  override def phases = Phases.replace(
    classOf[GenBCode], _ => new REPLGenBCode :: Nil, super.phases)

  /** whether to print out result lines */
  private var printResults: Boolean = true
  private var delayOutput: Boolean = false

  val previousOutput = ListBuffer.empty[String]

  override def lastOutput() = {
    val prev = previousOutput.toList
    previousOutput.clear()
    prev
  }

  override def delayOutputDuring[T](operation: => T): T = {
    val old = delayOutput
    try {
      delayOutput = true
      operation
    } finally {
      delayOutput = old
    }
  }

  /** Temporarily be quiet */
  override def beQuietDuring[T](operation: => T): T = {
    val wasPrinting = printResults
    try {
      printResults = false
      operation
    } finally {
      printResults = wasPrinting
    }
  }

  private def newReporter =
    new ConsoleReporter(Console.in, out) {
      override def printMessage(msg: String) =
        if (!delayOutput) {
          out.print(/*clean*/(msg) + "\n")
          // Suppress clean for now for compiler messages
          // Otherwise we will completely delete all references to
          // line$object$ module classes. The previous interpreter did not
          // have the project because the module class was written without the final `$'
          // and therefore escaped the purge. We can turn this back on once
          // we drop the final `$' from module classes.
          out.flush()
        } else {
          previousOutput += (/*clean*/(msg) + "\n")
        }
    }

  /** the previous requests this interpreter has processed */
  private val prevRequests = new ArrayBuffer[Request]()

  /** the compiler's classpath, as URL's */
  val compilerClasspath: List[URL] = ictx.platform.classPath(ictx).asURLs

  /* A single class loader is used for all commands interpreted by this Interpreter.
     It would also be possible to create a new class loader for each command
     to interpret.  The advantages of the current approach are:

       - Expressions are only evaluated one time.  This is especially
         significant for I/O, e.g. "val x = Console.readLine"

     The main disadvantage is:

       - Objects, classes, and methods cannot be rebound.  Instead, definitions
         shadow the old ones, and old code objects refer to the old
         definitions.
  */
  /** class loader used to load compiled code */
  val classLoader: ClassLoader = {
    lazy val parent = new URLClassLoader(compilerClasspath.toArray,
                                         classOf[Interpreter].getClassLoader)

    new AbstractFileClassLoader(virtualDirectory, parentClassLoader.getOrElse(parent))
  }

  // Set the current Java "context" class loader to this interpreter's class loader
  Thread.currentThread.setContextClassLoader(classLoader)

  /** Parse a line into a sequence of trees. Returns None if the input is incomplete. */
  private def parse(line: String)(implicit ctx: Context): Option[List[Tree]] = {
    var justNeedsMore = false
    val reporter = newReporter
    reporter.withIncompleteHandler { _ => _ => justNeedsMore = true } {
      // simple parse: just parse it, nothing else
      def simpleParse(code: String)(implicit ctx: Context): List[Tree] = {
        val source = new SourceFile("<console>", code.toCharArray())
        val parser = new Parser(source)
        val (selfDef, stats) = parser.templateStatSeq
        stats
      }
      val trees = simpleParse(line)(ctx.fresh.setReporter(reporter))
      if (reporter.hasErrors) {
        Some(Nil) // the result did not parse, so stop
      } else if (justNeedsMore) {
        None
      } else {
        Some(trees)
      }
    }
  }

  /** Compile a SourceFile.  Returns the root context of the run that compiled the file.
   */
  def compileSources(sources: List[SourceFile])(implicit ctx: Context): Context = {
    val reporter = newReporter
    val run = newRun(ctx.fresh.setReporter(reporter))
    run.compileSources(sources)
    run.runContext
  }

  /** Compile a string.  Returns true if there are no
   *  compilation errors, or false otherwise.
   */
  def compileString(code: String)(implicit ctx: Context): Boolean = {
    val runCtx = compileSources(List(new SourceFile("<script>", code.toCharArray)))
    !runCtx.reporter.hasErrors
  }

  override def interpret(line: String)(implicit ctx: Context): Interpreter.Result = {
    // if (prevRequests.isEmpty)
    //  new Run(this) // initialize the compiler // (not sure this is needed)
    // parse
    parse(line) match {
      case None => Interpreter.Incomplete
      case Some(Nil) => Interpreter.Error // parse error or empty input
      case Some(tree :: Nil) if tree.isTerm && !tree.isInstanceOf[Assign] =>
        previousOutput.clear() // clear previous error reporting
        interpret(s"val $newVarName =\n$line")
      case Some(trees) =>
        previousOutput.clear() // clear previous error reporting
        val req = new Request(line, newLineName)
        if (!req.compile())
          Interpreter.Error // an error happened during compilation, e.g. a type error
        else {
          val (resultStrings, succeeded) = req.loadAndRun()
          if (delayOutput)
            previousOutput ++= resultStrings.map(clean)
          else if (printResults || !succeeded)
            resultStrings.foreach(x => out.print(clean(x)))
          if (succeeded) {
            prevRequests += req
            Interpreter.Success
          }
          else Interpreter.Error
        }
    }
  }

  private def loadAndSetValue(objectName: String, value: AnyRef) = {
    /** This terrible string is the wrapped class's full name inside the
     *  classloader:
     *  lineX$object$$iw$$iw$list$object
     */
    val objName: String = List(
      currentLineName + INTERPRETER_WRAPPER_SUFFIX,
      INTERPRETER_IMPORT_WRAPPER,
      INTERPRETER_IMPORT_WRAPPER,
      objectName
    ).mkString("$")

    try {
      val resObj: Class[_] = Class.forName(objName, true, classLoader)
      val setMethod = resObj.getDeclaredMethods.find(_.getName == "set")

      setMethod.fold(false) { method =>
        method.invoke(resObj, value) == null
      }
    } catch {
      case NonFatal(_) =>
        // Unable to set value on object due to exception during reflection
        false
    }
  }

  /** This bind is implemented by creating an object with a set method and a
   *  field `value`. The value is then set via Java reflection.
   *
   *  Example: We want to bind a value `List(1,2,3)` to identifier `list` from
   *  sbt. The bind method accomplishes this by creating the following:
   *  {{{
   *    object ContainerObjectWithUniqueID {
   *      var value: List[Int] = _
   *      def set(x: Any) = value = x.asInstanceOf[List[Int]]
   *    }
   *    val list = ContainerObjectWithUniqueID.value
   *  }}}
   *
   *  Between the object being created and the value being assigned, the value
   *  inside the object is set via reflection.
   */
  override def bind(id: String, boundType: String, value: AnyRef)(implicit ctx: Context): Interpreter.Result =
    interpret(
      """
        |object %s {
        |  var value: %s = _
        |  def set(x: Any) = value = x.asInstanceOf[%s]
        |}
      """.stripMargin.format(id + INTERPRETER_WRAPPER_SUFFIX, boundType, boundType)
    ) match {
      case Interpreter.Success if loadAndSetValue(id + INTERPRETER_WRAPPER_SUFFIX, value) =>
        val line = "val %s = %s.value".format(id, id + INTERPRETER_WRAPPER_SUFFIX)
        interpret(line)
      case Interpreter.Error | Interpreter.Incomplete =>
        out.println("Set failed in bind(%s, %s, %s)".format(id, boundType, value))
        Interpreter.Error
    }

  /** Trait collecting info about one of the statements of an interpreter request */
  private trait StatementInfo {
    /** The statement */
    def statement: Tree

    /** The names defined previously and referred to in the statement */
    def usedNames: List[Name]

    /** The names defined in the statement */
    val boundNames: List[Name]

    /** Statement is an import that contains a wildcard */
    val importsWildcard: Boolean

    /** The names imported by the statement (if it is an import clause) */
    val importedNames: Seq[Name]

    /** Statement defines an implicit calue or method */
    val definesImplicit: Boolean
  }

  /** One line of code submitted by the user for interpretation */
  private class Request(val line: String, val lineName: String)(implicit ctx: Context) {
    private val trees = {
      val parsed = parse(line)
      previousOutput.clear() // clear previous error reporting
      parsed match {
        case Some(ts) => ts
        case None => Nil
      }
    }

    /** name to use for the object that will compute "line" */
    private def objectName = lineName + INTERPRETER_WRAPPER_SUFFIX

    /** name of the object that retrieves the result from the above object */
    private def resultObjectName = "RequestResult$" + objectName

    private def chooseHandler(stat: Tree): StatementHandler = stat match {
      case stat: DefDef => new DefHandler(stat)
      case stat: ValDef => new ValHandler(stat)
      case stat: PatDef => new PatHandler(stat)
      case stat @ Assign(Ident(_), _) => new AssignHandler(stat)
      case stat: ModuleDef => new ModuleHandler(stat)
      case stat: TypeDef if stat.isClassDef => new ClassHandler(stat)
      case stat: TypeDef => new TypeAliasHandler(stat)
      case stat: Import => new ImportHandler(stat)
//    case DocDef(_, documented) => chooseHandler(documented)
      case stat => new GenericHandler(stat)
    }

    private val handlers: List[StatementHandler] = trees.map(chooseHandler)

    /** all (public) names defined by these statements */
    private val boundNames = ListSet(handlers.flatMap(_.boundNames): _*).toList

    /** list of names used by this expression */
    private val usedNames: List[Name] = handlers.flatMap(_.usedNames)

    private val (importsPreamble, importsTrailer, accessPath) =
      importsCode(usedNames.toSet)

    /** Code to access a variable with the specified name */
    private def fullPath(vname: String): String = s"$objectName$accessPath.`$vname`"

    /** Code to access a variable with the specified name */
    private def fullPath(vname: Name): String = fullPath(vname.toString)

    /** the line of code to compute */
    private def toCompute = line

    /** generate the source code for the object that computes this request
     *  TODO Reformulate in a functional way
     */
    private def objectSourceCode: String =
      stringFrom { code =>
        // header for the wrapper object
        code.println(s"object $objectName{")
        code.print(importsPreamble)
        code.println(toCompute)
        handlers.foreach(_.extraCodeToEvaluate(this,code))
        code.println(importsTrailer)
        //end the wrapper object
        code.println(";}")
      }

    /** Types of variables defined by this request.  They are computed
        after compilation of the main object */
    private var typeOf: Map[Name, String] = _

    /** generate source code for the object that retrieves the result
        from objectSourceCode */
    private def resultObjectSourceCode: String =
      stringFrom(code => {
        code.println(s"object $resultObjectName")
        code.println("{ val result: String = {")
        code.println(s"$objectName$accessPath;")  // evaluate the object, to make sure its constructor is run
        code.print("(\"\"")  // print an initial empty string, so later code can
                            // uniformly be: + morestuff
        handlers.foreach(_.resultExtractionCode(this, code))
        code.println("\n)}")
        code.println(";}")
      })


    /** Compile the object file.  Returns whether the compilation succeeded.
     *  If all goes well, the "types" map is computed. */
    def compile(): Boolean = {
      val compileCtx = compileSources(
        List(new SourceFile("<console>", objectSourceCode.toCharArray)))
      !compileCtx.reporter.hasErrors && {
        this.typeOf = findTypes(compileCtx)
        val resultCtx = compileSources(
          List(new SourceFile("<console>", resultObjectSourceCode.toCharArray)))
        !resultCtx.reporter.hasErrors
      }
    }

    /** Dig the types of all bound variables out of the compiler run.
     *  TODO: Change the interface so that we typecheck, and then transform
     *  directly. Treating the compiler as less of a blackbox will require
     *  much less magic here.
     */
    private def findTypes(implicit ctx: Context): Map[Name, String] = {
      def valAndVarNames = handlers.flatMap(_.valAndVarNames)
      def defNames = handlers.flatMap(_.defNames)

      def getTypes(names: List[Name], nameMap: Name => Name): Map[Name, String] = {
        /** the outermost wrapper object */
        val outerResObjSym: Symbol =
          defn.EmptyPackageClass.info.decl(objectName.toTermName).symbol

        /** the innermost object inside the wrapper, found by
          * following accessPath into the outer one. */
        val resObjSym =
          (accessPath.split("\\.")).foldLeft(outerResObjSym) { (sym,str) =>
            if (str.isEmpty) sym
            else
              ctx.atPhase(ctx.typerPhase.next) { implicit ctx =>
                sym.info.member(str.toTermName).symbol
              }
          }

        names.foldLeft(Map.empty[Name,String]) { (map, name) =>
          val rawType =
            ctx.atPhase(ctx.typerPhase.next) { implicit ctx =>
              resObjSym.info.member(name).info
            }

          // the types are all =>T; remove the =>
          val cleanedType = rawType.widenExpr match {
            case tp: MethodType => tp.resultType
            case tp => tp
          }

          map + (name ->
            ctx.atPhase(ctx.typerPhase.next) { implicit ctx =>
              cleanedType.show
            })
        }
      }

      val names1 = getTypes(valAndVarNames, n => n.toTermName.fieldName)
      val names2 = getTypes(defNames, identity)
      names1 ++ names2
    }

    /** Sets both System.{out,err} and Console.{out,err} to supplied
     *  `os: OutputStream`
     */
    private def withOutput[T](os: ByteOutputStream)(op: ByteOutputStream => T) = {
      val ps     = new PrintStream(os)
      val oldOut = System.out
      val oldErr = System.err
      System.setOut(ps)
      System.setErr(ps)

      try {
        Console.withOut(os)(Console.withErr(os)(op(os)))
      } finally {
        System.setOut(oldOut)
        System.setErr(oldErr)
      }
    }

    /** load and run the code using reflection.
     *  @return  A pair consisting of the run's result as a `List[String]`, and
     *           a boolean indicating whether the run succeeded without throwing
     *           an exception.
     */
    def loadAndRun(): (List[String], Boolean) = {
      val interpreterResultObject: Class[_] =
        Class.forName(resultObjectName, true, classLoader)
      val valMethodRes: java.lang.reflect.Method =
        interpreterResultObject.getMethod("result")
      try {
        withOutput(new ByteOutputStream) { ps =>
          val rawRes = valMethodRes.invoke(interpreterResultObject).toString
          val res =
            if (ictx.useColors) new String(SyntaxHighlighting(rawRes).toArray)
            else rawRes
          val prints = ps.toString("utf-8")
          val printList = if (prints != "") prints :: Nil else Nil

          if (!delayOutput) out.print(prints)

          (printList :+ res, true)
        }
      } catch {
        case NonFatal(ex) =>
          def cause(ex: Throwable): Throwable =
            if (ex.getCause eq null) ex else cause(ex.getCause)
          val orig = cause(ex)
          (stringFrom(str => orig.printStackTrace(str)) :: Nil, false)
      }
    }

    /** Compute imports that allow definitions from previous
     *  requests to be visible in a new request.  Returns
     *  three pieces of related code as strings:
     *
     *  1. A _preamble_: An initial code fragment that should go before
     *  the code of the new request.
     *
     *  2. A _trailer_: A code fragment that should go after the code
     *  of the new request.
     *
     *  3. An _access path_ which can be traversed to access
     *  any bindings inside code wrapped by #1 and #2 .
     *
     *  The argument is a set of Names that need to be imported.
     *
     *  Limitations: This method is not as precise as it could be.
     *  (1) It does not process wildcard imports to see what exactly
     *  they import.
     *  (2) If it imports any names from a request, it imports all
     *  of them, which is not really necessary.
     *  (3) It imports multiple same-named implicits, but only the
     *  last one imported is actually usable.
     */
    private def importsCode(wanted: Set[Name]): (String, String, String) = {
      /** Narrow down the list of requests from which imports
       *  should be taken.  Removes requests which cannot contribute
       *  useful imports for the specified set of wanted names.
       */
      def reqsToUse: List[(Request, StatementInfo)] = {
        /** Loop through a list of StatementHandlers and select
         *  which ones to keep.  'wanted' is the set of
         *  names that need to be imported.
         */
        def select(reqs: List[(Request, StatementInfo)], wanted: Set[Name]): List[(Request, StatementInfo)] = {
          reqs match {
            case Nil => Nil

            case (req, handler) :: rest =>
              val keepit =
                (handler.definesImplicit ||
                  handler.importsWildcard ||
                  handler.importedNames.exists(wanted.contains(_)) ||
                  handler.boundNames.exists(wanted.contains(_)))

              val newWanted =
                if (keepit) {
                  (wanted
                    ++ handler.usedNames
                    -- handler.boundNames
                    -- handler.importedNames)
                } else {
                  wanted
                }

              val restToKeep = select(rest, newWanted)

              if (keepit)
                (req, handler) :: restToKeep
              else
                restToKeep
          }
        }

        val rhpairs = for {
          req <- prevRequests.toList.reverse
          handler <- req.handlers
        } yield (req, handler)

        select(rhpairs, wanted).reverse
      }

      val preamble = new StringBuffer
      val trailingBraces = new StringBuffer
      val accessPath = new StringBuffer
      val impname = INTERPRETER_IMPORT_WRAPPER
      val currentImps = mutable.Set[Name]()

      // add code for a new object to hold some imports
      def addWrapper(): Unit = {
        preamble.append("object " + impname + "{\n")
        trailingBraces.append("}\n")
        accessPath.append("." + impname)
        currentImps.clear
      }

      addWrapper()

      // loop through previous requests, adding imports
      // for each one
      for ((req, handler) <- reqsToUse) {
        // If the user entered an import, then just use it

        // add an import wrapping level if the import might
        // conflict with some other import
        if (handler.importsWildcard ||
          currentImps.exists(handler.importedNames.contains))
          if (!currentImps.isEmpty)
            addWrapper()

        if (handler.statement.isInstanceOf[Import])
          preamble.append(handler.statement.show + ";\n")

        // give wildcard imports a import wrapper all to their own
        if (handler.importsWildcard)
          addWrapper()
        else
          currentImps ++= handler.importedNames

        // For other requests, import each bound variable.
        // import them explicitly instead of with _, so that
        // ambiguity errors will not be generated. Also, quote
        // the name of the variable, so that we don't need to
        // handle quoting keywords separately.
        for (imv <- handler.boundNames) {
          if (currentImps.contains(imv))
            addWrapper()
          preamble.append("import ")
          preamble.append(req.objectName + req.accessPath + ".`" + imv + "`;\n")
          currentImps += imv
        }
      }

      addWrapper() // Add one extra wrapper, to prevent warnings
      // in the frequent case of redefining
      // the value bound in the last interpreter
      // request.

      (preamble.toString, trailingBraces.toString, accessPath.toString)
    }

    // ------ Handlers ------------------------------------------

    /** Class to handle one statement among all the statements included
     *  in a single interpreter request.
     */
    private sealed abstract class StatementHandler(val statement: Tree) extends StatementInfo {
      val usedNames: List[Name] = {
        val ivt = new UntypedTreeAccumulator[mutable.Set[Name]] {
          override def apply(ns: mutable.Set[Name], tree: Tree)(implicit ctx: Context) =
            tree match {
              case Ident(name) => ns += name
              case _ => foldOver(ns, tree)
            }
        }
        ivt.foldOver(HashSet(), statement).toList
      }
      val boundNames: List[Name] = Nil
      def valAndVarNames: List[Name] = Nil
      def defNames: List[Name] = Nil
      val importsWildcard = false
      val importedNames: Seq[Name] = Nil
      val definesImplicit = statement match {
        case tree: MemberDef => tree.mods.is(Flags.Implicit)
        case _ => false
      }

      def extraCodeToEvaluate(req: Request, code: PrintWriter) = {}
      def resultExtractionCode(req: Request, code: PrintWriter) = {}
    }

    private class GenericHandler(statement: Tree) extends StatementHandler(statement)

    private abstract class ValOrPatHandler(statement: Tree)
        extends StatementHandler(statement) {
      override val boundNames: List[Name] = _boundNames
      override def valAndVarNames = boundNames

      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        if (!shouldShowResult(req)) return
        val resultExtractors = boundNames.map(name => resultExtractor(req, name))
        code.print(resultExtractors.mkString(""))
      }

      private val ListReg = """^.*List\[(\w+)\]$""".r
      private val MapReg = """^.*Map\[(\w+),[ ]*(\w+)\]$""".r
      private val LitReg = """^.*\((.+)\)$""".r

      private def resultExtractor(req: Request, varName: Name): String = {
        val prettyName = varName.decode
        // FIXME: `varType` is prettified to abbreviate common types where
        // appropriate, and to also prettify literal types
        //
        // This should be rewritten to use the actual types once we have a
        // semantic representation available to the REPL
        val varType = string2code(req.typeOf(varName)) match {
          // Extract List's paremeter from full path
          case ListReg(param) => s"List[$param]"
          // Extract Map's paremeters from full path
          case MapReg(k, v) => s"Map[$k, $v]"
          // Extract literal type from literal type representation. Example:
          //
          // ```
          // scala> val x: 42 = 42
          // val x: Int(42) = 42
          // scala> val y: "hello" = "hello"
          // val y: String("hello") = "hello"
          // ```
          case LitReg(lit) => lit
          // When the type is a singleton value like None, don't show `None$`
          // instead show `None.type`.
          case x if x.lastOption == Some('$') => x.init + ".type"
          case x => x
        }
        val fullPath = req.fullPath(varName)

        val varOrVal = statement match {
          case v: ValDef if v.mods is Flags.Mutable => "var"
          case _ => "val"
        }

        s""" + "$varOrVal $prettyName: $varType = " + {
           |  if ($fullPath.asInstanceOf[AnyRef] != null) {
           |    (if ($fullPath.toString().contains('\\n')) "\\n" else "") + {
           |      import dotty.Show._
           |      $fullPath.show /*toString()*/ + "\\n"
           |    }
           |  } else {
           |    "null\\n"
           |  }
           |}""".stripMargin
      }

      protected def _boundNames: List[Name]
      protected def shouldShowResult(req: Request): Boolean
    }

    private class ValHandler(statement: ValDef) extends ValOrPatHandler(statement) {
      override def _boundNames = List(statement.name)

      override def shouldShowResult(req: Request): Boolean =
        !statement.mods.is(Flags.AccessFlags) &&
          !(isGeneratedVarName(statement.name.toString) &&
            req.typeOf(statement.name.encode) == "Unit")
    }


    private class PatHandler(statement: PatDef) extends ValOrPatHandler(statement) {
      override def _boundNames = statement.pats.flatMap(findVariableNames)

      override def shouldShowResult(req: Request): Boolean =
        !statement.mods.is(Flags.AccessFlags)

      private def findVariableNames(tree: Tree): List[Name] = tree match {
        case Ident(name) if name.toString != "_" => List(name)
        case _ => VariableNameFinder(Nil, tree).reverse
      }

      private object VariableNameFinder extends UntypedDeepFolder[List[Name]](
        (acc: List[Name], t: Tree) => t match {
          case _: BackquotedIdent => acc
          case Ident(name) if name.isVariableName && name.toString != "_" => name :: acc
          case Bind(name, _) if name.isVariableName => name :: acc
          case _ => acc
        }
      )
    }

    private class DefHandler(defDef: DefDef) extends StatementHandler(defDef) {
      override val boundNames = List(defDef.name)
      override def defNames = boundNames

      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        /** TODO: This is the result of the state of the REPL - this would be
          * entirely unnecessary with a better structure where we could just
          * use the type printer
          *
          * @see `def findTypes` for an explanation of what should be done
          */
        if (!defDef.mods.is(Flags.AccessFlags)) {
          // Take the DefDef and remove the `rhs` and ascribed type `tpt`
          val copy = ast.untpd.cpy.DefDef(defDef)(
            rhs = EmptyTree,
            tpt = TypeTree
          )

          val tpt = defDef.tpt match {
            // ascribed TypeExpr e.g: `def foo: Int = 5`
            case Ident(tpt) if defDef.vparamss.isEmpty =>
              ": " + tpt.show
            case tpt =>
              ": " + req.typeOf(defDef.name)
          }
          code.print {
            "+\"" + string2code(copy.show) + tpt + "\\n\""
          }
        }
      }
    }

    private class AssignHandler(statement: Assign) extends StatementHandler(statement) {
      val lhs = statement.lhs.asInstanceOf[Ident] // an unfortunate limitation

      val helperName = newInternalVarName().toTermName
      override val valAndVarNames = List(helperName)

      override def extraCodeToEvaluate(req: Request, code: PrintWriter): Unit = {
        code.println(i"val $helperName = ${statement.lhs};")
      }

      /** Print out lhs instead of the generated varName */
      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        code.print(" + \"" + lhs.show + ": " +
          string2code(req.typeOf(helperName.encode)) +
          " = \" + " +
          string2code(req.fullPath(helperName))
          + " + \"\\n\"")
      }
    }

    private class ModuleHandler(module: ModuleDef) extends StatementHandler(module) {
      override val boundNames = List(module.name)

      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        code.println(" + \"defined module " +
          string2code(module.name.toString)
          + "\\n\"")
      }
    }

    private class ClassHandler(classdef: TypeDef)
      extends StatementHandler(classdef) {
      override val boundNames =
        List(classdef.name) :::
          (if (classdef.mods.is(Flags.Case))
            List(classdef.name.toTermName)
          else
            Nil)

      // TODO: MemberDef.keyword does not include "trait";
      // otherwise it could be used here
      def keyword: String =
        if (classdef.mods.is(Flags.Trait)) "trait" else "class"

      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        code.print(
          " + \"defined " +
            keyword +
            " " +
            string2code(classdef.name.toString) +
            "\\n\"")
      }
    }

    private class TypeAliasHandler(typeDef: TypeDef)
      extends StatementHandler(typeDef) {
      override val boundNames =
        if (!typeDef.mods.is(Flags.AccessFlags) && !typeDef.rhs.isInstanceOf[TypeBoundsTree])
          List(typeDef.name)
        else
          Nil

      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        code.println(" + \"defined type alias " +
          string2code(typeDef.name.toString) + "\\n\"")
      }
    }

    private class ImportHandler(imp: Import) extends StatementHandler(imp) {
      override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
        code.println("+ \"" + imp.show + "\\n\"")
      }

      def isWildcardSelector(tree: Tree) = tree match {
        case Ident(nme.USCOREkw) => true
        case _ => false
      }

      /** Whether this import includes a wildcard import */
      override val importsWildcard = imp.selectors.exists(isWildcardSelector)

      /** The individual names imported by this statement */
      override val importedNames: Seq[Name] =
        imp.selectors.filterNot(isWildcardSelector).flatMap {
          case sel: RefTree => List(sel.name.toTypeName, sel.name.toTermName)
          case _ => Nil
        }
    }

  } // end Request

  // ------- String handling ----------------------------------

  /** next line number to use */
  private var nextLineNo = 0

  /** allocate a fresh line name */
  private def newLineName = {
    val num = nextLineNo
    nextLineNo += 1
    INTERPRETER_LINE_PREFIX + num
  }

  private def currentLineName =
    INTERPRETER_LINE_PREFIX + (nextLineNo - 1)

  /** next result variable number to use */
  private var nextVarNameNo = 0

  /** allocate a fresh variable name */
  private def newVarName = {
    val num = nextVarNameNo
    nextVarNameNo += 1
    INTERPRETER_VAR_PREFIX + num
  }

  /** next internal variable number to use */
  private var nextInternalVarNo = 0

  /** allocate a fresh internal variable name */
  private def newInternalVarName() = {
    val num = nextVarNameNo
    nextVarNameNo += 1
    INTERPRETER_SYNTHVAR_PREFIX + num
  }

  /** Check if a name looks like it was generated by newVarName */
  private def isGeneratedVarName(name: String): Boolean =
    name.startsWith(INTERPRETER_VAR_PREFIX) && {
      val suffix = name.drop(INTERPRETER_VAR_PREFIX.length)
      suffix.forall(_.isDigit)
    }

  /** generate a string using a routine that wants to write on a stream */
  private def stringFrom(writer: PrintWriter => Unit): String = {
    val stringWriter = new StringWriter()
    val stream = new NewLinePrintWriter(stringWriter)
    writer(stream)
    stream.close()
    stringWriter.toString
  }

  /** Truncate a string if it is longer than settings.maxPrintString */
  private def truncPrintString(str: String)(implicit ctx: Context): String = {
    val maxpr = ctx.settings.XreplLineWidth.value

    if (maxpr <= 0)
      return str

    if (str.length <= maxpr)
      return str

    val trailer = "..."
    if (maxpr >= trailer.length-1)
      str.substring(0, maxpr-3) + trailer + "\n"
    else
      str.substring(0, maxpr-1)
  }

  /** Clean up a string for output */
  private def clean(str: String)(implicit ctx: Context) =
    truncPrintString(stripWrapperGunk(str))
}

/** Utility methods for the Interpreter. */
object CompilingInterpreter {
  val INTERPRETER_WRAPPER_SUFFIX = "$object"
  val INTERPRETER_LINE_PREFIX = "line"
  val INTERPRETER_VAR_PREFIX = "res"
  val INTERPRETER_IMPORT_WRAPPER = "$iw"
  val INTERPRETER_SYNTHVAR_PREFIX = "synthvar$"

  /** Delete a directory tree recursively.  Use with care!
   */
  private[repl] def deleteRecursively(path: File): Unit = {
    path match  {
      case _ if !path.exists =>
        ()
      case _ if path.isDirectory =>
        for (p <- path.listFiles)
          deleteRecursively(p)
        path.delete
      case _ =>
        path.delete
    }
  }

  /** Heuristically strip interpreter wrapper prefixes
   *  from an interpreter output string.
   */
  def stripWrapperGunk(str: String): String = {
    val wrapregex = "(line[0-9]+\\$object[$.])?(\\$iw[$.])*"
    str.replaceAll(wrapregex, "")
  }

  /** Convert a string into code that can recreate the string.
   *  This requires replacing all special characters by escape
   *  codes. It does not add the surrounding " marks.  */
  def string2code(str: String): String = {
    /** Convert a character to a backslash-u escape */
    def char2uescape(c: Char): String = {
      var rest = c.toInt
      val buf = new StringBuilder
      for (i <- 1 to 4) {
	      buf ++= (rest % 16).toHexString
	      rest = rest / 16
      }
      "\\" + "u" + buf.toString.reverse
    }
    val res = new StringBuilder
    for (c <- str) {
      if ("'\"\\" contains c) {
	      res += '\\'
	      res += c
      } else if (!c.isControl) {
	      res += c
      } else {
	      res ++= char2uescape(c)
      }
    }
    res.toString
  }
}