--- a/turing.scala Thu Feb 21 00:34:41 2013 +0000
+++ b/turing.scala Thu Feb 21 05:33:57 2013 +0000
@@ -78,6 +78,9 @@
def shift(n: Int) =
TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)})
+ def adjust(n: Int) : TM =
+ TM(p.map{case (a, s) => (a, if (s == 0) n else s)})
+ def adjust : TM = adjust(p.length / 2 + 1)
def fetch(s: State, a: Cell) = (s, a) match {
case (0, _) => (Nop, 0)
@@ -167,11 +170,11 @@
case class Abacus(p: AProg) {
+ def ++ (that: Abacus) = Abacus(this.p ::: that.p)
+
def shift(offset: Int, jump: Int) = Abacus(p.map(ashift(_, offset, jump)))
def adjust(old_jump: Int, jump: Int) = Abacus(p.map(aadjust(_, old_jump, jump)))
- //def toTM()
-
def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match {
case (None, _) => cf
case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
@@ -201,18 +204,15 @@
Abacus(List(Dec(in, jump), Inc(out), Goto(0)))
def Plus(m: Int, n: Int, tmp: Int, jump: Int) =
- Abacus(List(Dec(m, 4), Inc(n), Inc(tmp),
- Goto(0), Dec(tmp, jump), Inc(m), Goto(4)))
+ Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4)))
-def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = {
- val tm = Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0)
- Abacus(Dec(in1, jump) :: tm.p)
-}
+def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) =
+ Abacus(List(Dec(in1, jump))) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0)
def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = {
- val tm1 = Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10)
- val tm2 = Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1)
- Abacus(Inc(out) :: Dec(in1, jump) :: tm1.p ::: tm2.p)
+ Abacus(List(Inc(out), Dec(in1, jump))) ++
+ Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++
+ Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1)
}
@@ -234,11 +234,11 @@
(R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14),
(R, 0), (WBk, 16)))
-def TMINC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1)
+def TMINC(s: Int, n: Int, e: Int) = (TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n)).shift(s - 1).adjust(e)
-def TMDEC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1)
+def TMDEC(s: Int, n: Int, e: Int) = (TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n)).shift(s - 1).adjust(e)
-
+//def toTM()
//Recursive Functions