Skip to content

Instantly share code, notes, and snippets.

@okram
Created February 25, 2020 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okram/34ccc2bd7f5618900def8b8c5152706a to your computer and use it in GitHub Desktop.
Save okram/34ccc2bd7f5618900def8b8c5152706a to your computer and use it in GitHub Desktop.
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()
}
}
@okram
Copy link
Author

okram commented Feb 25, 2020

int{0,10}<=int{10}[plus,2][mult,int[plus,4]][is,bool<=int[gt,20]]

instruction              domain         range       state
----------------------------------------------------------
[plus,2]                 int{10}    =>  int{10}      < >
[mult,int[plus,4]]       int{10}    =>  int{10}      < >
  [plus,4]                 int      =>    int        < >
[is,bool<=int[gt,20]]    int{10}    =>  int{0,10}    < >
  [gt,20]                  int      =>    bool       < >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment