author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 21 Feb 2013 16:07:40 +0000 | |
changeset 193 | 317a2532c567 |
child 194 | fc2a5e9fbb97 |
permissions | -rw-r--r-- |
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 comp1 { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
import lib._ |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
import turing._ |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
import abacus._ |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
// TMs used in the translation |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
(L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
(L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9))) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
(R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
(L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
(R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
(R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
(R, 0), (WBk, 16))) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
val TMGoto = TM(List((Nop, 1), (Nop, 1))) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
def TMFindnth(n: Int) : TM = n match { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case 0 => TM(Nil) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
case n => TMFindnth(n - 1) ++ TM(List((WOc, 2 * n - 1), (R, 2 * n), (R, 2 * n + 1), (R, 2 * n))) |
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 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
def TMMopup(n: Int) = { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
def TMMopup1(n: Int) : TM = n match { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
case 0 => TM(Nil) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
case n => TMMopup1(n - 1) ++ TM(List((R, 2 * n + 1), (WBk, 2 * n), (R, 2 * n - 1), (WOc, 2 * n))) |
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 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
val TMMopup2 = TM(List((R, 2), (R, 1), (L, 5), (WBk, 3), (R, 4), (WBk, 3), |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
(R, 2), (WBk, 3), (L, 5), (L, 6), (R, 0), (L, 6))) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
TMMopup1(n) ++ TMMopup2.shift(2 * n) |
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 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
// Abacus to TM translation |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
def layout(p: AProg) = 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
|
43 |
case Inc(n) => 2 * n + 9 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
case Dec(n, _) => 2 * n + 16 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
case Goto(n) => 1 |
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 start(p: AProg, n: Int) = layout(p).take(n).sum + 1 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
def compile_Inc(s: Int, n: Int) = |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n).shift(s - 1) |
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 compile_Dec(s: Int, n: Int, e: Int) = |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
def compile_Goto(s: Int) = TMGoto.shift(s - 1) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
def compile(p: AProg, s: Int, i: AInst) = i match { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case Inc(n) => compile_Inc(s, n) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case Dec(n, e) => compile_Dec(s, n, start(p, e)) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case Goto(e) => compile_Goto(start(p, e)) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
// component TMs for each instruction |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
def TMs(p: AProg) = { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
val ss = (0 until p.length).map (start(p,_)) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
(ss zip p).map{case (n, i) => compile(p, n, i)} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
def toTM(p: AProg) = TMs(p).reduceLeft(_ ++ _) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |