scala/abacus.scala
changeset 221 18905d086cbb
parent 208 3267acc1f97f
child 222 d682591c63e1
equal deleted inserted replaced
220:262fc2c6371b 221:18905d086cbb
    33     case Inc(n) => Inc(n)
    33     case Inc(n) => Inc(n)
    34     case Dec(n, l) => if (l == old_jump) Dec(n, jump) else Dec(n, l)
    34     case Dec(n, l) => if (l == old_jump) Dec(n, jump) else Dec(n, l)
    35     case Goto(l) => if (l == old_jump) Goto(jump) else Goto(l)
    35     case Goto(l) => if (l == old_jump) Goto(jump) else Goto(l)
    36   }))
    36   }))
    37 
    37 
    38   def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match {
    38   def step(cf: AConfig) : AConfig = nth_of(p, cf.s) match {
    39     case (None, _) => cf
    39     case None => cf
    40     case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
    40     case Some(Inc(n)) => AConfig(cf.s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
    41     case (Some(Dec(n, l)), s) => {
    41     case Some(Dec(n, l)) => {
    42       if (dget(cf.regs, n) == 0) AConfig(l, cf.regs)
    42       if (dget(cf.regs, n) == 0) AConfig(l, cf.regs)
    43       else AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
    43       else AConfig(cf.s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
    44     }
    44     }
    45     case (Some(Goto(l)), _) => AConfig(l, cf.regs)
    45     case Some(Goto(l)) => AConfig(l, cf.regs)
    46   }  
    46   } 
    47 
    47   
    48   def steps(cf: AConfig, n: Int) : AConfig = n match {
    48   def steps(cf: AConfig, n: Int) : AConfig = n match {
    49     case 0 => cf
    49     case 0 => cf
    50     case n => steps(step(cf), n - 1)
    50     case n => steps(step(cf), n - 1)
    51   } 
    51   } 
    52 
    52