56 lines
1.4 KiB
Scala
56 lines
1.4 KiB
Scala
package wacc
|
|
|
|
import scala.collection.mutable
|
|
import parsley.{Failure, Success}
|
|
import scopt.OParser
|
|
import java.io.File
|
|
|
|
case class CliConfig(
|
|
file: File = new File(".")
|
|
)
|
|
|
|
val cliBuilder = OParser.builder[CliConfig]
|
|
val cliParser = {
|
|
import cliBuilder._
|
|
OParser.sequence(
|
|
programName("wacc-compiler"),
|
|
help('h', "help")
|
|
.text("Prints this help message"),
|
|
arg[File]("<file>")
|
|
.text("Input WACC source file")
|
|
.required()
|
|
.action((f, c) => c.copy(file = f))
|
|
.validate(f =>
|
|
if (!f.exists) failure("File does not exist")
|
|
else if (!f.isFile) failure("File must be a regular file")
|
|
else if (!f.getName.endsWith(".wacc"))
|
|
failure("File must have .wacc extension")
|
|
else success
|
|
)
|
|
)
|
|
}
|
|
|
|
def compile(contents: String): Int = {
|
|
parser.parse(contents) match {
|
|
case Success(prog) =>
|
|
given errors: mutable.Builder[Error, List[Error]] = List.newBuilder
|
|
val names = renamer.rename(prog)
|
|
given ctx: typeChecker.TypeCheckerCtx = typeChecker.TypeCheckerCtx(names, errors)
|
|
typeChecker.check(prog)
|
|
if (errors.result.nonEmpty) {
|
|
errors.result.foreach(println)
|
|
200
|
|
} else 0
|
|
case Failure(msg) =>
|
|
println(msg)
|
|
100
|
|
}
|
|
}
|
|
|
|
def main(args: Array[String]): Unit =
|
|
OParser.parse(cliParser, args, CliConfig()) match {
|
|
case Some(config) =>
|
|
System.exit(compile(os.read(os.Path(config.file.getAbsolutePath))))
|
|
case None =>
|
|
}
|