scala/abacus.scala
changeset 195 f06aa4e1c25b
parent 194 fc2a5e9fbb97
child 208 3267acc1f97f
equal deleted inserted replaced
194:fc2a5e9fbb97 195:f06aa4e1c25b
    16 
    16 
    17 // Abacus machines
    17 // Abacus machines
    18 case class Abacus(p: AProg) {
    18 case class Abacus(p: AProg) {
    19 
    19 
    20   //simple composition
    20   //simple composition
    21   def ++ (that: Abacus) = Abacus(this.p ::: that.p)
    21   def ++ (that: Abacus) = Abacus(p ::: that.p)
       
    22 
       
    23   def :+ (that: Abacus) = this ++ that.shift(p.length, -1)
    22 
    24 
    23   def shift(offset: Int, jump: Int) = Abacus(p.map(_ match {
    25   def shift(offset: Int, jump: Int) = Abacus(p.map(_ match {
    24     case Inc(n) => Inc(n)
    26     case Inc(n) => Inc(n)
    25     case Dec(n, l) => if (l == jump) Dec(n, l) else Dec(n, l + offset)
    27     case Dec(n, l) => if (l == jump) Dec(n, l) else Dec(n, l + offset)
    26     case Goto(l) => if (l == jump) Goto(l) else Goto(l + offset)
    28     case Goto(l) => if (l == jump) Goto(l) else Goto(l + offset)
    54   }
    56   }
    55  
    57  
    56   def run(regs: Regs): Regs = run(AConfig(0, regs)).regs
    58   def run(regs: Regs): Regs = run(AConfig(0, regs)).regs
    57 }
    59 }
    58 
    60 
       
    61 // some syntactical convenience
       
    62 object Abacus {
       
    63   def apply(is: AInst*) : Abacus = Abacus(is.toList)
    59 }
    64 }
    60 
    65 
       
    66 
       
    67 }
       
    68