218 println("Expo: 3 ^ 4 " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0)))) |
217 println("Expo: 3 ^ 4 " + (Expo(0, 1, 2, 3, 4, -1).run(Map(0 -> 4, 1 -> 3, 2 -> 0, 3 -> 0, 4 -> 0)))) |
219 |
218 |
220 |
219 |
221 |
220 |
222 // Abacus to TM translation |
221 // Abacus to TM translation |
223 type Layout = List[Int] |
|
224 |
|
225 def layout(p: AProg) = p.map(_ match { |
222 def layout(p: AProg) = p.map(_ match { |
226 case Inc(n) => 2 * n + 9 |
223 case Inc(n) => 2 * n + 9 |
227 case Dec(n, _) => 2 * n + 16 |
224 case Dec(n, _) => 2 * n + 16 |
228 case Goto(n) => 1 |
225 case Goto(n) => 1 |
229 }) |
226 }) |
230 |
227 |
231 def start(ly: Layout, n: Int) = ly.take(n).sum + 1 |
228 def start(p: AProg, n: Int) = layout(p).take(n).sum + 1 |
232 |
229 |
233 val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4), |
230 def compile_Inc(s: Int, n: Int) = { |
234 (L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6), |
231 val TMInc = TM(List((WOc, 1), (R, 2), (WOc, 3), (R, 2), (WOc, 3), (R, 4), |
235 (L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9))) |
232 (L, 7), (WBk, 5), (R, 6), (WBk, 5), (WOc, 3), (R, 6), |
236 |
233 (L, 8), (L, 7), (R, 9), (L, 7), (R, 10), (WBk, 9))) |
237 val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3), |
234 TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n).shift(s - 1) |
238 (R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8), |
235 } |
239 (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
236 |
240 (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
237 def compile_Dec(s: Int, n: Int, e: Int) = { |
241 (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
238 val TMDec = TM(List((WOc, 1), (R, 2), (L, 14), (R, 3), (L, 4), (R, 3), |
242 (R, 0), (WBk, 16))) |
239 (R, 5), (WBk, 4), (R, 6), (WBk, 5), (L, 7), (L, 8), |
243 |
240 (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
244 def TMINC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1) |
241 (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
245 |
242 (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
246 def TMDEC(s: Int, n: Int, e: Int) = TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e) |
243 (R, 0), (WBk, 16))) |
247 |
244 TMFindnth(n).shift(s - 1) ++ TMDec.shift(2 * n).shift(s - 1).adjust(e) |
248 def TMGOTO(n: Int) = TM(List((Nop, n), (Nop, n))) |
245 } |
249 |
246 |
250 def compile(ly: Layout, s: Int, i: AInst) = i match { |
247 def compile_Goto(s: Int) = { |
251 case Inc(n) => TMINC(s, n) |
248 val TMGoto = TM(List((Nop, 1), (Nop, 1))) |
252 case Dec(n, e) => TMDEC(s, n, start(ly, e)) |
249 TMGoto.shift(s - 1) |
253 case Goto(e) => TMGOTO(start(ly, e)) |
250 } |
|
251 |
|
252 def compile(p: AProg, s: Int, i: AInst) = i match { |
|
253 case Inc(n) => compile_Inc(s, n) |
|
254 case Dec(n, e) => compile_Dec(s, n, start(p, e)) |
|
255 case Goto(e) => compile_Goto(start(p, e)) |
254 } |
256 } |
255 |
257 |
256 def tms(p: AProg) = { |
258 def tms(p: AProg) = { |
257 val ss = (0 until p.length).map (start(layout(p),_)) |
259 val ss = (0 until p.length).map (start(p,_)) |
258 (ss zip p).map{case (n, i) => compile(layout(p), n, i)} |
260 (ss zip p).map{case (n, i) => compile(p, n, i)} |
259 } |
261 } |
260 |
262 |
261 def toTM(p: AProg) = tms(p).reduceLeft(_ ++ _) |
263 def toTM(p: AProg) = tms(p).reduceLeft(_ ++ _) |
262 |
264 |
263 //examples |
265 //examples |