summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-02-04 08:29:20 +0000
committerPaul Phillips <paulp@improving.org>2011-02-04 08:29:20 +0000
commit25a2d721899881ff8dde28d6975648a0bad769f5 (patch)
tree7f697a0dd0cce0c0acc51a80e39af786091c80bb /src/compiler/scala/tools/nsc/interpreter/ILoop.scala
parent9dc772f1636e181b3d038eba9ae2f5d1a8359386 (diff)
downloadscala-25a2d721899881ff8dde28d6975648a0bad769f5.tar.gz
scala-25a2d721899881ff8dde28d6975648a0bad769f5.tar.bz2
scala-25a2d721899881ff8dde28d6975648a0bad769f5.zip
After discovering #3376 was fixed I gave in and...
After discovering #3376 was fixed I gave in and tried to write a test. Now that the fiddling is over you can write repl tests without creating 5000 streams and settings. Look at test/files/run/bug3376.scala or jvm/interpreter.scala to see. Test case closes #3376, no review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/ILoop.scala')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/ILoop.scala22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
index e0a49554ed..b7cd227efc 100644
--- a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
@@ -17,7 +17,7 @@ import scala.annotation.tailrec
import scala.util.control.Exception.{ ignoring }
import scala.collection.mutable.ListBuffer
import scala.concurrent.ops
-import util.{ ClassPath }
+import util.{ ClassPath, stringFromWriter, stringFromStream }
import interpreter._
import io.File
@@ -770,6 +770,26 @@ class ILoop(in0: Option[BufferedReader], protected val out: PrintWriter)
object ILoop {
implicit def loopToInterpreter(repl: ILoop): IMain = repl.intp
+ /** Creates an interpreter loop with default settings and feeds
+ * the given code to it as input.
+ */
+ def run(code: String): String = {
+ import java.io.{ BufferedReader, StringReader, OutputStreamWriter }
+
+ stringFromStream { ostream =>
+ Console.withOut(ostream) {
+ val input = new BufferedReader(new StringReader(code))
+ val output = new PrintWriter(new OutputStreamWriter(ostream))
+ val repl = new ILoop(input, output)
+ val settings = new Settings
+ settings.classpath.value = sys.props("java.class.path")
+
+ repl main settings
+ }
+ }
+ }
+ def run(lines: List[String]): String = run(lines map (_ + "\n") mkString)
+
// provide the enclosing type T
// in order to set up the interpreter's classpath and parent class loader properly
def breakIf[T: Manifest](assertion: => Boolean, args: NamedParam*): Unit =