384
|
1 |
// Core Part about an Interpreter for
|
|
2 |
// the Brainf***++ language
|
|
3 |
//==============================================
|
|
4 |
|
|
5 |
|
|
6 |
object CW10a {
|
|
7 |
|
|
8 |
|
|
9 |
// representation of Bf memory
|
|
10 |
|
|
11 |
type Mem = Map[Int, Int]
|
|
12 |
|
|
13 |
|
|
14 |
// (1) Write a function that takes a file name as argument and
|
|
15 |
// and requests the corresponding file from disk. It Returns the
|
|
16 |
// content of the file as a String. If the file does not exists,
|
|
17 |
// the function should Return the empty string.
|
|
18 |
|
|
19 |
import io.Source
|
|
20 |
import scala.util._
|
|
21 |
import java.io.FileNotFoundException
|
|
22 |
import scala.annotation.tailrec
|
|
23 |
|
|
24 |
def load_bff(name: String) : String = {
|
|
25 |
try {
|
|
26 |
val bff = io.Source.fromFile(name).getLines().mkString(" ")
|
|
27 |
bff
|
|
28 |
}
|
|
29 |
catch {
|
|
30 |
case ex: FileNotFoundException =>
|
|
31 |
val bff = ""
|
|
32 |
bff
|
|
33 |
}
|
|
34 |
}
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
// (2) Complete the functions for safely reading
|
|
39 |
// and writing brainf***++ memory. Safely read should
|
|
40 |
// Return the value stored in the Map for a given memory
|
|
41 |
// pointer, provided it exists; otherwise it Returns 0. The
|
|
42 |
// writing function generates a new Map with the
|
|
43 |
// same data, except at the given memory pointer the
|
|
44 |
// value v is stored.
|
|
45 |
|
|
46 |
|
|
47 |
def sread(mem: Mem, mp: Int) : Int = {
|
|
48 |
mem.getOrElse(mp,0)
|
|
49 |
}
|
|
50 |
|
|
51 |
def write(mem: Mem, mp: Int, v: Int) : Mem = {
|
|
52 |
val newMem = mem + (mp -> v)
|
|
53 |
newMem
|
|
54 |
}
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
// (3) Implement the two jumping instructions in the
|
|
59 |
// brainf***++ language. In jumpRight, given a program and
|
|
60 |
// a program counter move the program counter to the right
|
|
61 |
// until after the *matching* ]-command. Similarly,
|
|
62 |
// jumpLeft implements the move to the left to just after
|
|
63 |
// the *matching* [-command.
|
|
64 |
@tailrec
|
|
65 |
def jumpRight(prog: String, pc: Int, level: Int) : Int = {
|
|
66 |
if (pc > prog.length-1) pc
|
|
67 |
else {
|
|
68 |
prog(pc) match{
|
|
69 |
case '[' => jumpRight(prog, pc+1, level+1)
|
|
70 |
case ']' => if (level == 0) pc+1
|
|
71 |
else jumpRight(prog, pc+1, level-1)
|
|
72 |
case _ => jumpRight(prog, pc+1, level)
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
@tailrec
|
|
77 |
def jumpLeft(prog: String, pc: Int, level: Int) : Int = {
|
|
78 |
if (pc < 0) pc
|
|
79 |
else {
|
|
80 |
prog(pc) match{
|
|
81 |
case ']' => jumpLeft(prog, pc-1, level+1)
|
|
82 |
case '[' => if (level == 0) pc+1
|
|
83 |
else jumpLeft(prog, pc-1, level-1)
|
|
84 |
case _ => jumpLeft(prog, pc-1, level)
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
|
|
90 |
// testcases
|
|
91 |
//jumpRight("""--[..+>--],>,++""", 3, 0) // => 10
|
|
92 |
//jumpLeft("""--[..+>--],>,++""", 8, 0) // => 3
|
|
93 |
// jumpRight("""--[..[+>]--],>,++""", 3, 0) // => 12
|
|
94 |
// jumpRight("""--[..[[-]+>[.]]--],>,++""", 3, 0) // => 18
|
|
95 |
//jumpRight("""--[..[[-]+>[.]]--,>,++""", 3, 0) // => 22 (outside)
|
|
96 |
//jumpLeft("""[******]***""", 7, 0) // => -1 (outside)
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
// (4) Complete the compute function that interprets (runs) a brainf***++
|
|
101 |
// program: the arguments are a program (represented as a string), a program
|
|
102 |
// counter, a memory counter and a brainf***++ memory. It Returns the
|
|
103 |
// memory at the stage when the execution of the brainf***++ program
|
|
104 |
// finishes. The interpretation finishes once the program counter
|
|
105 |
// pc is pointing to something outside the program string.
|
|
106 |
// If the pc points to a character inside the program, the pc,
|
|
107 |
// memory pointer and memory need to be updated according to
|
|
108 |
// rules of the brainf***++ language. Then, recursively, the compute
|
|
109 |
// function continues with the command at the new program
|
|
110 |
// counter.
|
|
111 |
//
|
|
112 |
// Implement the run function that calls compute with the program
|
|
113 |
// counter and memory counter set to 0.
|
|
114 |
|
|
115 |
@tailrec
|
|
116 |
def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = {
|
|
117 |
if (pc > prog.length-1) mem
|
|
118 |
else {
|
|
119 |
prog(pc) match{
|
|
120 |
case '>' => compute(prog, pc+1, mp+1, mem)
|
|
121 |
case '<' => compute(prog, pc+1, mp-1, mem)
|
|
122 |
case '+' => compute(prog, pc+1, mp,write(mem,mp,sread(mem,mp)+1))
|
|
123 |
case '-' => compute(prog, pc+1, mp,write(mem,mp,sread(mem,mp)-1))
|
|
124 |
case '.' => print(sread(mem,mp).toChar)
|
|
125 |
compute(prog, pc+1, mp, mem)
|
|
126 |
case '[' => if (sread(mem,mp) == 0) compute(prog,jumpRight(prog,pc+1,0),mp,mem)
|
|
127 |
else compute(prog,pc+1,mp,mem)
|
|
128 |
case ']' => if (sread(mem,mp) != 0) compute(prog,jumpLeft(prog,pc-1,0),mp,mem)
|
|
129 |
else compute(prog,pc+1,mp,mem)
|
|
130 |
case '*' => compute(prog, pc+1,mp, write(mem,mp,sread(mem,mp)*sread(mem,mp-1)))
|
|
131 |
case '@' => compute(prog,pc+1,mp, write(mem,sread(mem,mp),sread(mem,mp-1)))
|
|
132 |
case '#' => print(sread(mem,mp))
|
|
133 |
compute(prog,pc+1, mp, mem)
|
|
134 |
case _ => compute(prog,pc+1,mp,mem)
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
def run(prog: String, m: Mem = Map()) : Mem = {
|
|
140 |
compute(prog, 0, 0, m)
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
// some sample bf/bf++-programs collected from the Internet
|
|
146 |
//==========================================================
|
|
147 |
|
|
148 |
|
|
149 |
// some contrived (small) programs
|
|
150 |
//---------------------------------
|
|
151 |
|
|
152 |
// clears the 0-cell
|
|
153 |
//run("[-]", Map(0 -> 100)) // Map will be 0 -> 0
|
|
154 |
|
|
155 |
// moves content of the 0-cell to 1-cell
|
|
156 |
//run("[->+<]", Map(0 -> 10)) // Map will be 0 -> 0, 1 -> 10
|
|
157 |
|
|
158 |
|
|
159 |
// copies content of the 0-cell to 2-cell and 4-cell
|
|
160 |
//run("[>>+>>+<<<<-]", Map(0 -> 42)) // Map(0 -> 0, 2 -> 42, 4 -> 42)
|
|
161 |
|
|
162 |
|
|
163 |
// prints out numbers 0 to 9
|
|
164 |
//run("""+++++[->++++++++++<]>--<+++[->>++++++++++<<]>>++<<----------[+>.>.<+<]""")
|
|
165 |
|
|
166 |
// bf++ program calculating the cube-function, 10 * 10 * 10 = 1000
|
|
167 |
//run("""++++++++++#>+***#""") // Map(0 -> 10, 1 -> 1000)
|
|
168 |
|
|
169 |
|
|
170 |
// bf++ program copies 3 from 0-cell to to cells 1, 4, 5, 6 and 7
|
|
171 |
// (note that because of how the program wprks cell 1 will contain 7)
|
|
172 |
//run("""+++>+@+@+@+@+@""") // Map(0 -> 3, 1 -> 7, 4 -> 3, 5 -> 3, 6 -> 3, 7 -> 3)
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
// some more "useful" programs
|
|
177 |
//-----------------------------
|
|
178 |
|
|
179 |
// hello world program 1
|
|
180 |
// run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
|
|
181 |
// ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
|
|
182 |
|
|
183 |
// hello world program 2
|
|
184 |
// run("""++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>+
|
|
185 |
// +.<<+++++++++++++++.>.+++.------.--------.>+.>.""")
|
|
186 |
|
|
187 |
// hello world program 3
|
|
188 |
// run("""+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..
|
|
189 |
// +++.>-.------------.<++++++++.--------.+++.------.--------.>+.""")
|
|
190 |
|
|
191 |
|
|
192 |
// draws the Sierpinski triangle
|
|
193 |
//run(load_bff("sierpinski.bf"))
|
|
194 |
|
|
195 |
|
|
196 |
//fibonacci numbers below 100
|
|
197 |
// run("""+++++++++++
|
|
198 |
// >+>>>>++++++++++++++++++++++++++++++++++++++++++++
|
|
199 |
// >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
|
|
200 |
// +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
|
|
201 |
// <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
|
|
202 |
// -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
|
|
203 |
// >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
|
|
204 |
// +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
|
|
205 |
// ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
|
|
206 |
// <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
|
|
207 |
// [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""")
|
|
208 |
|
|
209 |
//outputs the square numbers up to 10000
|
|
210 |
// run("""++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
|
|
211 |
// >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]
|
|
212 |
// <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
|
|
213 |
|
|
214 |
|
|
215 |
// calculates 2 to the power of 6
|
|
216 |
//(example from a C-to-BF compiler at https://github.com/elikaski/BF-it)
|
|
217 |
// run(""">>[-]>[-]++>[-]++++++><<<>>>>[-]+><>[-]<<[-]>[>+<<+>-]>[<+>-]
|
|
218 |
// <><[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]
|
|
219 |
// <[>+>+<<-]>[<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<[[-]>[-]<<[>+>
|
|
220 |
// +<<-]>>[<<+>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]
|
|
221 |
// <<>>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<>[-]<<[-]
|
|
222 |
// >[>+<<+>-]>[<+>-]<><[-]>[-]<<<[>>+>+<<<-]>>>-[<<<+>>>-]<[-]>[-]
|
|
223 |
// <<<[>>+>+<<<-]>>>[<<<+>>>-][-]><<>>[-]>[-]<<<[>>[-]<[>+>+<<-]>
|
|
224 |
// [<+>-]+>[[-]<-<->>]<<<-]>>[<<+>>-]<<][-]>[-]<<[>+>+<<-]>>[<<+>
|
|
225 |
// >-]<<<<<[-]>>>>[<<<<+>>>>-]<<<<><>[-]<<[-]>[>+<<+>-]>[<+>-]<>
|
|
226 |
// <[-]>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<<>>[-]>[-]>[-]>[-]>[-]>
|
|
227 |
// [-]>[-]>[-]>[-]>[-]<<<<<<<<<<>>++++++++++<<[->+>-[>+>>]>[+[-<+
|
|
228 |
// >]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<
|
|
229 |
// ]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++
|
|
230 |
// ++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<<><<<""")
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
// a Mandelbrot set generator in brainf*** written by Erik Bosman
|
|
235 |
// (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/)
|
|
236 |
//
|
|
237 |
//run(load_bff("mandelbrot.bf"))
|
|
238 |
|
|
239 |
|
|
240 |
// a benchmark program (counts down from 'Z' to 'A')
|
|
241 |
//
|
|
242 |
//run(load_bff("benchmark.bf"))
|
|
243 |
|
|
244 |
// calculates the Collatz series for numbers from 1 to 30
|
|
245 |
//
|
|
246 |
//run(load_bff("collatz.bf"))
|
|
247 |
|
|
248 |
}
|