@@ -9,7 +9,7 @@ import utils.TraceLogger
9
9
import syntax .Literal
10
10
import Keyword .{as , and , `do` , `else` , is , let , `then` }
11
11
import collection .mutable .{HashMap , SortedSet }
12
- import Elaborator .{ctx , Ctxl }
12
+ import Elaborator .{ctx , Ctxl , UnderCtx }
13
13
import scala .annotation .targetName
14
14
import hkmc2 .semantics .ClassDef .Parameterized
15
15
@@ -25,11 +25,11 @@ object Desugarer:
25
25
val tupleLast : HashMap [Int , BlockLocalSymbol ] = HashMap .empty
26
26
end Desugarer
27
27
28
- class Desugarer (val elaborator : Elaborator )
28
+ class Desugarer (val elaborator : Elaborator )( using UnderCtx )
29
29
(using raise : Raise , state : Elaborator .State , c : Elaborator .Ctx ) extends DesugaringBase :
30
30
import Desugarer .*
31
31
import Elaborator .Ctx
32
- import elaborator .term , elaborator .tl .*
32
+ import elaborator .term , elaborator .subterm , elaborator . tl .*
33
33
34
34
given Ordering [Loc ] = Ordering .by: loc =>
35
35
(loc.spanStart, loc.spanEnd)
@@ -125,7 +125,7 @@ class Desugarer(val elaborator: Elaborator)
125
125
raise(ErrorReport (msg " only one branch is supported in shorthands " -> tree.toLoc :: Nil ))
126
126
termSplitShorthands(branch, finish)(fallback)(ctx)
127
127
case coda is rhs => fallback => ctx =>
128
- nominate(ctx, finish(term (coda)(using ctx))):
128
+ nominate(ctx, finish(subterm (coda)(using ctx))):
129
129
patternSplitShorthands(rhs, _)(fallback)
130
130
case matches => fallback =>
131
131
// There are N > 0 conjunct matches. We use `::[T]` instead of `List[T]`.
@@ -245,6 +245,25 @@ class Desugarer(val elaborator: Elaborator)
245
245
):
246
246
nominate(ctx, finish(term(headCoda)(using ctx))):
247
247
expandMatch(_, headPattern, tailSplit)(fallback)
248
+ // ! WARNING: this is just slop generated by Copilot. TODO: rewrite
249
+ case tree @ OpApp (lhs, opIdent @ Ident (opName), rhss) => fallback => ctx => trace(
250
+ pre = s " termSplit: after op <<< $opName" ,
251
+ post = (res : Split ) => s " termSplit: after op >>> $res"
252
+ ):
253
+ // Resolve the operator.
254
+ val opRef = term(opIdent)
255
+ // Elaborate and finish the LHS. Nominate the LHS if necessary.
256
+ nominate(ctx, finish(term(lhs)(using ctx))): lhsSymbol =>
257
+ // Compose a function that takes the RHS and finishes the application.
258
+ val finishInner = (rhsTerm : Term ) =>
259
+ val first = Fld (FldFlags .empty, lhsSymbol.ref(/* FIXME ident? */ ), N )
260
+ val second = Fld (FldFlags .empty, rhsTerm, N )
261
+ val arguments = Term .Tup (first :: second :: Nil )(Tree .DummyTup )
262
+ val joint = FlowSymbol (" ‹applied-result›" )
263
+ Term .App (opRef, arguments)(Tree .DummyApp , joint)
264
+ rhss match
265
+ case rhs :: Nil => termSplit(rhs, finishInner)(fallback)
266
+ case _ => ???
248
267
case tree @ App (opIdent @ Ident (opName), rawTup @ Tup (lhs :: rhs :: Nil )) => fallback => ctx => trace(
249
268
pre = s " termSplit: after op <<< $opName" ,
250
269
post = (res : Split ) => s " termSplit: after op >>> $res"
@@ -261,6 +280,20 @@ class Desugarer(val elaborator: Elaborator)
261
280
val joint = FlowSymbol (" ‹applied-result›" )
262
281
Term .App (opRef, arguments)(tree, joint)
263
282
termSplit(rhs, finishInner)(fallback)
283
+ // ! WARNING: this is just slop generated by Copilot. TODO: rewrite
284
+ case tree @ OpSplit (lhs, rhss) => fallback => ctx =>
285
+ nominate(ctx, finish(term(lhs)(using ctx))): vs =>
286
+ val mkInnerFinish = (op : Term ) => (rhsTerm : Term ) =>
287
+ val first = Fld (FldFlags .empty, vs.ref(/* FIXME ident? */ ), N )
288
+ val second = Fld (FldFlags .empty, rhsTerm, N )
289
+ val rawTup = Tup (lhs :: Nil ): Tup // <-- loc might be wrong
290
+ val arguments = Term .Tup (first :: second :: Nil )(rawTup)
291
+ val joint = FlowSymbol (" ‹applied-result›" )
292
+ Term .App (op, arguments)(Tree .DummyApp , joint)
293
+ rhss.foldRight(Function .const(fallback): Sequel ): (tt, elabFallback) =>
294
+ tt match
295
+ case _ =>
296
+ ???
264
297
case tree @ App (lhs, blk @ OpBlock (opRhsApps)) => fallback => ctx =>
265
298
nominate(ctx, finish(term(lhs)(using ctx))): vs =>
266
299
val mkInnerFinish = (op : Term ) => (rhsTerm : Term ) =>
@@ -447,7 +480,7 @@ class Desugarer(val elaborator: Elaborator)
447
480
def expandMatch (scrutSymbol : BlockLocalSymbol , pattern : Tree , sequel : Sequel ): Split => Sequel =
448
481
def ref = scrutSymbol.ref(/* FIXME ident? */ )
449
482
def dealWithCtorCase (ctor : Ctor , compile : Bool )(fallback : Split ): Sequel = ctx =>
450
- val clsTrm = elaborator.cls(ctor, inAppPrefix = false )
483
+ val clsTrm = elaborator.cls(term( ctor) , inAppPrefix = false )
451
484
clsTrm.symbol.flatMap(_.asClsLike) match
452
485
case S (cls : ClassSymbol ) =>
453
486
if compile then warn(msg " Cannot compile the class ` ${cls.name}` " -> ctor.toLoc)
@@ -476,7 +509,7 @@ class Desugarer(val elaborator: Elaborator)
476
509
pre = s " expandMatch <<< ${ctor}( ${args.iterator.map(_.showDbg).mkString(" , " )}) " ,
477
510
post = (r : Split ) => s " expandMatch >>> ${r.showDbg}"
478
511
):
479
- val clsTrm = elaborator.cls(ctor, inAppPrefix = false )
512
+ val clsTrm = elaborator.cls(term( ctor) , inAppPrefix = false )
480
513
clsTrm.symbol.flatMap(_.asClsLike) match
481
514
case S (cls : ClassSymbol ) =>
482
515
val paramSymbols = cls.defn match
@@ -597,6 +630,9 @@ class Desugarer(val elaborator: Elaborator)
597
630
dealWithAppCtorCase(app, ctor, args, false )
598
631
case app @ App (ctor : Ctor , Tup (args)) =>
599
632
dealWithAppCtorCase(app, ctor, args, false )
633
+ case app @ OpApp (lhs, ctor : Ctor , rhss) =>
634
+ // TODO improve (eventually remove DummyApp)
635
+ dealWithAppCtorCase(Tree .DummyApp , ctor, lhs :: rhss, false )
600
636
// A single literal pattern
601
637
case literal : Literal => fallback => ctx => trace(
602
638
pre = s " expandMatch: literal <<< $literal" ,
0 commit comments