fix: fix frontend tests failing due to expecting error codes instead of runtime exceptions

This commit is contained in:
Jonny
2025-02-28 15:20:32 +00:00
parent 345c652a57
commit cf1028454d

View File

@@ -40,9 +40,12 @@ val cliParser = {
def frontend(
contents: String
)(using stdout: PrintStream): IO[microWacc.Program] = {
IO(parser.parse(contents)).flatMap {
case Failure(msg) => IO.raiseError(new RuntimeException(msg))
)(using stdout: PrintStream): IO[Either[Int, microWacc.Program]] = {
IO(parser.parse(contents)).map {
case Failure(msg) =>
stdout.println(msg)
Left(100) // Syntax error
case Success(prog) =>
given errors: mutable.Builder[Error, List[Error]] = List.newBuilder
given errorContent: String = contents
@@ -52,13 +55,13 @@ def frontend(
val typedProg = typeChecker.check(prog)
if (errors.result.isEmpty) IO.pure(typedProg)
if (errors.result.isEmpty) Right(typedProg)
else {
errors.result.foreach(printError)
IO.raiseError(new RuntimeException("Compilation failed with code: " + errors.result.view.map {
Left(errors.result.view.map {
case _: Error.InternalError => 201
case _ => 200
}.max))
}.max)
}
}
}
@@ -73,14 +76,18 @@ def compile(filename: String, outFile: Option[File] = None)(using
): IO[Int] =
for {
contents <- IO(os.read(os.Path(filename)))
typedProg <- frontend(contents)
_ <- IO {
result <- frontend(contents)
exitCode <- result match {
case Left(code) => IO.pure(code) // Return error code
case Right(typedProg) =>
IO {
writer.writeTo(
backend(typedProg),
PrintStream(outFile.getOrElse(File(filename.stripSuffix(".wacc") + ".s")))
)
}.as(0) // Compilation succeeded
}
} yield 0
} yield exitCode
object Main extends IOApp.Simple {
override def run: IO[Unit] =