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