Skip to content

Commit

Permalink
Merge pull request #1 from stg-tud/invariants
Browse files Browse the repository at this point in the history
add integrated Scala DSL
  • Loading branch information
haaase authored Apr 10, 2024
2 parents 5cebc12 + 3ff2863 commit bdc87ca
Show file tree
Hide file tree
Showing 38 changed files with 1,121 additions and 8,492 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ target
.bsp
.bloop
metals.sbt
tmp
tmp
.idea
42 changes: 20 additions & 22 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
val circeVersion = "0.14.4"

lazy val root = (project in file("."))
.settings(
name := "lore",
organization := "de.tu-darmstadt.stg",
version := "0.2-SNAPSHOT",
scalaVersion := "3.3.0",
libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0",
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.0",
scalaVersion := "3.4.0",
libraryDependencies += "org.typelevel" %% "cats-core" % "2.10.0",
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.4",
libraryDependencies += "com.monovore" %% "decline" % "2.4.1",
// libraryDependencies += ("org.scalameta" %% "scalafmt-core" % "3.7.4").cross(
// CrossVersion.for3Use2_13
// ),
libraryDependencies += "org.typelevel" %% "cats-parse" % "0.3.9",
libraryDependencies += "io.circe" %% "circe-core" % circeVersion,
libraryDependencies += "io.circe" %% "circe-generic" % circeVersion,
libraryDependencies += "io.circe" %% "circe-parser" % circeVersion,
libraryDependencies += "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.28.4",
// Use the "provided" scope instead when the "compile-internal" scope is not supported
libraryDependencies += "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.28.4" % "compile-internal",
libraryDependencies += "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.28.4" % "test",
libraryDependencies += "org.typelevel" %% "cats-parse" % "1.0.0",
// libraryDependencies += ("com.lihaoyi" %% "fansi" % "0.4.0").cross(
// CrossVersion.for3Use2_13 // needed because scalafmt is 2.13
// ),
libraryDependencies += ("com.lihaoyi" %% "fansi" % "0.4.0"),
// REScala deps
libraryDependencies += ("de.tu-darmstadt.stg" %% "rescala" % "0.33.0"),
libraryDependencies += ("de.tu-darmstadt.stg" %% "rescala" % "0.35.1"),
// optics dependencies
libraryDependencies ++= Seq(
"dev.optics" %% "monocle-core" % "3.2.0"
Expand All @@ -33,26 +29,28 @@ lazy val root = (project in file("."))
scalacOptions ++= List(
"-deprecation",
"-explain", // explain errors in more detail
"-new-syntax", // force new syntax
// "-new-syntax", // force new syntax
"-rewrite",
"-no-indent",
// warn in case of unused imports and values
"-Wunused:imports",
/*"-Wunused:imports",
"-Wunused:locals",
"-Wunused:privates",
"-Wunused:params",
"-Wunused:implicits",
"-Wunused:linted", // not sure what this does actually
"-Wunused:linted", */ // not sure what this does actually
"-Xfatal-warnings", // turn warnings into errors
"-Xmax-inlines:200" // needed for circe generic
),
// native-image flag "--initialize-at-build-time" is required for Cats Effect applications
nativeImageOptions ++= List(
"--no-fallback",
"--initialize-at-build-time=lore",
"--initialize-at-build-time=com.monovore.decline",
"--initialize-at-build-time=cats",
"--initialize-at-build-time=scala",
"--initialize-at-build-time=com.lihaoyi.fansi",
"--initialize-at-build-time=monocle"
// "--initialize-at-build-time=lore",
// "--initialize-at-build-time=com.monovore.decline",
// "--initialize-at-build-time=cats",
// "--initialize-at-build-time=scala",
"--initialize-at-build-time=com.lihaoyi.fansi"
// "--initialize-at-build-time=monocle"
),
nativeImageJvm := "graalvm-java19",
nativeImageVersion := "22.3.0"
Expand Down
78 changes: 44 additions & 34 deletions src/main/scala/lore/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@ import java.nio.file.{Files, Path, Paths}
import com.monovore.decline._
import cats.implicits._
import cats.data.NonEmptyList
import lore.AST.Term
import lore.ast.Term
import java.nio.file.NoSuchFileException
import lore.Parser.ParsingException
import lore.backends.ViperBackend
import lore.cli._

object Compiler extends IOApp:
object Compiler extends IOApp {

def readFile(path: Path): IO[String] =
private def readFile(path: Path): IO[String] =
IO.blocking(String(Files.readAllBytes(path), StandardCharsets.UTF_8))

def writeFile(path: Path, content: String): IO[Unit] =
for
// create parent directories
_ <- IO.blocking(Files.createDirectories(path.getParent)).attempt
_ <- IO.blocking(
Files.write(path, content.getBytes(StandardCharsets.UTF_8))
)
yield ()
private def writeFile(path: Path, content: String): IO[Unit] = for {
// create parent directories
_ <- IO.blocking(Files.createDirectories(path.getParent)).attempt
_ <- IO.blocking(
Files.write(path, content.getBytes(StandardCharsets.UTF_8))
)
}
yield ()

// def interprete(ast: Seq[AST.ParsedExpression]): IO[Unit] =
// IO.blocking(Interpreter.interprete(ast))

def toScala(ast: NonEmptyList[AST.Term], options: Options): IO[Unit] =
def toScala(ast: NonEmptyList[Term], options: Options): IO[Unit] = {
???
// for
// result <- IO(ScalaBackend.toAmm(ast))
// _ <- options.toFile match
// case None => IO.println(result)
// case Some(path) => writeFile(path, result)
// yield result
}

def toViper(ast: NonEmptyList[AST.Term], options: Options): IO[Unit] =
def toViper(ast: NonEmptyList[Term], options: Options): IO[Unit] = {
ViperBackend.compileAsSingleFile(ast.toList)
options.outputOptions match
case SplitMode(outDir) =>
options.outputOptions match {
case OutputOptions.SplitMode(outDir) =>
ViperBackend
.compileAsSeparateFiles(ast.toList)
.map((filename, program) =>
Expand All @@ -51,14 +49,17 @@ object Compiler extends IOApp:
.as(())
case _ =>
val result = ViperBackend.compileAsSingleFile(ast.toList)
options.toFile match
case None => IO.println(result)
options.toFile match {
case None => IO.println(result)
case Some(path) => writeFile(path, result)
}
}
}

def run(args: List[String]): IO[ExitCode] =
def run(args: List[String]): IO[ExitCode] = {
// parse arguments and combine requested actions
val subcommand: Subcommand = mainCommand.parse(args) match
case h @ Left(Help(errors, _, _, _)) =>
val subcommand: Subcommand = mainCommand.parse(args) match {
case h@Left(Help(errors, _, _, _)) =>
if errors.isEmpty
then
return IO.println(h.value).as(ExitCode.Success) // --help flag given
Expand All @@ -68,35 +69,40 @@ object Compiler extends IOApp:
.as(ExitCode.Error) // parsing subcommands error
case Right(s) =>
s
}
val options = subcommand.options

val result = for
val result = for {
// read program
program <-
if options.file.isDefined
then (readFile(options.file.get))
else IO((options.inline.get))
// parse program
ast <- Parser.parse(program) match
ast <- Parser.parse(program) match {
case Left(e) =>
IO.raiseError(Parser.ParsingException(e.show))
case Right(a) => IO(a)
case Right(a) => IO.pure(a)
}
// perform requested subcommand
result <-
subcommand match
case ToRescala(_) => toScala(ast, options)
case ToViper(_) => toViper(ast, options)
case Parse(_) =>
subcommand match {
case ToREScala(_) => toScala(ast, options)
case ToViper(_) => toViper(ast, options)
case Parse(_) =>
// we already parsed, simply produce output
options.toFile.match
case None => IO.println(ast.toString)
options.toFile.match {
case None => IO.println(ast.toString)
case Some(path) => writeFile(path, ast.toString)
}
}
}
yield result

// check if anything went wrong: print error messages and set return code
for
for {
resultWithErrors <- result.attempt
exitCode <- resultWithErrors match
exitCode <- resultWithErrors match {
case Left(e: NoSuchFileException) =>
IO.println(
fansi.Color
Expand All @@ -117,4 +123,8 @@ object Compiler extends IOApp:
) >> IO(e.printStackTrace())
.as(ExitCode.Error)
case Right(io) => IO(io).as(ExitCode.Success)
}
}
yield exitCode
}
}
66 changes: 0 additions & 66 deletions src/main/scala/lore/DSL/DSL.scala

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/scala/lore/DSL/DSLTest.scala

This file was deleted.

Loading

0 comments on commit bdc87ca

Please sign in to comment.