--- 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)