@@ -4,28 +4,26 @@ package dotty.tools
44import scala .annotation .tailrec
55import scala .io .Source
66import scala .util .{ Try , Success , Failure }
7- import java .net .URLClassLoader
8- import sys .process ._
97import java .io .File
108import java .lang .Thread
119import scala .annotation .internal .sharable
1210import dotty .tools .dotc .util .ClasspathFromClassloader
1311import dotty .tools .runner .ObjectRunner
1412import dotty .tools .dotc .config .Properties .envOrNone
1513import java .util .jar ._
16- import java .util .jar .Attributes .Name
1714import dotty .tools .io .Jar
1815import dotty .tools .runner .ScalaClassLoader
1916import java .nio .file .{Files , Paths , Path }
20- import scala .collection .JavaConverters ._
2117import dotty .tools .dotc .config .CommandLineParser
18+ import dotty .tools .scripting .StringDriver
2219
2320enum ExecuteMode :
2421 case Guess
2522 case Script
2623 case Repl
2724 case Run
2825 case PossibleRun
26+ case Expression
2927
3028case class Settings (
3129 verbose : Boolean = false ,
@@ -38,6 +36,7 @@ case class Settings(
3836 possibleEntryPaths : List [String ] = List .empty,
3937 scriptArgs : List [String ] = List .empty,
4038 targetScript : String = " " ,
39+ targetExpression : String = " " ,
4140 targetToRun : String = " " ,
4241 save : Boolean = false ,
4342 modeShouldBePossibleRun : Boolean = false ,
@@ -78,6 +77,9 @@ case class Settings(
7877 def withTargetToRun (targetToRun : String ): Settings =
7978 this .copy(targetToRun = targetToRun)
8079
80+ def withExpression (scalaSource : String ): Settings =
81+ this .copy(targetExpression = scalaSource)
82+
8183 def withSave : Settings =
8284 this .copy(save = true )
8385
@@ -150,12 +152,12 @@ object MainGenericRunner {
150152 case (o @ colorOption(_* )) :: tail =>
151153 process(tail, settings.withScalaArgs(o))
152154 case " -e" :: expression :: tail =>
153- val tempScript = writeFile( s " @main def main(args: String *): Unit = \n ${expression}" )
155+ val mainSource = s " @main def main(args: String *): Unit = \n ${expression}"
154156 settings
155- .withExecuteMode(ExecuteMode .Script )
156- .withTargetScript(tempScript )
157+ .withExecuteMode(ExecuteMode .Expression )
158+ .withExpression(mainSource )
157159 .withScriptArgs(tail* )
158- .noSave // useless
160+ .noSave // -save not useful here
159161 case arg :: tail =>
160162 val line = Try (Source .fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
161163 lazy val hasScalaHashbang = { val s = line.getOrElse(" " ) ; s.startsWith(" #!" ) && s.contains(" scala" ) }
@@ -243,6 +245,16 @@ object MainGenericRunner {
243245 ++ List (" -script" , settings.targetScript)
244246 ++ settings.scriptArgs
245247 scripting.Main .main(properArgs.toArray)
248+ case ExecuteMode .Expression =>
249+ val cp = settings.classPath match {
250+ case Nil => " "
251+ case list => list.mkString(classpathSeparator)
252+ }
253+ val cpArgs = if cp.isEmpty then Nil else List (" -classpath" , cp)
254+ val properArgs = cpArgs ++ settings.residualArgs ++ settings.scalaArgs
255+ val driver = StringDriver (properArgs.toArray, settings.targetExpression)
256+ driver.compileAndRun(settings.classPath)
257+
246258 case ExecuteMode .Guess =>
247259 if settings.modeShouldBePossibleRun then
248260 run(settings.withExecuteMode(ExecuteMode .PossibleRun ))
@@ -259,11 +271,4 @@ object MainGenericRunner {
259271 e.foreach(_.printStackTrace())
260272 ! isFailure
261273 }
262- def writeFile (contents : String ): String = {
263- val file = Files .createTempFile(" exec" , " .scala" )
264- file.toFile.deleteOnExit()
265- Files .write(file, contents.getBytes)
266- file.toString.replace('\\ ' , '/' )
267- }
268-
269274}
0 commit comments