Skip to content

Commit b01306b

Browse files
committed
Merge branch 'mlscript' into tidy-ir
# Conflicts: # compiler/shared/main/scala/mlscript/compiler/Helpers.scala # compiler/shared/main/scala/mlscript/compiler/ir/Builder.scala # compiler/shared/main/scala/mlscript/compiler/ir/Fresh.scala # compiler/shared/main/scala/mlscript/compiler/ir/IR.scala # compiler/shared/main/scala/mlscript/compiler/ir/Interp.scala # compiler/shared/main/scala/mlscript/compiler/ir/Validator.scala # compiler/shared/main/scala/mlscript/compiler/optimizer/Analysis.scala # compiler/shared/test/diff-ir/IR.mls # compiler/shared/test/diff-ir/IRComplex.mls # compiler/shared/test/diff-ir/IRRec.mls # compiler/shared/test/scala/mlscript/compiler/Test.scala # compiler/shared/test/scala/mlscript/compiler/TestIR.scala # shared/src/main/scala/mlscript/helpers.scala # shared/src/test/scala/mlscript/DiffTests.scala
2 parents 6eb0eb1 + 13ba521 commit b01306b

File tree

378 files changed

+27916
-22625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

378 files changed

+27916
-22625
lines changed

bin/mlscript-opt.js

Lines changed: 3125 additions & 3025 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.sbt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ ThisBuild / scalacOptions ++= Seq(
1010
"-deprecation",
1111
"-feature",
1212
"-unchecked",
13+
"-language:higherKinds",
14+
if (insideCI.value) "-Wconf:any:error"
15+
else "-Wconf:any:warning",
1316
)
1417

1518
lazy val root = project.in(file("."))
@@ -23,14 +26,9 @@ lazy val mlscript = crossProject(JSPlatform, JVMPlatform).in(file("."))
2326
.settings(
2427
name := "mlscript",
2528
scalacOptions ++= Seq(
26-
"-language:higherKinds",
2729
"-Ywarn-value-discard",
2830
"-Ypatmat-exhaust-depth:160",
2931
),
30-
scalacOptions ++= {
31-
if (insideCI.value) Seq("-Wconf:any:error")
32-
else Seq("-Wconf:any:warning")
33-
},
3432
wartremoverWarnings ++= Warts.allBut(
3533
Recursion, Throw, Nothing, Return, While, IsInstanceOf,
3634
Var, MutableDataStructures, NonUnitStatements,

compiler/shared/main/scala/mlscript/compiler/ClassLifter.scala

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import scala.collection.mutable.Map as MutMap
88
import scala.collection.mutable.Set as MutSet
99
import scala.collection.mutable.ArrayBuffer as ArrayBuffer
1010
import mlscript.codegen.CodeGenError
11-
import mlscript.compiler.mono.MonomorphError
11+
12+
class CompilerError(error: String) extends Error(error)
1213

1314
class ClassLifter(logDebugMsg: Boolean = false) {
1415
type ClassName = String
@@ -65,7 +66,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
6566
val primiTypes = new mlscript.Typer(false, false, false, true).primitiveTypes
6667

6768
private def log(str: String): Unit = {
68-
logOutput.append(str)
69+
logOutput.append(str+"\n")
6970
if(logDebugMsg){
7071
println(str)
7172
}
@@ -189,7 +190,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
189190
case TyApp(trm, tpLst) =>
190191
getFreeVars(trm).addT(tpLst.flatMap(_.collectTypeNames.map(TypeName(_))))
191192
case NuTypeDef(_, nm, tps, param, _, _, pars, _, _, body) =>
192-
val prmVs = getFreeVars(param.getOrElse(Tup(Nil)))(using emptyCtx, Map(), globFuncs, None)
193+
val prmVs = param.map(getFreeVars(_)(using emptyCtx, Map(), globFuncs, None)).getOrElse(emptyCtx)
193194
val newVs = prmVs.vSet ++ getFields(body.entities) + Var(nm.name)
194195
val nCtx = ctx.addV(newVs).addT(nm).addT(tps.map(_._2))
195196
val parVs = pars.map(getFreeVars(_)(using nCtx)).fold(emptyCtx)(_ ++ _)
@@ -227,14 +228,14 @@ class ClassLifter(logDebugMsg: Boolean = false) {
227228
}
228229

229230
private def liftCaseBranch(brn: CaseBranches)(using ctx: LocalContext, cache: ClassCache, globFuncs: Map[Var, (Var, LocalContext)], outer: Option[ClassInfoCache]): (CaseBranches, LocalContext) = brn match{
230-
case Case(v: Var, body, rest) =>
231+
case k @ Case(v: Var, body, rest) =>
231232
val nTrm = liftTerm(body)(using ctx.addV(v))
232233
val nRest = liftCaseBranch(rest)
233-
(Case(v, nTrm._1, nRest._1), nTrm._2 ++ nRest._2)
234-
case Case(pat, body, rest) =>
234+
(Case(v, nTrm._1, nRest._1)(k.refined), nTrm._2 ++ nRest._2)
235+
case k @ Case(pat, body, rest) =>
235236
val nTrm = liftTerm(body)
236237
val nRest = liftCaseBranch(rest)
237-
(Case(pat, nTrm._1, nRest._1), nTrm._2 ++ nRest._2)
238+
(Case(pat, nTrm._1, nRest._1)(k.refined), nTrm._2 ++ nRest._2)
238239
case Wildcard(body) =>
239240
val nTrm = liftTerm(body)
240241
(Wildcard(nTrm._1), nTrm._2)
@@ -249,7 +250,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
249250
val nE = liftTerm(expr)
250251
val nR = liftTerm(rhs)
251252
(IfThen(nE._1, nR._1), nE._2 ++ nR._2)
252-
case _ => throw MonomorphError(s"Unknown IfBody: ${body}")
253+
case _ => throw CompilerError(s"Unknown IfBody: ${body}")
253254
}
254255

255256
private def liftTuple(tup: Tup)(using ctx: LocalContext, cache: ClassCache, globFuncs: Map[Var, (Var, LocalContext)], outer: Option[ClassInfoCache]): (Tup, LocalContext) = {
@@ -299,17 +300,20 @@ class ClassLifter(logDebugMsg: Boolean = false) {
299300
}
300301

301302
private def newLambObj(lhs: Term, rhs: Term) =
302-
New(None, TypingUnit(List(NuFunDef(None, Var("apply"), None, Nil, Left(Lam(lhs, rhs)))(N, N, N, N, N, false, Nil)))) //TODO: Use Proper Arguments
303+
New(None, TypingUnit(List(NuFunDef(None, Var("apply"), None, Nil, Left(Lam(lhs, rhs)))(N, N, N, N, N, false, Nil))))
303304

304305
private def liftTerm(target: Term)(using ctx: LocalContext, cache: ClassCache, globFuncs: Map[Var, (Var, LocalContext)], outer: Option[ClassInfoCache]): (Term, LocalContext) =
305306
log(s"liftTermNew $target in $ctx, $cache, $globFuncs, $outer")
306307
target match {
307308
case v: Var =>
308-
if(ctx.contains(v) || v.name.equals("this") || primiTypes.contains(v.name)) (v, emptyCtx)
309+
if(globFuncs.contains(v)) {
310+
(globFuncs.get(v).get)
311+
}
309312
else if(cache.contains(TypeName(v.name))){
310-
val ret = liftConstr(TypeName(v.name), Tup(Nil))
311-
App(Var(ret._1.name), ret._2) -> ret._3
313+
val cls@ClassInfoCache(_, nm, capParams, _, _, _, out, _, _) = cache.get(TypeName(v.name)).get
314+
(Var(nm.name), emptyCtx)
312315
}
316+
else if(ctx.contains(v) || v.name.equals("this") || primiTypes.contains(v.name)) (v, emptyCtx)
313317
else {
314318
buildPathToVar(v) match{
315319
case Some(value) => (value, emptyCtx)
@@ -321,8 +325,8 @@ class ClassLifter(logDebugMsg: Boolean = false) {
321325
val nTpNm = TypeName(genAnoName("Lambda"+prmCnt))
322326
val anoCls = NuTypeDef(
323327
Cls, nTpNm, Nil, S(Tup(Nil)), N, N, Nil, N, N,
324-
TypingUnit(List(NuFunDef(None, Var("apply"), N, Nil, Left(Lam(lhs, rhs)))(N, N, N, N, N, false, Nil))))(N, N, Nil) //TODO: Use Proper Arguments
325-
val nSta = New(Some((nTpNm, Tup(Nil))), TypingUnit(Nil))
328+
TypingUnit(List(NuFunDef(None, Var("apply"), N, Nil, Left(Lam(lhs, rhs)))(N, N, N, N, N, false, Nil))))(N, N, Nil)
329+
val nSta = App(Var(nTpNm.name),Tup(Nil))
326330
val ret = liftEntities(List(anoCls, nSta))
327331
(Blk(ret._1), ret._2)
328332
case t: Tup =>
@@ -341,7 +345,12 @@ class ClassLifter(logDebugMsg: Boolean = false) {
341345
case NuNew(cls) =>
342346
liftTerm(App(NuNew(cls), Tup(Nil)))
343347
case App(NuNew(cls), args) =>
344-
liftTerm(Rft(App(NuNew(cls), args), TypingUnit(Nil)))
348+
(cls, args) match {
349+
case (v: Var, args: Tup) =>
350+
val ret = liftConstr(TypeName(v.name), args)
351+
(App(NuNew(Var(ret._1.name)), ret._2), ret._3)
352+
case _ => ???
353+
}
345354
case Rft(NuNew(cls), tu) =>
346355
liftTerm(Rft(App(NuNew(cls), Tup(Nil)), TypingUnit(Nil)))
347356
case Rft(App(NuNew(cls), args), tu) =>
@@ -385,13 +394,13 @@ class ClassLifter(logDebugMsg: Boolean = false) {
385394
val nTrm = liftTerm(trm)
386395
(If(ret._1, Some(nTrm._1)), ret._2 ++ nTrm._2)
387396
case Let(isRec, name, rhs, body) =>
388-
val nRhs = if(isRec) liftTerm(rhs)(using ctx.addV(name)) else liftTerm(rhs)
389-
val nBody = liftTerm(body)(using ctx.addV(name))
397+
val nRhs = if(isRec) liftTerm(rhs)(using ctx.addV(name), cache, globFuncs.-(name)) else liftTerm(rhs)
398+
val nBody = liftTerm(body)(using ctx.addV(name), cache, globFuncs.-(name))
390399
(Let(isRec, name, nRhs._1, nBody._1), nRhs._2 ++ nBody._2)
391400
case Sel(receiver, fieldName) =>
392401
val nRec = liftTerm(receiver)
393402
(Sel(nRec._1, fieldName), nRec._2)
394-
case Splc(fields) => throw MonomorphError(s"Unimplemented liftTerm: ${target}")
403+
case Splc(fields) => throw CompilerError(s"Unimplemented liftTerm: ${target}")
395404
case Subs(arr, idx) =>
396405
val (ltrm, lctx) = liftTerm(arr)
397406
val (rtrm, rctx) = liftTerm(idx)
@@ -404,7 +413,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
404413
val ret = liftTerm(lhs)
405414
val nTs = targs.map(liftType).unzip
406415
(TyApp(ret._1, nTs._1), nTs._2.fold(ret._2)(_ ++ _))
407-
case With(trm, fields) => throw MonomorphError(s"Unimplemented liftTerm: ${target}")
416+
case With(trm, fields) => throw CompilerError(s"Unimplemented liftTerm: ${target}")
408417
case New(Some((t: TypeName, prm: Tup)), TypingUnit(Nil)) =>
409418
val ret = liftConstr(t, prm)
410419
(New(Some((ret._1, ret._2)), TypingUnit(Nil)), ret._3)
@@ -424,7 +433,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
424433
val nSta = New(Some((nTpNm, Tup(Nil))), TypingUnit(Nil))
425434
val ret = liftEntities(List(anoCls, nSta))
426435
(Blk(ret._1), ret._2)
427-
case New(head, body) => throw MonomorphError(s"Unimplemented liftTerm: ${target}")
436+
case New(head, body) => throw CompilerError(s"Unimplemented liftTerm: ${target}")
428437
case Blk(stmts) =>
429438
val ret = liftEntities(stmts)
430439
(Blk(ret._1), ret._2)
@@ -439,7 +448,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
439448
val (bod2, ctx) = liftTerm(bod)
440449
val (sts2, ctx2) = liftEntities(sts)
441450
(Where(bod2, sts2), ctx2)
442-
case _: Eqn | _: Super | _: Rft | _: While | _: Quoted | _: Unquoted | _: Ann => throw MonomorphError(s"Unimplemented liftTerm: ${target}") // TODO
451+
case _: Eqn | _: Super | _: Rft | _: While | _: Quoted | _: Unquoted | _: Ann => throw CompilerError(s"Unimplemented liftTerm: ${target}") // TODO
443452
case patmat: AdtMatchWith => lastWords(s"Cannot liftTermNew ${patmat}")
444453
}
445454

@@ -476,7 +485,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
476485
((v, Fld(flags, tmp._1)), tmp._2)
477486
}.unzip
478487
(Rcd(ret._1), ret._2.fold(emptyCtx)(_ ++ _))
479-
case _ => throw MonomorphError(s"Unimplemented liftTermAsType: ${target}")
488+
case _ => throw CompilerError(s"Unimplemented liftTermAsType: ${target}")
480489
}
481490

482491
private def liftTypeName(target: TypeName)(using ctx: LocalContext, cache: ClassCache, globFuncs: Map[Var, (Var, LocalContext)], outer: Option[ClassInfoCache]): (TypeName, LocalContext) = {
@@ -570,7 +579,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
570579
val (body2, ctx) = liftType(body)
571580
PolyType(targs, body2) -> ctx
572581
case Top | Bot | _: Literal | _: TypeTag | _: TypeVar => target.asInstanceOf[Type] -> emptyCtx
573-
case _: Selection => throw MonomorphError(s"Unimplemented liftType: ${target}") // TODO
582+
case _: Selection => throw CompilerError(s"Unimplemented liftType: ${target}") // TODO
574583
}
575584

576585

@@ -582,11 +591,10 @@ class ClassLifter(logDebugMsg: Boolean = false) {
582591
val lctx = getFreeVars(lhs)(using emptyCtx, cache, globFuncs, None)
583592
val lret = liftTuple(lhs)(using ctx.addV(lctx.vSet))
584593
val ret = liftTerm(rhs)(using ctx.addV(lctx.vSet).addT(tpVs))
585-
(func.copy(rhs = Left(Lam(lret._1, ret._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations), ret._2 -+ lret._2) //TODO: Check correctness
594+
(func.copy(rhs = Left(Lam(lret._1, ret._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations), ret._2 -+ lret._2)
586595
case Left(value) =>
587-
// will be treated as Lam(Tup(Nil), rhs)
588596
val ret = liftTerm(value)(using ctx.addT(tpVs))
589-
(func.copy(rhs = Left(Lam(Tup(Nil), ret._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations), ret._2) //TODO: Check correctness
597+
(func.copy(rhs = Left(ret._1))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations), ret._2)
590598
case Right(PolyType(targs, body)) =>
591599
val nBody = liftType(body)(using ctx.addT(tpVs))
592600
val nTargs = targs.map {
@@ -595,7 +603,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
595603
}.unzip
596604
(func.copy(rhs = Right(PolyType(nTargs._1, nBody._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations),
597605
nTargs._2.fold(nBody._2)(_ ++ _))
598-
case _ => throw MonomorphError(s"Unimplemented liftMemberFunc: ${func}") // TODO
606+
case _ => throw CompilerError(s"Unimplemented liftMemberFunc: ${func}") // TODO
599607
}
600608
}
601609

@@ -609,23 +617,20 @@ class ClassLifter(logDebugMsg: Boolean = false) {
609617
val lctx = getFreeVars(lhs)(using emptyCtx, cache, globFuncs, None)
610618
val lret = liftTuple(lhs)(using ctx.addV(lctx.vSet) ++ globFuncs.get(nm).get._2, cache, globFuncs)
611619
val ret = liftTerm(rhs)(using ctx.addV(lctx.vSet) ++ globFuncs.get(nm).get._2, cache, globFuncs)
612-
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Left(Lam(Tup(lret._1.fields ++ tmp), ret._1)))(N, N, N, N, N, true, Nil) //TODO: Use proper arguments
620+
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Left(Lam(Tup(lret._1.fields ++ tmp), ret._1)))(N, N, N, N, N, true, Nil)
613621
case Left(rhs) =>
614-
// will be treated as Lam(Tup(Nil), rhs)
615622
val tmp = globFuncs.get(nm).get._2.vSet.toList.map(toFldsEle)
616623
val ret = liftTerm(rhs)(using ctx ++ globFuncs.get(nm).get._2, cache, globFuncs)
617-
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Left(Lam(Tup(tmp), ret._1)))(N, N, N, N, N, true, Nil) //TODO: Use proper arguments
618-
// val ret = liftTermNew(value)(using ctx.addV(nm) ++ globFuncs.get(nm).get._2, cache, globFuncs)
619-
// NuFunDef(rec, globFuncs.get(nm).get._1, nTpVs, Left(ret._1))
624+
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Left(ret._1))(N, N, N, N, N, true, Nil)
620625
case Right(PolyType(targs, body)) =>
621626
val nBody = liftType(body)(using ctx ++ globFuncs.get(nm).get._2, cache, globFuncs, None)
622627
val nTargs = targs.map({
623628
case L(tn) =>
624629
liftTypeName(tn)(using ctx.addT(nTpVs), cache, globFuncs, None) match
625630
case (tn, ctx) => (L(tn), ctx)
626631
case R(tv) => R(tv) -> emptyCtx}).unzip
627-
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Right(PolyType(nTargs._1, nBody._1)))(N, N, N, N, N, true, Nil) //TODO: Use proper arguments
628-
case _ => throw MonomorphError(s"Unimplemented liftGlobalFunc: ${func}")
632+
NuFunDef(rec, globFuncs.get(nm).get._1, N, nTpVs, Right(PolyType(nTargs._1, nBody._1)))(N, N, N, N, N, true, Nil)
633+
case _ => throw CompilerError(s"Unimplemented liftGlobalFunc: ${func}")
629634
})
630635
}
631636

@@ -639,7 +644,6 @@ class ClassLifter(logDebugMsg: Boolean = false) {
639644
val nameInfoMap: MutMap[String, ClassInfoCache] = MutMap(clsInfos.toSeq: _*)
640645
val nameFuncMap: MutMap[String, LocalContext] = MutMap(funcInfos.toSeq: _*)
641646
log(s"mix cls infos $nameInfoMap, $nameFuncMap")
642-
// val fullMp = cache ++ nameInfoMap
643647
val clsNmsAsTypeNm = clsInfos.keySet.map(x => TypeName(x))
644648
val len = clsInfos.size + nameFuncMap.size
645649
for(_ <- 0 to len){
@@ -734,15 +738,20 @@ class ClassLifter(logDebugMsg: Boolean = false) {
734738
val nCtx = freeVs.addT(nTps)
735739
val nParams =
736740
outer.map(x => List(toFldsEle(Var(genParName(x.liftedNm.name))))).getOrElse(Nil)
737-
++ params.fold(Nil)(t => t.fields)
741+
++ params.fold(Nil)(t => t.fields.map{
742+
case (Some(nm), Fld(flags, term)) => (Some(nm), Fld(flags, liftTerm(term)(using emptyCtx, nCache, globFuncs, nOuter)._1))
743+
case other => other
744+
})
738745
++ freeVs.vSet.map(toFldsEle)
739746
val nPars = pars.map(liftTerm(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
740747
val nFuncs = funcList.map(liftMemberFunc(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
741748
val nTerms = termList.map(liftTerm(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
742749
clsList.foreach(x => liftTypeDef(x)(using nCache, globFuncs, nOuter))
743750
retSeq = retSeq.appended(NuTypeDef(
744-
kind, nName, nTps.map((None, _)), S(Tup(nParams)), None, None, nPars._1,
745-
None, None, TypingUnit(nFuncs._1 ++ nTerms._1))(None, None, Nil))
751+
kind, nName, nTps.map((None, _)), kind match
752+
case Mod => None
753+
case _ => S(Tup(nParams))
754+
, None, None, nPars._1, None, None, TypingUnit(nFuncs._1 ++ nTerms._1))(None, None, Nil))
746755
}
747756

748757
def liftTypingUnit(rawUnit: TypingUnit): TypingUnit = {
@@ -752,7 +761,6 @@ class ClassLifter(logDebugMsg: Boolean = false) {
752761
globalFunctions.clear()
753762
val re = liftEntities(rawUnit.entities)(using emptyCtx, Map(), Map(), None)
754763
log(s"freeVars: ${re._2}")
755-
// println(logOutput.toString())
756764
TypingUnit(retSeq.toList ++ globalFunctions.toList ++ re._1)
757765
}
758766
}

compiler/shared/main/scala/mlscript/compiler/DataType.scala

Lines changed: 0 additions & 36 deletions
This file was deleted.

compiler/shared/main/scala/mlscript/compiler/DataTypeInferer.scala

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)