scala/abacus.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 01 Mar 2013 23:46:02 +0000
changeset 208 3267acc1f97f
parent 195 f06aa4e1c25b
child 221 18905d086cbb
permissions -rw-r--r--
added examples for the rec to abacus compilation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
package object abacus {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
208
3267acc1f97f added examples for the rec to abacus compilation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 195
diff changeset
     3
import scala.annotation.tailrec
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import lib._
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
     6
// Abacus instructions
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
sealed abstract class AInst 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class Inc(n: Int) extends AInst
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class Dec(n: Int, l: Int) extends AInst
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class Goto(l: Int) extends AInst
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
type AProg = List[AInst]
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
type Regs = Map[Int, Int]
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    15
// Abacus configurations
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
case class AConfig(s: Int, regs: Regs)  
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    18
// Abacus machines
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
case class Abacus(p: AProg) {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
194
fc2a5e9fbb97 updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 193
diff changeset
    21
  //simple composition
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    22
  def ++ (that: Abacus) = Abacus(p ::: that.p)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    23
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    24
  def :+ (that: Abacus) = this ++ that.shift(p.length, -1)
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def shift(offset: Int, jump: Int) = Abacus(p.map(_ match {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    case Inc(n) => Inc(n)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    case Dec(n, l) => if (l == jump) Dec(n, l) else Dec(n, l + offset)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    case Goto(l) => if (l == jump) Goto(l) else Goto(l + offset)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  }))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
      
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def adjust(old_jump: Int, jump: Int) = Abacus(p.map(_ match {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    case Inc(n) => Inc(n)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    case Dec(n, l) => if (l == old_jump) Dec(n, jump) else Dec(n, l)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    case Goto(l) => if (l == old_jump) Goto(jump) else Goto(l)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  }))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    case (None, _) => cf
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    case (Some(Dec(n, l)), s) => {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
      if (dget(cf.regs, n) == 0) AConfig(l, cf.regs)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
      else AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
    }
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
    case (Some(Goto(l)), _) => AConfig(l, cf.regs)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  }  
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  def steps(cf: AConfig, n: Int) : AConfig = n match {
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    case 0 => cf
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    case n => steps(step(cf), n - 1)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  } 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  def steps(regs: Regs, n: Int) : AConfig = steps(AConfig(0, regs), n)
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
208
3267acc1f97f added examples for the rec to abacus compilation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 195
diff changeset
    55
  @tailrec
3267acc1f97f added examples for the rec to abacus compilation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 195
diff changeset
    56
  final def run(cf: AConfig) : AConfig = {
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
    if (cf.s >= p.length || cf.s < 0) cf else run(step(cf))
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  }
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
 
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  def run(regs: Regs): Regs = run(AConfig(0, regs)).regs
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
}
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    63
// some syntactical convenience
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    64
object Abacus {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    65
  def apply(is: AInst*) : Abacus = Abacus(is.toList)
193
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
}
317a2532c567 split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    69
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
    70