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

Printer fixes

parent 800df59d
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -42,7 +42,7 @@ class PrettyPrinter(opts: PrinterOptions,
} }
protected def printNameWithPath(df: Definition)(implicit ctx: PrinterContext) { protected def printNameWithPath(df: Definition)(implicit ctx: PrinterContext) {
(opgm, ctx.parents.collectFirst { case (d: Definition) => d }) match { (opgm, ctx.parents.collectFirst { case (d: Definition) if !d.isInstanceOf[ValDef] => d }) match {
case (Some(pgm), Some(scope)) => case (Some(pgm), Some(scope)) =>
sb.append(fullNameFrom(df, scope, opts.printUniqueIds)(pgm)) sb.append(fullNameFrom(df, scope, opts.printUniqueIds)(pgm))
...@@ -192,7 +192,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -192,7 +192,7 @@ class PrettyPrinter(opts: PrinterOptions,
case FcallMethodInvocation(rec, fd, id, tps, args) => case FcallMethodInvocation(rec, fd, id, tps, args) =>
p"$rec.$id${nary(tps, " ,", "[", "]")}" p"$rec.$id${nary(tps, ", ", "[", "]")}"
if (fd.isRealFunction) { if (fd.isRealFunction) {
// The non-present arguments are synthetic function invocations // The non-present arguments are synthetic function invocations
...@@ -207,7 +207,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -207,7 +207,7 @@ class PrettyPrinter(opts: PrinterOptions,
case FunctionInvocation(TypedFunDef(fd, tps), args) => case FunctionInvocation(TypedFunDef(fd, tps), args) =>
printNameWithPath(fd) printNameWithPath(fd)
p"${nary(tps, " ,", "[", "]")}" p"${nary(tps, ", ", "[", "]")}"
if (fd.isRealFunction) { if (fd.isRealFunction) {
// The non-present arguments are synthetic function invocations // The non-present arguments are synthetic function invocations
...@@ -398,11 +398,11 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -398,11 +398,11 @@ class PrettyPrinter(opts: PrinterOptions,
case FunctionType(fts, tt) => p"($fts) => $tt" case FunctionType(fts, tt) => p"($fts) => $tt"
case c: ClassType => case c: ClassType =>
printNameWithPath(c.classDef) printNameWithPath(c.classDef)
p"${nary(c.tps, " ,", "[", "]")}" p"${nary(c.tps, ", ", "[", "]")}"
// Definitions // Definitions
case Program(units) => case Program(units) =>
p"""${nary(units filter {_.isMainUnit}, "\n\n")}""" p"""${nary(units, "\n\n")}"""
case UnitDef(id,pack, imports, defs,_) => case UnitDef(id,pack, imports, defs,_) =>
if (pack.nonEmpty){ if (pack.nonEmpty){
...@@ -427,7 +427,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -427,7 +427,7 @@ class PrettyPrinter(opts: PrinterOptions,
|}""" |}"""
case acd @ AbstractClassDef(id, tparams, parent) => case acd @ AbstractClassDef(id, tparams, parent) =>
p"abstract class $id${nary(tparams, " ,", "[", "]")}" p"abstract class $id${nary(tparams, ", ", "[", "]")}"
parent.foreach{ par => parent.foreach{ par =>
p" extends ${par.id}" p" extends ${par.id}"
...@@ -446,7 +446,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -446,7 +446,7 @@ class PrettyPrinter(opts: PrinterOptions,
p"case class $id" p"case class $id"
} }
p"${nary(tparams, " ,", "[", "]")}" p"${nary(tparams, ", ", "[", "]")}"
if (!isObj) { if (!isObj) {
p"(${ccd.fields})" p"(${ccd.fields})"
...@@ -454,7 +454,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -454,7 +454,7 @@ class PrettyPrinter(opts: PrinterOptions,
parent.foreach { par => parent.foreach { par =>
// Remember child and parents tparams are simple bijection // Remember child and parents tparams are simple bijection
p" extends ${par.id}${nary(tparams, " ,", "[", "]")}" p" extends ${par.id}${nary(tparams, ", ", "[", "]")}"
} }
if (ccd.methods.nonEmpty) { if (ccd.methods.nonEmpty) {
...@@ -464,14 +464,17 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -464,14 +464,17 @@ class PrettyPrinter(opts: PrinterOptions,
} }
case fd: FunDef => case fd: FunDef =>
p"${nary(fd.annotations.toSeq, "\n")}" for (an <- fd.annotations) {
p"""|@$an
|"""
}
if (fd.canBeStrictField) { if (fd.canBeStrictField) {
p"val ${fd.id} : " p"val ${fd.id} : "
} else if (fd.canBeLazyField) { } else if (fd.canBeLazyField) {
p"lazy val ${fd.id} : " p"lazy val ${fd.id} : "
} else { } else {
p"def ${fd.id}${nary(fd.tparams, ",", "[", "]")}(${fd.params}): " p"def ${fd.id}${nary(fd.tparams, ", ", "[", "]")}(${fd.params}): "
} }
p"${fd.returnType} = ${fd.fullBody}" p"${fd.returnType} = ${fd.fullBody}"
...@@ -519,11 +522,9 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -519,11 +522,9 @@ class PrettyPrinter(opts: PrinterOptions,
val fname = fid.name val fname = fid.name
val decoded = scala.reflect.NameTransformer.decode(fname)
val realtps = tfd.tps.drop(cd.tparams.size) val realtps = tfd.tps.drop(cd.tparams.size)
(rec, tfd.fd, decoded, realtps, rargs) (rec, tfd.fd, fname, realtps, rargs)
} }
} }
} }
...@@ -552,23 +553,20 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -552,23 +553,20 @@ class PrettyPrinter(opts: PrinterOptions,
case _ => true case _ => true
} }
protected def noBracesSub(e: Expr) = e match { protected def noBracesSub(e: Expr): Seq[Expr] = e match {
case Assert(_, _, bd) => Some(bd) case Assert(_, _, bd) => Seq(bd)
case Let(_, _, bd) => Some(bd) case Let(_, _, bd) => Seq(bd)
case LetDef(_, bd) => Some(bd) case LetDef(_, bd) => Seq(bd)
case Require(_, bd) => Some(bd) case Require(_, bd) => Seq(bd)
case _ => None case _ => Seq()
} }
protected def requiresBraces(ex: Tree, within: Option[Tree]) = (ex, within) match { protected def requiresBraces(ex: Tree, within: Option[Tree]) = (ex, within) match {
case (e: Expr, _) if isSimpleExpr(e) => case (e: Expr, _) if isSimpleExpr(e) => false
false case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e => false
case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e => case (_: Expr, Some(_: MatchCase)) => false
false case (e: Expr, Some(_)) => true
case (e: Expr, Some(_)) => case _ => false
true
case _ =>
false
} }
protected def precedence(ex: Expr): Int = ex match { protected def precedence(ex: Expr): Int = ex match {
...@@ -587,11 +585,12 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -587,11 +585,12 @@ class PrettyPrinter(opts: PrinterOptions,
protected def requiresParentheses(ex: Tree, within: Option[Tree]): Boolean = (ex, within) match { protected def requiresParentheses(ex: Tree, within: Option[Tree]): Boolean = (ex, within) match {
case (pa: PrettyPrintable, _) => pa.printRequiresParentheses(within) case (pa: PrettyPrintable, _) => pa.printRequiresParentheses(within)
case (_, None) => false case (_, None) => false
case (me: MatchExpr, Some(_ :Ensuring)) => true
case (_, Some(_: Ensuring)) => false case (_, Some(_: Ensuring)) => false
case (_, Some(_: Assert)) => false case (_, Some(_: Assert)) => false
case (_, Some(_: Require)) => false case (_, Some(_: Require)) => false
case (_, Some(_: Definition)) => false case (_, Some(_: Definition)) => false
case (_, Some(_: MatchExpr | _: MatchCase | _: Let | _: LetDef | _: IfExpr | _ : CaseClass)) => false case (_, Some(_: MatchExpr | _: MatchCase | _: Let | _: LetDef | _: IfExpr | _ : CaseClass | _ : Lambda)) => false
case (b1 @ BinaryMethodCall(_, _, _), Some(b2 @ BinaryMethodCall(_, _, _))) if precedence(b1) > precedence(b2) => false case (b1 @ BinaryMethodCall(_, _, _), Some(b2 @ BinaryMethodCall(_, _, _))) if precedence(b1) > precedence(b2) => false
case (BinaryMethodCall(_, _, _), Some(_: FunctionInvocation)) => true case (BinaryMethodCall(_, _, _), Some(_: FunctionInvocation)) => true
case (_, Some(_: FunctionInvocation)) => false case (_, Some(_: FunctionInvocation)) => false
......
...@@ -12,8 +12,6 @@ import purescala.Types._ ...@@ -12,8 +12,6 @@ import purescala.Types._
import purescala.Definitions._ import purescala.Definitions._
import purescala.Constructors._ import purescala.Constructors._
import evaluators._
class TemplateGenerator[T](val encoder: TemplateEncoder[T], class TemplateGenerator[T](val encoder: TemplateEncoder[T],
val assumePreHolds: Boolean) { val assumePreHolds: Boolean) {
private var cache = Map[TypedFunDef, FunctionTemplate[T]]() private var cache = Map[TypedFunDef, FunctionTemplate[T]]()
...@@ -140,7 +138,7 @@ class TemplateGenerator[T](val encoder: TemplateEncoder[T], ...@@ -140,7 +138,7 @@ class TemplateGenerator[T](val encoder: TemplateEncoder[T],
// id => expr && ... && expr // id => expr && ... && expr
var guardedExprs = Map[Identifier, Seq[Expr]]() var guardedExprs = Map[Identifier, Seq[Expr]]()
def storeGuarded(guardVar : Identifier, expr : Expr) : Unit = { def storeGuarded(guardVar : Identifier, expr : Expr) : Unit = {
assert(expr.getType == BooleanType) assert(expr.getType == BooleanType, expr.asString(Program.empty)(LeonContext.empty))
val prev = guardedExprs.getOrElse(guardVar, Nil) val prev = guardedExprs.getOrElse(guardVar, Nil)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment