6
|
1 |
\documentclass{article}
|
62
|
2 |
\usepackage{../style}
|
78
|
3 |
\usepackage{../langs}
|
218
|
4 |
\usepackage{disclaimer}
|
|
5 |
\usepackage{tikz}
|
|
6 |
\usepackage{pgf}
|
|
7 |
\usepackage{pgfplots}
|
|
8 |
\usepackage{stackengine}
|
|
9 |
%% \usepackage{accents}
|
|
10 |
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
|
|
11 |
|
6
|
12 |
|
|
13 |
\begin{document}
|
|
14 |
|
218
|
15 |
% BF IDE
|
|
16 |
% https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5
|
|
17 |
|
221
|
18 |
\section*{Coursework 9 (Scala)}
|
6
|
19 |
|
229
|
20 |
This coursework is worth 10\%. It is about a small programming
|
|
21 |
language called brainf***. The first part is due on 13 December at
|
|
22 |
11pm; the second, more advanced part, is due on 20 December at
|
|
23 |
11pm.\bigskip
|
218
|
24 |
|
|
25 |
\IMPORTANT{}
|
62
|
26 |
|
|
27 |
\noindent
|
218
|
28 |
Also note that the running time of each part will be restricted to a
|
|
29 |
maximum of 30 seconds on my laptop.
|
|
30 |
|
|
31 |
\DISCLAIMER{}
|
86
|
32 |
|
230
|
33 |
\subsection*{Reference Implementation}
|
|
34 |
|
|
35 |
As usual, this Scala assignment comes with a reference implementation in form of
|
|
36 |
two \texttt{jar}-files. You can download them from KEATS. This allows you to run any
|
|
37 |
test cases on your own computer. For example you can call Scala on the command line with the
|
|
38 |
option \texttt{-cp bf.jar} and then query any function from the
|
|
39 |
\texttt{bf.scala} template file. You have to
|
|
40 |
prefix the calls with \texttt{CW10a} and \texttt{CW10b}, respectively. For example
|
|
41 |
|
|
42 |
|
|
43 |
\begin{lstlisting}[language={},xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small]
|
|
44 |
$ scala -cp bf.jar
|
|
45 |
scala> import CW10a._
|
|
46 |
scala> run(load_bff("sierpinski.bf"))
|
|
47 |
*
|
|
48 |
* *
|
|
49 |
* *
|
|
50 |
* * * *
|
|
51 |
* *
|
|
52 |
* * * *
|
|
53 |
* * * *
|
|
54 |
* * * * * * * *
|
|
55 |
* *
|
|
56 |
* * * *
|
|
57 |
* * * *
|
|
58 |
* * * * * * * *
|
|
59 |
* * * *
|
|
60 |
* * * * * * * *
|
|
61 |
* * * * * * * *
|
|
62 |
* * * * * * * * * * * * * * * *
|
|
63 |
* *
|
|
64 |
* * * *
|
|
65 |
* * * *
|
|
66 |
* * * * * * * *
|
|
67 |
* * * *
|
|
68 |
* * * * * * * *
|
|
69 |
* * * * * * * *
|
|
70 |
* * * * * * * * * * * * * * * *
|
|
71 |
* * * *
|
|
72 |
* * * * * * * *
|
|
73 |
* * * * * * * *
|
|
74 |
* * * * * * * * * * * * * * * *
|
|
75 |
* * * * * * * *
|
|
76 |
* * * * * * * * * * * * * * * *
|
|
77 |
* * * * * * * * * * * * * * * *
|
|
78 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
79 |
\end{lstlisting}%$
|
|
80 |
|
6
|
81 |
|
218
|
82 |
|
229
|
83 |
\subsection*{Part 1 (6 Marks)}
|
218
|
84 |
|
229
|
85 |
Coming from Java or C++, you might think Scala is a rather esoteric
|
218
|
86 |
programming language. But remember, some serious companies have built
|
|
87 |
their business on
|
|
88 |
Scala.\footnote{\url{https://en.wikipedia.org/wiki/Scala_(programming_language)\#Companies}}
|
230
|
89 |
I claim functional programming is not a fad. And there are far, far
|
|
90 |
more esoteric languages out there. One is called \emph{brainf***}. You
|
|
91 |
are asked in this part to implement an interpreter for
|
|
92 |
this language.
|
218
|
93 |
|
|
94 |
Urban M\"uller developed brainf*** in 1993. A close relative of this
|
|
95 |
language was already introduced in 1964 by Corado B\"ohm, an Italian
|
229
|
96 |
computer pioneer. The main feature of brainf*** is its minimalistic
|
|
97 |
set of instructions---just 8 instructions in total and all of which
|
|
98 |
are single characters. Despite the minimalism, this language has been
|
|
99 |
shown to be Turing complete\ldots{}if this doesn't ring any bell with
|
|
100 |
you: it roughly means that every algorithm we know can, in principle,
|
|
101 |
be implemented in brainf***. It just takes a lot of determination and
|
|
102 |
quite a lot of memory resources. Some relatively sophisticated sample
|
|
103 |
programs in brainf*** are given in the file \texttt{bf.scala}, including
|
230
|
104 |
a brainf*** program for the Sierpinski triangle and the Mandelbrot set.\bigskip
|
218
|
105 |
|
|
106 |
\noindent
|
|
107 |
As mentioned above, brainf*** has 8 single-character commands, namely
|
|
108 |
\texttt{'>'}, \texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'},
|
|
109 |
\texttt{','}, \texttt{'['} and \texttt{']'}. Every other character is
|
|
110 |
considered a comment. Brainf*** operates on memory cells containing
|
|
111 |
integers. For this it uses a single memory pointer that points at each
|
|
112 |
stage to one memory cell. This pointer can be moved forward by one
|
|
113 |
memory cell by using the command \texttt{'>'}, and backward by using
|
|
114 |
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase,
|
|
115 |
respectively decrease, by 1 the content of the memory cell to which
|
|
116 |
the memory pointer currently points to. The commands for input/output
|
|
117 |
are \texttt{','} and \texttt{'.'}. Output works by reading the content
|
|
118 |
of the memory cell to which the memory pointer points to and printing
|
|
119 |
it out as an ASCII character. Input works the other way, taking some
|
|
120 |
user input and storing it in the cell to which the memory pointer
|
|
121 |
points to. The commands \texttt{'['} and \texttt{']'} are looping
|
|
122 |
constructs. Everything in between \texttt{'['} and \texttt{']'} is
|
|
123 |
repeated until a counter (memory cell) reaches zero. A typical
|
|
124 |
program in brainf*** looks as follows:
|
|
125 |
|
|
126 |
\begin{center}
|
|
127 |
\begin{verbatim}
|
230
|
128 |
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.++
|
|
129 |
+++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
|
218
|
130 |
\end{verbatim}
|
|
131 |
\end{center}
|
|
132 |
|
|
133 |
\noindent
|
230
|
134 |
This one prints out Hello World\ldots{}obviously ;o)
|
218
|
135 |
|
|
136 |
\subsubsection*{Tasks (file bf.scala)}
|
109
|
137 |
|
|
138 |
\begin{itemize}
|
230
|
139 |
\item[(1)] Write a function that takes a file name as an argument
|
229
|
140 |
and requests the corresponding file from disk. It returns the
|
|
141 |
content of the file as a String. If the file does not exists,
|
|
142 |
the function should return the empty string.\\
|
|
143 |
\mbox{}\hfill[1 Mark]
|
|
144 |
|
|
145 |
\item[(2)] Brainf*** memory is represented by a \texttt{Map} from
|
218
|
146 |
integers to integers. The empty memory is represented by
|
|
147 |
\texttt{Map()}, that is nothing is stored in the
|
229
|
148 |
memory; \texttt{Map(0 -> 1, 2 -> 3)} stores \texttt{1} at
|
|
149 |
memory location \texttt{0}, and at \texttt{2} it stores \texttt{3}. The
|
218
|
150 |
convention is that if we query the memory at a location that is
|
|
151 |
\emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write
|
|
152 |
a function, \texttt{sread}, that takes a memory (a \texttt{Map}) and
|
229
|
153 |
a memory pointer (an \texttt{Int}) as argument, and `safely' reads the
|
218
|
154 |
corresponding memory location. If the \texttt{Map} is not defined at
|
|
155 |
the memory pointer, \texttt{sread} returns \texttt{0}.
|
|
156 |
|
|
157 |
Write another function \texttt{write}, which takes a memory, a
|
|
158 |
memory pointer and an integer value as argument and updates the
|
|
159 |
\texttt{Map} with the value at the given memory location. As usual
|
|
160 |
the \texttt{Map} is not updated `in-place' but a new map is created
|
|
161 |
with the same data, except the value is stored at the given memory
|
|
162 |
pointer.\hfill[1 Mark]
|
|
163 |
|
229
|
164 |
\item[(3)] Write two functions, \texttt{jumpRight} and
|
218
|
165 |
\texttt{jumpLeft} that are needed to implement the loop constructs
|
|
166 |
of brainf***. They take a program (a \texttt{String}) and a program
|
|
167 |
counter (an \texttt{Int}) as argument and move right (respectively
|
|
168 |
left) in the string in order to find the \textbf{matching}
|
|
169 |
opening/closing bracket. For example, given the following program
|
|
170 |
with the program counter indicated by an arrow:
|
|
171 |
|
|
172 |
\begin{center}
|
|
173 |
\texttt{--[\barbelow{.}.+>--],>,++}
|
|
174 |
\end{center}
|
|
175 |
|
|
176 |
then the matching closing bracket is in 9th position (counting from 0) and
|
|
177 |
\texttt{jumpRight} is supposed to return the position just after this
|
109
|
178 |
|
218
|
179 |
\begin{center}
|
|
180 |
\texttt{--[..+>--]\barbelow{,}>,++}
|
|
181 |
\end{center}
|
|
182 |
|
|
183 |
meaning it jumps to after the loop. Similarly, if you are in 8th position
|
|
184 |
then \texttt{jumpLeft} is supposed to jump to just after the opening
|
|
185 |
bracket (that is jumping to the beginning of the loop):
|
109
|
186 |
|
218
|
187 |
\begin{center}
|
|
188 |
\texttt{--[..+>-\barbelow{-}],>,++}
|
|
189 |
\qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad
|
|
190 |
\texttt{--[\barbelow{.}.+>--],>,++}
|
|
191 |
\end{center}
|
|
192 |
|
|
193 |
Unfortunately we have to take into account that there might be
|
|
194 |
other opening and closing brackets on the `way' to find the
|
|
195 |
matching bracket. For example in the brainf*** program
|
|
196 |
|
|
197 |
\begin{center}
|
|
198 |
\texttt{--[\barbelow{.}.[+>]--],>,++}
|
|
199 |
\end{center}
|
109
|
200 |
|
218
|
201 |
we do not want to return the index for the \texttt{'-'} in the 9th
|
|
202 |
position, but the program counter for \texttt{','} in 12th
|
|
203 |
position. The easiest to find out whether a bracket is matched is by
|
|
204 |
using levels (which are the third argument in \texttt{jumpLeft} and
|
|
205 |
\texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the
|
|
206 |
level by one whenever you find an opening bracket and decrease by
|
|
207 |
one for a closing bracket. Then in \texttt{jumpRight} you are looking
|
|
208 |
for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you
|
|
209 |
do the opposite. In this way you can find \textbf{matching} brackets
|
|
210 |
in strings such as
|
|
211 |
|
|
212 |
\begin{center}
|
|
213 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--],>,++}
|
|
214 |
\end{center}
|
109
|
215 |
|
218
|
216 |
for which \texttt{jumpRight} should produce the position:
|
|
217 |
|
|
218 |
\begin{center}
|
|
219 |
\texttt{--[..[[-]+>[.]]--]\barbelow{,}>,++}
|
|
220 |
\end{center}
|
|
221 |
|
|
222 |
It is also possible that the position returned by \texttt{jumpRight} or
|
|
223 |
\texttt{jumpLeft} is outside the string in cases where there are
|
|
224 |
no matching brackets. For example
|
|
225 |
|
|
226 |
\begin{center}
|
|
227 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--,>,++}
|
|
228 |
\qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad
|
|
229 |
\texttt{--[..[[-]+>[.]]-->,++\barbelow{\;\phantom{+}}}
|
|
230 |
\end{center}
|
229
|
231 |
\hfill[2 Marks]
|
109
|
232 |
|
|
233 |
|
230
|
234 |
\item[(4)] Write a recursive function \texttt{compute} that runs a
|
218
|
235 |
brainf*** program. It takes a program, a program counter, a memory
|
|
236 |
pointer and a memory as arguments. If the program counter is outside
|
230
|
237 |
the program string, the execution stops and \texttt{compute} returns the
|
218
|
238 |
memory. If the program counter is inside the string, it reads the
|
|
239 |
corresponding character and updates the program counter \texttt{pc},
|
|
240 |
memory pointer \texttt{mp} and memory \texttt{mem} according to the
|
|
241 |
rules shown in Figure~\ref{comms}. It then calls recursively
|
230
|
242 |
\texttt{compute} with the updated data. The most convenient way to
|
|
243 |
implement the brainf**k rules in Scala is to use pattern-matching
|
|
244 |
and to calculate a triple consisting of the updated \texttt{pc},
|
229
|
245 |
\texttt{mp} and \texttt{mem}.
|
218
|
246 |
|
230
|
247 |
Write another function \texttt{run} that calls \texttt{compute} with a
|
218
|
248 |
given brainfu** program and memory, and the program counter and memory pointer
|
230
|
249 |
set to~$0$. Like \texttt{compute}, it returns the memory after the execution
|
218
|
250 |
of the program finishes. You can test your brainf**k interpreter with the
|
229
|
251 |
Sierpinski triangle or the Hello world programs (they seem to be particularly
|
|
252 |
useful for debugging purposes), or have a look at
|
109
|
253 |
|
218
|
254 |
\begin{center}
|
|
255 |
\url{https://esolangs.org/wiki/Brainfuck}
|
|
256 |
\end{center}\hfill[2 Marks]
|
109
|
257 |
|
218
|
258 |
\begin{figure}[p]
|
|
259 |
\begin{center}
|
230
|
260 |
\begin{tabular}{|@{\hspace{0.5mm}}p{0.8cm}|l|}
|
218
|
261 |
\hline
|
|
262 |
\hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
263 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
264 |
$\bullet$ & $\texttt{mp} + 1$\\
|
|
265 |
$\bullet$ & \texttt{mem} unchanged
|
|
266 |
\end{tabular}\\\hline
|
|
267 |
\hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
268 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
269 |
$\bullet$ & $\texttt{mp} - 1$\\
|
|
270 |
$\bullet$ & \texttt{mem} unchanged
|
|
271 |
\end{tabular}\\\hline
|
|
272 |
\hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
273 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
274 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
275 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\
|
|
276 |
\end{tabular}\\\hline
|
|
277 |
\hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
278 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
279 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
280 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\
|
|
281 |
\end{tabular}\\\hline
|
|
282 |
\hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
283 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
284 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
|
285 |
$\bullet$ & print out \,\texttt{mem(mp)} as a character\\
|
|
286 |
\end{tabular}\\\hline
|
|
287 |
\hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
288 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
289 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
290 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
|
|
291 |
\multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
|
|
292 |
\end{tabular}\\\hline
|
|
293 |
\hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
294 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\
|
|
295 |
$\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\
|
|
296 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
|
|
297 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\
|
|
298 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
299 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
|
300 |
\end{tabular}
|
|
301 |
\\\hline
|
|
302 |
\hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
303 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\
|
|
304 |
$\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\
|
|
305 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
|
|
306 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\
|
|
307 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
308 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
|
309 |
\end{tabular}\\\hline
|
|
310 |
any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
311 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
312 |
$\bullet$ & \texttt{mp} and \texttt{mem} unchanged
|
|
313 |
\end{tabular}\\
|
|
314 |
\hline
|
|
315 |
\end{tabular}
|
|
316 |
\end{center}
|
|
317 |
\caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc},
|
230
|
318 |
the memory pointer \texttt{mp} and the memory \texttt{mem}.\label{comms}}
|
218
|
319 |
\end{figure}
|
|
320 |
\end{itemize}\bigskip
|
|
321 |
|
229
|
322 |
\subsection*{Part 2 (4 Marks)}
|
218
|
323 |
|
229
|
324 |
While it is fun to look at bf-programs, like the Sierpinski triangle or the Mandelbrot
|
|
325 |
program, being interpreted, it is much more fun to write a compiler for the bf-language.
|
218
|
326 |
|
6
|
327 |
|
|
328 |
\end{document}
|
|
329 |
|
68
|
330 |
|
6
|
331 |
%%% Local Variables:
|
|
332 |
%%% mode: latex
|
|
333 |
%%% TeX-master: t
|
|
334 |
%%% End:
|