Skip to content

Commit d811d7f

Browse files
Elide some details when printing core for human inspection
1 parent 1331499 commit d811d7f

File tree

7 files changed

+29
-21
lines changed

7 files changed

+29
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package effekt.core
22

33
import effekt.util.messages.ErrorReporter
4-
import PrettyPrinter.*
4+
import HumanReadablePrettyPrinter.*
55

66
/**
77
* Context for transformations of a [[core.ModuleDecl]] that provides the declarations for this module.

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@ package core
44
import effekt.core.Type.{PromptSymbol, ResumeSymbol}
55
import effekt.source.FeatureFlag
66
import kiama.output.ParenPrettyPrinter
7+
import kiama.output.PrettyPrinterTypes.Document
78

89
import scala.language.implicitConversions
910
import effekt.symbols.{Name, Wildcard, builtins}
1011

11-
object PrettyPrinter extends ParenPrettyPrinter {
12-
13-
import kiama.output.PrettyPrinterTypes.Document
14-
12+
class PrettyPrinter(humanReadable: Boolean) extends ParenPrettyPrinter {
1513
override val defaultIndent = 2
1614

17-
def formatHumanReadable(t: ModuleDecl): Document = {
18-
val renamer = effekt.core.TestRenamer(Names(builtins.coreBuiltins))
19-
format(renamer(t))
20-
}
21-
2215
def format(t: ModuleDecl): Document =
2316
pretty(toDoc(t), 4)
2417

@@ -57,9 +50,9 @@ object PrettyPrinter extends ParenPrettyPrinter {
5750
// The order of toplevel items must match the parser (where the order is currently fixed).
5851
val includes = vsep(m.includes.map { im => "import" <+> im })
5952
val decls = vsep(m.declarations.map(toDoc))
60-
val externs = vsep(m.externs.map(toDoc))
53+
val externs = if humanReadable then emptyDoc else vsep(m.externs.map(toDoc))
6154
val defs = toDoc(m.definitions)
62-
val exports = vsep(m.exports.map { id => "export" <+> toDoc(id) })
55+
val exports = if humanReadable then emptyDoc else vsep(m.exports.map { id => "export" <+> toDoc(id) })
6356

6457
"module" <+> m.path <>
6558
emptyline <>
@@ -102,7 +95,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
10295

10396
def toDoc(b: Block, preventBraces: Boolean = false): Doc = b match {
10497
case BlockVar(id, tpe, capt) =>
105-
toDoc(id) <> ":" <+> toDoc(tpe) <+> "@" <+> toDoc(capt)
98+
toDoc(id) <> (if humanReadable then emptyDoc else ":" <+> toDoc(tpe) <+> "@" <+> toDoc(capt))
10699
case BlockLit(tps, cps, vps, bps, body) =>
107100
val doc = space <> paramsToDoc(tps, cps, vps, bps) <+> "=>" <+> nest(line <> toDocStmts(body)) <> line
108101
if preventBraces then doc else braces { doc }
@@ -124,7 +117,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
124117
case Literal((), _) => "()"
125118
case Literal(s: String, _) => stringLiteral(s)
126119
case Literal(value, _) => value.toString
127-
case ValueVar(id, tpe) => toDoc(id) <> ":" <+> toDoc(tpe)
120+
case ValueVar(id, tpe) => toDoc(id) <> (if humanReadable then emptyDoc else ":" <+> toDoc(tpe))
128121

129122
case PureApp(b, targs, vargs) => parens(toDoc(b)) <> argsToDoc(targs, vargs, Nil)
130123
case Make(data, tag, targs, vargs) => "make" <+> toDoc(data) <+> toDoc(tag) <> argsToDoc(targs, vargs, Nil)
@@ -346,3 +339,18 @@ object PrettyPrinter extends ParenPrettyPrinter {
346339
multi <> s <> multi
347340
}
348341
}
342+
343+
/**
344+
* Instance of PrettyPrinter that produces output that can be parsed back by the core parser.
345+
*/
346+
object ReparsablePrettyPrinter extends PrettyPrinter(false) {}
347+
348+
/**
349+
* Instance of PrettyPrinter that produces less verbose, more human-readable output.
350+
*/
351+
object HumanReadablePrettyPrinter extends PrettyPrinter(true) {
352+
def renameAndFormat(t: ModuleDecl): Document = {
353+
val renamer = effekt.core.TestRenamer(Names(builtins.coreBuiltins))
354+
format(renamer(t))
355+
}
356+
}

effekt/shared/src/main/scala/effekt/generator/chez/ChezScheme.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trait ChezScheme extends Compiler[String] {
3636
override def supportedFeatureFlags: List[String] = List("chez")
3737

3838
override def prettyIR(source: Source, stage: Stage)(using Context): Option[Document] = stage match {
39-
case Stage.Core => Core(source).map { res => core.PrettyPrinter.formatHumanReadable(res.core) }
39+
case Stage.Core => Core(source).map { res => core.HumanReadablePrettyPrinter.renameAndFormat(res.core) }
4040
case Stage.CPS => None
4141
case Stage.Machine => None
4242
case Stage.Target => Separate(source).map { res => pretty(res) }

effekt/shared/src/main/scala/effekt/generator/chez/ChezSchemeCPS.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class ChezSchemeCPS extends Compiler[String] {
1414
override def supportedFeatureFlags: List[String] = List("chez", "chezCPS")
1515

1616
override def prettyIR(source: Source, stage: Stage)(using C: Context): Option[Document] = stage match {
17-
case Stage.Core if C.config.optimize() => Optimized(source).map { (_, _, res) => core.PrettyPrinter.formatHumanReadable(res) }
18-
case Stage.Core => Core(source).map { res => core.PrettyPrinter.formatHumanReadable(res.core) }
17+
case Stage.Core if C.config.optimize() => Optimized(source).map { (_, _, res) => core.HumanReadablePrettyPrinter.renameAndFormat(res) }
18+
case Stage.Core => Core(source).map { res => core.HumanReadablePrettyPrinter.renameAndFormat(res.core) }
1919
case Stage.CPS => CPSTransformed(source).map { (_, _, _, res) => cps.PrettyPrinter.format(res) }
2020
case Stage.Machine => None
2121
case Stage.Target => LSP(source)

effekt/shared/src/main/scala/effekt/generator/js/JavaScript.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class JavaScript(additionalFeatureFlags: List[String] = Nil) extends Compiler[St
1818
override def supportedFeatureFlags: List[String] = additionalFeatureFlags ++ TransformerCps.jsFeatureFlags
1919

2020
override def prettyIR(source: Source, stage: Stage)(using C: Context): Option[Document] = stage match {
21-
case Stage.Core if C.config.optimize() => Optimized(source).map { (_, _, res) => core.PrettyPrinter.formatHumanReadable(res) }
21+
case Stage.Core if C.config.optimize() => Optimized(source).map { (_, _, res) => core.HumanReadablePrettyPrinter.renameAndFormat(res) }
2222
case Stage.CPS => CPSTransformed(source).map { (_, _, _, res) => cps.PrettyPrinter.format(res) }
23-
case Stage.Core => Core(source).map { res => core.PrettyPrinter.formatHumanReadable(res.core) }
23+
case Stage.Core => Core(source).map { res => core.HumanReadablePrettyPrinter.renameAndFormat(res.core) }
2424
case Stage.Machine => None
2525
case Stage.Target => CompileLSP(source).map { pretty }
2626
}

effekt/shared/src/main/scala/effekt/generator/llvm/LLVM.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LLVM extends Compiler[String] {
1818
override def supportedFeatureFlags: List[String] = Transformer.llvmFeatureFlags
1919

2020
override def prettyIR(source: Source, stage: Stage)(using Context): Option[Document] = stage match {
21-
case Stage.Core => steps.afterCore(source).map { res => core.PrettyPrinter.formatHumanReadable(res.core) }
21+
case Stage.Core => steps.afterCore(source).map { res => core.HumanReadablePrettyPrinter.renameAndFormat(res.core) }
2222
case Stage.CPS => None
2323
case Stage.Machine => steps.afterMachine(source).map { res => machine.PrettyPrinter.format(res.program) }
2424
case Stage.Target => steps.afterLLVM(source).map { res => pretty(res) }

effekt/shared/src/main/scala/effekt/util/Debug.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ val showGeneric: PartialFunction[Any, String] = {
1414

1515
val show: PartialFunction[Any, String] =
1616
TypePrinter.show orElse
17-
core.PrettyPrinter.show orElse
17+
core.HumanReadablePrettyPrinter.show orElse
1818
generator.js.PrettyPrinter.show orElse
1919
cps.PrettyPrinter.show orElse
2020
showGeneric

0 commit comments

Comments
 (0)