src/Pure/System/scala.scala
changeset 72294 25c6423ec538
parent 72213 6157757bb133
child 72332 319dd5c618a5
equal deleted inserted replaced
72293:584aea0b29bb 72294:25c6423ec538
     8 
     8 
     9 
     9 
    10 import java.io.{File => JFile, StringWriter, PrintWriter}
    10 import java.io.{File => JFile, StringWriter, PrintWriter}
    11 
    11 
    12 import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter}
    12 import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter}
    13 import scala.tools.nsc.interpreter.IMain
    13 import scala.tools.nsc.interpreter.{IMain, Results}
    14 
    14 
    15 
    15 
    16 object Scala
    16 object Scala
    17 {
    17 {
    18   /** registered functions **/
    18   /** registered functions **/
    98             if (class_loader == null) super.parentClassLoader
    98             if (class_loader == null) super.parentClassLoader
    99             else class_loader
    99             else class_loader
   100         }
   100         }
   101       }
   101       }
   102 
   102 
   103       def toplevel(source: String): List[String] =
   103       def toplevel(interpret: Boolean, source: String): List[String] =
   104       {
   104       {
   105         val out = new StringWriter
   105         val out = new StringWriter
   106         val interp = interpreter(new PrintWriter(out))
   106         val interp = interpreter(new PrintWriter(out))
   107         val rep = new interp.ReadEvalPrint
   107         val ok =
   108         val ok = interp.withLabel("\u0001") { rep.compile(source) }
   108           interp.withLabel("\u0001") {
       
   109             if (interpret) interp.interpret(source) == Results.Success
       
   110             else (new interp.ReadEvalPrint).compile(source)
       
   111           }
   109         out.close
   112         out.close
   110 
   113 
   111         val Error = """(?s)^\S* error: (.*)$""".r
   114         val Error = """(?s)^\S* error: (.*)$""".r
   112         val errors =
   115         val errors =
   113           space_explode('\u0001', Library.strip_ansi_color(out.toString)).
   116           space_explode('\u0001', Library.strip_ansi_color(out.toString)).
   118     }
   121     }
   119   }
   122   }
   120 
   123 
   121   object Toplevel extends Fun("scala_toplevel")
   124   object Toplevel extends Fun("scala_toplevel")
   122   {
   125   {
   123     def apply(source: String): String =
   126     def apply(arg: String): String =
   124     {
   127     {
       
   128       val (interpret, source) =
       
   129         YXML.parse_body(arg) match {
       
   130           case Nil => (false, "")
       
   131           case List(XML.Text(source)) => (false, source)
       
   132           case body => import XML.Decode._; pair(bool, string)(body)
       
   133         }
   125       val errors =
   134       val errors =
   126         try { Compiler.context().toplevel(source) }
   135         try { Compiler.context().toplevel(interpret, source) }
   127         catch { case ERROR(msg) => List(msg) }
   136         catch { case ERROR(msg) => List(msg) }
   128       locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) }
   137       locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) }
   129     }
   138     }
   130   }
   139   }
   131 
   140