refactor: style fixes and fold combinator used instead of explicit pattern match
This commit is contained in:
@@ -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"))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user