feat: setup cli parsing library, return exit code

Merge request lab2425_spring/WACC_37!3
This commit is contained in:
Gleb Koval 2025-01-31 16:39:07 +00:00
commit ef00cf1093
4 changed files with 38 additions and 17 deletions

View File

@ -1,2 +1,2 @@
version = 3.8.6
runner.dialect = scala36
runner.dialect = scala3

View File

@ -4,6 +4,7 @@
// dependencies
//> using dep com.github.j-mie6::parsley::5.0.0-M10
//> using dep com.lihaoyi::os-lib::0.11.3
//> using dep com.github.scopt::scopt::4.1.0
//> using test.dep org.scalatest::scalatest::3.2.19
// these are all sensible defaults to catch annoying issues

View File

@ -1,16 +1,36 @@
package wacc
import parsley.{Success, Failure}
import scopt.OParser
import java.io.File
def main(args: Array[String]): Unit = {
println("hello WACC!")
case class CliConfig(
file: File = new File(".")
)
args.headOption match {
case Some(expr) =>
parser.parse(expr) match {
case Success(x) => println(s"$expr = $x")
case Failure(msg) => println(msg)
}
case None => println("please enter an expression")
}
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 main(args: Array[String]): Unit =
OParser.parse(cliParser, args, CliConfig()) match {
case Some(config) =>
System.exit(scala.util.Random.between(0, 3) * 100)
case None =>
}

View File

@ -11,16 +11,16 @@ class ParallelExamplesSpec
with ParallelTestExecution {
val files =
allWaccFiles("wacc-examples/valid").map { p =>
(p.toString, (_: Int) == 0)
(p.toString, List(0))
} ++
allWaccFiles("wacc-examples/invalid/syntaxErr").map { p =>
(p.toString, (_: Int) == 100)
(p.toString, List(100))
} ++
allWaccFiles("wacc-examples/invalid/semanticErr").map { p =>
(p.toString, (_: Int) == 200)
(p.toString, List(200))
} ++
allWaccFiles("wacc-examples/invalid/whack").map { p =>
(p.toString, List(0, 100, 200).contains)
(p.toString, List(0, 100, 200))
}
// tests go here
@ -30,7 +30,7 @@ class ParallelExamplesSpec
s"$filename" should "be parsed with correct result" in {
val contents = os.read(os.Path(filename))
parser.parse(contents) match {
case Success(x) => assert(expectedResult(x.toInt))
case Success(x) => assert(expectedResult.contains(x))
case Failure(msg) => fail(msg)
}
}