Skip to content

Commit 62ebc06

Browse files
committed
Pretty print the items
1 parent 3c66451 commit 62ebc06

File tree

12 files changed

+5690
-11689
lines changed

12 files changed

+5690
-11689
lines changed

compiler/shared/main/scala/mlscript/compiler/ir/IR.scala

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ case class Program(
2727
Sorting.quickSort(t2)
2828
s"Program({${t1.mkString(",\n")}}, {\n${t2.mkString("\n")}\n},\n$main)"
2929

30+
def show = toDocument.print
31+
32+
def toDocument: Document =
33+
val t1 = classes.toArray
34+
val t2 = defs.toArray
35+
Sorting.quickSort(t1)
36+
Sorting.quickSort(t2)
37+
given Conversion[String, Document] = raw
38+
stack(
39+
"Program:",
40+
stack_list(t1.map(_.toDocument).toList) |> indent,
41+
stack_list(t2.map(_.toDocument).toList) |> indent,
42+
main.toDocument |> indent
43+
)
44+
3045
implicit object ClassInfoOrdering extends Ordering[ClassInfo] {
3146
def compare(a: ClassInfo, b: ClassInfo) = a.id.compare(b.id)
3247
}
@@ -42,6 +57,19 @@ case class ClassInfo(
4257
override def toString: String =
4358
s"ClassInfo($id, $name, [${fields mkString ","}], parents: ${parents mkString ","}, methods:\n${methods mkString ",\n"})"
4459

60+
def show = toDocument.print
61+
def toDocument: Document =
62+
given Conversion[String, Document] = raw
63+
val extension = if parents.isEmpty then "" else " extends " + parents.mkString(", ")
64+
if methods.isEmpty then
65+
"class" <:> name <#> "(" <#> fields.mkString(",") <#> ")" <#> extension
66+
else
67+
stack(
68+
"class" <:> name <#> "(" <#> fields.mkString(",") <#> ")" <#> extension <:> "{",
69+
stack_list( methods.map { (_, defn) => defn.toDocument |> indent }.toList),
70+
"}"
71+
)
72+
4573
case class Name(val str: Str):
4674
private var intro: Opt[Intro] = None
4775
private var elim: Set[Elim] = Set.empty
@@ -103,6 +131,15 @@ case class Defn(
103131
val ars = activeResults.map(_.toString).mkString("[", ",", "]")
104132
s"Def($id, $name, $ps, $naps,\nI: $ais,\nR: $ars,\nRec: $recBoundary,\n$resultNum, \n$body\n)"
105133

134+
def show = toDocument.print
135+
136+
def toDocument: Document =
137+
given Conversion[String, Document] = raw
138+
stack(
139+
"def" <:> name <#> "(" <#> params.map(_.toString).mkString(",") <#> ")" <:> "=",
140+
body.toDocument |> indent
141+
)
142+
106143
sealed trait TrivialExpr:
107144
import Expr._
108145
override def toString: String
@@ -224,7 +261,7 @@ enum Node:
224261
val names_copy = names.map(_.copy)
225262
LetCall(names_copy, defn, args.map(_.mapNameOfTrivialExpr(_.trySubst(ctx))), body.copy(ctx ++ names_copy.map(x => x.str -> x)))
226263

227-
private def toDocument: Document =
264+
def toDocument: Document =
228265
given Conversion[String, Document] = raw
229266
this match
230267
case Result(res) => (res |> showArguments) <:> s"-- $tag"

0 commit comments

Comments
 (0)