1 // Part 2 about a "Compiler" for the Brainf*** language |
1 // Part 2 about a "Compiler" for the Brainf*** language |
2 //====================================================== |
2 //====================================================== |
3 |
3 |
4 object CW10b { |
4 object M5b { |
5 |
5 |
6 // !!! Copy any function you need from file bf.scala !!! |
6 // !!! Copy any function you need from file bf.scala !!! |
7 // |
7 // |
8 // If you need any auxiliary function, feel free to |
8 // If you need any auxiliary function, feel free to |
9 // implement it, but do not make any changes to the |
9 // implement it, but do not make any changes to the |
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("""[^<>+-.\[\]@*#]""","") |
180 s.replaceAll("""[^<>+-.\[\]]""","") |
181 .replaceAll("""\[-\]""", "0") |
181 .replaceAll("""\[-\]""", "0") |
182 } |
182 } |
183 |
183 |
184 |
184 |
185 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 = { |
210 |
210 |
211 // testcases |
211 // testcases |
212 |
212 |
213 //println(optimise(load_bff("collatz.bf"))) |
213 //println(optimise(load_bff("collatz.bf"))) |
214 //optimise(load_bff("benchmark.bf")) // should have inserted 0's |
214 //optimise(load_bff("benchmark.bf")) // should have inserted 0's |
215 //optimise(load_bff("mandelbrot.bf")).length // => 11203 |
215 //optimise(load_bff("mandelbrot.bf")).length // => 11205 |
216 |
216 |
217 //time_needed(1, run3(load_bff("benchmark.bf"))) |
217 //time_needed(1, run3(load_bff("benchmark.bf"))) |
|
218 //time_needed(1, run3(load_bff("mandelbrot.bf"))) |
218 |
219 |
219 |
220 |
220 |
221 |
221 // (7) Write a function combine which replaces sequences |
222 // (7) Write a function combine which replaces sequences |
222 // of repated increment and decrement commands by appropriate |
223 // of repated increment and decrement commands by appropriate |