author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 25 Feb 2013 15:12:48 +0000 | |
changeset 195 | f06aa4e1c25b |
parent 194 | fc2a5e9fbb97 |
child 207 | b93ec66cf4bb |
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 |
|
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
3 |
// Abacus to TM translation |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
4 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
import lib._ |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
import turing._ |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
import abacus._ |
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 |
// 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
|
10 |
|
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
11 |
val TMInc = TM((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
12 |
(L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
13 |
(L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9)) |
193
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 |
val TMDec = TM((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
16 |
(R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
17 |
(L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
18 |
(R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
19 |
(R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
20 |
(R, 0), (WBk, 16)) |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
22 |
val TMGoto = TM((Nop, 1), (Nop, 1)) |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
23 |
|
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
24 |
val TMMopup_aux = TM((R, 2), (R, 1), (L, 5), (WBk, 3), (R, 4), (WBk, 3), |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
25 |
(R, 2), (WBk, 3), (L, 5), (L, 6), (R, 0), (L, 6)) |
193
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 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
|
28 |
case 0 => TM(Nil) |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
29 |
case n => TMFindnth(n - 1) ++ TM((WOc, 2 * n - 1), (R, 2 * n), (R, 2 * n + 1), (R, 2 * n)) |
193
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 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
|
33 |
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
|
34 |
case 0 => TM(Nil) |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
35 |
case n => TMMopup1(n - 1) ++ TM((R, 2 * n + 1), (WBk, 2 * n), (R, 2 * n - 1), (WOc, 2 * n)) |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
37 |
TMMopup1(n) ++ TMMopup_aux.shift(2 * n) |
193
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 |
|
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
41 |
// start address of the nth instruction |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
42 |
def address(p: AProg, n: Int) = { |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
43 |
def layout(p: AProg) = p.map(_ match { |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
44 |
case Inc(n) => 2 * n + 9 |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
45 |
case Dec(n, _) => 2 * n + 16 |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
46 |
case Goto(n) => 1 |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
47 |
}) |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
48 |
layout(p).take(n).sum + 1 |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
49 |
} |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
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
|
58 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
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
|
60 |
case Inc(n) => compile_Inc(s, n) |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
61 |
case Dec(n, e) => compile_Dec(s, n, address(p, e)) |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
62 |
case Goto(e) => compile_Goto(address(p, e)) |
193
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 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
// component TMs for each instruction |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
66 |
def TMs(p: AProg) = |
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
67 |
p.zipWithIndex.map{case (i, n) => compile(p, address(p, n), i)} |
193
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 |
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
|
70 |
|
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 |