better printing of register programs in Scala
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 12 Mar 2013 15:03:23 +0000
changeset 222 d682591c63e1
parent 221 18905d086cbb
child 223 db6ba2232945
better printing of register programs in Scala
paper.pdf
scala/abacus.scala
scala/ex.scala
Binary file paper.pdf has changed
--- a/scala/abacus.scala	Sun Mar 10 07:10:50 2013 +0000
+++ b/scala/abacus.scala	Tue Mar 12 15:03:23 2013 +0000
@@ -4,10 +4,18 @@
 import lib._
 
 // Abacus instructions
-sealed abstract class AInst 
-case class Inc(n: Int) extends AInst
-case class Dec(n: Int, l: Int) extends AInst
-case class Goto(l: Int) extends AInst
+sealed abstract class AInst {
+  def print : String
+} 
+case class Inc(n: Int) extends AInst {
+  override def print = "Inc(" + n.toString + ")\n"
+}
+case class Dec(n: Int, l: Int) extends AInst {
+  override def print = "Dec(" + n.toString + "," +  l.toString + ")\n"
+}
+case class Goto(l: Int) extends AInst {
+  override def print = "Goto(" + l.toString + ")\n"
+}
 
 type AProg = List[AInst]
 type Regs = Map[Int, Int]
@@ -17,6 +25,7 @@
 
 // Abacus machines
 case class Abacus(p: AProg) {
+  def print = p.foldLeft[String]("")(_ + _.print)
 
   //simple composition
   def ++ (that: Abacus) = Abacus(p ::: that.p)
--- a/scala/ex.scala	Sun Mar 10 07:10:50 2013 +0000
+++ b/scala/ex.scala	Tue Mar 12 15:03:23 2013 +0000
@@ -5,6 +5,8 @@
 import comp1._
 import comp2._
 
+print(compile_rec(S)._1.print)
+
 // Turing machine examples
 val TMCopy = TM((WBk, 5), (R, 2), (R, 3), (R, 2), (WOc, 3), 
                 (L, 4), (L, 4), (L, 5), (R, 11), (R, 6), 
@@ -94,7 +96,7 @@
   val res = (abc_f.run(abc_map))(arity)
   val end = System.nanoTime()
   val time = (end - start)/1.0e9
-  ("Result: " + res + "  length: " + abc_f.p.length + " time: " + "%.5f".format(time))
+  ("Result: " + res + "  length: " + abc_f.p.length + " time: " + "%.5f".format(time) + "\n Prog:" + abc_f)
 }
 
 println("S(3)          " + test_comp2(S, 3))