summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interactive/REPL.scala
blob: 11def170b58e2e9fd75b89b9c223d2f3a238b2c4 (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
package scala.tools.nsc.interactive

import scala.concurrent.SyncVar
import scala.tools.nsc.util._
import scala.tools.nsc.symtab._
import scala.tools.nsc.ast._
import scala.tools.nsc.reporters._
import scala.tools.nsc.io._

/** Interface of interactive compiler to a client such as an IDE
 */
object REPL extends EvalLoop {

  val settings = new Settings()
  val comp = new Global(settings, new ConsoleReporter(settings))

  def prompt = "> "

  /** Commands:
   *
   *  reload file1 ... fileN
   *  typeat file line col
   *
   *
   */
  def run() {
    val reloadResult = new SyncVar[Either[Unit, Throwable]]
    val typeatResult = new SyncVar[Either[comp.Tree, Throwable]]
    loop { line =>
      (line split " ").toList match {
        case "reload" :: args =>
          comp.askReload(args map toSourceFile, reloadResult)
          show(reloadResult)
        case List("typeat", file, line, col1, col2) =>
          val source = toSourceFile(file)
          val linestart = source.lineToOffset(line.toInt)
          val pos = comp.rangePos(source, linestart + col1.toInt, linestart + col1.toInt, linestart + col2.toInt)
          comp.askTypeAt(pos, typeatResult)
          show(typeatResult)
        case List("quit") =>
          System.exit(1)
        case _ =>
          println("unrecongized command")
      }
    }
  }

  def toSourceFile(name: String) = new BatchSourceFile(new PlainFile(new java.io.File(name)))

  def show[T](svar: SyncVar[Either[T, Throwable]]) {
    svar.get match {
      case Left(result) => println("==> "+result)
      case Right(exc/*: Throwable ??*/) => exc.printStackTrace; println("ERROR: "+exc)
    }
  }

  def main(args: Array[String]) {
    run()
  }
}