Skip to content
Snippets Groups Projects
Commit e4d4b51a authored by Mikaël Mayer's avatar Mikaël Mayer
Browse files

Merge pull request #177 from MikaelMayer/hole-render-correct

If holes are used but `leon.lang.synthesis.???` is not in scope,
parents 09fa5275 8b2c1c9c
No related branches found
No related tags found
No related merge requests found
...@@ -93,6 +93,29 @@ object DefOps { ...@@ -93,6 +93,29 @@ object DefOps {
} }
} }
private def stripPrefix(off: List[String], from: List[String]): List[String] = {
val commonPrefix = (off zip from).takeWhile(p => p._1 == p._2)
val res = off.drop(commonPrefix.size)
if (res.isEmpty) {
if (off.isEmpty) List()
else List(off.last)
} else {
res
}
}
def simplifyPath(namesOf: List[String], from: Definition, useUniqueIds: Boolean)(implicit pgm: Program) = {
val pathFrom = pathFromRoot(from).dropWhile(_.isInstanceOf[Program])
val namesFrom = pathToNames(pathFrom, useUniqueIds)
val names: Set[List[String]] = Set(namesOf, stripPrefix(namesOf, namesFrom)) ++
getNameUnderImports(pathFrom, namesOf)
names.toSeq.minBy(_.size).mkString(".")
}
def fullNameFrom(of: Definition, from: Definition, useUniqueIds: Boolean)(implicit pgm: Program): String = { def fullNameFrom(of: Definition, from: Definition, useUniqueIds: Boolean)(implicit pgm: Program): String = {
val pathFrom = pathFromRoot(from).dropWhile(_.isInstanceOf[Program]) val pathFrom = pathFromRoot(from).dropWhile(_.isInstanceOf[Program])
...@@ -100,30 +123,22 @@ object DefOps { ...@@ -100,30 +123,22 @@ object DefOps {
val namesFrom = pathToNames(pathFrom, useUniqueIds) val namesFrom = pathToNames(pathFrom, useUniqueIds)
val namesOf = pathToNames(pathFromRoot(of), useUniqueIds) val namesOf = pathToNames(pathFromRoot(of), useUniqueIds)
def stripPrefix(off: List[String], from: List[String]) = {
val commonPrefix = (off zip from).takeWhile(p => p._1 == p._2)
val res = off.drop(commonPrefix.size)
if (res.isEmpty) {
if (off.isEmpty) List()
else List(off.last)
} else {
res
}
}
val sp = stripPrefix(namesOf, namesFrom) val sp = stripPrefix(namesOf, namesFrom)
if (sp.isEmpty) return "**** " + of.id.uniqueName if (sp.isEmpty) return "**** " + of.id.uniqueName
var names: Set[List[String]] = Set(namesOf, stripPrefix(namesOf, namesFrom)) val names: Set[List[String]] =
Set(namesOf, stripPrefix(namesOf, namesFrom)) ++ getNameUnderImports(pathFrom, namesOf)
pathFrom match { names.toSeq.minBy(_.size).mkString(".")
}
private def getNameUnderImports(pathFrom: List[Definition], namesOf: List[String]): Seq[List[String]] = {
(pathFrom match {
case (u: UnitDef) :: _ => case (u: UnitDef) :: _ =>
val imports = u.imports.map { val imports = u.imports.map {
case Import(path, true) => path case Import(path, true) => path
case Import(path, false) => path.init case Import(path, false) => path.init
}.toList }.toList
def stripImport(of: List[String], imp: List[String]): Option[List[String]] = { def stripImport(of: List[String], imp: List[String]): Option[List[String]] = {
if (of.startsWith(imp)) { if (of.startsWith(imp)) {
Some(stripPrefix(of, imp)) Some(stripPrefix(of, imp))
...@@ -131,15 +146,13 @@ object DefOps { ...@@ -131,15 +146,13 @@ object DefOps {
None None
} }
} }
for (imp <- imports) { for {imp <- imports
names ++= stripImport(namesOf, imp) strippedImport <- stripImport(namesOf, imp)
} } yield strippedImport
case _ => case _ =>
} Nil
})
names.toSeq.minBy(_.size).mkString(".")
} }
def pathToNames(path: List[Definition], useUniqueIds: Boolean): List[String] = { def pathToNames(path: List[Definition], useUniqueIds: Boolean): List[String] = {
......
...@@ -42,9 +42,12 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -42,9 +42,12 @@ class PrettyPrinter(opts: PrinterOptions,
body body
} }
} }
protected def getScope(implicit ctx: PrinterContext) =
ctx.parents.collectFirst { case (d: Definition) if !d.isInstanceOf[ValDef] => d }
protected def printNameWithPath(df: Definition)(implicit ctx: PrinterContext) { protected def printNameWithPath(df: Definition)(implicit ctx: PrinterContext) {
(opgm, ctx.parents.collectFirst { case (d: Definition) if !d.isInstanceOf[ValDef] => d }) match { (opgm, getScope) 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))
...@@ -133,7 +136,11 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -133,7 +136,11 @@ class PrettyPrinter(opts: PrinterOptions,
case h @ Hole(tpe, es) => case h @ Hole(tpe, es) =>
if (es.isEmpty) { if (es.isEmpty) {
p"???[$tpe]" val hole = (for{scope <- getScope
program <- opgm }
yield simplifyPath("leon" :: "lang" :: "synthesis" :: "???" :: Nil, scope, false)(program))
.getOrElse("leon.lang.synthesis.???")
p"$hole[$tpe]"
} else { } else {
p"?($es)" p"?($es)"
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment