-
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.
- Loading branch information
Showing
18 changed files
with
457 additions
and
237 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.bykn.bosatsu | ||
|
||
import cats.{Applicative, Functor, Order, Parallel} | ||
import scala.collection.immutable.SortedMap | ||
|
||
import cats.syntax.all._ | ||
|
||
/** There are all the distinct imported names and the original ImportedName | ||
*/ | ||
case class ImportMap[A, B](toMap: SortedMap[Identifier, (A, ImportedName[B])]) { | ||
def apply(name: Identifier): Option[(A, ImportedName[B])] = | ||
toMap.get(name) | ||
|
||
def +(that: (A, ImportedName[B])): ImportMap[A, B] = | ||
ImportMap(toMap.updated(that._2.localName, that)) | ||
|
||
def toList(implicit ord: Order[A]): List[Import[A, B]] = | ||
toMap.iterator | ||
.map { case (_, ab) => ab } | ||
.toList | ||
.groupByNel(_._1) | ||
.iterator | ||
.map { case (a, bs) => | ||
Import(a, bs.map(_._2)) | ||
} | ||
.toList | ||
|
||
def traverse[F[_]: Applicative, C, D](fn: (A, ImportedName[B]) => F[(C, ImportedName[D])]): F[ImportMap[C, D]] = | ||
toMap.traverse[F, (C, ImportedName[D])] { case (a, ib) => fn(a, ib) } | ||
.map(ImportMap(_)) | ||
|
||
def parTraverse[F[_]: Parallel: Functor, C, D](fn: (A, ImportedName[B]) => F[(C, ImportedName[D])]): F[ImportMap[C, D]] = | ||
toMap.parTraverse[F, (C, ImportedName[D])] { case (a, ib) => fn(a, ib) } | ||
.map(ImportMap(_)) | ||
} | ||
|
||
object ImportMap { | ||
def empty[A, B]: ImportMap[A, B] = ImportMap(SortedMap.empty) | ||
|
||
sealed abstract class Unify | ||
object Unify { | ||
case object Error extends Unify | ||
case object Left extends Unify | ||
case object Right extends Unify | ||
} | ||
// Return the list of collisions in local names along with a map | ||
// with the last name overwriting the import | ||
def fromImports[A, B]( | ||
is: List[Import[A, B]] | ||
)(unify: ((A, ImportedName[B]), (A, ImportedName[B])) => Unify): (List[(A, ImportedName[B])], ImportMap[A, B]) = | ||
is.iterator | ||
.flatMap { case Import(p, is) => is.toList.iterator.map((p, _)) } | ||
.foldLeft((List.empty[(A, ImportedName[B])], ImportMap.empty[A, B])) { | ||
case (old @ (dups, imap), pim @ (_, im)) => | ||
val (dups1, imap1) = imap(im.localName) match { | ||
case Some(nm) => | ||
unify(nm, pim) match { | ||
case Unify.Error => | ||
// pim and nm are a collision, add both | ||
(pim :: nm :: dups, imap + pim) | ||
case Unify.Left => old | ||
case Unify.Right => (dups, imap + pim) | ||
} | ||
case None => (dups, imap + pim) | ||
} | ||
|
||
(dups1.reverse.distinct, imap1) | ||
} | ||
|
||
// This is only safe after verifying there are not collisions | ||
// which has been done on compiled packages | ||
def fromImportsUnsafe[A, B](is: List[Import[A, B]]): ImportMap[A, B] = | ||
fromImports(is)((a, b) => sys.error(s"collision in $a and $b: $is"))._2 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.bykn.bosatsu | ||
|
||
import cats.Functor | ||
import cats.parse.{Parser => P} | ||
import org.typelevel.paiges.{Doc, Document} | ||
import Parser.spaces | ||
|
||
sealed abstract class ImportedName[+T] { | ||
def originalName: Identifier | ||
def localName: Identifier | ||
def tag: T | ||
def isRenamed: Boolean = originalName != localName | ||
def withTag[U](tag: U): ImportedName[U] | ||
|
||
def map[U](fn: T => U): ImportedName[U] = | ||
this match { | ||
case ImportedName.OriginalName(n, t) => | ||
ImportedName.OriginalName(n, fn(t)) | ||
case ImportedName.Renamed(o, l, t) => | ||
ImportedName.Renamed(o, l, fn(t)) | ||
} | ||
|
||
def traverse[F[_], U]( | ||
fn: T => F[U] | ||
)(implicit F: Functor[F]): F[ImportedName[U]] = | ||
this match { | ||
case ImportedName.OriginalName(n, t) => | ||
F.map(fn(t))(ImportedName.OriginalName(n, _)) | ||
case ImportedName.Renamed(o, l, t) => | ||
F.map(fn(t))(ImportedName.Renamed(o, l, _)) | ||
} | ||
} | ||
|
||
object ImportedName { | ||
case class OriginalName[T](originalName: Identifier, tag: T) | ||
extends ImportedName[T] { | ||
def localName = originalName | ||
def withTag[U](tag: U): ImportedName[U] = copy(tag = tag) | ||
} | ||
case class Renamed[T](originalName: Identifier, localName: Identifier, tag: T) | ||
extends ImportedName[T] { | ||
def withTag[U](tag: U): ImportedName[U] = copy(tag = tag) | ||
} | ||
|
||
implicit val document: Document[ImportedName[Unit]] = | ||
Document.instance[ImportedName[Unit]] { | ||
case ImportedName.OriginalName(nm, _) => Document[Identifier].document(nm) | ||
case ImportedName.Renamed(from, to, _) => | ||
Document[Identifier].document(from) + Doc.text(" as ") + | ||
Document[Identifier].document(to) | ||
} | ||
|
||
val parser: P[ImportedName[Unit]] = { | ||
def basedOn(of: P[Identifier]): P[ImportedName[Unit]] = | ||
(of ~ (spaces.soft *> P.string("as") *> spaces *> of).?) | ||
.map { | ||
case (from, Some(to)) => ImportedName.Renamed(from, to, ()) | ||
case (orig, None) => ImportedName.OriginalName(orig, ()) | ||
} | ||
|
||
basedOn(Identifier.bindableParser) | ||
.orElse(basedOn(Identifier.consParser)) | ||
} | ||
} |
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
Oops, something went wrong.