Skip to content

Commit

Permalink
Reformat with scalafmt 3.7.14
Browse files Browse the repository at this point in the history
Executed command: scalafmt --non-interactive
  • Loading branch information
scala-steward committed Sep 18, 2023
1 parent f211b17 commit f6cde92
Show file tree
Hide file tree
Showing 124 changed files with 14,926 additions and 8,734 deletions.
20 changes: 12 additions & 8 deletions base/src/main/scala/org/bykn/bosatsu/Macro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class Macro(val c: Context) {
if (f.exists()) {
val res = Source.fromFile(s, "UTF-8").getLines().mkString("\n")
Some(c.Expr[String](q"$res"))
}
else {
} else {
None
}
}
catch {
} catch {
case NonFatal(err) =>
c.abort(c.enclosingPosition, s"could not read existing file: $s. Exception: $err")
c.abort(
c.enclosingPosition,
s"could not read existing file: $s. Exception: $err"
)
}

file.tree match {
Expand All @@ -34,11 +35,14 @@ class Macro(val c: Context) {
.getOrElse {
c.abort(
c.enclosingPosition,
s"no file found at: $s. working directory is ${System.getProperty("user.dir")}")
s"no file found at: $s. working directory is ${System.getProperty("user.dir")}"
)
}
case otherTree =>
c.abort(c.enclosingPosition, s"expected string literal, found: $otherTree")
c.abort(
c.enclosingPosition,
s"expected string literal, found: $otherTree"
)
}
}
}

31 changes: 23 additions & 8 deletions bench/src/main/scala/org/bykn/bosatsu/TestBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class TestBench {
// don't use threads in the benchmark which will complicate matters
import DirectEC.directEC

private def prepPackages(packages: List[String], mainPackS: String): (PackageMap.Inferred, PackageName) = {
private def prepPackages(
packages: List[String],
mainPackS: String
): (PackageMap.Inferred, PackageName) = {
val mainPack = PackageName.parse(mainPackS).get

val parsed = packages.zipWithIndex.traverse { case (pack, i) =>
Expand All @@ -28,23 +31,32 @@ class TestBench {
val d = p.showContext(LocationMap.Colorize.None)
System.err.println(d.render(100))
}
sys.error("failed to parse") //errs.toString)
sys.error("failed to parse") // errs.toString)
}

implicit val show: Show[(String, LocationMap)] = Show.show { case (s, _) => s }
PackageMap.resolveThenInfer(PackageMap.withPredefA(("predef", LocationMap("")), parsedPaths), Nil).strictToValidated match {
implicit val show: Show[(String, LocationMap)] = Show.show { case (s, _) =>
s
}
PackageMap
.resolveThenInfer(
PackageMap.withPredefA(("predef", LocationMap("")), parsedPaths),
Nil
)
.strictToValidated match {
case Validated.Valid(packMap) =>
(packMap, mainPack)
case other => sys.error(s"expected clean compilation: $other")
}
}

def gauss(n: Int) = prepPackages(
List(s"""
List(s"""
package Gauss

gauss$n = range($n).foldLeft(0, add)
"""), "Gauss")
"""),
"Gauss"
)

val compiled0: (PackageMap.Inferred, PackageName) =
gauss(10)
Expand All @@ -69,7 +81,8 @@ gauss$n = range($n).foldLeft(0, add)
}

val compiled2 =
prepPackages(List("""
prepPackages(
List("""
package Euler4
def operator >(a, b):
Expand Down Expand Up @@ -132,7 +145,9 @@ max_pal_opt = max_of(99, \n1 -> first_of(99, product_palindrome(n1)))
max_pal = match max_pal_opt:
Some(m): m
None: 0
"""), "Euler4")
"""),
"Euler4"
)

@Benchmark def bench2(): Unit = {
val c = compiled2
Expand Down
82 changes: 53 additions & 29 deletions cli/src/main/scala/org/bykn/bosatsu/PathModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ object PathModule extends MainModule[IO] {
def readInterfaces(paths: List[Path]): IO[List[Package.Interface]] =
ProtoConverter.readInterfaces(paths)

def writeInterfaces(interfaces: List[Package.Interface], path: Path): IO[Unit] =
def writeInterfaces(
interfaces: List[Package.Interface],
path: Path
): IO[Unit] =
ProtoConverter.writeInterfaces(interfaces, path)

def writePackages[A](packages: List[Package.Typed[A]], path: Path): IO[Unit] =
Expand All @@ -54,14 +57,14 @@ object PathModule extends MainModule[IO] {
Some(IO {
f.listFiles.iterator.map(_.toPath).toList
})
}
else None
} else None
}
}
}

def hasExtension(str: String): Path => Boolean =
{ (path: Path) => path.toString.endsWith(str) }
def hasExtension(str: String): Path => Boolean = { (path: Path) =>
path.toString.endsWith(str)
}

def print(str: => String): IO[Unit] =
IO(println(str))
Expand All @@ -71,39 +74,55 @@ object PathModule extends MainModule[IO] {
def report(io: IO[Output]): IO[ExitCode] =
io.attempt.flatMap {
case Right(out) => reportOutput(out)
case Left(err) => reportException(err).as(ExitCode.Error)
case Left(err) => reportException(err).as(ExitCode.Error)
}

def reportOutput(out: Output): IO[ExitCode] =
out match {
case Output.TestOutput(resMap, color) =>
val noTests = resMap.collect { case (p, None) => p }.toList
val results = resMap.collect { case (p, Some(t)) => (p, Test.report(t.value, color)) }.toList.sortBy(_._1)
val results = resMap
.collect { case (p, Some(t)) => (p, Test.report(t.value, color)) }
.toList
.sortBy(_._1)

val successes = results.iterator.map { case (_, (s, _, _)) => s }.sum
val failures = results.iterator.map { case (_, (_, f, _)) => f }.sum
val success = noTests.isEmpty && (failures == 0)
val suffix =
if (results.lengthCompare(1) > 0) (Doc.hardLine + Doc.hardLine + Test.summary(successes, failures, color))
if (results.lengthCompare(1) > 0)
(Doc.hardLine + Doc.hardLine + Test.summary(
successes,
failures,
color
))
else Doc.empty
val docRes: Doc =
Doc.intercalate(Doc.hardLine + Doc.hardLine,
Doc.intercalate(
Doc.hardLine + Doc.hardLine,
results.map { case (p, (_, _, d)) =>
Doc.text(p.asString) + Doc.char(':') + (Doc.lineOrSpace + d).nested(2)
}) + suffix

Doc.text(p.asString) + Doc.char(':') + (Doc.lineOrSpace + d)
.nested(2)
}
) + suffix

if (success) print(docRes.render(80)).as(ExitCode.Success)
else {
val missingDoc =
if (noTests.isEmpty) Nil
else {
val prefix = Doc.text("packages with missing tests: ")
val missingDoc = Doc.intercalate(Doc.comma + Doc.lineOrSpace, noTests.sorted.map { p => Doc.text(p.asString) })
val missingDoc = Doc.intercalate(
Doc.comma + Doc.lineOrSpace,
noTests.sorted.map { p => Doc.text(p.asString) }
)
(prefix + missingDoc.nested(2)) :: Nil
}

val fullOut = Doc.intercalate(Doc.hardLine + Doc.hardLine + (Doc.char('#') * 80) + Doc.line, docRes :: missingDoc)
val fullOut = Doc.intercalate(
Doc.hardLine + Doc.hardLine + (Doc.char('#') * 80) + Doc.line,
docRes :: missingDoc
)

val failureStr =
if (failures == 1) "1 test failure"
Expand All @@ -114,33 +133,37 @@ object PathModule extends MainModule[IO] {
if (missingCount > 0) {
val packString = if (missingCount == 1) "package" else "packages"
s"$failureStr and $missingCount $packString with no tests found"
}
else failureStr
} else failureStr

print((fullOut + Doc.hardLine + Doc.hardLine + Doc.text(excepMessage)).render(80))
print(
(fullOut + Doc.hardLine + Doc.hardLine + Doc.text(excepMessage))
.render(80)
)
.as(ExitCode.Error)
}
case Output.EvaluationResult(_, tpe, resDoc) =>
val tDoc = rankn.Type.fullyResolvedDocument.document(tpe)
val doc = resDoc.value + (Doc.lineOrEmpty + Doc.text(": ") + tDoc).nested(4)
val doc =
resDoc.value + (Doc.lineOrEmpty + Doc.text(": ") + tDoc).nested(4)
print(doc.render(100)).as(ExitCode.Success)
case Output.JsonOutput(json, pathOpt) =>
val jdoc = json.toDoc
(pathOpt match {
case Some(path) => CodeGenWrite.writeDoc(path, jdoc)
case None => IO(println(jdoc.renderTrim(80)))
case None => IO(println(jdoc.renderTrim(80)))
}).as(ExitCode.Success)

case Output.TranspileOut(outs, base) =>
def path(p: List[String]): Path =
p.foldLeft(base)(_.resolve(_))

outs.toList.map { case (p, d) =>
(p, CodeGenWrite.writeDoc(path(p.toList), d))
}
.sortBy(_._1)
.traverse_ { case (_, w) => w }
.as(ExitCode.Success)
outs.toList
.map { case (p, d) =>
(p, CodeGenWrite.writeDoc(path(p.toList), d))
}
.sortBy(_._1)
.traverse_ { case (_, w) => w }
.as(ExitCode.Success)

case Output.CompileOut(packList, ifout, output) =>
val ifres = ifout match {
Expand Down Expand Up @@ -168,7 +191,8 @@ object PathModule extends MainModule[IO] {
import scala.jdk.CollectionConverters._

def getP(p: Path): Option[PackageName] = {
val subPath = p.relativize(packFile)
val subPath = p
.relativize(packFile)
.asScala
.map { part =>
part.toString.toLowerCase.capitalize
Expand All @@ -178,17 +202,17 @@ object PathModule extends MainModule[IO] {
val dropExtension = """(.*)\.[^.]*$""".r
val toParse = subPath match {
case dropExtension(prefix) => prefix
case _ => subPath
case _ => subPath
}
PackageName.parse(toParse)
}

@annotation.tailrec
def loop(roots: List[Path]): Option[PackageName] =
roots match {
case Nil => None
case Nil => None
case h :: _ if packFile.startsWith(h) => getP(h)
case _ :: t => loop(t)
case _ :: t => loop(t)
}

if (packFile.toString.isEmpty) None
Expand Down
Loading

0 comments on commit f6cde92

Please sign in to comment.