Skip to content

Commit dc5b8de

Browse files
committed
WIP start cleanup
1 parent 3278445 commit dc5b8de

File tree

9 files changed

+20
-78
lines changed

9 files changed

+20
-78
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ object Elaborator:
5656
case NotInFunction
5757
case Forbidden
5858

59+
/** Context used to keep track of underscores representing lambda shorthands, eg `_ + 1`. */
5960
class UnderCtx(val unders: Opt[mutable.ArrayBuffer[VarSymbol]])
6061

6162
case class Ctx(outer: OuterCtx, parent: Opt[Ctx], env: Map[Str, Ctx.Elem],

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ class Desugarer(val elaborator: Elaborator)(using UnderCtx)
246246
):
247247
nominate(ctx, finish(term(headCoda)(using ctx))):
248248
expandMatch(_, headPattern, tailSplit)(fallback)
249-
// ! WARNING: this is just slop generated by Copilot. TODO: rewrite
250249
case tree @ OpApp(lhs, opIdent @ Ident(opName), rhss) => fallback => ctx => trace(
251250
pre = s"termSplit: after op <<< $opName",
252251
post = (res: Split) => s"termSplit: after op >>> $res"
@@ -320,45 +319,6 @@ class Desugarer(val elaborator: Elaborator)(using UnderCtx)
320319
case rhs => ctx =>
321320
raise(ErrorReport(msg"Unrecognized operator branch." -> rhs.toLoc :: Nil))
322321
elabFallback(ctx)
323-
// TODO: BEGIN rm
324-
case tree @ App(lhs, blk @ OpBlock(opRhsApps)) => fallback => ctx =>
325-
nominate(ctx, finish(term(lhs)(using ctx))): vs =>
326-
val mkInnerFinish = (op: Term) => (rhsTerm: Term) =>
327-
val first = Fld(FldFlags.empty, vs.ref(/* FIXME ident? */), N)
328-
val second = Fld(FldFlags.empty, rhsTerm, N)
329-
val rawTup = Tup(lhs :: Nil): Tup // <-- loc might be wrong
330-
val arguments = Term.Tup(first :: second :: Nil)(rawTup)
331-
val joint = FlowSymbol("‹applied-result›")
332-
Term.App(op, arguments)(tree, N, joint)
333-
opRhsApps.foldRight(Function.const(fallback): Sequel): (tt, elabFallback) =>
334-
tt match
335-
case (Tree.Empty(), LetLike(`let`, pat, termTree, N)) => ctx =>
336-
val ident = pat match // TODO handle patterns and rm special cases
337-
case ident: Ident => ident
338-
case und: Under => new Ident("_").withLocOf(und)
339-
case _ => ???
340-
termTree match
341-
case S(termTree) =>
342-
val sym = VarSymbol(ident)
343-
val fallbackCtx = ctx + (ident.name -> sym)
344-
Split.Let(sym, term(termTree)(using ctx), elabFallback(fallbackCtx))
345-
case (Tree.Empty(), Modified(Keyword.`do`, doLoc, computation)) => ctx => trace(
346-
pre = s"termSplit: do $computation",
347-
post = (res: Split) => s"termSplit: else >>> $res"
348-
):
349-
val sym = TempSymbol(N, "doTemp")
350-
Split.Let(sym, term(computation)(using ctx), elabFallback(ctx))
351-
case (Tree.Empty(), Modified(Keyword.`else`, elsLoc, default)) => ctx =>
352-
// TODO: report `rest` as unreachable
353-
Split.default(term(default)(using ctx))
354-
case ((rawOp @ Ident(_)), rhs) => ctx =>
355-
val op = term(rawOp)(using ctx)
356-
val innerFinish = mkInnerFinish(op)
357-
termSplit(rhs, innerFinish)(elabFallback(ctx))(ctx)
358-
case (op, rhs) => ctx =>
359-
raise(ErrorReport(msg"Unrecognized operator branch." -> op.toLoc :: Nil))
360-
elabFallback(ctx)
361-
// TODO: END rm
362322
case _ => fallback => _ =>
363323
raise(ErrorReport(msg"Unrecognized term split (${tree.describe})." -> tree.toLoc :: Nil))
364324
fallback.withoutLoc // Hacky... a loc is always added for the result

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package semantics
44
import sourcecode.{Name, Line, FileName}
55

66
package object ucs:
7-
def error(using Line, FileName)(msgs: (Message, Option[Loc])*)(using Raise)(using Name): Unit =
7+
def error(using Line, FileName)(msgs: (Message, Option[Loc])*)(using Raise, Name): Unit =
88
raise(ErrorReport(msgs.toList))
99

10-
def warn(using Line, FileName)(msgs: (Message, Option[Loc])*)(using Raise)(using Name): Unit =
10+
def warn(using Line, FileName)(msgs: (Message, Option[Loc])*)(using Raise, Name): Unit =
1111
raise(WarningReport(msgs.toList))
1212
end ucs

hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ enum Tree extends AutoLocated:
8080
case IfLike(kw: Keyword.`if`.type | Keyword.`while`.type, kwLoc: Opt[Loc], split: Tree)
8181
case SplitPoint()
8282
case OpSplit(lhs: Tree, ops_rhss: Ls[Tree]) // the rhss trees are expressions rooted in `SplitPoint`s
83-
@deprecated("Use If instead", "hkmc2-ucs")
84-
case IfElse(cond: Tree, alt: Tree)
8583
case Case(kwLoc: Opt[Loc], branches: Tree)
8684
case Region(name: Tree, body: Tree)
8785
case RegRef(reg: Tree, value: Tree)
@@ -90,10 +88,9 @@ enum Tree extends AutoLocated:
9088
case Spread(kw: Keyword.Ellipsis, kwLoc: Opt[Loc], body: Opt[Tree])
9189
case Annotated(annotation: Tree, target: Tree)
9290
case Constructor(decl: Tree)
93-
/** It represents a term that has already been elaborated. When desugaring the
94-
* operator splits in UCS, the `lhs` of `OpSplit` has already been elaborated
95-
* into a `Term`, we need to embed `Term` into `Tree` using `Trm`.
96-
*/
91+
/** Represents a term that has already been elaborated. When desugaring
92+
* operator splits in the UCS, the `lhs` of `OpSplit` has already been elaborated
93+
* into a `Term`, so we need to embed `Term` into `Tree` using `Trm`. */
9794
case Trm(term: semantics.Term)
9895

9996
def splitOn(acc: Tree): Tree = this match
@@ -105,7 +102,7 @@ enum Tree extends AutoLocated:
105102
case InfixApp(lhs, kw, rhs) => InfixApp(lhs.splitOn(acc), kw, rhs)
106103
case _: (Ident | Literal | Error) => acc
107104

108-
def children: Ls[Tree] = this match
105+
def children: Ls[Located] = this match
109106
case _: Empty | _: Error | _: Ident | _: Literal | _: Under | _: Unt => Nil
110107
case Pun(_, e) => e :: Nil
111108
case Bra(_, e) => e :: Nil
@@ -129,7 +126,6 @@ enum Tree extends AutoLocated:
129126
case TermDef(k, head, rhs) => head :: rhs.toList
130127
case New(body, rft) => body.toList ::: rft.toList
131128
case IfLike(_, _, split) => split :: Nil
132-
case IfElse(cond, alt) => cond :: alt :: Nil
133129
case Case(_, bs) => Ls(bs)
134130
case Region(name, body) => name :: body :: Nil
135131
case RegRef(reg, value) => reg :: value :: Nil
@@ -150,7 +146,7 @@ enum Tree extends AutoLocated:
150146
case Dummy => Nil
151147
case OpSplit(lhs, ops_rhss) => lhs :: ops_rhss
152148
case SplitPoint() => Nil
153-
case Trm(_) => Nil
149+
case Trm(trm) => trm :: Nil
154150

155151
def describe: Str = this match
156152
case Empty() => "empty"
@@ -175,9 +171,7 @@ enum Tree extends AutoLocated:
175171
case Tup(fields) => "tuple"
176172
case TyTup(tys) => "type tuple"
177173
case App(lhs, rhs) => "application"
178-
case OpApp(lhs, op, rhss) => "operator application" + (op match
179-
case Ident(nme) => s" `$nme`"
180-
case _ => "")
174+
case OpApp(lhs, op, rhss) => "operator application"
181175
case Jux(lhs, rhs) => "juxtaposition"
182176
case Sel(prefix, name) => "selection"
183177
case SynthSel(prefix, name) => "synthetic selection"
@@ -200,9 +194,8 @@ enum Tree extends AutoLocated:
200194
case Constructor(_) => "constructor"
201195
case MemberProj(_, _) => "member projection"
202196
case Keywrd(kw) => s"'${kw.name}' keyword"
203-
case Unt() => "unit"
204197
case Dummy => "‹dummy›"
205-
case Trm(_) => "term"
198+
case Trm(t) => t.describe + " term"
206199

207200
def deparenthesized: Tree = this match
208201
case Bra(BracketKind.Round, inner) => inner.deparenthesized

hkmc2/shared/src/test/mlscript/backlog/UCS.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ if 1 + 1
122122
//│ ╔══[PARSE ERROR] Expected end of input; found literal instead
123123
//│ ║ l.107: * 2 == 4 then "X"
124124
//│ ╙── ^
125-
//│ ═══[ERROR] Unrecognized term split (term).
125+
//│ ═══[ERROR] Unrecognized term split (reference term).
126126
//│ ╔══[ERROR] Unrecognized term split (integer literal).
127127
//│ ║ l.107: * 2 == 4 then "X"
128128
//│ ╙── ^

hkmc2/shared/src/test/mlscript/bbml/bbBasics.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let t = new Some(true) in t.Some#value
130130
//│ ╔══[LEXICAL ERROR] Expect at least one digit after the decimal point
131131
//│ ║ l.129: 42.Some#value
132132
//│ ╙── ^
133-
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application `#`).
133+
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application).
134134
//│ ║ l.129: 42.Some#value
135135
//│ ╙── ^^^^^^^^^^
136136
//│ Type: Num

hkmc2/shared/src/test/mlscript/codegen/Juxtapositions.mls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ inc(2) * 2
125125
42
126126
inc() * 1
127127
add(2)
128-
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application `*`).
128+
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application).
129129
//│ ║ l.126: inc() * 1
130130
//│ ║ ^^^^^^^^^
131131
//│ ║ l.127: add(2)
@@ -148,7 +148,7 @@ inc(2) * 2
148148
:todo // ?
149149
42
150150
inc() + 1
151-
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application `+`).
151+
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application).
152152
//│ ║ l.150: inc() + 1
153153
//│ ╙── ^^^^^^^^^
154154
//│ = 42
@@ -157,7 +157,7 @@ inc(2) * 2
157157
42
158158
add(2) + 1
159159
inc()
160-
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application `+`).
160+
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application).
161161
//│ ║ l.158: add(2) + 1
162162
//│ ╙── ^^^^^^^^^^
163163
//│ = 43
@@ -166,7 +166,7 @@ inc(2) * 2
166166
42
167167
add(2)
168168
inc() + 1
169-
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application `+`).
169+
//│ ╔══[ERROR] Illegal juxtaposition right-hand side (operator application).
170170
//│ ║ l.168: inc() + 1
171171
//│ ╙── ^^^^^^^^^
172172
//│ = 44

hkmc2/shared/src/test/mlscript/handlers/Debugging.mls

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,16 @@ let res =
184184
Runtime.try(() => Test.main())
185185
//│ res = EffectHandle()
186186

187-
res
188-
//│ = EffectHandle()
189-
190187
:re
191188
res.raise()
192189
//│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$
193190

194-
res
195-
//│ = EffectHandle()
196-
197191
set res = res.resumeWith(42)
198192

199-
res
200-
//│ = EffectHandle()
201-
202193
:re
203194
res.raise()
204195
//│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$
205196

206-
res
207-
//│ = EffectHandle()
208-
209197
res.resumeWith(666)
210198
//│ = 0.33676533676533676
211199

@@ -223,9 +211,9 @@ fun f() =
223211
f()
224212
//│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])]
225213
//│ > Stack Trace:
226-
//│ > at f (Debugging.mls:216:3) with locals: j=200
214+
//│ > at f (Debugging.mls:204:3) with locals: j=200
227215
//│ > Stack Trace:
228-
//│ > at f (Debugging.mls:218:3)
216+
//│ > at f (Debugging.mls:206:3)
229217
//│ > Stack Trace:
230-
//│ > at f (Debugging.mls:219:3) with locals: j=300
218+
//│ > at f (Debugging.mls:207:3) with locals: j=300
231219
//│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])]

hkmc2/shared/src/test/mlscript/rp/RangePatterns.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pattern UnsignedByte = 0..< 256
5858
//│ ╔══[ERROR] Name not found: .<
5959
//│ ║ l.51: pattern UnsignedByte = 0..< 256
6060
//│ ╙── ^^
61-
//│ ╔══[ERROR] Unrecognized pattern (operator application `.<`)
61+
//│ ╔══[ERROR] Unrecognized pattern (operator application)
6262
//│ ║ l.51: pattern UnsignedByte = 0..< 256
6363
//│ ╙── ^^^^^^^^
6464

0 commit comments

Comments
 (0)