Skip to content

Commit ffde209

Browse files
authored
Add function to abort phase non-fatally (#852)
Closes #850, related to #747. We can now abort a phase without printing an error while also not printing an error message. This is useful for notifying the user of opening a file in another program (`js-web`, browser) while producing no actual executable file.
1 parent 77398dc commit ffde209

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

effekt/jvm/src/main/scala/effekt/Runner.scala

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ trait Runner[Executable] {
8787
/**
8888
* Builds a given executable and returns the resulting path to the executable.
8989
*/
90-
def build(executable: Executable)(using Context): String
90+
def build(executable: Executable)(using Context): Option[String]
9191

9292
/**
9393
* Runs the executable (e.g. the main file) by calling the build function.
9494
*/
95-
def eval(executable: Executable)(using C: Context): Unit = {
96-
val execFile = build(executable)
95+
def eval(executable: Executable)(using C: Context): Unit = build(executable).foreach { execFile =>
9796
val valgrindArgs = Seq("--leak-check=full", "--undef-value-errors=no", "--quiet", "--log-file=valgrind.log", "--error-exitcode=1")
9897
val process = if (C.config.valgrind())
9998
Process("valgrind", valgrindArgs ++ (execFile +: Context.config.runArgs()))
@@ -165,7 +164,7 @@ object JSNodeRunner extends Runner[String] {
165164
* Creates an executable `.js` file besides the given `.js` file ([[path]])
166165
* and then returns the absolute path of the created executable.
167166
*/
168-
def build(path: String)(using C: Context): String =
167+
def build(path: String)(using C: Context): Option[String] =
169168
val out = C.config.outputPath().getAbsolutePath
170169
val jsFilePath = (out / path).canonicalPath.escape
171170
val jsFileName = path.unixPath.split("/").last
@@ -177,14 +176,14 @@ object JSNodeRunner extends Runner[String] {
177176
val shebang = "#!/usr/bin/env node"
178177
val jsScriptFilePath = jsFilePath.stripSuffix(s".$extension")
179178
IO.createFile(jsScriptFilePath, s"$shebang\n$jsScript", true)
180-
jsScriptFilePath
179+
Some(jsScriptFilePath)
181180

182181
case OS.Windows =>
183182
val jsMainFilePath = jsFilePath.stripSuffix(s".$extension") + "__main.js"
184183
val jsMainFileName = jsFileName.stripSuffix(s".$extension") + "__main.js"
185184
val exePath = jsFilePath.stripSuffix(s".$extension")
186185
IO.createFile(jsMainFilePath, jsScript)
187-
createScript(exePath, "node", "$SCRIPT_DIR/" + jsMainFileName)
186+
Some(createScript(exePath, "node", "$SCRIPT_DIR/" + jsMainFileName))
188187
}
189188
}
190189
object JSWebRunner extends Runner[String] {
@@ -202,7 +201,7 @@ object JSWebRunner extends Runner[String] {
202201
* Creates an openable `.html` file besides the given `.js` file ([[path]])
203202
* and then errors out, printing it's path.
204203
*/
205-
def build(path: String)(using C: Context): String =
204+
def build(path: String)(using C: Context): Option[String] =
206205
val out = C.config.outputPath().getAbsolutePath
207206
val jsFilePath = (out / path).unixPath
208207
val jsFileName = path.unixPath.split("/").last
@@ -222,10 +221,8 @@ object JSWebRunner extends Runner[String] {
222221
|""".stripMargin
223222
IO.createFile(htmlFilePath, htmlContent, false)
224223

225-
// TODO: In ErrorReporter, add a way to terminate the program with a message, but not report a exit failure.
226-
// Workaround: print and then 'exit(0)'
227-
println(s"Open file://${htmlFilePath} in your browser or include ${jsFilePath}.")
228-
scala.sys.exit(0)
224+
C.info(s"Open file://${htmlFilePath} in your browser or include ${jsFilePath}.")
225+
None
229226
}
230227

231228
trait ChezRunner extends Runner[String] {
@@ -241,12 +238,12 @@ trait ChezRunner extends Runner[String] {
241238
* Creates an executable bash script besides the given `.ss` file ([[path]])
242239
* and returns the resulting absolute path.
243240
*/
244-
def build(path: String)(using C: Context): String =
241+
def build(path: String)(using C: Context): Option[String] =
245242
val out = C.config.outputPath().getAbsolutePath
246243
val schemeFilePath = (out / path).canonicalPath.escape
247244
val exeScriptPath = schemeFilePath.stripSuffix(s".$extension")
248245
val schemeFileName = ("./" + (path.unixPath.split('/').last)).escape
249-
createScript(exeScriptPath, "scheme", "--script", "$SCRIPT_DIR/" + schemeFileName)
246+
Some(createScript(exeScriptPath, "scheme", "--script", "$SCRIPT_DIR/" + schemeFileName))
250247
}
251248

252249
object ChezMonadicRunner extends ChezRunner {
@@ -310,7 +307,7 @@ object LLVMRunner extends Runner[String] {
310307
* Requires LLVM and GCC to be installed on the machine.
311308
* Assumes [[path]] has the format "SOMEPATH.ll".
312309
*/
313-
override def build(path: String)(using C: Context): String =
310+
override def build(path: String)(using C: Context): Option[String] =
314311

315312
val out = C.config.outputPath()
316313
val basePath = (out / path.stripSuffix(".ll")).unixPath
@@ -339,5 +336,5 @@ object LLVMRunner extends Runner[String] {
339336

340337
exec(gccArgs: _*)
341338

342-
executableFile
339+
Some(executableFile)
343340
}

0 commit comments

Comments
 (0)