-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Transpiler options into instances (#1272)
* Move Transpiler options into instances * fix tests
- Loading branch information
Showing
6 changed files
with
110 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 28 additions & 19 deletions
47
core/src/main/scala/org/bykn/bosatsu/codegen/Transpiler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
package org.bykn.bosatsu.codegen | ||
|
||
import cats.data.{NonEmptyList, ValidatedNel, Validated} | ||
import com.monovore.decline.Argument | ||
import cats.{Applicative, Traverse} | ||
import cats.data.NonEmptyList | ||
import com.monovore.decline.{Argument, Opts} | ||
import org.bykn.bosatsu.{PackageMap, Par} | ||
import org.typelevel.paiges.Doc | ||
import scala.util.Try | ||
|
||
import cats.syntax.all._ | ||
|
||
trait Transpiler { | ||
def name: String | ||
type Args[P] | ||
def traverseArgs: Traverse[Args] | ||
|
||
// this gives the argument for reading files into strings | ||
// this is a bit limited, but good enough for now | ||
def opts[P](pathArg: Argument[P]): Opts[Transpiler.Optioned[P]] | ||
|
||
def renderAll( | ||
pm: PackageMap.Typed[Any], | ||
externals: List[String], | ||
evaluators: List[String] | ||
args: Args[String] | ||
)(implicit ec: Par.EC): Try[List[(NonEmptyList[String], Doc)]] | ||
} | ||
|
||
object Transpiler { | ||
def argumentFromTranspilers(all: List[Transpiler]): Argument[Transpiler] = | ||
new Argument[Transpiler] { | ||
val nameTo = all.iterator.map(t => (t.name, t)).toMap | ||
lazy val keys = nameTo.keys.toList.sorted.mkString(",") | ||
|
||
def defaultMetavar: String = "transpiler" | ||
def read(string: String): ValidatedNel[String, Transpiler] = | ||
nameTo.get(string) match { | ||
case Some(t) => Validated.valid(t) | ||
case None => | ||
Validated.invalidNel( | ||
s"unknown transpiler: $string, expected one of: $keys" | ||
) | ||
} | ||
def optioned[P](t: Transpiler)(argsP: t.Args[P]): Optioned[P] = | ||
new Optioned[P] { | ||
val transpiler: t.type = t | ||
val args = argsP | ||
} | ||
|
||
sealed abstract class Optioned[P] { self => | ||
val transpiler: Transpiler | ||
def args: transpiler.Args[P] | ||
def traverse[F[_]: Applicative](fn: P => F[String]): F[Optioned[String]] = | ||
self.transpiler.traverseArgs.traverse(self.args)(fn) | ||
.map { argsString => | ||
new Optioned[String] { | ||
val transpiler: self.transpiler.type = self.transpiler | ||
val args = argsString | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters