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 scopt.OParser
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
import cats.implicits._
|
import cats.implicits.*
|
||||||
import cats.effect.unsafe.implicits.global
|
|
||||||
|
|
||||||
import assemblyIR as asm
|
import assemblyIR as asm
|
||||||
import cats.effect.IO
|
import cats.effect.IO
|
||||||
@@ -70,31 +69,31 @@ val s = "enter an integer to echo"
|
|||||||
def backend(typedProg: microWacc.Program): Chain[asm.AsmLine] =
|
def backend(typedProg: microWacc.Program): Chain[asm.AsmLine] =
|
||||||
asmGenerator.generateAsm(typedProg)
|
asmGenerator.generateAsm(typedProg)
|
||||||
|
|
||||||
|
|
||||||
def compile(filename: String, outFile: Option[File] = None)(using
|
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)))
|
||||||
result <- frontend(contents)
|
result <- frontend(contents)
|
||||||
exitCode <- result match {
|
exitCode <- result.fold(
|
||||||
case Left(code) => IO.pure(code) // Return error code
|
IO.pure, // Return error code (handles Left case)
|
||||||
case Right(typedProg) =>
|
typedProg =>
|
||||||
IO {
|
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
|
}.as(0) // Compilation succeeded
|
||||||
}
|
)
|
||||||
} yield exitCode
|
} yield exitCode
|
||||||
|
|
||||||
object Main extends IOApp.Simple {
|
object Main extends IOApp.Simple {
|
||||||
override def run: IO[Unit] =
|
override def run: IO[Unit] =
|
||||||
OParser.parse(cliParser, sys.env.getOrElse("WACC_ARGS", "").split(" "), CliConfig()).traverse_ { config =>
|
OParser.parse(cliParser, sys.env.getOrElse("WACC_ARGS", "").split(" "), CliConfig()).traverse_ {
|
||||||
compile(
|
config =>
|
||||||
config.file.getAbsolutePath,
|
compile(
|
||||||
outFile = Some(File(".", config.file.getName.stripSuffix(".wacc") + ".s"))
|
config.file.getAbsolutePath,
|
||||||
)
|
outFile = Some(File(".", config.file.getName.stripSuffix(".wacc") + ".s"))
|
||||||
}
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user