Skip to content

Commit c71990d

Browse files
committed
WIP (dirty commit; will be squashed)
1 parent d7f53d2 commit c71990d

Some content is hidden

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

82 files changed

+5854
-1507
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ object Printer:
1919
// ts.trmTree
2020
case ts: semantics.InnerSymbol => ts.nme
2121
case ts: semantics.BuiltinSymbol => ts.nme
22-
case _ => summon[Scope].lookup_!(l)
22+
case _ => summon[Scope].lookup(l) match
23+
case S(str) => str
24+
case N => s"‹not in scope: ${l}"
2325

2426
def mkDocument(blk: Block)(using Raise, Scope): Document = blk match
2527
case Match(scrut, arms, dflt, rest) =>
@@ -95,6 +97,10 @@ object Printer:
9597
case Value.Arr(elems) =>
9698
val docElems = elems.map(x => mkDocument(x)).mkString(", ")
9799
doc"[${docElems}]"
100+
case Value.Rcd(args) => // case Rcd(elems: Ls[RcdArg])
101+
doc"{ ${
102+
args.map(x => x.idx.fold(doc"...")(p => mkDocument(p) :: ": ") :: mkDocument(x.value)).mkString(", ")
103+
} }"
98104

99105
def mkDocument(path: Path)(using Raise, Scope): Document = path match
100106
case Select(qual, name) =>

hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala

Lines changed: 123 additions & 98 deletions
Large diffs are not rendered by default.

hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/DeBrujinSplit.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object DeBrujinSplit:
2121
/** Resolve the constructor in the elaborator context. */
2222
def resolve(ctor: Ident | Sel, params: Ls[Tree]): Opt[F] =
2323
val term = scoped("ucs:mute"):
24-
elaborator.cls(ctor, inAppPrefix = false)
24+
elaborator.cls(elaborator.term(ctor), inAppPrefix = false)
2525
term.symbol.flatMap(_.asClsLike).map:
2626
case symbol: (ClassSymbol | ModuleSymbol) =>
2727
val pattern = ClassLike(ConstructorLike.Symbol(symbol))

hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Desugarer.scala

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import utils.TraceLogger
99
import syntax.Literal
1010
import Keyword.{as, and, `do`, `else`, is, let, `then`}
1111
import collection.mutable.{HashMap, SortedSet}
12-
import Elaborator.{ctx, Ctxl}
12+
import Elaborator.{ctx, Ctxl, UnderCtx}
1313
import scala.annotation.targetName
1414
import hkmc2.semantics.ClassDef.Parameterized
1515

@@ -25,11 +25,11 @@ object Desugarer:
2525
val tupleLast: HashMap[Int, BlockLocalSymbol] = HashMap.empty
2626
end Desugarer
2727

28-
class Desugarer(val elaborator: Elaborator)
28+
class Desugarer(val elaborator: Elaborator)(using UnderCtx)
2929
(using raise: Raise, state: Elaborator.State, c: Elaborator.Ctx) extends DesugaringBase:
3030
import Desugarer.*
3131
import Elaborator.Ctx
32-
import elaborator.term, elaborator.tl.*
32+
import elaborator.term, elaborator.subterm, elaborator.tl.*
3333

3434
given Ordering[Loc] = Ordering.by: loc =>
3535
(loc.spanStart, loc.spanEnd)
@@ -125,7 +125,7 @@ class Desugarer(val elaborator: Elaborator)
125125
raise(ErrorReport(msg"only one branch is supported in shorthands" -> tree.toLoc :: Nil))
126126
termSplitShorthands(branch, finish)(fallback)(ctx)
127127
case coda is rhs => fallback => ctx =>
128-
nominate(ctx, finish(term(coda)(using ctx))):
128+
nominate(ctx, finish(subterm(coda)(using ctx))):
129129
patternSplitShorthands(rhs, _)(fallback)
130130
case matches => fallback =>
131131
// There are N > 0 conjunct matches. We use `::[T]` instead of `List[T]`.
@@ -245,6 +245,25 @@ class Desugarer(val elaborator: Elaborator)
245245
):
246246
nominate(ctx, finish(term(headCoda)(using ctx))):
247247
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 _ => ???
248267
case tree @ App(opIdent @ Ident(opName), rawTup @ Tup(lhs :: rhs :: Nil)) => fallback => ctx => trace(
249268
pre = s"termSplit: after op <<< $opName",
250269
post = (res: Split) => s"termSplit: after op >>> $res"
@@ -261,6 +280,20 @@ class Desugarer(val elaborator: Elaborator)
261280
val joint = FlowSymbol("‹applied-result›")
262281
Term.App(opRef, arguments)(tree, joint)
263282
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+
???
264297
case tree @ App(lhs, blk @ OpBlock(opRhsApps)) => fallback => ctx =>
265298
nominate(ctx, finish(term(lhs)(using ctx))): vs =>
266299
val mkInnerFinish = (op: Term) => (rhsTerm: Term) =>
@@ -447,7 +480,7 @@ class Desugarer(val elaborator: Elaborator)
447480
def expandMatch(scrutSymbol: BlockLocalSymbol, pattern: Tree, sequel: Sequel): Split => Sequel =
448481
def ref = scrutSymbol.ref(/* FIXME ident? */)
449482
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)
451484
clsTrm.symbol.flatMap(_.asClsLike) match
452485
case S(cls: ClassSymbol) =>
453486
if compile then warn(msg"Cannot compile the class `${cls.name}`" -> ctor.toLoc)
@@ -476,7 +509,7 @@ class Desugarer(val elaborator: Elaborator)
476509
pre = s"expandMatch <<< ${ctor}(${args.iterator.map(_.showDbg).mkString(", ")})",
477510
post = (r: Split) => s"expandMatch >>> ${r.showDbg}"
478511
):
479-
val clsTrm = elaborator.cls(ctor, inAppPrefix = false)
512+
val clsTrm = elaborator.cls(term(ctor), inAppPrefix = false)
480513
clsTrm.symbol.flatMap(_.asClsLike) match
481514
case S(cls: ClassSymbol) =>
482515
val paramSymbols = cls.defn match
@@ -597,6 +630,9 @@ class Desugarer(val elaborator: Elaborator)
597630
dealWithAppCtorCase(app, ctor, args, false)
598631
case app @ App(ctor: Ctor, Tup(args)) =>
599632
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)
600636
// A single literal pattern
601637
case literal: Literal => fallback => ctx => trace(
602638
pre = s"expandMatch: literal <<< $literal",

hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Translator.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Translator(val elaborator: Elaborator)
7171
case Under() => inner(Map.empty)
7272
case ctor @ (_: Ident | _: Sel) =>
7373
lazy val resolved =
74-
val clsTrm = elaborator.cls(ctor, inAppPrefix = false)
74+
val clsTrm = elaborator.cls(elaborator.term(ctor), inAppPrefix = false)
7575
clsTrm.symbol.flatMap(_.asClsLike) match
7676
case S(cls: (ClassSymbol | ModuleSymbol)) =>
7777
Branch(scrut(), Pattern.ClassLike(cls, clsTrm, N, false)(ctor), inner(Map.empty)) ~: Split.End
@@ -87,7 +87,7 @@ class Translator(val elaborator: Elaborator)
8787
case ctor: Sel => resolved
8888
case App(ctor @ (_: Ident | _: Sel), Tup(params)) =>
8989
lazy val resolved =
90-
val clsTrm = elaborator.cls(ctor, inAppPrefix = false)
90+
val clsTrm = elaborator.cls(elaborator.term(ctor), inAppPrefix = false)
9191
clsTrm.symbol.flatMap(_.asClsLike) match
9292
case S(cls: (ClassSymbol | ModuleSymbol)) =>
9393
// TODO: handle parameters
@@ -138,7 +138,7 @@ class Translator(val elaborator: Elaborator)
138138
inner(captures2 ++ captures1, postfixScrut2)))
139139
case Under() => inner(Map.empty, scrut) // TODO: check if this is correct
140140
case ctor @ (_: Ident | _: Sel) =>
141-
val clsTrm = elaborator.cls(ctor, inAppPrefix = false)
141+
val clsTrm = elaborator.cls(elaborator.term(ctor), inAppPrefix = false)
142142
clsTrm.symbol.flatMap(_.asClsLike) match
143143
case S(cls: (ClassSymbol | ModuleSymbol)) =>
144144
val kind = cls match { case _: ClassSymbol => "class" case _ => "module" }

0 commit comments

Comments
 (0)