@@ -8,7 +8,8 @@ import scala.collection.mutable.Map as MutMap
8
8
import scala .collection .mutable .Set as MutSet
9
9
import scala .collection .mutable .ArrayBuffer as ArrayBuffer
10
10
import mlscript .codegen .CodeGenError
11
- import mlscript .compiler .mono .MonomorphError
11
+
12
+ class CompilerError (error : String ) extends Error (error)
12
13
13
14
class ClassLifter (logDebugMsg : Boolean = false ) {
14
15
type ClassName = String
@@ -65,7 +66,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
65
66
val primiTypes = new mlscript.Typer (false , false , false , true ).primitiveTypes
66
67
67
68
private def log (str : String ): Unit = {
68
- logOutput.append(str)
69
+ logOutput.append(str+ " \n " )
69
70
if (logDebugMsg){
70
71
println(str)
71
72
}
@@ -189,7 +190,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
189
190
case TyApp (trm, tpLst) =>
190
191
getFreeVars(trm).addT(tpLst.flatMap(_.collectTypeNames.map(TypeName (_))))
191
192
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 )
193
194
val newVs = prmVs.vSet ++ getFields(body.entities) + Var (nm.name)
194
195
val nCtx = ctx.addV(newVs).addT(nm).addT(tps.map(_._2))
195
196
val parVs = pars.map(getFreeVars(_)(using nCtx)).fold(emptyCtx)(_ ++ _)
@@ -227,14 +228,14 @@ class ClassLifter(logDebugMsg: Boolean = false) {
227
228
}
228
229
229
230
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) =>
231
232
val nTrm = liftTerm(body)(using ctx.addV(v))
232
233
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) =>
235
236
val nTrm = liftTerm(body)
236
237
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)
238
239
case Wildcard (body) =>
239
240
val nTrm = liftTerm(body)
240
241
(Wildcard (nTrm._1), nTrm._2)
@@ -249,7 +250,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
249
250
val nE = liftTerm(expr)
250
251
val nR = liftTerm(rhs)
251
252
(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}" )
253
254
}
254
255
255
256
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) {
299
300
}
300
301
301
302
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 ))))
303
304
304
305
private def liftTerm (target : Term )(using ctx : LocalContext , cache : ClassCache , globFuncs : Map [Var , (Var , LocalContext )], outer : Option [ClassInfoCache ]): (Term , LocalContext ) =
305
306
log(s " liftTermNew $target in $ctx, $cache, $globFuncs, $outer" )
306
307
target match {
307
308
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
+ }
309
312
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)
312
315
}
316
+ else if (ctx.contains(v) || v.name.equals(" this" ) || primiTypes.contains(v.name)) (v, emptyCtx)
313
317
else {
314
318
buildPathToVar(v) match {
315
319
case Some (value) => (value, emptyCtx)
@@ -321,8 +325,8 @@ class ClassLifter(logDebugMsg: Boolean = false) {
321
325
val nTpNm = TypeName (genAnoName(" Lambda" + prmCnt))
322
326
val anoCls = NuTypeDef (
323
327
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 ))
326
330
val ret = liftEntities(List (anoCls, nSta))
327
331
(Blk (ret._1), ret._2)
328
332
case t : Tup =>
@@ -341,7 +345,12 @@ class ClassLifter(logDebugMsg: Boolean = false) {
341
345
case NuNew (cls) =>
342
346
liftTerm(App (NuNew (cls), Tup (Nil )))
343
347
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
+ }
345
354
case Rft (NuNew (cls), tu) =>
346
355
liftTerm(Rft (App (NuNew (cls), Tup (Nil )), TypingUnit (Nil )))
347
356
case Rft (App (NuNew (cls), args), tu) =>
@@ -385,13 +394,13 @@ class ClassLifter(logDebugMsg: Boolean = false) {
385
394
val nTrm = liftTerm(trm)
386
395
(If (ret._1, Some (nTrm._1)), ret._2 ++ nTrm._2)
387
396
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) )
390
399
(Let (isRec, name, nRhs._1, nBody._1), nRhs._2 ++ nBody._2)
391
400
case Sel (receiver, fieldName) =>
392
401
val nRec = liftTerm(receiver)
393
402
(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}" )
395
404
case Subs (arr, idx) =>
396
405
val (ltrm, lctx) = liftTerm(arr)
397
406
val (rtrm, rctx) = liftTerm(idx)
@@ -404,7 +413,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
404
413
val ret = liftTerm(lhs)
405
414
val nTs = targs.map(liftType).unzip
406
415
(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}" )
408
417
case New (Some ((t : TypeName , prm : Tup )), TypingUnit (Nil )) =>
409
418
val ret = liftConstr(t, prm)
410
419
(New (Some ((ret._1, ret._2)), TypingUnit (Nil )), ret._3)
@@ -424,7 +433,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
424
433
val nSta = New (Some ((nTpNm, Tup (Nil ))), TypingUnit (Nil ))
425
434
val ret = liftEntities(List (anoCls, nSta))
426
435
(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}" )
428
437
case Blk (stmts) =>
429
438
val ret = liftEntities(stmts)
430
439
(Blk (ret._1), ret._2)
@@ -439,7 +448,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
439
448
val (bod2, ctx) = liftTerm(bod)
440
449
val (sts2, ctx2) = liftEntities(sts)
441
450
(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
443
452
case patmat : AdtMatchWith => lastWords(s " Cannot liftTermNew ${patmat}" )
444
453
}
445
454
@@ -476,7 +485,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
476
485
((v, Fld (flags, tmp._1)), tmp._2)
477
486
}.unzip
478
487
(Rcd (ret._1), ret._2.fold(emptyCtx)(_ ++ _))
479
- case _ => throw MonomorphError (s " Unimplemented liftTermAsType: ${target}" )
488
+ case _ => throw CompilerError (s " Unimplemented liftTermAsType: ${target}" )
480
489
}
481
490
482
491
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) {
570
579
val (body2, ctx) = liftType(body)
571
580
PolyType (targs, body2) -> ctx
572
581
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
574
583
}
575
584
576
585
@@ -582,11 +591,10 @@ class ClassLifter(logDebugMsg: Boolean = false) {
582
591
val lctx = getFreeVars(lhs)(using emptyCtx, cache, globFuncs, None )
583
592
val lret = liftTuple(lhs)(using ctx.addV(lctx.vSet))
584
593
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)
586
595
case Left (value) =>
587
- // will be treated as Lam(Tup(Nil), rhs)
588
596
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)
590
598
case Right (PolyType (targs, body)) =>
591
599
val nBody = liftType(body)(using ctx.addT(tpVs))
592
600
val nTargs = targs.map {
@@ -595,7 +603,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
595
603
}.unzip
596
604
(func.copy(rhs = Right (PolyType (nTargs._1, nBody._1)))(func.declareLoc, func.virtualLoc, func.mutLoc, func.signature, func.outer, func.genField, func.annotations),
597
605
nTargs._2.fold(nBody._2)(_ ++ _))
598
- case _ => throw MonomorphError (s " Unimplemented liftMemberFunc: ${func}" ) // TODO
606
+ case _ => throw CompilerError (s " Unimplemented liftMemberFunc: ${func}" ) // TODO
599
607
}
600
608
}
601
609
@@ -609,23 +617,20 @@ class ClassLifter(logDebugMsg: Boolean = false) {
609
617
val lctx = getFreeVars(lhs)(using emptyCtx, cache, globFuncs, None )
610
618
val lret = liftTuple(lhs)(using ctx.addV(lctx.vSet) ++ globFuncs.get(nm).get._2, cache, globFuncs)
611
619
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 )
613
621
case Left (rhs) =>
614
- // will be treated as Lam(Tup(Nil), rhs)
615
622
val tmp = globFuncs.get(nm).get._2.vSet.toList.map(toFldsEle)
616
623
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 )
620
625
case Right (PolyType (targs, body)) =>
621
626
val nBody = liftType(body)(using ctx ++ globFuncs.get(nm).get._2, cache, globFuncs, None )
622
627
val nTargs = targs.map({
623
628
case L (tn) =>
624
629
liftTypeName(tn)(using ctx.addT(nTpVs), cache, globFuncs, None ) match
625
630
case (tn, ctx) => (L (tn), ctx)
626
631
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}" )
629
634
})
630
635
}
631
636
@@ -639,7 +644,6 @@ class ClassLifter(logDebugMsg: Boolean = false) {
639
644
val nameInfoMap : MutMap [String , ClassInfoCache ] = MutMap (clsInfos.toSeq: _* )
640
645
val nameFuncMap : MutMap [String , LocalContext ] = MutMap (funcInfos.toSeq: _* )
641
646
log(s " mix cls infos $nameInfoMap, $nameFuncMap" )
642
- // val fullMp = cache ++ nameInfoMap
643
647
val clsNmsAsTypeNm = clsInfos.keySet.map(x => TypeName (x))
644
648
val len = clsInfos.size + nameFuncMap.size
645
649
for (_ <- 0 to len){
@@ -734,15 +738,20 @@ class ClassLifter(logDebugMsg: Boolean = false) {
734
738
val nCtx = freeVs.addT(nTps)
735
739
val nParams =
736
740
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
+ })
738
745
++ freeVs.vSet.map(toFldsEle)
739
746
val nPars = pars.map(liftTerm(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
740
747
val nFuncs = funcList.map(liftMemberFunc(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
741
748
val nTerms = termList.map(liftTerm(_)(using emptyCtx, nCache, globFuncs, nOuter)).unzip
742
749
clsList.foreach(x => liftTypeDef(x)(using nCache, globFuncs, nOuter))
743
750
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 ))
746
755
}
747
756
748
757
def liftTypingUnit (rawUnit : TypingUnit ): TypingUnit = {
@@ -752,7 +761,6 @@ class ClassLifter(logDebugMsg: Boolean = false) {
752
761
globalFunctions.clear()
753
762
val re = liftEntities(rawUnit.entities)(using emptyCtx, Map (), Map (), None )
754
763
log(s " freeVars: ${re._2}" )
755
- // println(logOutput.toString())
756
764
TypingUnit (retSeq.toList ++ globalFunctions.toList ++ re._1)
757
765
}
758
766
}
0 commit comments