Skip to content

Commit d9b8292

Browse files
authored
Fix nitpicks in core pretty-printing (#789)
Resolves #767 + renames `vet` to `let` for top-level value bindings. Furthermore, I tried aligning the printing of params and args + added a rule that `()` won't be printed if there's a block arg afterwards (much like the current style in the frontend) Here's a screenshot of a pretty diff: <img width="1252" alt="Screenshot 2025-01-22 at 12 33 33" src="https://github.com/user-attachments/assets/4148a1d9-604e-4b22-9d48-aa63816fd8f0" /> And here's a machine-readable diff: ```diff -vet oneIncrement2362 = return 1; -def useCounter2359(){c2358} = { +let oneIncrement2362 = return 1; +def useCounter2359{c2358} = { { val v_r_23873305: Int398 = c2358.get(); val v_r_23883306: Unit394 = println4(v_r_23873305); @@ -56,10 +56,10 @@ def useCounter2359(){c2358} = { def counterAsEffect2360() = { val v_r_23933303: Int398 = return 0; var count2365 = v_r_23933303 ; - val v_r_24053322: Unit394 = reset { (){p3311} => + val v_r_24053322: Unit394 = reset { {p3311} => def counter2369 = new Counter2357 { def increment2363() = - shift(p3311) { (){k3314} => + shift(p3311) { {k3314} => def resume2367(a3315: Unit394) = resume(k3314) { return a3315 } @@ -72,7 +72,7 @@ def counterAsEffect2360() = { return v_r_23983316 } def get2364() = - shift(p3311) { (){k3317} => + shift(p3311) { {k3317} => def resume2368(a3318: Int398) = resume(k3317) { return a3318 } @@ -81,7 +81,7 @@ def counterAsEffect2360() = { return v_r_24023320 } } - val v_r_24043321: Unit394 = useCounter2359(counter2369); + val v_r_24043321: Unit394 = useCounter2359{counter2369}; return v_r_24043321 }; return v_r_24053322 ```
1 parent 63435f9 commit d9b8292

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ object PrettyPrinter extends ParenPrettyPrinter {
7777
/// TODO
7878
hsep(t.args.map(toDoc), comma)
7979

80-
def toDoc(b: Block): Doc = b match {
80+
def toDoc(b: Block, preventBraces: Boolean = false): Doc = b match {
8181
case BlockVar(id, _, _) => toDoc(id)
8282
case BlockLit(tps, cps, vps, bps, body) =>
83-
braces { space <> paramsToDoc(tps, vps, bps) <+> "=>" <+> nest(line <> toDocStmts(body)) <> line }
83+
val doc = space <> paramsToDoc(tps, vps, bps) <+> "=>" <+> nest(line <> toDocStmts(body)) <> line
84+
if preventBraces then doc else braces { doc }
8485
case Unbox(e) => parens("unbox" <+> toDoc(e))
8586
case New(handler) => "new" <+> toDoc(handler)
8687
}
@@ -108,13 +109,17 @@ object PrettyPrinter extends ParenPrettyPrinter {
108109
def argsToDoc(targs: List[core.ValueType], vargs: List[core.Pure], bargs: List[core.Block]): Doc =
109110
val targsDoc = if targs.isEmpty then emptyDoc else brackets(targs.map(toDoc))
110111
//val cargsDoc = if cargs.isEmpty then emptyDoc else brackets(cargs.map(toDoc))
111-
val vargsDoc = vargs.map(toDoc)
112-
val bargsDoc = bargs.map(toDoc)
113-
targsDoc <> parens(vargsDoc ++ bargsDoc)
112+
val vargsDoc = if vargs.isEmpty && !bargs.isEmpty then emptyDoc else parens(vargs.map(toDoc))
113+
114+
// Wrap in braces individually, then concat with a space between. Force BlockLits to not add a layer of braces on top.
115+
val bargsDoc = if bargs.isEmpty then emptyDoc else hcat { bargs.map { b => braces(toDoc(b, preventBraces = true)) } }
116+
targsDoc <> vargsDoc <> bargsDoc
114117

115118
def paramsToDoc(tps: List[symbols.Symbol], vps: List[ValueParam], bps: List[BlockParam]): Doc = {
116-
val tpsDoc = if (tps.isEmpty) emptyDoc else brackets(tps.map(toDoc))
117-
tpsDoc <> parens(hsep(vps map toDoc, comma)) <> hcat(bps map toDoc)
119+
val tpsDoc = if tps.isEmpty then emptyDoc else brackets(tps.map(toDoc))
120+
val vpsDoc = if vps.isEmpty && !bps.isEmpty then emptyDoc else parens(vps.map(toDoc))
121+
val bpsDoc = if bps.isEmpty then emptyDoc else hcat(bps.map(toDoc)) // already are in braces!
122+
tpsDoc <> vpsDoc <> bpsDoc
118123
}
119124

120125
def toDoc(instance: Implementation): Doc = {
@@ -155,7 +160,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
155160
case Toplevel.Def(id, block) =>
156161
"def" <+> toDoc(id) <+> "=" <+> toDoc(block)
157162
case Toplevel.Val(id, _, binding) =>
158-
"vet" <+> toDoc(id) <+> "=" <+> toDoc(binding)
163+
"let" <+> toDoc(id) <+> "=" <+> toDoc(binding)
159164
}
160165

161166
def toDoc(s: Stmt): Doc = s match {

0 commit comments

Comments
 (0)