@@ -8,7 +8,8 @@ import scala.collection.mutable.Map as MutMap
88import scala .collection .mutable .Set as MutSet
99import scala .collection .mutable .ArrayBuffer as ArrayBuffer
1010import mlscript .codegen .CodeGenError
11- import mlscript .compiler .mono .MonomorphError
11+
12+ class CompilerError (error : String ) extends Error (error)
1213
1314class ClassLifter (logDebugMsg : Boolean = false ) {
1415 type ClassName = String
@@ -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 ) = {
@@ -399,7 +400,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
399400 case Sel (receiver, fieldName) =>
400401 val nRec = liftTerm(receiver)
401402 (Sel (nRec._1, fieldName), nRec._2)
402- case Splc (fields) => throw MonomorphError (s " Unimplemented liftTerm: ${target}" )
403+ case Splc (fields) => throw CompilerError (s " Unimplemented liftTerm: ${target}" )
403404 case Subs (arr, idx) =>
404405 val (ltrm, lctx) = liftTerm(arr)
405406 val (rtrm, rctx) = liftTerm(idx)
@@ -412,7 +413,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
412413 val ret = liftTerm(lhs)
413414 val nTs = targs.map(liftType).unzip
414415 (TyApp (ret._1, nTs._1), nTs._2.fold(ret._2)(_ ++ _))
415- case With (trm, fields) => throw MonomorphError (s " Unimplemented liftTerm: ${target}" )
416+ case With (trm, fields) => throw CompilerError (s " Unimplemented liftTerm: ${target}" )
416417 case New (Some ((t : TypeName , prm : Tup )), TypingUnit (Nil )) =>
417418 val ret = liftConstr(t, prm)
418419 (New (Some ((ret._1, ret._2)), TypingUnit (Nil )), ret._3)
@@ -432,7 +433,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
432433 val nSta = New (Some ((nTpNm, Tup (Nil ))), TypingUnit (Nil ))
433434 val ret = liftEntities(List (anoCls, nSta))
434435 (Blk (ret._1), ret._2)
435- case New (head, body) => throw MonomorphError (s " Unimplemented liftTerm: ${target}" )
436+ case New (head, body) => throw CompilerError (s " Unimplemented liftTerm: ${target}" )
436437 case Blk (stmts) =>
437438 val ret = liftEntities(stmts)
438439 (Blk (ret._1), ret._2)
@@ -447,7 +448,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
447448 val (bod2, ctx) = liftTerm(bod)
448449 val (sts2, ctx2) = liftEntities(sts)
449450 (Where (bod2, sts2), ctx2)
450- 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
451452 case patmat : AdtMatchWith => lastWords(s " Cannot liftTermNew ${patmat}" )
452453 }
453454
@@ -484,7 +485,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
484485 ((v, Fld (flags, tmp._1)), tmp._2)
485486 }.unzip
486487 (Rcd (ret._1), ret._2.fold(emptyCtx)(_ ++ _))
487- case _ => throw MonomorphError (s " Unimplemented liftTermAsType: ${target}" )
488+ case _ => throw CompilerError (s " Unimplemented liftTermAsType: ${target}" )
488489 }
489490
490491 private def liftTypeName (target : TypeName )(using ctx : LocalContext , cache : ClassCache , globFuncs : Map [Var , (Var , LocalContext )], outer : Option [ClassInfoCache ]): (TypeName , LocalContext ) = {
@@ -509,7 +510,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
509510 val nlhs = liftType(lb)
510511 val nrhs = liftType(ub)
511512 Bounds (nlhs._1, nrhs._1) -> (nlhs._2 ++ nrhs._2)
512- case Constrained (base : Type , bounds, where) =>
513+ case Constrained (base : Type , bounds, where, tscs ) =>
513514 val (nTargs, nCtx) = bounds.map { case (tv, Bounds (lb, ub)) =>
514515 val nlhs = liftType(lb)
515516 val nrhs = liftType(ub)
@@ -520,10 +521,18 @@ class ClassLifter(logDebugMsg: Boolean = false) {
520521 val nrhs = liftType(ub)
521522 Bounds (nlhs._1, nrhs._1) -> (nlhs._2 ++ nrhs._2)
522523 }.unzip
524+ val (tscs0, nCtx3) = tscs.map { case (tvs, cs) =>
525+ val (ntvs,c0) = tvs.map { case (p,v) =>
526+ val (nv, c) = liftType(v)
527+ (p,nv) -> c
528+ }.unzip
529+ val (ncs,c1) = cs.map(_.map(liftType).unzip).unzip
530+ (ntvs,ncs) -> (c0 ++ c1.flatten)
531+ }.unzip
523532 val (nBase, bCtx) = liftType(base)
524- Constrained (nBase, nTargs, bounds2) ->
525- ((nCtx ++ nCtx2).fold(emptyCtx)(_ ++ _) ++ bCtx)
526- case Constrained (_, _, _) => die
533+ Constrained (nBase, nTargs, bounds2, tscs0 ) ->
534+ ((nCtx ++ nCtx2 ++ nCtx3.flatten ).fold(emptyCtx)(_ ++ _) ++ bCtx)
535+ case Constrained (_, _, _, _ ) => die
527536 case Function (lhs, rhs) =>
528537 val nlhs = liftType(lhs)
529538 val nrhs = liftType(rhs)
@@ -578,7 +587,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
578587 val (body2, ctx) = liftType(body)
579588 PolyType (targs, body2) -> ctx
580589 case Top | Bot | _ : Literal | _ : TypeTag | _ : TypeVar => target.asInstanceOf [Type ] -> emptyCtx
581- case _ : Selection => throw MonomorphError (s " Unimplemented liftType: ${target}" ) // TODO
590+ case _ : Selection => throw CompilerError (s " Unimplemented liftType: ${target}" ) // TODO
582591 }
583592
584593
@@ -602,7 +611,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
602611 }.unzip
603612 (func.copy(rhs = Right (PolyType (nTargs._1, nBody._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations),
604613 nTargs._2.fold(nBody._2)(_ ++ _))
605- case _ => throw MonomorphError (s " Unimplemented liftMemberFunc: ${func}" ) // TODO
614+ case _ => throw CompilerError (s " Unimplemented liftMemberFunc: ${func}" ) // TODO
606615 }
607616 }
608617
@@ -629,7 +638,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
629638 case (tn, ctx) => (L (tn), ctx)
630639 case R (tv) => R (tv) -> emptyCtx}).unzip
631640 NuFunDef (rec, globFuncs.get(nm).get._1, N , nTpVs, Right (PolyType (nTargs._1, nBody._1)))(N , N , N , N , N , true , Nil )
632- case _ => throw MonomorphError (s " Unimplemented liftGlobalFunc: ${func}" )
641+ case _ => throw CompilerError (s " Unimplemented liftGlobalFunc: ${func}" )
633642 })
634643 }
635644
0 commit comments