Skip to content
Snippets Groups Projects
Commit 5bf8a57c authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Improve printing

parent 859443eb
No related branches found
No related tags found
No related merge requests found
...@@ -4,14 +4,14 @@ package leon ...@@ -4,14 +4,14 @@ package leon
package purescala package purescala
import leon.utils._ import leon.utils._
import leon.purescala.Common._ import Common._
import leon.purescala.DefOps._ import DefOps._
import leon.purescala.Definitions._ import Definitions._
import leon.purescala.Extractors._ import Extractors._
import leon.purescala.PrinterHelpers._ import PrinterHelpers._
import leon.purescala.ExprOps.{isListLiteral, simplestValue} import ExprOps.{isListLiteral, simplestValue}
import leon.purescala.Expressions._ import Expressions._
import leon.purescala.Types._ import Types._
case class PrinterContext( case class PrinterContext(
current: Tree, current: Tree,
...@@ -52,6 +52,15 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -52,6 +52,15 @@ class PrettyPrinter(opts: PrinterOptions,
} }
def pp(tree: Tree)(implicit ctx: PrinterContext): Unit = { def pp(tree: Tree)(implicit ctx: PrinterContext): Unit = {
if (requiresBraces(tree, ctx.parent)) {
val ctx1: PrinterContext = ctx.copy(parents = Nil)
p"""|{
| $tree
|}"""(ctx1)
return
}
if (opts.printTypes) { if (opts.printTypes) {
tree match { tree match {
case t: Expr => case t: Expr =>
...@@ -81,15 +90,8 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -81,15 +90,8 @@ class PrettyPrinter(opts: PrinterOptions,
p"$id" p"$id"
case Let(b,d,e) => case Let(b,d,e) =>
if (isSimpleExpr(d)) {
p"""|val $b = $d p"""|val $b = $d
|$e""" |$e"""
} else {
p"""|val $b = {
| $d
|}
|$e"""
}
case LetDef(fd,body) => case LetDef(fd,body) =>
p"""|$fd p"""|$fd
...@@ -108,9 +110,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -108,9 +110,7 @@ class PrettyPrinter(opts: PrinterOptions,
|$body""" |$body"""
case Ensuring(body, post) => case Ensuring(body, post) =>
p"""|{ p"""|$body ensuring {
| $body
|} ensuring {
| $post | $post
|}""" |}"""
...@@ -340,17 +340,12 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -340,17 +340,12 @@ class PrettyPrinter(opts: PrinterOptions,
|}""" |}"""
} }
/*case LetPattern(p,s,rhs) => /*
if (isSimpleExpr(s)) { case LetPattern(p,s,rhs) =>
p"""|val $p = $s p"""|val $p = $s
|$rhs""" |$rhs"""
} else {
p"""|val $p = {
| $s
|}
|$rhs"""
}
*/ */
case MatchExpr(s, csc) => case MatchExpr(s, csc) =>
optP { optP {
p"""|$s match { p"""|$s match {
...@@ -509,13 +504,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -509,13 +504,7 @@ class PrettyPrinter(opts: PrinterOptions,
p"${fd.returnType} = " p"${fd.returnType} = "
if (isSimpleExpr(fd.fullBody)) { p"${fd.fullBody}"
p"${fd.fullBody}"
} else {
p"""|{
| ${fd.fullBody}
|}"""
}
case (tree: PrettyPrintable) => tree.printWith(ctx) case (tree: PrettyPrintable) => tree.printWith(ctx)
...@@ -547,6 +536,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -547,6 +536,7 @@ class PrettyPrinter(opts: PrinterOptions,
p"@(?)" p"@(?)"
} }
} }
} }
object FcallMethodInvocation { object FcallMethodInvocation {
...@@ -587,10 +577,31 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -587,10 +577,31 @@ class PrettyPrinter(opts: PrinterOptions,
} }
def isSimpleExpr(e: Expr): Boolean = e match { def isSimpleExpr(e: Expr): Boolean = e match {
case _: LetDef | _: Let | LetPattern(_, _, _) | _: Assert => false case _: LetDef | _: Let | LetPattern(_, _, _) | _: Assert | _: Require => false
case _ => true case _ => true
} }
def noBracesSub(e: Expr) = e match {
case Assert(_, _, bd) => Some(bd)
case Let(_, _, bd) => Some(bd)
case LetDef(_, bd) => Some(bd)
case Require(_, bd) => Some(bd)
case _ => None
}
def requiresBraces(ex: Tree, within: Option[Tree]) = (ex, within) match {
case (e: Expr, _) if isSimpleExpr(e) =>
false
case (_, None) =>
false
case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e =>
false
case (e: Expr, Some(_)) =>
true
case _ =>
false
}
def precedence(ex: Expr): Int = ex match { def precedence(ex: Expr): Int = ex match {
case (pa: PrettyPrintable) => pa.printPrecedence case (pa: PrettyPrintable) => pa.printPrecedence
case (_: ElementOfSet) => 0 case (_: ElementOfSet) => 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment