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