Skip to content
Snippets Groups Projects
Commit 3a371576 authored by Nicolas Voirol's avatar Nicolas Voirol
Browse files

A few small fixes

parent ed286908
Branches
Tags
No related merge requests found
...@@ -78,7 +78,7 @@ trait Definitions { self: Trees => ...@@ -78,7 +78,7 @@ trait Definitions { self: Trees =>
def toVariable: Variable = to[Variable] def toVariable: Variable = to[Variable]
def freshen: ValDef = ValDef(id.freshen, tpe).copiedFrom(this) def freshen: ValDef = ValDef(id.freshen, tpe).copiedFrom(this)
val flags: Set[Annotation] = Set.empty val flags: Set[Flag] = Set.empty
override def equals(that: Any): Boolean = super[VariableSymbol].equals(that) override def equals(that: Any): Boolean = super[VariableSymbol].equals(that)
override def hashCode: Int = super[VariableSymbol].hashCode override def hashCode: Int = super[VariableSymbol].hashCode
...@@ -171,30 +171,30 @@ trait Definitions { self: Trees => ...@@ -171,30 +171,30 @@ trait Definitions { self: Trees =>
def freshen = TypeParameterDef(tp.freshen) def freshen = TypeParameterDef(tp.freshen)
val id = tp.id val id = tp.id
} }
// Compiler annotations given in the source code as @annot /** Represents source code annotations and some other meaningful flags. */
case class Annotation(val annot: String, val args: Seq[Option[Any]]) extends Printable { abstract class Flag(name: String, args: Seq[Any]) extends Printable {
def asString(implicit opts: PrinterOptions): String = annot + (if (args.isEmpty) "" else { def asString(implicit opts: PrinterOptions): String = name + (if (args.isEmpty) "" else {
args.map { case p: Printable => p.asString case arg => arg.toString }.mkString("(", ",", ")") args.map(arg => self.asString(arg)(opts)).mkString("(", ", ", ")")
}) })
} }
/** Denotes that this adt is refined by invariant ''id'' */ /** Denotes that this adt is refined by invariant ''id'' */
class HasADTInvariant(id: Identifier) extends Annotation("invariant", Seq(Some(id))) case class HasADTInvariant(id: Identifier) extends Flag("invariant", Seq(id))
object HasADTInvariant { // Compiler annotations given in the source code as @annot
def apply(id: Identifier): HasADTInvariant = new HasADTInvariant(id) case class Annotation(val name: String, val args: Seq[Any]) extends Flag(name, args)
def unapply(annot: Annotation): Option[Identifier] = annot match {
case Annotation("invariant", Seq(Some(id: Identifier))) => Some(id) def extractFlag(name: String, args: Seq[Any]): Flag = (name, args) match {
case _ => None case ("invariant", id: Identifier) => HasADTInvariant(id)
} case _ => Annotation(name, args)
} }
/** Represents an ADT definition (either the ADT sort or a constructor). */ /** Represents an ADT definition (either the ADT sort or a constructor). */
sealed trait ADTDefinition extends Definition { sealed trait ADTDefinition extends Definition {
val id: Identifier val id: Identifier
val tparams: Seq[TypeParameterDef] val tparams: Seq[TypeParameterDef]
val flags: Set[Annotation] val flags: Set[Flag]
/** The root of the class hierarchy */ /** The root of the class hierarchy */
def root(implicit s: Symbols): ADTDefinition def root(implicit s: Symbols): ADTDefinition
...@@ -221,7 +221,7 @@ trait Definitions { self: Trees => ...@@ -221,7 +221,7 @@ trait Definitions { self: Trees =>
class ADTSort(val id: Identifier, class ADTSort(val id: Identifier,
val tparams: Seq[TypeParameterDef], val tparams: Seq[TypeParameterDef],
val cons: Seq[Identifier], val cons: Seq[Identifier],
val flags: Set[Annotation]) extends ADTDefinition { val flags: Set[Flag]) extends ADTDefinition {
val isSort = true val isSort = true
def constructors(implicit s: Symbols): Seq[ADTConstructor] = cons def constructors(implicit s: Symbols): Seq[ADTConstructor] = cons
...@@ -272,7 +272,7 @@ trait Definitions { self: Trees => ...@@ -272,7 +272,7 @@ trait Definitions { self: Trees =>
val tparams: Seq[TypeParameterDef], val tparams: Seq[TypeParameterDef],
val sort: Option[Identifier], val sort: Option[Identifier],
val fields: Seq[ValDef], val fields: Seq[ValDef],
val flags: Set[Annotation]) extends ADTDefinition { val flags: Set[Flag]) extends ADTDefinition {
val isSort = false val isSort = false
/** Returns the index of the field with the specified id */ /** Returns the index of the field with the specified id */
...@@ -355,7 +355,7 @@ trait Definitions { self: Trees => ...@@ -355,7 +355,7 @@ trait Definitions { self: Trees =>
val params: Seq[ValDef], val params: Seq[ValDef],
val returnType: Type, val returnType: Type,
val fullBody: Expr, val fullBody: Expr,
val flags: Set[Annotation] val flags: Set[Flag]
) extends Definition { ) extends Definition {
/** Wraps this [[FunDef]] in a in [[TypedFunDef]] with the specified type parameters */ /** Wraps this [[FunDef]] in a in [[TypedFunDef]] with the specified type parameters */
......
...@@ -77,19 +77,18 @@ trait Printers { ...@@ -77,19 +77,18 @@ trait Printers {
private val dbquote = "\"" private val dbquote = "\""
def pp(tree: Tree)(implicit ctx: PrinterContext): Unit = { def pp(tree: Tree)(implicit ctx: PrinterContext): Unit = {
ppPrefix(tree)
ppBody(tree)
ppSuffix(tree)
}
protected def ppPrefix(tree: Tree)(implicit ctx: PrinterContext): Unit = {
if (requiresBraces(tree, ctx.parent) && !ctx.parent.contains(tree)) { if (requiresBraces(tree, ctx.parent) && !ctx.parent.contains(tree)) {
p"""|{ p"""|{
| $tree | $tree
|}""" |}"""
return } else {
ppPrefix(tree)
ppBody(tree)
ppSuffix(tree)
} }
}
protected def ppPrefix(tree: Tree)(implicit ctx: PrinterContext): Unit = {
if (ctx.opts.printTypes) { if (ctx.opts.printTypes) {
tree match { tree match {
case t: Expr => case t: Expr =>
...@@ -234,9 +233,9 @@ trait Printers { ...@@ -234,9 +233,9 @@ trait Printers {
case BVLShiftRight(l, r) => optP { case BVLShiftRight(l, r) => optP {
p"$l >>> $r" p"$l >>> $r"
} }
case fs@FiniteSet(rs, _) => p"{${rs.distinct}}" case fs @ FiniteSet(rs, _) => p"{${rs.distinct}}"
case fs@FiniteBag(rs, _) => p"{${rs.toMap.toSeq}}" case fs @ FiniteBag(rs, _) => p"{${rs.toMap.toSeq}}"
case fm@FiniteMap(rs, _, _) => p"{${rs.toMap.toSeq}}" case fm @ FiniteMap(rs, _, _) => p"{${rs.toMap.toSeq}}"
case Not(ElementOfSet(e, s)) => p"$e \u2209 $s" case Not(ElementOfSet(e, s)) => p"$e \u2209 $s"
case ElementOfSet(e, s) => p"$e \u2208 $s" case ElementOfSet(e, s) => p"$e \u2208 $s"
case SubsetOf(l, r) => p"$l \u2286 $r" case SubsetOf(l, r) => p"$l \u2286 $r"
...@@ -254,8 +253,8 @@ trait Printers { ...@@ -254,8 +253,8 @@ trait Printers {
case Not(expr) => p"\u00AC$expr" case Not(expr) => p"\u00AC$expr"
case vd@ValDef(id, tpe) => case vd @ ValDef(id, tpe) =>
p"$id : $tpe" p"$id: $tpe"
case (tfd: TypedFunDef) => p"typed def ${tfd.id}[${tfd.tps}]" case (tfd: TypedFunDef) => p"typed def ${tfd.id}[${tfd.tps}]"
case TypeParameterDef(tp) => p"$tp" case TypeParameterDef(tp) => p"$tp"
...@@ -311,20 +310,18 @@ trait Printers { ...@@ -311,20 +310,18 @@ trait Printers {
case fd: FunDef => case fd: FunDef =>
for (an <- fd.flags) { for (an <- fd.flags) {
p"""|@$an p"""|@${an.asString(ctx.opts)}
|""" |"""
} }
p"def ${fd.id}${nary(fd.tparams, ", ", "[", "]")}" p"def ${fd.id}${nary(fd.tparams, ", ", "[", "]")}"
if (fd.params.nonEmpty) { if (fd.params.nonEmpty) {
p"(${fd.params}): " p"(${fd.params})"
} }
p"${fd.returnType} = " p": ${fd.returnType} = "
p"${fd.fullBody}" p"${fd.fullBody}"
case (tree: PrettyPrintable) => tree.printWith(ctx)
case _ => ctx.sb.append("Tree? (" + tree.getClass + ")") case _ => ctx.sb.append("Tree? (" + tree.getClass + ")")
} }
...@@ -359,11 +356,10 @@ trait Printers { ...@@ -359,11 +356,10 @@ trait Printers {
protected def isSimpleExpr(e: Expr): Boolean = e match { protected def isSimpleExpr(e: Expr): Boolean = e match {
case _: Let => false case _: Let => false
case p: PrettyPrintable => p.isSimpleExpr
case _ => true case _ => true
} }
protected def noBracesSub(e: Expr): Seq[Expr] = e match { protected def noBracesSub(e: Tree): Seq[Expr] = e match {
case Let(_, _, bd) => Seq(bd) case Let(_, _, bd) => Seq(bd)
case IfExpr(_, t, e) => Seq(t, e) // if-else always has braces anyway case IfExpr(_, t, e) => Seq(t, e) // if-else always has braces anyway
case _ => Seq() case _ => Seq()
...@@ -371,13 +367,12 @@ trait Printers { ...@@ -371,13 +367,12 @@ trait Printers {
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) => false case (e: Expr, _) if isSimpleExpr(e) => false
case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e => false case (e: Expr, Some(within)) if noBracesSub(within) contains e => false
case (e: Expr, Some(_)) => true case (e: Expr, Some(_)) => true
case _ => false case _ => false
} }
protected def precedence(ex: Expr): Int = ex match { protected def precedence(ex: Expr): Int = ex match {
case (pa: PrettyPrintable) => pa.printPrecedence
// 0: Letters // 0: Letters
case (_: ElementOfSet | _: Modulo) => 0 case (_: ElementOfSet | _: Modulo) => 0
// 1: | // 1: |
...@@ -403,7 +398,6 @@ trait Printers { ...@@ -403,7 +398,6 @@ trait Printers {
} }
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 (_, None) => false case (_, None) => false
case (_, Some( case (_, Some(
_: Definition | _: Let | _: IfExpr | _: ADT | _: Lambda | _: Choose | _: Tuple _: Definition | _: Let | _: IfExpr | _: ADT | _: Lambda | _: Choose | _: Tuple
...@@ -511,22 +505,16 @@ trait Printers { ...@@ -511,22 +505,16 @@ trait Printers {
nary(ts.map(typed)) nary(ts.map(typed))
} }
trait PrettyPrintable {
self: Tree =>
def printWith(implicit pctx: PrinterContext): Unit
def printPrecedence: Int = 1000
def printRequiresParentheses(within: Option[Tree]): Boolean = false
def isSimpleExpr: Boolean = false
}
def prettyPrint(tree: Tree, opts: PrinterOptions = PrinterOptions()): String = { def prettyPrint(tree: Tree, opts: PrinterOptions = PrinterOptions()): String = {
val ctx = PrinterContext(tree, Nil, opts.baseIndent, opts) val ctx = PrinterContext(tree, Nil, opts.baseIndent, opts)
pp(tree)(ctx) pp(tree)(ctx)
ctx.sb.toString ctx.sb.toString
} }
def asString(obj: Any)(implicit opts: PrinterOptions): String = obj match {
case tree: Tree => prettyPrint(tree, opts)
case id: Identifier => if (opts.printUniqueIds) id.uniqueName else id.toString
case _ => obj.toString
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment