2 |
2 |
3 import scala.annotation.tailrec |
3 import scala.annotation.tailrec |
4 import lib._ |
4 import lib._ |
5 |
5 |
6 // Abacus instructions |
6 // Abacus instructions |
7 sealed abstract class AInst |
7 sealed abstract class AInst { |
8 case class Inc(n: Int) extends AInst |
8 def print : String |
9 case class Dec(n: Int, l: Int) extends AInst |
9 } |
10 case class Goto(l: Int) extends AInst |
10 case class Inc(n: Int) extends AInst { |
|
11 override def print = "Inc(" + n.toString + ")\n" |
|
12 } |
|
13 case class Dec(n: Int, l: Int) extends AInst { |
|
14 override def print = "Dec(" + n.toString + "," + l.toString + ")\n" |
|
15 } |
|
16 case class Goto(l: Int) extends AInst { |
|
17 override def print = "Goto(" + l.toString + ")\n" |
|
18 } |
11 |
19 |
12 type AProg = List[AInst] |
20 type AProg = List[AInst] |
13 type Regs = Map[Int, Int] |
21 type Regs = Map[Int, Int] |
14 |
22 |
15 // Abacus configurations |
23 // Abacus configurations |
16 case class AConfig(s: Int, regs: Regs) |
24 case class AConfig(s: Int, regs: Regs) |
17 |
25 |
18 // Abacus machines |
26 // Abacus machines |
19 case class Abacus(p: AProg) { |
27 case class Abacus(p: AProg) { |
|
28 def print = p.foldLeft[String]("")(_ + _.print) |
20 |
29 |
21 //simple composition |
30 //simple composition |
22 def ++ (that: Abacus) = Abacus(p ::: that.p) |
31 def ++ (that: Abacus) = Abacus(p ::: that.p) |
23 |
32 |
24 def :+ (that: Abacus) = this ++ that.shift(p.length, -1) |
33 def :+ (that: Abacus) = this ++ that.shift(p.length, -1) |