1 // Part 2 about an Interpreter for the Brainf*** language |
|
2 //======================================================== |
|
3 |
|
4 object CW8b { |
|
5 |
|
6 type Mem = Map[Int, Int] |
|
7 |
|
8 // (2a) Complete the functions for safely reading |
|
9 // and writing brainf*** memory. Safely read should |
|
10 // Return the value stored in the Map for a given memory |
|
11 // pointer, if it exists; otherwise it Returns 0. The |
|
12 // writing function generates a new Map with the |
|
13 // same data, except at the given memory pointer the |
|
14 // value v is stored. |
|
15 |
|
16 |
|
17 def sread(mem: Mem, mp: Int) : Int = |
|
18 mem.getOrElse(mp, 0) |
|
19 |
|
20 def write(mem: Mem, mp: Int, v: Int) : Mem = |
|
21 mem.updated(mp, v) |
|
22 |
|
23 |
|
24 // (2b) Implement the two jumping instructions in the |
|
25 // brainf*** language. In jumpRight, given a program and |
|
26 // a program counter move the program counter to the right |
|
27 // until after the *matching* ]-command. Similarly, |
|
28 // jumpLeft implements the move to the left to just after |
|
29 // the *matching* [--command. |
|
30 |
|
31 def jumpRight(prog: String, pc: Int, level: Int) : Int = { |
|
32 if (prog.length <= pc) pc |
|
33 else (prog(pc), level) match { |
|
34 case (']', 0) => pc + 1 |
|
35 case (']', l) => jumpRight(prog, pc + 1, l - 1) |
|
36 case ('[', l) => jumpRight(prog, pc + 1, l + 1) |
|
37 case (_, l) => jumpRight(prog, pc + 1, l) |
|
38 } |
|
39 } |
|
40 |
|
41 def jumpLeft(prog: String, p: Int, level: Int) : Int = { |
|
42 if (p < 0) p |
|
43 else (prog(p), level) match { |
|
44 case ('[', 0) => p + 1 |
|
45 case ('[', l) => jumpLeft(prog, p - 1, l - 1) |
|
46 case (']', l) => jumpLeft(prog, p - 1, l + 1) |
|
47 case (_, l) => jumpLeft(prog, p - 1, l) |
|
48 } |
|
49 } |
|
50 |
|
51 //jumpLeft("[******]***", 7, 0) |
|
52 |
|
53 // (2c) Complete the run function that interpretes (runs) a brainf*** |
|
54 // program: the arguments are a program, a program counter, |
|
55 // a memory counter and a brainf*** memory. It Returns the |
|
56 // memory at the stage when the excution of the brainf*** program |
|
57 // finishes. The interpretation finishes once the program counter |
|
58 // pc is pointing to something outside the program string. |
|
59 // If the pc points to a character inside the program, the pc, |
|
60 // memory pointer and memory need to be updated according to |
|
61 // rules of the brainf*** language. Then, recursively, run |
|
62 // function continues with the command at the new program |
|
63 // counter. |
|
64 // Implement the start function that calls run with the program |
|
65 // counter and memory counter set to 0. |
|
66 |
|
67 def run(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = { |
|
68 if (0 <= pc && pc < prog.length) { |
|
69 val (new_pc, new_mp, new_mem) = prog(pc) match { |
|
70 case '>' => (pc + 1, mp + 1, mem) |
|
71 case '<' => (pc + 1, mp - 1, mem) |
|
72 case '+' => (pc + 1, mp, write(mem, mp, sread(mem, mp) + 1)) |
|
73 case '-' => (pc + 1, mp, write(mem, mp, sread(mem, mp) - 1)) |
|
74 case '.' => { print(sread(mem, mp).toChar); (pc + 1, mp, mem) } |
|
75 case ',' => (pc + 1, mp, write(mem, mp, Console.in.read().toByte)) |
|
76 case '[' => |
|
77 if (sread(mem, mp) == 0) (jumpRight(prog, pc + 1, 0), mp, mem) else (pc + 1, mp, mem) |
|
78 case ']' => |
|
79 if (sread(mem, mp) != 0) (jumpLeft(prog, pc - 1, 0), mp, mem) else (pc + 1, mp, mem) |
|
80 case _ => (pc + 1, mp, mem) |
|
81 } |
|
82 run(prog, new_pc, new_mp, new_mem) |
|
83 } |
|
84 else mem |
|
85 } |
|
86 |
|
87 def start(prog: String, m: Mem) = run(prog, 0, 0, m) |
|
88 |
|
89 // some sample programs collected from the Internet |
|
90 //================================================== |
|
91 |
|
92 |
|
93 /* |
|
94 // first some contrived (small) programs |
|
95 |
|
96 // clears the 0-cell |
|
97 start("[-]", Map(0 -> 100)) |
|
98 |
|
99 // copies content of the 0-cell to 1-cell |
|
100 start("[->+<]", Map(0 -> 10)) |
|
101 |
|
102 // copies content of the 0-cell to 2-cell and 4-cell |
|
103 start("[>>+>>+<<<<-]", Map(0 -> 42)) |
|
104 |
|
105 |
|
106 // prints out numbers 0 to 9 |
|
107 start("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""", Map()) |
|
108 |
|
109 |
|
110 // some more "useful" programs |
|
111 |
|
112 // hello world program 1 |
|
113 start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ |
|
114 ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map()) |
|
115 |
|
116 // hello world program 2 |
|
117 start("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+ |
|
118 +.<<+++++++++++++++.>.+++.------.--------.>+.>.""", Map()) |
|
119 |
|
120 |
|
121 // draws the Sierpinski triangle |
|
122 start("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ |
|
123 ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< |
|
124 ]>.>+[>>]>+]""", Map()) |
|
125 |
|
126 println(start("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ |
|
127 ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< |
|
128 ]>.>+[>>]>+]""", Map())) |
|
129 |
|
130 //fibonacci numbers below 100 |
|
131 start("""+++++++++++ |
|
132 >+>>>>++++++++++++++++++++++++++++++++++++++++++++ |
|
133 >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> |
|
134 +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- |
|
135 <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< |
|
136 -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] |
|
137 >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ |
|
138 +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ |
|
139 ++++++++++++++++++++++++++++++++++++++++++++.[-]<< |
|
140 <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< |
|
141 [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", Map()) |
|
142 |
|
143 |
|
144 //outputs the square numbers up to 10000 |
|
145 start("""++++[>+++++<-]>[<+++++>-]+<+[ |
|
146 >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ |
|
147 >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] |
|
148 <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""", Map()) |
|
149 |
|
150 //collatz numbers (need to be typed in) |
|
151 start(""">,[[----------[ |
|
152 >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] |
|
153 ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[ |
|
154 <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] |
|
155 >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] |
|
156 >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> |
|
157 ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""", Map()) |
|
158 |
|
159 |
|
160 // infinite collatz (never stops) |
|
161 start(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[< |
|
162 <]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++ |
|
163 +<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+< |
|
164 [<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>-> |
|
165 [<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[ |
|
166 <+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+ |
|
167 ++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[< |
|
168 +>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<- |
|
169 ]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<- |
|
170 >>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+> |
|
171 -[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++ |
|
172 +++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[< |
|
173 <]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++ |
|
174 +++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-< |
|
175 <]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>- |
|
176 [<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""", Map()) |
|
177 |
|
178 |
|
179 start("""+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]""", Map()) |
|
180 |
|
181 */ |
|
182 |
|
183 /* |
|
184 def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { |
|
185 case (Nil, acc) => acc |
|
186 case (c :: cs, Nil) => splice(cs, List((c, 1))) |
|
187 case (c :: cs, (d, n) :: acc) => |
|
188 if (c == d) splice(cs, (c, n + 1) :: acc) |
|
189 else splice(cs, (c, 1) :: (d, n) :: acc) |
|
190 } |
|
191 |
|
192 def spl(s: String) = splice(s.toList, Nil).reverse |
|
193 |
|
194 |
|
195 // simple instructions |
|
196 def instr(c: Char) : String = c match { |
|
197 case '>' => "ptr++;" |
|
198 case '<' => "ptr--;" |
|
199 case '+' => "(*ptr)++;" |
|
200 case '-' => "(*ptr)--;" |
|
201 case '.' => "putchar(*ptr);" |
|
202 case ',' => "*ptr = getchar();\n" |
|
203 case '[' => "while(*ptr){" |
|
204 case ']' => "}" |
|
205 case _ => "" |
|
206 } |
|
207 |
|
208 def instrs(prog: String) : String = |
|
209 prog.toList.map(instr(_)).mkString |
|
210 |
|
211 |
|
212 //optimised instructions |
|
213 |
|
214 def instr2(c: Char, n: Int) : String = c match { |
|
215 case '>' => "ptr += " + n.toString + ";" |
|
216 case '<' => "ptr -= " + n.toString + ";" |
|
217 case '+' => "(*ptr) += " + n.toString + ";" |
|
218 case '-' => "(*ptr) -= " + n.toString + ";" |
|
219 case '.' => "putchar(*ptr);" * n |
|
220 case ',' => "*ptr = getchar();\n" * n |
|
221 case '[' => "while(*ptr){" * n |
|
222 case ']' => "}" * n |
|
223 case _ => "" |
|
224 } |
|
225 |
|
226 def instrs2(prog: String) : String = |
|
227 spl(prog).map{ case (c, n) => instr2(c, n) }.mkString |
|
228 |
|
229 |
|
230 // peephole optimisers are at |
|
231 // https://github.com/Wilfred/bfc#peephole-optimisations |
|
232 // http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html |
|
233 |
|
234 def compile_str(prog: String) : String = { |
|
235 "#include <string.h>\n" ++ |
|
236 "#include <stdio.h>\n" ++ |
|
237 "char field[30000];\n" ++ |
|
238 "char *ptr = &field[15000];" ++ |
|
239 "int main()\n{\n" ++ |
|
240 "memset(field, '\\0', 30000);\n" ++ |
|
241 instrs2(prog) ++ |
|
242 "\n re turn 0;\n}" |
|
243 } |
|
244 |
|
245 def compile(name: String, prog: String) = { |
|
246 val fw = new java.io.FileWriter(name + ".c") |
|
247 fw.write(compile_str(prog)) |
|
248 fw.close() |
|
249 } |
|
250 |
|
251 import sys.process._ |
|
252 |
|
253 def compile_run(prog: String) = { |
|
254 compile("tmp", prog) |
|
255 "gcc -O3 -o tmp tmp.c".! |
|
256 "./tmp".! |
|
257 () |
|
258 } |
|
259 |
|
260 |
|
261 compile_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ |
|
262 ..+++.>>.<-.<.+++.------.--------.>>+.>++.""") |
|
263 |
|
264 compile_run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[ |
|
265 ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< |
|
266 ]>.>+[>>]>+]""") |
|
267 |
|
268 compile_run(""" A mandelbrot set fractal viewer in brainf*** written by Erik Bosman |
|
269 +++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[ |
|
270 >>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+ |
|
271 <<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>> |
|
272 >+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>> |
|
273 >>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>> |
|
274 >>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>> |
|
275 >>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>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>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++ |
|
411 +[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<< |
|
412 <<<<<]]>>>]""") |
|
413 |
|
414 compile_run(""" |
|
415 >+>+>+>+>++<[>[<+++>- |
|
416 |
|
417 >>>>> |
|
418 >+>+>+>+>++<[>[<+++>- |
|
419 |
|
420 >>>>> |
|
421 >+>+>+>+>++<[>[<+++>- |
|
422 |
|
423 >>>>> |
|
424 >+>+>+>+>++<[>[<+++>- |
|
425 |
|
426 >>>>> |
|
427 +++[->+++++<]>[-]< |
|
428 <<<<< |
|
429 |
|
430 ]<<]>[-] |
|
431 <<<<< |
|
432 |
|
433 ]<<]>[-] |
|
434 <<<<< |
|
435 |
|
436 ]<<]>[-] |
|
437 <<<<< |
|
438 |
|
439 ]<<]>.""") |
|
440 |
|
441 // benchmarks |
|
442 compile_run(""">++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++ |
|
443 [>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++ |
|
444 ++++++++[>++++++++++[>++++++++++[>++++++++++[>+ |
|
445 +++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.""") |
|
446 |
|
447 // |
|
448 compile_run("""++++[>++++<-]>[>+>++>[+++++++>]+++[<]>-]>>>>>>>>-. |
|
449 <<<<.<..+++.<.>>>>.<<<.+++.------.>-.<<+.<------.""") |
|
450 |
|
451 compile_run("""++++++++[->-[->-[->-[-]<]<]<] |
|
452 >++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>""") |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 // register to BF compiler |
|
458 // https://gergo.erdi.hu/blog/2010-09-06-from_register_machines_to_brainfuck,_part_1/ |
|
459 |
|
460 */ |
|
461 |
|
462 } |
|