Skip to content

Commit 288885d

Browse files
committed
Add support for parameterized record key desugaring
1 parent bbd2a61 commit 288885d

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -989,25 +989,30 @@ extends Importer with ucs.SplitElaborator:
989989
go(sts, Nil, RcdSpread(term(body)) :: acc)
990990
case InfixApp(lhs, Keywrd(Keyword.`:`), rhs) :: sts =>
991991
var newCtx = ctx
992-
val rhs_t = rhs match
993-
case _: Under => subterm(rhs)
994-
case _ => term(rhs)
995-
val newAcc = lhs match
992+
val (rlhs, rhs_t) = rhs match
993+
case _: Under => (lhs, subterm(rhs))
994+
case _ =>
995+
lhs match
996+
case Apps(base, tups) =>
997+
val rrhs = tups.foldRight(rhs):
998+
InfixApp(_, Keywrd(Keyword.`=>`), _)
999+
(base, term(rrhs))
1000+
val newAcc = rlhs match
9961001
case id: Ident =>
9971002
val sym = new VarSymbol(id)
9981003
newCtx += id.name -> sym
999-
RcdField(Term.Lit(StrLit(id.name)).withLocOf(id), sym.ref(id)) ::
1000-
DefineVar(sym, rhs_t) ::
1001-
LetDecl(sym, annotations) ::
1002-
acc
1004+
RcdField(Term.Lit(StrLit(id.name)).withLocOf(id), sym.ref(id))
1005+
:: DefineVar(sym, rhs_t)
1006+
:: LetDecl(sym, annotations)
1007+
:: acc
10031008
case lit: Literal =>
10041009
reportUnusedAnnotations
10051010
RcdField(Term.Lit(lit).withLocOf(lit), rhs_t) :: acc
10061011
case Bra(Round, inner) =>
10071012
reportUnusedAnnotations
10081013
RcdField(term(inner), rhs_t) :: acc
10091014
case _ =>
1010-
raise(ErrorReport(msg"Unexpected record key shape." -> lhs.toLoc :: Nil))
1015+
raise(ErrorReport(msg"Unexpected record key shape." -> rlhs.toLoc :: Nil))
10111016
RcdField(Term.Error, rhs_t) :: acc
10121017
newCtx.givenIn:
10131018
go(sts, Nil, newAcc)

hkmc2/shared/src/test/mlscript/basics/Records.mls

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ id of { x: 1, y: 2 }
121121
//│ = {x: 1, y: 2}
122122

123123

124+
let m0 = {get(k): k + 1}
125+
//│ m0 = {get: fun get}
126+
127+
m0.get(41)
128+
//│ = 42
129+
130+
131+
let m0 = {("get")(k): k + 1}
132+
//│ m0 = {get: fun}
133+
134+
m0.get(41)
135+
//│ = 42
136+
137+
138+
:e
139+
{get(k): _}
140+
//│ ╔══[ERROR] Unexpected record key shape.
141+
//│ ║ l.139: {get(k): _}
142+
//│ ╙── ^^^^^^
143+
//│ = fun
144+
145+
124146

125147
:silent
126148
import "../../mlscript-compile/Iter.mls"
@@ -171,14 +193,14 @@ display(x: 88, y: 99, i: 0)
171193
:todo
172194
t("group")
173195
//│ ╔══[ERROR] Expected 2 arguments, got 1
174-
//│ ║ l.172: t("group")
196+
//│ ║ l.194: t("group")
175197
//│ ╙── ^^^^^^^^^
176198
//│ ═══[RUNTIME ERROR] Error: Function 't' expected 2 arguments but got 1
177199

178200
:todo
179201
t("group")
180202
//│ ╔══[ERROR] Expected 2 arguments, got 1
181-
//│ ║ l.179: t("group")
203+
//│ ║ l.201: t("group")
182204
//│ ╙── ^^^^^^^^^
183205
//│ ═══[RUNTIME ERROR] Error: Function 't' expected 2 arguments but got 1
184206

0 commit comments

Comments
 (0)