168 // memory at the current location to 0. In the compute3 and run3 functions |
168 // memory at the current location to 0. In the compute3 and run3 functions |
169 // below you implement this command by writing the number 0 to mem(mp), |
169 // below you implement this command by writing the number 0 to mem(mp), |
170 // that is write(mem, mp, 0). |
170 // that is write(mem, mp, 0). |
171 // |
171 // |
172 // The easiest way to modify a string in this way is to use the regular |
172 // The easiest way to modify a string in this way is to use the regular |
173 // expression """[^<>+-.\[\]]""", which recognises everything that is |
173 // expression """[^<>+-.\[\]@*#]""", which recognises everything that is |
174 // not a bf-command and replace it by the empty string. Similarly the |
174 // not a bf-command and replace it by the empty string. Similarly the |
175 // regular expression """\[-\]""" finds all occurences of [-] and |
175 // regular expression """\[-\]""" finds all occurences of [-] and |
176 // by using the Scala method .replaceAll you can repplace it with the |
176 // by using the Scala method .replaceAll you can repplace it with the |
177 // string "0" standing for the new bf-command. |
177 // string "0" standing for the new bf-command. |
178 |
178 |
179 def optimise(s: String) : String = |
179 def optimise(s: String) : String = { |
180 s.replaceAll("""[^<>+-.\[\]]""","").replaceAll("""\[-\]""", "0") |
180 s.replaceAll("""[^<>+-.\[\]@*#]""","") |
|
181 .replaceAll("""\[-\]""", "0") |
|
182 } |
181 |
183 |
182 |
184 |
183 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
185 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
184 if (0 <= pc && pc < pg.length) { |
186 if (0 <= pc && pc < pg.length) { |
185 val (new_pc, new_mp, new_mem) = pg(pc) match { |
187 val (new_pc, new_mp, new_mem) = pg(pc) match { |
289 val pg_opt = combine(optimise(pg)) |
292 val pg_opt = combine(optimise(pg)) |
290 compute4(pg_opt, jtable(pg_opt), 0, 0, m) |
293 compute4(pg_opt, jtable(pg_opt), 0, 0, m) |
291 } |
294 } |
292 |
295 |
293 // testcases |
296 // testcases |
|
297 //println(combine(optimise(load_bff("collatz.bf")))) |
|
298 |
294 //combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[.....""" |
299 //combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[.....""" |
295 |
300 |
296 //time_needed(1, run4(load_bff("benchmark.bf"))) |
301 //time_needed(1, run4(load_bff("benchmark.bf"))) |
297 |
302 |
298 //time_needed(1, run(load_bff("sierpinski.bf"))) |
303 //time_needed(1, run(load_bff("sierpinski.bf"))) |