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