153
|
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
|
154
|
11 |
// pointer, if it exists; otherwise it Returns 0. The
|
153
|
12 |
// writing function generates a new Map with the
|
|
13 |
// same data, except at the given memory pointer the
|
154
|
14 |
// value v is stored.
|
153
|
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 |
|
167
|
51 |
//jumpLeft("[******]***", 7, 0)
|
161
|
52 |
|
153
|
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 |
//fibonacci numbers below 100
|
|
127 |
start("""+++++++++++
|
|
128 |
>+>>>>++++++++++++++++++++++++++++++++++++++++++++
|
|
129 |
>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
|
|
130 |
+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
|
|
131 |
<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
|
|
132 |
-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
|
|
133 |
>[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
|
|
134 |
+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
|
|
135 |
++++++++++++++++++++++++++++++++++++++++++++.[-]<<
|
|
136 |
<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
|
|
137 |
[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", Map())
|
|
138 |
|
|
139 |
|
|
140 |
//outputs the square numbers up to 10000
|
|
141 |
start("""++++[>+++++<-]>[<+++++>-]+<+[
|
|
142 |
>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
|
|
143 |
>>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
|
|
144 |
<<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""", Map())
|
|
145 |
|
|
146 |
//collatz numbers (need to be typed in)
|
|
147 |
start(""">,[[----------[
|
|
148 |
>>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
|
|
149 |
++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<]>]>>>++>+>>[
|
|
150 |
<<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
|
|
151 |
>[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
|
|
152 |
>[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
|
|
153 |
]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,]""", Map())
|
|
154 |
|
|
155 |
|
|
156 |
// infinite collatz (never stops)
|
|
157 |
start(""">>+>+<[[->>[>>]>>>[>>]+[<<]<<<[<<]>[>[>>]>>+>[>>]<+<[<<]<<<[<
|
|
158 |
<]>-]>[>>]>>[<<<<[<<]>+>[>>]>>-]<<<<[<<]+>>]<<[+++++[>+++++++
|
|
159 |
+<-]>.<++++++[>--------<-]+<<]>>[>>]+[>>>>[<<+>+>-]<-[>+<-]+<
|
|
160 |
[<<->>-[<<+>>[-]]]>>>[<<<+<<+>>>>>-]<<<[>>>+<<<-]<<[[-]>+>>->
|
|
161 |
[<+<[<<+>>-]<[>+<-]<[>+<-]>>>>-]<[>+<-]+<[->[>>]<<[->[<+++>-[
|
|
162 |
<+++>-[<+++>-[<[-]++>>[-]+>+<<-[<+++>-[<+++>-[<[-]+>>>+<<-[<+
|
|
163 |
++>-[<+++>-]]]]]]]]]<[>+<-]+<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<
|
|
164 |
+>-[<+>-[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-[<+>-]]]]]]]]]]]<[>+<-
|
|
165 |
]+>>]<<[<<]>]<[->>[->+>]<[-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<-
|
|
166 |
>>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>-[<->>+<-[<+>
|
|
167 |
-[<->>+<-[<+>-[<->>+<-[<+>-]]]]]]]]]]]]]]]]]]]>[<+>-]<+<[<+++
|
|
168 |
+++++++>-]<]>>[<+>->>]<<[>+>+<<-]>[<+>-]+>[<->[-]]<[-<<-]<<[<
|
|
169 |
<]]++++++[>+++++++<-]>++.------------.[-]>[>>]<<[+++++[>+++++
|
|
170 |
+++<-]>.<++++++[>--------<-]+<<]+<]>[<+>-]<]>>>[>>]<<[>[-]<-<
|
|
171 |
<]++++++++++.[-]<<<[<<]>>>+<[->[<+>-[<+>-[<+>-[<+>-[<+>-[<+>-
|
|
172 |
[<+>-[<+>-[<+>-[<[-]>>[-]+>+<<-]]]]]]]]]]<[>+<-]+>>]<<[<<]>>]""", Map())
|
|
173 |
|
|
174 |
|
167
|
175 |
start("""+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]""", Map())
|
|
176 |
|
153
|
177 |
*/
|
|
178 |
|
167
|
179 |
|
|
180 |
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
|
|
181 |
case (Nil, acc) => acc
|
|
182 |
case (c :: cs, Nil) => splice(cs, List((c, 1)))
|
|
183 |
case (c :: cs, (d, n) :: acc) =>
|
|
184 |
if (c == d) splice(cs, (c, n + 1) :: acc)
|
|
185 |
else splice(cs, (c, 1) :: (d, n) :: acc)
|
153
|
186 |
}
|
167
|
187 |
|
|
188 |
def spl(s: String) = splice(s.toList, Nil).reverse
|
|
189 |
|
|
190 |
|
|
191 |
// simple instructions
|
|
192 |
def instr(c: Char) : String = c match {
|
|
193 |
case '>' => "ptr++;"
|
|
194 |
case '<' => "ptr--;"
|
|
195 |
case '+' => "(*ptr)++;"
|
|
196 |
case '-' => "(*ptr)--;"
|
|
197 |
case '.' => "putchar(*ptr);"
|
|
198 |
case ',' => "*ptr = getchar();\n"
|
|
199 |
case '[' => "while(*ptr){"
|
|
200 |
case ']' => "}"
|
|
201 |
case _ => ""
|
|
202 |
}
|
|
203 |
|
|
204 |
def instrs(prog: String) : String =
|
|
205 |
prog.toList.map(instr(_)).mkString
|
|
206 |
|
|
207 |
|
|
208 |
//optimised instructions
|
|
209 |
|
|
210 |
def instr2(c: Char, n: Int) : String = c match {
|
|
211 |
case '>' => "ptr += " + n.toString + ";"
|
|
212 |
case '<' => "ptr -= " + n.toString + ";"
|
|
213 |
case '+' => "(*ptr) += " + n.toString + ";"
|
|
214 |
case '-' => "(*ptr) -= " + n.toString + ";"
|
|
215 |
case '.' => "putchar(*ptr);" * n
|
|
216 |
case ',' => "*ptr = getchar();\n" * n
|
|
217 |
case '[' => "while(*ptr){" * n
|
|
218 |
case ']' => "}" * n
|
|
219 |
case _ => ""
|
|
220 |
}
|
|
221 |
|
|
222 |
def instrs2(prog: String) : String =
|
|
223 |
spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
|
|
224 |
|
|
225 |
|
|
226 |
// peephole optimisers are at
|
|
227 |
// https://github.com/Wilfred/bfc#peephole-optimisations
|
|
228 |
// http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html
|
|
229 |
|
|
230 |
def compile_str(prog: String) : String = {
|
|
231 |
"#include <string.h>\n" ++
|
|
232 |
"#include <stdio.h>\n" ++
|
|
233 |
"char field[30000];\n" ++
|
|
234 |
"char *ptr = &field[15000];" ++
|
|
235 |
"int main()\n{\n" ++
|
|
236 |
"memset(field, '\\0', 30000);\n" ++
|
|
237 |
instrs2(prog) ++
|
|
238 |
"\n return 0;\n}"
|
|
239 |
}
|
|
240 |
|
|
241 |
def compile(name: String, prog: String) = {
|
|
242 |
val fw = new java.io.FileWriter(name + ".c")
|
|
243 |
fw.write(compile_str(prog))
|
|
244 |
fw.close()
|
|
245 |
}
|
|
246 |
|
|
247 |
import sys.process._
|
|
248 |
|
|
249 |
def compile_run(prog: String) = {
|
|
250 |
compile("tmp", prog)
|
|
251 |
"gcc -O3 -o tmp tmp.c".!
|
|
252 |
"./tmp".!
|
|
253 |
()
|
|
254 |
}
|
|
255 |
|
|
256 |
|
195
|
257 |
compile("test.bf", """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
|
167
|
258 |
..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
|
|
259 |
|
|
260 |
compile_run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
|
|
261 |
->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
|
|
262 |
]>.>+[>>]>+]""")
|
|
263 |
|
|
264 |
compile_run(""" A mandelbrot set fractal viewer in brainf*** written by Erik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<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
|
|
408 |
<<<<<]]>>>]""")
|
|
409 |
|
|
410 |
compile_run("""
|
|
411 |
>+>+>+>+>++<[>[<+++>-
|
|
412 |
|
|
413 |
>>>>>
|
|
414 |
>+>+>+>+>++<[>[<+++>-
|
|
415 |
|
|
416 |
>>>>>
|
|
417 |
>+>+>+>+>++<[>[<+++>-
|
|
418 |
|
|
419 |
>>>>>
|
|
420 |
>+>+>+>+>++<[>[<+++>-
|
|
421 |
|
|
422 |
>>>>>
|
|
423 |
+++[->+++++<]>[-]<
|
|
424 |
<<<<<
|
|
425 |
|
|
426 |
]<<]>[-]
|
|
427 |
<<<<<
|
|
428 |
|
|
429 |
]<<]>[-]
|
|
430 |
<<<<<
|
|
431 |
|
|
432 |
]<<]>[-]
|
|
433 |
<<<<<
|
|
434 |
|
|
435 |
]<<]>.""")
|
|
436 |
|
|
437 |
// benchmarks
|
|
438 |
compile_run(""">++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
|
|
439 |
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
|
|
440 |
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
|
|
441 |
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.""")
|
|
442 |
|
|
443 |
//
|
|
444 |
compile_run("""++++[>++++<-]>[>+>++>[+++++++>]+++[<]>-]>>>>>>>>-.
|
|
445 |
<<<<.<..+++.<.>>>>.<<<.+++.------.>-.<<+.<------.""")
|
|
446 |
|
|
447 |
compile_run("""++++++++[->-[->-[->-[-]<]<]<]
|
|
448 |
>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.>""")
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
// register to BF compiler
|
|
454 |
// https://gergo.erdi.hu/blog/2010-09-06-from_register_machines_to_brainfuck,_part_1/
|
|
455 |
}
|