From 8bb9c849c6e918c31dea5910498319e96bcadd6b Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 30 Nov 2016 15:01:56 +0100 Subject: Make repl use show instances where possible --- .../dotty/tools/dotc/repl/CompilingInterpreter.scala | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'compiler') diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index f269fef64..ad718100c 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -695,8 +695,21 @@ class CompilingInterpreter( s""" + "$varOrVal $prettyName: $varType = " + { | if ($fullPath.asInstanceOf[AnyRef] != null) { - | (if ($fullPath.toString().contains('\\n')) "\\n" else "") + - | $fullPath.toString() + "\\n" + | (if ($fullPath.toString().contains('\\n')) "\\n" else "") + { + | import dotty.Show._ + | if ("$varType".matches(".*Map\\\\[.*,.*\\\\]")) { + | import dotty.Show.Map._ + | $fullPath.show /*toString()*/ + "\\n" + | } else if ("$varType".matches(".*List\\\\[.*\\\\]")) { + | import dotty.Show.List._ + | $fullPath.show /*toString()*/ + "\\n" + | } else if ("$varType".matches(".*Option\\\\[.*\\\\]")) { + | import dotty.Show.Option._ + | $fullPath.show /*toString()*/ + "\\n" + | } else { + | $fullPath.show /*toString()*/ + "\\n" + | } + | } | } else { | "null\\n" | } -- cgit v1.2.3 From 8b7a7ea0cf25b55ea842fe1f299e1576b8bc3e5a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 30 Nov 2016 15:02:14 +0100 Subject: Print List and Map abbreviated --- compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'compiler') diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index ad718100c..d193148ef 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -683,9 +683,16 @@ class CompilingInterpreter( code.print(resultExtractors.mkString("")) } + private val ListReg = """^.*List\[(\w+)\]$""".r + private val MapReg = """^.*Map\[(\w+),[ ]*(\w+)\]$""".r + private def resultExtractor(req: Request, varName: Name): String = { val prettyName = varName.decode - val varType = string2code(req.typeOf(varName)) + val varType = string2code(req.typeOf(varName)) match { + case ListReg(param) => s"List[$param]" + case MapReg(k, v) => s"Map[$k, $v]" + case x => x + } val fullPath = req.fullPath(varName) val varOrVal = statement match { -- cgit v1.2.3 From a190cfe4f0ece8221d8d7e4b27e4bf73ca665a56 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 30 Nov 2016 15:17:14 +0100 Subject: Get rid of nesting implicits --- .../tools/dotc/repl/CompilingInterpreter.scala | 13 +----- library/src/dotty/Show.scala | 48 +++++++++++----------- library/test/dotty/ShowTests.scala | 8 +--- 3 files changed, 26 insertions(+), 43 deletions(-) (limited to 'compiler') diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index d193148ef..e0fda8258 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -704,18 +704,7 @@ class CompilingInterpreter( | if ($fullPath.asInstanceOf[AnyRef] != null) { | (if ($fullPath.toString().contains('\\n')) "\\n" else "") + { | import dotty.Show._ - | if ("$varType".matches(".*Map\\\\[.*,.*\\\\]")) { - | import dotty.Show.Map._ - | $fullPath.show /*toString()*/ + "\\n" - | } else if ("$varType".matches(".*List\\\\[.*\\\\]")) { - | import dotty.Show.List._ - | $fullPath.show /*toString()*/ + "\\n" - | } else if ("$varType".matches(".*Option\\\\[.*\\\\]")) { - | import dotty.Show.Option._ - | $fullPath.show /*toString()*/ + "\\n" - | } else { - | $fullPath.show /*toString()*/ + "\\n" - | } + | $fullPath.show /*toString()*/ + "\\n" | } | } else { | "null\\n" diff --git a/library/src/dotty/Show.scala b/library/src/dotty/Show.scala index 123fffdc7..103341d00 100644 --- a/library/src/dotty/Show.scala +++ b/library/src/dotty/Show.scala @@ -15,7 +15,9 @@ object Show { } implicit val stringShow = new Show[String] { - //charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) + // From 2.12 spec: + // + // charEscapeSeq ::= ‘\‘ (‘b‘ | ‘t‘ | ‘n‘ | ‘f‘ | ‘r‘ | ‘"‘ | ‘'‘ | ‘\‘) def show(str: String) = "\"" + str @@ -54,35 +56,33 @@ object Show { }) + "'" } - object List { - implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] { - def show(xs: List[T]) = - if (xs.isEmpty) "Nil" - else "List(" + xs.map(_.show).mkString(", ") + ")" - } + implicit def showList[T](implicit st: Show[T]) = new Show[List[T]] { + def show(xs: List[T]) = + if (xs.isEmpty) "Nil" + else "List(" + xs.map(_.show).mkString(", ") + ")" + } - implicit val showNil = new Show[List[Nothing]] { - def show(xs: List[Nothing]) = "Nil" - } + implicit val showNil = new Show[List[Nothing]] { + def show(xs: List[Nothing]) = "Nil" } - object Option { - implicit def showOption[T](implicit st: Show[T]) = new Show[Option[T]] { - def show(ot: Option[T]): String = ot match { - case Some(t) => "Some("+ st.show(t) + ")" - case none => "None" - } + implicit def showOption[T](implicit st: Show[T]) = new Show[Option[T]] { + def show(ot: Option[T]): String = ot match { + case Some(t) => "Some("+ st.show(t) + ")" + case none => "None" } + } - implicit val showNone = new Show[Option[Nothing]] { - def show(n: Option[Nothing]) = "None" - } + implicit val showNone = new Show[Option[Nothing]] { + def show(n: Option[Nothing]) = "None" } - object Map { - implicit def showMap[K, V](implicit sk: Show[K], sv: Show[V]) = new Show[Map[K, V]] { - def show(m: Map[K, V]) = - "Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")" - } + implicit def showMap[K, V](implicit sk: Show[K], sv: Show[V]) = new Show[Map[K, V]] { + def show(m: Map[K, V]) = + "Map(" + m.map { case (k, v) => sk.show(k) + " -> " + sv.show(v) } .mkString (", ") + ")" + } + + implicit def showMapOfNothing = new Show[Map[Nothing, Nothing]] { + def show(m: Map[Nothing, Nothing]) = m.toString } } diff --git a/library/test/dotty/ShowTests.scala b/library/test/dotty/ShowTests.scala index 92539d0c4..7230106d5 100644 --- a/library/test/dotty/ShowTests.scala +++ b/library/test/dotty/ShowTests.scala @@ -34,7 +34,6 @@ class ShowTests { } @Test def showCar = { - import Show.List._ case class Car(model: String, manufacturer: String, year: Int) implicit val showCar = new Show[Car] { def show(c: Car) = @@ -51,14 +50,12 @@ class ShowTests { } @Test def showOptions = { - import Show.Option._ assertEquals("None", None.show) assertEquals("None", (None: Option[String]).show) assertEquals("Some(\"hello opt\")", Some("hello opt").show) } @Test def showMaps = { - import Show.Map._ val mp = scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2") assertEquals("Map(\"str1\" -> \"val1\", \"str2\" -> \"val2\")", mp.show) } @@ -67,9 +64,6 @@ class ShowTests { case class Car(model: String, manufacturer: String, year: Int) assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show) - assertEquals( - "Map(str1 -> val1, str2 -> val2)", - scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2").show - ) + assertEquals("Map()", Map().show) } } -- cgit v1.2.3 From 454f09e5741c72f8edd6040585eb07588bf2763e Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Thu, 1 Dec 2016 14:02:46 +0100 Subject: Fix literal type printing --- compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala | 3 +++ 1 file changed, 3 insertions(+) (limited to 'compiler') diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index e0fda8258..7fe3d815d 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -685,12 +685,15 @@ class CompilingInterpreter( private val ListReg = """^.*List\[(\w+)\]$""".r private val MapReg = """^.*Map\[(\w+),[ ]*(\w+)\]$""".r + private val LitReg = """^.*\((.+)\)$""".r private def resultExtractor(req: Request, varName: Name): String = { val prettyName = varName.decode val varType = string2code(req.typeOf(varName)) match { case ListReg(param) => s"List[$param]" case MapReg(k, v) => s"Map[$k, $v]" + case LitReg(lit) => lit + case x if x.lastOption == Some('$') => x.init + ".type" case x => x } val fullPath = req.fullPath(varName) -- cgit v1.2.3 From 35e8fcb805e7780555cf48160f90c9da71bb1811 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Thu, 1 Dec 2016 15:34:37 +0100 Subject: Add comment regarding prettification --- .../dotty/tools/dotc/repl/CompilingInterpreter.scala | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'compiler') diff --git a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index 7fe3d815d..14a3b4ad0 100644 --- a/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -689,10 +689,27 @@ class CompilingInterpreter( private def resultExtractor(req: Request, varName: Name): String = { val prettyName = varName.decode + // FIXME: `varType` is prettified to abbreviate common types where + // appropriate, and to also prettify literal types + // + // This should be rewritten to use the actual types once we have a + // semantic representation available to the REPL val varType = string2code(req.typeOf(varName)) match { + // Extract List's paremeter from full path case ListReg(param) => s"List[$param]" + // Extract Map's paremeters from full path case MapReg(k, v) => s"Map[$k, $v]" + // Extract literal type from literal type representation. Example: + // + // ``` + // scala> val x: 42 = 42 + // val x: Int(42) = 42 + // scala> val y: "hello" = "hello" + // val y: String("hello") = "hello" + // ``` case LitReg(lit) => lit + // When the type is a singleton value like None, don't show `None$` + // instead show `None.type`. case x if x.lastOption == Some('$') => x.init + ".type" case x => x } -- cgit v1.2.3