From 7e9934428f38a8e76cacd313195568bd70e6a4e6 Mon Sep 17 00:00:00 2001 From: ysthakur <45539777+ysthakur@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:04:15 -0500 Subject: [PATCH] Add a main method --- build.sbt | 1 + src/main/scala/Main.scala | 6 ---- src/main/scala/fred/Compiler.scala | 53 ++++++++++++++++++++++++++-- src/main/scala/fred/Translator.scala | 10 +++++- src/test/scala/fred/FuzzTests.scala | 2 +- 5 files changed, 62 insertions(+), 10 deletions(-) delete mode 100644 src/main/scala/Main.scala diff --git a/build.sbt b/build.sbt index b6644cc..6cce5ba 100644 --- a/build.sbt +++ b/build.sbt @@ -17,6 +17,7 @@ lazy val root = project libraryDependencies ++= Seq( "org.typelevel" %% "cats-parse" % "0.3.9", + "com.github.scopt" %% "scopt" % "4.1.0", "org.scalactic" %% "scalactic" % "3.2.19", "org.scalatest" %% "scalatest" % "3.2.19" % Test, "org.scalatestplus" %% "scalacheck-1-18" % "3.2.19.0" % Test, diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala deleted file mode 100644 index c193594..0000000 --- a/src/main/scala/Main.scala +++ /dev/null @@ -1,6 +0,0 @@ -@main -def hello(): Unit = - println("Hello world!") - println(msg) - -def msg = "I was compiled by Scala 3. :)" diff --git a/src/main/scala/fred/Compiler.scala b/src/main/scala/fred/Compiler.scala index dce53e7..dfaf3ad 100644 --- a/src/main/scala/fred/Compiler.scala +++ b/src/main/scala/fred/Compiler.scala @@ -2,11 +2,59 @@ package fred import scala.sys.process.* import java.nio.file.Paths +import java.io.File + +import scopt.OParser object Compiler { private val RuntimeHeader = "runtime/runtime.h" - def compile(code: String, outExe: String): Unit = { + case class Settings( + translatorSettings: Translator.Settings = Translator.Settings() + ) + + def main(args: Array[String]): Unit = { + case class Options( + inFile: Option[File] = None, + outExe: Option[String] = None, + rcAlgo: Translator.RcAlgo = Translator.RcAlgo.Mine + ) + + val builder = OParser.builder[Options] + val parser = { + import builder._ + OParser.sequence( + programName("fred"), + arg[File]("file").action((f, opts) => opts.copy(inFile = Some(f))) + .text("Input file"), + opt[String]('o', "out").action((f, opts) => opts.copy(outExe = Some(f))) + .text("Output executable"), + opt[Unit]("lazy-mark-scan-only").action((_, opts) => + opts.copy(rcAlgo = Translator.RcAlgo.LazyMarkScan) + ).text("Use base lazy mark scan algorithm instead of my cool one :(") + ) + } + OParser.parse(parser, args, Options()) match { + case Some(opts) => + val codeSource = io.Source.fromFile(opts.inFile.get) + val code = + try { codeSource.mkString } + finally { codeSource.close() } + Compiler.compile( + code, + opts.outExe.get, + settings = + Settings(translatorSettings = Translator.Settings(opts.rcAlgo)) + ) + case None => + } + } + + def compile( + code: String, + outExe: String, + settings: Settings = Settings() + ): Unit = { val parsedFile = Parser.parse(code) given typer: Typer = try { Typer.resolveAllTypes(parsedFile) } @@ -17,7 +65,8 @@ object Compiler { System.exit(1) throw new AssertionError("Shouldn't get here") } - val generatedC = Translator.toC(parsedFile) + val generatedC = Translator + .toC(parsedFile, settings = settings.translatorSettings) invokeGCC(generatedC, outExe) } diff --git a/src/main/scala/fred/Translator.scala b/src/main/scala/fred/Translator.scala index f3d4c3f..19928cd 100644 --- a/src/main/scala/fred/Translator.scala +++ b/src/main/scala/fred/Translator.scala @@ -7,7 +7,15 @@ object Translator { private val NoMangleFns = Set("main", "printf") - def toC(file: ParsedFile)(using typer: Typer): String = { + case class Settings(algo: RcAlgo = RcAlgo.Mine) + + enum RcAlgo { + case LazyMarkScan, Mine + } + + def toC(file: ParsedFile, settings: Settings = Settings())(using + typer: Typer + ): String = { given Bindings = Bindings.fromFile(file) given cycles: Cycles = Cycles.fromFile(file) val helper = Helper(typer) diff --git a/src/test/scala/fred/FuzzTests.scala b/src/test/scala/fred/FuzzTests.scala index 5e17cb3..0c3d9c4 100644 --- a/src/test/scala/fred/FuzzTests.scala +++ b/src/test/scala/fred/FuzzTests.scala @@ -20,7 +20,7 @@ class FuzzTests } } - property("Generated programs", Slow) { + property("No intermediate checks", Slow) { given PropertyCheckConfiguration = PropertyCheckConfiguration(minSize = 1, sizeRange = 30)