Skip to content

Commit 0f55f34

Browse files
committed
Add comments for simpledef
1 parent 3e40aeb commit 0f55f34

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

compiler/shared/main/scala/mlscript/compiler/simpledef/Simpledef.scala

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type TermId = Uid[Term]
1212
type TypeVarId = Uid[TypeVar]
1313
type Cnstr = ProdStrat -> ConsStrat
1414

15+
/** Performs defunctionalization on selections on objects using simple-sub as for control-flow analysis.
16+
* First we traverse the program and process all terms, constraining them to Producers and Consumers.
17+
* During the constraining, we keep track of the input points of selection terms.
18+
* Lastly, we rewrite selection terms by generating pattern matches on their possible inputs.
19+
*/
20+
1521

1622
enum ProdStrat(using val euid: TermId) {
1723
case NoProd()(using TermId)
@@ -61,8 +67,6 @@ class SimpleDef(debug: Debug) {
6167
})
6268
}
6369

64-
var log: Str => Unit = (s) => ()
65-
var constraints: Ls[Cnstr] = Nil
6670
val termMap = new IdentityHashMap[Term, TermId]().asScala
6771
val varsName = mutable.Map.empty[TypeVarId, Str]
6872
val vuid = Uid.TypeVar.State()
@@ -80,14 +84,17 @@ class SimpleDef(debug: Debug) {
8084
freshVar(n.name)
8185

8286
def apply(p: TypingUnit)(using ctx: Context = Context(Map(), Map())): (Ls[Var -> ProdStrat], ProdStrat) =
87+
// Top-level def prototypes
8388
val vars: Map[Var, ProdVar] = p.rawEntities.collect {
8489
case fun: NuFunDef =>
8590
fun.nme -> freshVar(fun.name)(using noExprId)._1
8691
}.toMap
92+
// Top-level constructor prototypes
8793
val constructorPrototypes: Map[Var, Cnstr] = p.rawEntities.collect {
8894
case ty: NuTypeDef =>
8995
ty.nameVar -> freshVar(ty.nameVar)(using noExprId)
9096
}.toMap
97+
// Prototypes of constructor outputs, used for inheritance
9198
val objectPrototypes: Map[Var, Cnstr] = p.rawEntities.collect {
9299
case ty: NuTypeDef =>
93100
ty.nameVar -> freshVar(ty.nameVar)(using noExprId)
@@ -166,8 +173,9 @@ class SimpleDef(debug: Debug) {
166173
}
167174
}
168175
(vars.toList, tys.lastOption.getOrElse(ProdObj(Some(Var("prim$Unit")), Nil)(using noExprId)))
169-
176+
170177
val termToProdType = mutable.Map.empty[TermId, ProdStrat]
178+
// Selection terms -> Object types that they Consume
171179
val selTermToType = mutable.Map.empty[TermId, ConsObj]
172180

173181
def builtinOps: Map[Var, ProdFun] = {
@@ -264,11 +272,8 @@ class SimpleDef(debug: Debug) {
264272
apply(TypingUnit(stmts))._2
265273
case Bra(false, term) =>
266274
process(term)
267-
case CaseOf(trm, cases) =>
268-
val scrutRes = process(trm)
269-
val sv = freshVar(s"${t.uid}_caseres")(using t.uid)
275+
case CaseOf(trm, cases) => //TODO
270276
???
271-
// TODO
272277
case Eqn(lhs, rhs) =>
273278
process(lhs)
274279
process(rhs)
@@ -277,6 +282,7 @@ class SimpleDef(debug: Debug) {
277282
debug.outdent()
278283
registerTermToType(t, res)
279284

285+
//TODO
280286
def processCases(scrut: ProdVar, cs: CaseBranches)(using ctx: Context, resCons: ConsVar): Unit =
281287
cs match
282288
case Wildcard(body) =>
@@ -351,10 +357,12 @@ class SimpleDef(debug: Debug) {
351357
case other => lastWords(s"Could not constrain ${other}")
352358
}
353359

360+
// Selection terms -> Producers that they consume
354361
lazy val selToResTypes: Map[TermId, Set[ProdStrat]] = selTermToType.map((termId, cons) =>
355362
(termId, cons.selectionSource)
356363
).toMap
357364

365+
// Rewrite terms, replacing selections with pattern matches if they only select on objects
358366
def rewriteTerm(t: Term): Term =
359367
def objSetToMatchBranches(receiver: Var, fieldName: Var, objSet: List[ProdObj], acc: CaseBranches = NoCases)(using funcApp: Option[Term] = None): CaseBranches =
360368
objSet match

0 commit comments

Comments
 (0)