1 // Main Part 5 about a "Compiler" for the Brainf*** language |
1 // Part 2 about a "Compiler" for the Brainf*** language |
2 //============================================================ |
2 //====================================================== |
3 |
|
4 |
3 |
5 object M5b { |
4 object M5b { |
6 |
5 |
7 // !!! Copy any function you need from file bf.scala !!! |
6 // !!! Copy any function you need from file bf.scala !!! |
8 // |
7 // |
9 // If you need any auxiliary function, feel free to |
8 // If you need any auxiliary function, feel free to |
10 // implement it, but do not make any changes to the |
9 // implement it, but do not make any changes to the |
11 // templates below. |
10 // templates below. |
12 |
11 |
13 |
12 |
14 // DEBUGGING INFORMATION FOR COMPILERS!!! |
|
15 // |
|
16 // Compiler, even real ones, are fiendishly difficult to get |
|
17 // to produce correct code. One way to debug them is to run |
|
18 // example programs ``unoptimised''; and then optimised. Does |
|
19 // the optimised version still produce the same result? |
|
20 |
|
21 |
|
22 // for timing purposes |
|
23 def time_needed[T](n: Int, code: => T) = { |
13 def time_needed[T](n: Int, code: => T) = { |
24 val start = System.nanoTime() |
14 val start = System.nanoTime() |
25 for (i <- 0 until n) code |
15 for (i <- 0 until n) code |
26 val end = System.nanoTime() |
16 val end = System.nanoTime() |
27 (end - start)/(n * 1.0e9) |
17 (end - start)/(n * 1.0e9) |
28 } |
18 } |
29 |
19 |
30 |
|
31 type Mem = Map[Int, Int] |
20 type Mem = Map[Int, Int] |
|
21 |
32 |
22 |
33 import io.Source |
23 import io.Source |
34 import scala.util._ |
24 import scala.util._ |
35 |
25 |
36 // ADD YOUR CODE BELOW |
26 def load_bff(name: String) : String = |
37 //====================== |
27 Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("") |
38 |
28 |
39 // (6) |
29 def sread(mem: Mem, mp: Int) : Int = |
|
30 mem.getOrElse(mp, 0) |
|
31 |
|
32 def write(mem: Mem, mp: Int, v: Int) : Mem = |
|
33 mem.updated(mp, v) |
|
34 |
40 def jumpRight(prog: String, pc: Int, level: Int) : Int = { |
35 def jumpRight(prog: String, pc: Int, level: Int) : Int = { |
41 if (pc >= prog.length) prog.length |
36 if (prog.length <= pc) pc |
42 else if (prog(pc) == '[') jumpRight(prog, pc + 1, level + 1) |
37 else (prog(pc), level) match { |
43 else if (prog(pc) == ']' && level == 0) pc + 1 |
38 case (']', 0) => pc + 1 |
44 else if (prog(pc) == ']') jumpRight(prog, pc + 1, level - 1) |
39 case (']', l) => jumpRight(prog, pc + 1, l - 1) |
45 else jumpRight(prog, pc + 1, level) |
40 case ('[', l) => jumpRight(prog, pc + 1, l + 1) |
46 } |
41 case (_, l) => jumpRight(prog, pc + 1, l) |
47 |
42 } |
48 def jtable(pg: String) : Map[Int, Int] = { |
43 } |
49 val pairs = for { |
44 |
50 i <- 0 until pg.length |
45 def jumpLeft(prog: String, pc: Int, level: Int) : Int = { |
51 if pg.charAt(i) == '[' |
46 if (pc < 0) pc |
52 j = jumpRight(pg, i+1, 0) |
47 else (prog(pc), level) match { |
53 } yield (i, j) |
48 case ('[', 0) => pc + 1 |
54 pairs.flatMap { case (i, j) => |
49 case ('[', l) => jumpLeft(prog, pc - 1, l - 1) |
55 List((i, j), (j-1, i+1)) |
50 case (']', l) => jumpLeft(prog, pc - 1, l + 1) |
56 }.toMap |
51 case (_, l) => jumpLeft(prog, pc - 1, l) |
57 } |
52 } |
58 |
53 } |
59 def write(mem: Mem, mp: Int, v: Int) : Mem = mem + (mp -> v) |
54 |
|
55 def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = { |
|
56 if (0 <= pc && pc < prog.length) { |
|
57 val (new_pc, new_mp, new_mem) = prog(pc) match { |
|
58 case '>' => (pc + 1, mp + 1, mem) |
|
59 case '<' => (pc + 1, mp - 1, mem) |
|
60 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
|
61 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
|
62 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
|
63 case '[' => if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
|
64 case ']' => if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
|
65 case _ => (pc + 1, mp, mem) |
|
66 } |
|
67 compute(prog, new_pc, new_mp, new_mem) |
|
68 } |
|
69 else mem |
|
70 } |
|
71 |
|
72 def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m) |
|
73 |
|
74 |
|
75 // The baseline to what we can compare our "compiler" |
|
76 // implemented below. It should require something like |
|
77 // 60 seconds for the calculation on my laptop |
|
78 // |
|
79 //time_needed(1, run(load_bff("benchmark.bf"))) |
|
80 |
|
81 |
|
82 |
|
83 // DEBUGGING INFORMATION!!! |
|
84 // |
|
85 // Compiler, even real ones, are fiedishly difficult to get |
|
86 // to prduce correct code. The point is that for example for |
|
87 // the sierpinski program, they need to still generate code |
|
88 // that displays such a triangle. If yes, then one usually |
|
89 // can take comfort that all is well. If not, then something |
|
90 // went wrong during the optimisations. |
|
91 |
|
92 |
|
93 |
|
94 // (5) Write a function jtable that precomputes the "jump |
|
95 // table" for a bf-program. This function takes a bf-program |
|
96 // as an argument and Returns a Map[Int, Int]. The |
|
97 // purpose of this map is to record the information |
|
98 // that given on the position pc is a '[' or a ']', |
|
99 // then to which pc-position do we need to jump next? |
|
100 // |
|
101 // For example for the program |
|
102 // |
|
103 // "+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]" |
|
104 // |
|
105 // we obtain the map |
|
106 // |
|
107 // Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6) |
|
108 // |
|
109 // This states that for the '[' on position 5, we need to |
|
110 // jump to position 20, which is just after the corresponding ']'. |
|
111 // Similarly, for the ']' on position 19, we need to jump to |
|
112 // position 6, which is just after the '[' on position 5, and so |
|
113 // on. The idea is to not calculate this information each time |
|
114 // we hit a bracket, but just look up this information in the |
|
115 // jtable. You can use the jumpLeft and jumpRight functions |
|
116 // from Part 1 for calculating the jtable. |
|
117 // |
|
118 // Then adapt the compute and run functions from Part 1 in order |
|
119 // to take advantage of the information stored in the jtable. |
|
120 // This means whenever jumpLeft and jumpRight was called previously, |
|
121 // you should look up the jump address in the jtable. |
|
122 |
|
123 |
|
124 def jtable(pg: String) : Map[Int, Int] = |
|
125 (0 until pg.length).collect { pc => pg(pc) match { |
|
126 case '[' => (pc -> jumpRight(pg, pc + 1, 0)) |
|
127 case ']' => (pc -> jumpLeft(pg, pc - 1, 0)) |
|
128 }}.toMap |
|
129 |
60 |
130 |
61 // testcase |
131 // testcase |
62 // |
132 // jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") |
63 // jtable("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""") |
|
64 // => Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6) |
133 // => Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6) |
65 |
134 |
|
135 |
66 def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
136 def compute2(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
67 if (pc >= pg.length) mem |
137 if (0 <= pc && pc < pg.length) { |
68 else { |
138 val (new_pc, new_mp, new_mem) = pg(pc) match { |
69 val (npc, nmp, nmem) = pg(pc) match { |
139 case '>' => (pc + 1, mp + 1, mem) |
70 case '>' => (pc + 1, mp + 1, mem) |
140 case '<' => (pc + 1, mp - 1, mem) |
71 case '<' => (pc + 1, mp - 1, mem) |
141 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
72 case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1))) |
142 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
73 case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1))) |
143 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
74 case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)} |
144 case '[' => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
75 case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
145 case ']' => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
76 case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
146 case _ => (pc + 1, mp, mem) |
77 case _ => (pc + 1, mp, mem) |
147 } |
78 } |
148 compute2(pg, tb, new_pc, new_mp, new_mem) |
79 compute2(pg, tb, npc, nmp, nmem) |
149 } |
80 } |
150 else mem |
81 } |
151 } |
|
152 |
82 |
153 |
83 def run2(pg: String, m: Mem = Map()) = |
154 def run2(pg: String, m: Mem = Map()) = |
84 compute2(pg, jtable(pg), 0, 0, m) |
155 compute2(pg, jtable(pg), 0, 0, m) |
85 |
156 |
|
157 //time_needed(1, run2(load_bff("benchmark.bf"))) |
|
158 |
|
159 |
|
160 |
|
161 // (6) Write a function optimise which deletes "dead code" (everything |
|
162 // that is not a bf-command) and also replaces substrings of the form |
|
163 // [-] by a new command 0. The idea is that the loop [-] just resets the |
|
164 // memory at the current location to 0. In the compute3 and run3 functions |
|
165 // below you implement this command by writing the number 0 to mem(mp), |
|
166 // that is write(mem, mp, 0). |
|
167 // |
|
168 // The easiest way to modify a string in this way is to use the regular |
|
169 // expression """[^<>+-.\[\]""", which recognises everything that is |
|
170 // not a bf-command and replace it by the empty string. Similarly the |
|
171 // regular expression """\[-\]""" finds all occurences of [-] and |
|
172 // by using the Scala method .replaceAll you can repplace it with the |
|
173 // string "0" standing for the new bf-command. |
|
174 |
|
175 def optimise(s: String) : String = { |
|
176 s.replaceAll("""[^<>+-.\[\]]""","") |
|
177 .replaceAll("""\[-\]""", "0") |
|
178 } |
|
179 |
|
180 |
|
181 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
|
182 if (0 <= pc && pc < pg.length) { |
|
183 val (new_pc, new_mp, new_mem) = pg(pc) match { |
|
184 case '0' => (pc + 1, mp, write(mem, mp, 0)) |
|
185 case '>' => (pc + 1, mp + 1, mem) |
|
186 case '<' => (pc + 1, mp - 1, mem) |
|
187 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
|
188 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
|
189 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
|
190 case '[' => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
|
191 case ']' => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
|
192 case _ => (pc + 1, mp, mem) |
|
193 } |
|
194 compute3(pg, tb, new_pc, new_mp, new_mem) |
|
195 } |
|
196 else mem |
|
197 } |
|
198 |
|
199 def run3(pg: String, m: Mem = Map()) = { |
|
200 val pg_opt = optimise(pg) |
|
201 compute3(pg_opt, jtable(pg_opt), 0, 0, m) |
|
202 } |
|
203 |
86 |
204 |
87 // testcases |
205 // testcases |
88 // time_needed(1, run2(load_bff("benchmark.bf"))) |
206 |
89 // time_needed(1, run2(load_bff("sierpinski.bf"))) |
207 //println(optimise(load_bff("collatz.bf"))) |
90 |
208 //optimise(load_bff("benchmark.bf")) // should have inserted 0's |
91 |
209 //optimise(load_bff("mandelbrot.bf")).length // => 11203 |
92 |
210 |
93 // (7) |
211 //time_needed(1, run3(load_bff("benchmark.bf"))) |
94 |
212 |
95 def optimise(s: String) : String = |
213 |
96 s.replaceAll("""[^<>+-.,\[\]]""","").replaceAll("""\[-\]""","0") |
214 |
97 |
215 // (7) Write a function combine which replaces sequences |
98 def compute3(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
216 // of repated increment and decrement commands by appropriate |
99 if (pc >= pg.length) mem |
217 // two-character commands. For example for sequences of + |
100 else { |
218 // |
101 val (npc, nmp, nmem) = pg(pc) match { |
219 // orig bf-cmds | replacement |
102 case '>' => (pc + 1, mp + 1, mem) |
220 // ------------------------------ |
103 case '<' => (pc + 1, mp - 1, mem) |
221 // + | +A |
104 case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1))) |
222 // ++ | +B |
105 case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1))) |
223 // +++ | +C |
106 case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)} |
224 // | |
107 case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
225 // ... | |
108 case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
226 // | |
109 case _ => (pc + 1, mp, mem) |
227 // +++....+++ | +Z |
110 } |
228 // (where length = 26) |
111 compute3(pg, tb, npc, nmp, nmem) |
229 // |
112 } |
230 // Similar for the bf-command -, > and <. All other commands should |
113 } |
231 // be unaffected by this change. |
114 |
232 // |
115 def run3(pg: String, m: Mem = Map()) = { |
233 // Adapt the compute4 and run4 functions such that they can deal |
116 val opt_pg = optimise(pg) |
234 // appropriately with such two-character commands. |
117 val jt = jtable(opt_pg) |
235 |
118 compute3(opt_pg, jt, 0, 0, m) |
236 def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { |
119 } |
237 case (Nil, acc) => acc |
120 |
238 case ('[' :: cs, acc) => splice(cs, ('[', 1) :: acc) |
|
239 case (']' :: cs, acc) => splice(cs, (']', 1) :: acc) |
|
240 case ('.' :: cs, acc) => splice(cs, ('.', 1) :: acc) |
|
241 case ('0' :: cs, acc) => splice(cs, ('0', 1) :: acc) |
|
242 case (c :: cs, Nil) => splice(cs, List((c, 1))) |
|
243 case (c :: cs, (d, n) :: acc) => |
|
244 if (c == d && n < 26) splice(cs, (c, n + 1) :: acc) |
|
245 else splice(cs, (c, 1) :: (d, n) :: acc) |
|
246 } |
|
247 |
|
248 def spl(s: String) = splice(s.toList, Nil).reverse |
|
249 |
|
250 //spl(load_bff("benchmark.bf")) |
|
251 |
|
252 def combine(s: String) : String = { |
|
253 (for ((c, n) <- spl(s)) yield c match { |
|
254 case '>' => List('>', (n + '@').toChar) |
|
255 case '<' => List('<', (n + '@').toChar) |
|
256 case '+' => List('+', (n + '@').toChar) |
|
257 case '-' => List('-', (n + '@').toChar) |
|
258 case _ => List(c) |
|
259 }).flatten.mkString |
|
260 } |
|
261 |
|
262 |
|
263 //combine(load_bff("benchmark.bf")) |
|
264 |
|
265 def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
|
266 if (0 <= pc && pc < pg.length) { |
|
267 val (new_pc, new_mp, new_mem) = pg(pc) match { |
|
268 case '0' => (pc + 1, mp, write(mem, mp, 0)) |
|
269 case '>' => (pc + 2, mp + (pg(pc + 1) - '@'), mem) |
|
270 case '<' => (pc + 2, mp - (pg(pc + 1) - '@'), mem) |
|
271 case '+' => (pc + 2, mp, write(mem, mp, sread(mem, mp) + (pg(pc + 1) - '@'))) |
|
272 case '-' => (pc + 2, mp, write(mem, mp, sread(mem, mp) - (pg(pc + 1) - '@'))) |
|
273 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
|
274 case '[' => if (sread(mem, mp) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
|
275 case ']' => if (sread(mem, mp) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
|
276 case _ => (pc + 1, mp, mem) |
|
277 } |
|
278 compute4(pg, tb, new_pc, new_mp, new_mem) |
|
279 } |
|
280 else mem |
|
281 } |
|
282 |
|
283 def run4(pg: String, m: Mem = Map()) = { |
|
284 val pg_opt = combine(optimise(pg)) |
|
285 compute4(pg_opt, jtable(pg_opt), 0, 0, m) |
|
286 } |
121 |
287 |
122 // testcases |
288 // testcases |
123 // |
289 //println(combine(optimise(load_bff("mandelbrot.bf").drop(123)))) |
124 // optimise(load_bff("benchmark.bf")) // should have inserted 0's |
290 |
125 // optimise(load_bff("mandelbrot.bf")).length // => 11203 |
291 //combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[.....""" |
126 // optimise(load_bff("benchmark.bf")).length |
292 |
127 // time_needed(1, run3(load_bff("benchmark.bf"))) |
293 //time_needed(1, run4(load_bff("benchmark.bf"))) |
128 |
294 |
129 |
295 //time_needed(1, run(load_bff("sierpinski.bf"))) |
130 // (8) |
296 //time_needed(1, run4(load_bff("sierpinski.bf"))) |
131 def combine(s: String): String = ??? |
297 |
132 |
298 //println(time_needed(1, run4(load_bff("mandelbrot.bf")))) |
133 // testcase |
299 |
134 // combine(load_bff("benchmark.bf")) |
300 |
135 |
301 |
136 def compute4(pg: String, tb: Map[Int, Int], pc: Int, mp: Int, mem: Mem) : Mem = { |
302 |
137 if (pc >= pg.length) mem |
303 |
138 else { |
304 } |
139 val (npc, nmp, nmem) = pg(pc) match { |
305 |
140 case '>' => (pc + 1, mp + 1, mem) |
306 /* |
141 case '<' => (pc + 1, mp - 1, mem) |
307 import CW10b._ |
142 case '+' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) + 1))) |
308 println(time_needed(1, run(load_bff("collatz.bf")))) |
143 case '-' => (pc + 1, mp, mem + (mp -> (mem.getOrElse(mp, 0) - 1))) |
309 println(time_needed(1, run2(load_bff("collatz.bf")))) |
144 case '.' => {print(mem.getOrElse(mp,0).toChar);(pc + 1, mp, mem)} |
310 println(time_needed(1, run3(load_bff("collatz.bf")))) |
145 case '[' => if (mem.getOrElse(mp, 0) == 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
311 println(time_needed(1, run4(load_bff("collatz.bf")))) |
146 case ']' => if (mem.getOrElse(mp, 0) != 0) (tb(pc), mp, mem) else (pc + 1, mp, mem) |
312 */ |
147 case _ => (pc + 1, mp, mem) |
|
148 } |
|
149 compute3(pg, tb, npc, nmp, nmem) |
|
150 } |
|
151 } |
|
152 |
|
153 // should call first optimise and then combine on the input string |
|
154 // |
|
155 def run4(pg: String, m: Mem = Map()) = { |
|
156 val co_opt_pg = combine(optimise(pg)) |
|
157 val jt = jtable(co_opt_pg) |
|
158 compute3(co_opt_pg, jt, 0, 0, m) |
|
159 } |
|
160 |
|
161 // testcases |
|
162 // combine(optimise(load_bff("benchmark.bf"))) // => """>A+B[<A+M>A-A]<A[[.....""" |
|
163 |
|
164 // testcases (they should now run much faster) |
|
165 // time_needed(1, run4(load_bff("benchmark.bf"))) |
|
166 // time_needed(1, run4(load_bff("sierpinski.bf"))) |
|
167 // time_needed(1, run4(load_bff("mandelbrot.bf"))) |
|
168 |
|
169 |
|
170 } |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 // This template code is subject to copyright |
|
177 // by King's College London, 2022. Do not |
|
178 // make the template code public in any shape |
|
179 // or form, and do not exchange it with other |
|
180 // students under any circumstance. |
|