76 |
76 |
77 def ++ (that: TM) = TM(this.p ::: that.p) |
77 def ++ (that: TM) = TM(this.p ::: that.p) |
78 |
78 |
79 def shift(n: Int) = |
79 def shift(n: Int) = |
80 TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)}) |
80 TM(p.map{case (a, s) => (a, if (s == 0) 0 else s + n)}) |
|
81 def adjust(n: Int) : TM = |
|
82 TM(p.map{case (a, s) => (a, if (s == 0) n else s)}) |
|
83 def adjust : TM = adjust(p.length / 2 + 1) |
81 |
84 |
82 def fetch(s: State, a: Cell) = (s, a) match { |
85 def fetch(s: State, a: Cell) = (s, a) match { |
83 case (0, _) => (Nop, 0) |
86 case (0, _) => (Nop, 0) |
84 case (s, Bk) => nth_of(p, 2 * (s - 1)) match { |
87 case (s, Bk) => nth_of(p, 2 * (s - 1)) match { |
85 case None => (Nop, 0) |
88 case None => (Nop, 0) |
165 |
168 |
166 case class AConfig(s: Int, regs: Regs) |
169 case class AConfig(s: Int, regs: Regs) |
167 |
170 |
168 case class Abacus(p: AProg) { |
171 case class Abacus(p: AProg) { |
169 |
172 |
|
173 def ++ (that: Abacus) = Abacus(this.p ::: that.p) |
|
174 |
170 def shift(offset: Int, jump: Int) = Abacus(p.map(ashift(_, offset, jump))) |
175 def shift(offset: Int, jump: Int) = Abacus(p.map(ashift(_, offset, jump))) |
171 def adjust(old_jump: Int, jump: Int) = Abacus(p.map(aadjust(_, old_jump, jump))) |
176 def adjust(old_jump: Int, jump: Int) = Abacus(p.map(aadjust(_, old_jump, jump))) |
172 |
177 |
173 //def toTM() |
|
174 |
|
175 def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match { |
178 def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match { |
176 case (None, _) => cf |
179 case (None, _) => cf |
177 case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1))) |
180 case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1))) |
178 case (Some(Dec(n, l)), s) => { |
181 case (Some(Dec(n, l)), s) => { |
179 if (dget(cf.regs, n) == 0) AConfig(l, cf.regs) |
182 if (dget(cf.regs, n) == 0) AConfig(l, cf.regs) |
199 // Abacus examples |
202 // Abacus examples |
200 def Copy(in: Int, out: Int, jump: Int) = |
203 def Copy(in: Int, out: Int, jump: Int) = |
201 Abacus(List(Dec(in, jump), Inc(out), Goto(0))) |
204 Abacus(List(Dec(in, jump), Inc(out), Goto(0))) |
202 |
205 |
203 def Plus(m: Int, n: Int, tmp: Int, jump: Int) = |
206 def Plus(m: Int, n: Int, tmp: Int, jump: Int) = |
204 Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), |
207 Abacus(List(Dec(m, 4), Inc(n), Inc(tmp), Goto(0), Dec(tmp, jump), Inc(m), Goto(4))) |
205 Goto(0), Dec(tmp, jump), Inc(m), Goto(4))) |
208 |
206 |
209 def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = |
207 def Mult(in1: Int, in2: Int, out: Int, tmp: Int, jump: Int) = { |
210 Abacus(List(Dec(in1, jump))) ++ Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0) |
208 val tm = Plus(in2, out, tmp, -1).shift(1, -1).adjust(-1, 0) |
|
209 Abacus(Dec(in1, jump) :: tm.p) |
|
210 } |
|
211 |
211 |
212 def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = { |
212 def Expo(in1: Int, in2: Int, out: Int, tmp1: Int, tmp2: Int, jump: Int) = { |
213 val tm1 = Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) |
213 Abacus(List(Inc(out), Dec(in1, jump))) ++ |
214 val tm2 = Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1) |
214 Mult(out, in2, tmp2, tmp1, -1).shift(2, -1).adjust(-1, 10) ++ |
215 Abacus(Inc(out) :: Dec(in1, jump) :: tm1.p ::: tm2.p) |
215 Copy(tmp2, out, -1).shift(10, -1). adjust(-1, 1) |
216 } |
216 } |
217 |
217 |
218 |
218 |
219 println("Copy: " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0)))) |
219 println("Copy: " + (Copy(0, 1, -1).run(Map(0 -> 3, 1 -> 0)))) |
220 println("Plus: 3 + 4 " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0)))) |
220 println("Plus: 3 + 4 " + (Plus(0, 1, 2, -1).run(Map(0 -> 3, 1 -> 4, 2 -> 0)))) |
232 (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
232 (L, 11), (WBk, 7), (WOc, 8), (R, 9), (L, 10), (R, 9), |
233 (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
233 (R, 5), (WBk, 10), (L, 12), (L, 11), (R, 13), (L, 11), |
234 (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
234 (R, 17), (WBk, 13), (L, 15), (L, 14), (R, 16), (L, 14), |
235 (R, 0), (WBk, 16))) |
235 (R, 0), (WBk, 16))) |
236 |
236 |
237 def TMINC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1) |
237 def TMINC(s: Int, n: Int, e: Int) = (TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n)).shift(s - 1).adjust(e) |
238 |
238 |
239 def TMDEC(s: Int, n: Int) = (TMFindnth(n) ++ TMInc.shift(2 * n)).shift(s - 1) |
239 def TMDEC(s: Int, n: Int, e: Int) = (TMFindnth(n).shift(s - 1) ++ TMInc.shift(2 * n)).shift(s - 1).adjust(e) |
240 |
240 |
241 |
241 //def toTM() |
242 |
242 |
243 |
243 |
244 //Recursive Functions |
244 //Recursive Functions |
245 abstract class Rec { |
245 abstract class Rec { |
246 def eval(ns: List[Int]) : Int |
246 def eval(ns: List[Int]) : Int |