Skip to content

Commit

Permalink
Add a main method
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Dec 2, 2024
1 parent d591f95 commit 7e99344
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 0 additions & 6 deletions src/main/scala/Main.scala

This file was deleted.

53 changes: 51 additions & 2 deletions src/main/scala/fred/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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)
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/fred/Translator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/fred/FuzzTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FuzzTests
}
}

property("Generated programs", Slow) {
property("No intermediate checks", Slow) {
given PropertyCheckConfiguration =
PropertyCheckConfiguration(minSize = 1, sizeRange = 30)

Expand Down

0 comments on commit 7e99344

Please sign in to comment.