Skip to content

Commit

Permalink
move more code to Package
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Dec 13, 2024
1 parent 14949ae commit cb45e72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
33 changes: 31 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/Package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.bykn.bosatsu

import cats.{Functor, Order, Parallel}
import cats.{Functor, Order, Parallel, Applicative}
import cats.data.{Ior, ValidatedNel, Validated, NonEmptyList}
import cats.implicits._
import cats.syntax.all._
import cats.parse.{Parser0 => P0, Parser => P}
import org.typelevel.paiges.{Doc, Document}
import scala.util.hashing.MurmurHash3
Expand Down Expand Up @@ -69,6 +69,9 @@ object Package {
type Parsed = Package[PackageName, Unit, Unit, List[Statement]]
type Resolved =
FixPackage[Unit, Unit, (List[Statement], ImportMap[PackageName, Unit])]
type ResolvedPackage =
Package[Resolved, Unit, Unit, (List[Statement], ImportMap[PackageName, Unit])]

type TypedProgram[T] = (
Program[TypeEnv[Kind.Arg], TypedExpr[T], Any],
ImportMap[Interface, NonEmptyList[Referant[Kind.Arg]]]
Expand Down Expand Up @@ -588,6 +591,32 @@ object Package {
}
}

implicit class ResolvedMedthods(private val resolved: Resolved) extends AnyVal {
def importName[F[_], A](
fromPackage: PackageName,
item: ImportedName[Unit]
)(recurse: ResolvedPackage => F[Typed[A]])(implicit F: Applicative[F]): F[Either[PackageError, (Package.Interface, ImportedName[NonEmptyList[Referant[Kind.Arg]]])]] =
Package.unfix(resolved) match {
case Right(p) =>
/*
* Here we have a source we need to fully resolve
*/
recurse(p)
.map { packF =>
val packInterface = Package.interfaceOf(packF)
packF.getImport(fromPackage, item)
.map((packInterface, _))
}
case Left(iface) =>
/*
* this import is already an interface, we can stop here
*/
// this is very fast and does not need to be done in a thread
F.pure(iface.getImportIface(fromPackage, item)
.map((iface, _)))
}
}

def orderByName[A, B, C, D]: Order[Package[A, B, C, D]] =
Order.by[Package[A, B, C, D], PackageName](_.name)
}
46 changes: 11 additions & 35 deletions core/src/main/scala/org/bykn/bosatsu/PackageMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,44 +424,20 @@ object PackageMap {
* type can have the same name as a constructor. After this step, each
* distinct object has its own entry in the list
*/
type IName = NonEmptyList[Referant[Kind.Arg]]

def resFromEither[A, B](either: Either[A, B]): IorT[F, NonEmptyList[A], B] =
IorT.fromEither(either.left.map(NonEmptyList.one(_)))

def stepImport(
fixpack: Package.Resolved,
item: ImportedName[Unit]
): FutVal[(Package.Interface, ImportedName[IName])] =
Package.unfix(fixpack) match {
case Right(p) =>
/*
* Here we have a source we need to fully resolve
*/
IorT(recurse(p))
.flatMap { case (_, packF) =>
val packInterface = Package.interfaceOf(packF)
val either = packF.getImport(nm, item)
.map((packInterface, _))

resFromEither(either)
}
case Left(iface) =>
/*
* this import is already an interface, we can stop here
*/
// this is very fast and does not need to be done in a thread
val e =
iface.getImportIface(nm, item)
.map((iface, _))

resFromEither(e)
}

val inferImports: FutVal[
ImportMap[Package.Interface, NonEmptyList[Referant[Kind.Arg]]]
] =
resolvedImports.parTraverse(stepImport(_, _))
] = {
// here we just need the interface, not the TypeEnv
val rec1 = recurse.andThen { res => IorT(res).map(_._2) }

resolvedImports.parTraverse { (fixpack: Package.Resolved, item: ImportedName[Unit]) =>
fixpack.importName(nm, item)(rec1(_))
.flatMap { either =>
IorT.fromEither(either.left.map(NonEmptyList.one(_)))
}
}
}

val inferBody =
inferImports
Expand Down

0 comments on commit cb45e72

Please sign in to comment.