From e54e5ce15158715e211aa37fca6802d5b0d90eb5 Mon Sep 17 00:00:00 2001 From: Jonny Date: Fri, 28 Feb 2025 15:50:53 +0000 Subject: [PATCH] refactor: style fixes and fold combinator used instead of explicit pattern match --- src/main/wacc/Main.scala | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/wacc/Main.scala b/src/main/wacc/Main.scala index 71a65bb..4d3c101 100644 --- a/src/main/wacc/Main.scala +++ b/src/main/wacc/Main.scala @@ -6,8 +6,7 @@ import parsley.{Failure, Success} import scopt.OParser import java.io.File import java.io.PrintStream -import cats.implicits._ -import cats.effect.unsafe.implicits.global +import cats.implicits.* import assemblyIR as asm import cats.effect.IO @@ -70,31 +69,31 @@ val s = "enter an integer to echo" def backend(typedProg: microWacc.Program): Chain[asm.AsmLine] = asmGenerator.generateAsm(typedProg) - def compile(filename: String, outFile: Option[File] = None)(using stdout: PrintStream = Console.out ): IO[Int] = for { contents <- IO(os.read(os.Path(filename))) result <- frontend(contents) - exitCode <- result match { - case Left(code) => IO.pure(code) // Return error code - case Right(typedProg) => + exitCode <- result.fold( + IO.pure, // Return error code (handles Left case) + typedProg => IO { writer.writeTo( backend(typedProg), PrintStream(outFile.getOrElse(File(filename.stripSuffix(".wacc") + ".s"))) ) }.as(0) // Compilation succeeded - } + ) } yield exitCode object Main extends IOApp.Simple { override def run: IO[Unit] = - OParser.parse(cliParser, sys.env.getOrElse("WACC_ARGS", "").split(" "), CliConfig()).traverse_ { config => - compile( - config.file.getAbsolutePath, - outFile = Some(File(".", config.file.getName.stripSuffix(".wacc") + ".s")) - ) - } + OParser.parse(cliParser, sys.env.getOrElse("WACC_ARGS", "").split(" "), CliConfig()).traverse_ { + config => + compile( + config.file.getAbsolutePath, + outFile = Some(File(".", config.file.getName.stripSuffix(".wacc") + ".s")) + ) + } }