fix: fix frontend tests failing due to expecting error codes instead of runtime exceptions
This commit is contained in:
@@ -40,26 +40,29 @@ 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
|
||||||
|
|
||||||
val (names, funcs) = renamer.rename(prog)
|
val (names, funcs) = renamer.rename(prog)
|
||||||
given ctx: typeChecker.TypeCheckerCtx = typeChecker.TypeCheckerCtx(names, funcs, errors)
|
given ctx: typeChecker.TypeCheckerCtx = typeChecker.TypeCheckerCtx(names, funcs, errors)
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,15 +75,19 @@ def compile(filename: String, outFile: Option[File] = None)(using
|
|||||||
stdout: PrintStream = Console.out
|
stdout: PrintStream = Console.out
|
||||||
): 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 {
|
||||||
writer.writeTo(
|
case Left(code) => IO.pure(code) // Return error code
|
||||||
backend(typedProg),
|
case Right(typedProg) =>
|
||||||
PrintStream(outFile.getOrElse(File(filename.stripSuffix(".wacc") + ".s")))
|
IO {
|
||||||
)
|
writer.writeTo(
|
||||||
}
|
backend(typedProg),
|
||||||
} yield 0
|
PrintStream(outFile.getOrElse(File(filename.stripSuffix(".wacc") + ".s")))
|
||||||
|
)
|
||||||
|
}.as(0) // Compilation succeeded
|
||||||
|
}
|
||||||
|
} yield exitCode
|
||||||
|
|
||||||
object Main extends IOApp.Simple {
|
object Main extends IOApp.Simple {
|
||||||
override def run: IO[Unit] =
|
override def run: IO[Unit] =
|
||||||
|
|||||||
Reference in New Issue
Block a user