Skip to content

Commit

Permalink
Get rid of Res in CompilerLifecycleManager
Browse files Browse the repository at this point in the history
A simple Option is enough, and that makes for a cleaner
interface for CompilerLifecycleManager later on.
  • Loading branch information
alexarchambault committed Jan 14, 2021
1 parent 00b3664 commit d6c3530
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ammonite.interp

import ammonite.runtime._
import ammonite.util.Util._
import ammonite.util._
import ammonite.util.Printer

import java.nio.file.Path

Expand Down Expand Up @@ -125,28 +125,23 @@ class CompilerLifecycleManager(

def compileClass(processed: Preprocessor.Output,
printer: Printer,
fileName: String): Res[Compiler.Output] = synchronized{
fileName: String): Option[Compiler.Output] = synchronized{
// Enforce the invariant that every piece of code Ammonite ever compiles,
// gets run within the `ammonite` package. It's further namespaced into
// things like `ammonite.$file` or `ammonite.$sess`, but it has to be
// within `ammonite`
assert(processed.code.trim.startsWith("package ammonite"))

init()
for {
compiled <- Res.Success{
compiler.compile(
processed.code.getBytes(scala.util.Properties.sourceEncoding),
printer,
processed.prefixCharLength,
processed.userCodeNestingLevel,
fileName
)
}
_ = Internal.compilationCount += 1
output <- Res(compiled, "Compilation Failed")
} yield output

val compiled = compiler.compile(
processed.code.getBytes(scala.util.Properties.sourceEncoding),
printer,
processed.prefixCharLength,
processed.userCodeNestingLevel,
fileName
)
Internal.compilationCount += 1
compiled
}

def configureCompiler(callback: scala.tools.nsc.Global => Unit) = synchronized{
Expand Down
18 changes: 12 additions & 6 deletions amm/interp/src/main/scala/ammonite/interp/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ class Interpreter(val printer: Printer,
incrementLine: () => Unit): Res[(Evaluated, Tag)] = synchronized{
for{
_ <- Catching{ case e: ThreadDeath => Evaluator.interrupted(e) }
output <- compilerManager.compileClass(
processed,
printer,
fileName
output <- Res(
compilerManager.compileClass(
processed,
printer,
fileName
),
"Compilation Failed"
)
_ = incrementLine()
res <- eval.processLine(
Expand Down Expand Up @@ -313,8 +316,11 @@ class Interpreter(val printer: Printer,

for {
_ <- Catching{case e: Throwable => e.printStackTrace(); throw e}
output <- compilerManager.compileClass(
processed, printer, codeSource.fileName
output <- Res(
compilerManager.compileClass(
processed, printer, codeSource.fileName
),
"Compilation Failed"
)
cls <- eval.loadClass(fullyQualifiedName, output.classFiles)

Expand Down

0 comments on commit d6c3530

Please sign in to comment.