Created
February 25, 2020 16:06
-
-
Save okram/34ccc2bd7f5618900def8b8c5152706a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TypeUtil { | |
private type Row = (Int,Inst,OType,OType,Map[String,Obj]) | |
def explain(atype:OType,depth:Int = 0):List[Row] ={ | |
val report = atype.insts().foldLeft(List[Row]())((a,b) => { | |
val temp = a :+ (depth,b._2,b._1.domain(),b._2.apply(b._1).asInstanceOf[OType].range(),Map.empty[String,Obj]) | |
val inner = b._2.args().foldLeft(List[Row]())((x,y) => x ++ (y match { | |
case btype:OType => explain(btype,depth + 1) | |
case _ => Nil | |
})) | |
temp ++ inner | |
}) | |
report | |
} | |
def printTable(atype:OType,report:List[Row]):String ={ | |
val c1 = report.map(x => x._2.toString.length).max + 4 | |
val c2 = report.map(x => x._3.toString.length).max + 4 | |
val c3 = report.map(x => x._4.toString.length).max + 4 | |
val builder:StringBuilder = new StringBuilder() | |
builder | |
.append("instruction").append(" ".repeat(Math.abs(11 - c1))) | |
.append("domain").append(" ".repeat(Math.abs(6 - c2))) | |
.append(" ".repeat(4)) | |
.append("range").append(" ".repeat(Math.abs(6 - c3))) | |
.append("state").append("\n") | |
builder.append("-".repeat(builder.length)).append("\n") | |
report.foldLeft(builder)((a,b) => a | |
.append(" ".repeat(b._1)) | |
.append(b._2).append(" ".repeat(Math.abs(c1 - (b._2.toString.length)))) | |
.append(b._3).append(" ".repeat(Math.abs(c2 - (b._3.toString.length) - (2 * b._1)))) | |
.append("=>").append(" ".repeat(2)).append(" ".repeat(2 * b._1)) | |
.append(b._4).append(" ".repeat(Math.abs(c3 - (b._4.toString.length) - (2 * b._1)))) | |
.append(b._5.foldLeft("< ")((x,y) => x + y) + ">").append("\n")) | |
atype.toString + "\n\n" + builder.toString() | |
} | |
} |
Author
okram
commented
Feb 25, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment