251
|
1 |
% !TEX program = xelatex
|
6
|
2 |
\documentclass{article}
|
62
|
3 |
\usepackage{../style}
|
78
|
4 |
\usepackage{../langs}
|
218
|
5 |
\usepackage{disclaimer}
|
|
6 |
\usepackage{tikz}
|
|
7 |
\usepackage{pgf}
|
|
8 |
\usepackage{pgfplots}
|
|
9 |
\usepackage{stackengine}
|
|
10 |
%% \usepackage{accents}
|
|
11 |
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
|
|
12 |
|
325
|
13 |
%% change Console to scala.io.StdIn.readByte()
|
|
14 |
|
6
|
15 |
|
|
16 |
\begin{document}
|
|
17 |
|
307
|
18 |
\section*{Part 10 (Scala)}
|
6
|
19 |
|
336
|
20 |
\mbox{}\hfill\textit{``If there's one feature that makes Scala, `Scala',}\\
|
|
21 |
\mbox{}\hfill\textit{ I would pick implicits.''}\smallskip\\
|
|
22 |
\mbox{}\hfill\textit{ --- Martin Odersky (creator of the Scala language)}\bigskip\bigskip
|
278
|
23 |
|
|
24 |
|
|
25 |
\noindent
|
336
|
26 |
This part is about a small (esotheric) programming language called
|
|
27 |
brainf***. Actually, we will implement an interpreter for our own version
|
|
28 |
of this language called brainf*ck++.\bigskip
|
218
|
29 |
|
336
|
30 |
\IMPORTANT{This part is worth 10\% and you need to submit on \cwTEN{} at 4pm.}
|
62
|
31 |
|
|
32 |
\noindent
|
218
|
33 |
Also note that the running time of each part will be restricted to a
|
|
34 |
maximum of 30 seconds on my laptop.
|
|
35 |
|
|
36 |
\DISCLAIMER{}
|
268
|
37 |
\newpage
|
86
|
38 |
|
230
|
39 |
\subsection*{Reference Implementation}
|
|
40 |
|
268
|
41 |
As usual, this Scala assignment comes with a reference implementation in
|
|
42 |
form of two \texttt{jar}-files. You can download them from KEATS. They
|
|
43 |
allow you to run any test cases on your own computer. For example you
|
|
44 |
can call Scala on the command line with the option \texttt{-cp bf.jar}
|
|
45 |
and then query any function from the \texttt{bf.scala} template file.
|
|
46 |
You have to prefix the calls with \texttt{CW10a} and \texttt{CW10b},
|
|
47 |
respectively. For example
|
230
|
48 |
|
|
49 |
|
|
50 |
\begin{lstlisting}[language={},xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small]
|
|
51 |
$ scala -cp bf.jar
|
|
52 |
scala> import CW10a._
|
292
|
53 |
scala> run(load_bff("sierpinski.bf")) ; ()
|
230
|
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 |
* * * * * * * *
|
|
80 |
* * * * * * * *
|
|
81 |
* * * * * * * * * * * * * * * *
|
|
82 |
* * * * * * * *
|
|
83 |
* * * * * * * * * * * * * * * *
|
|
84 |
* * * * * * * * * * * * * * * *
|
|
85 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
86 |
\end{lstlisting}%$
|
|
87 |
|
268
|
88 |
\newpage
|
218
|
89 |
|
307
|
90 |
\subsection*{Part A (6 Marks)}
|
218
|
91 |
|
229
|
92 |
Coming from Java or C++, you might think Scala is a rather esoteric
|
218
|
93 |
programming language. But remember, some serious companies have built
|
|
94 |
their business on
|
|
95 |
Scala.\footnote{\url{https://en.wikipedia.org/wiki/Scala_(programming_language)\#Companies}}
|
230
|
96 |
I claim functional programming is not a fad. And there are far, far
|
336
|
97 |
more esoteric languages out there. One is called \emph{brainf***}.
|
|
98 |
\here{https://esolangs.org/wiki/Brainfuck}
|
|
99 |
You
|
230
|
100 |
are asked in this part to implement an interpreter for
|
336
|
101 |
a slight extension of this language.
|
218
|
102 |
|
336
|
103 |
Urban M\"uller developed the original version of brainf*** in 1993. A close
|
|
104 |
relative of this language was already introduced in 1964 by Corado
|
|
105 |
B\"ohm, an Italian computer pioneer. The main feature of brainf*** is
|
|
106 |
its minimalistic set of instructions---just 8 instructions in total
|
|
107 |
and all of which are single characters. Despite the minimalism, this
|
|
108 |
language has been shown to be Turing complete\ldots{}if this doesn't
|
|
109 |
ring any bell with you: it roughly means that every(!) algorithm can,
|
|
110 |
in principle, be implemented in brainf***. It just takes a lot of
|
|
111 |
determination and quite a lot of memory resources.
|
|
112 |
|
|
113 |
Some relatively sophisticated sample programs in brainf*** are given
|
|
114 |
in the file \texttt{bf.scala}, including a brainf*** program for the
|
|
115 |
Sierpinski triangle and the Mandelbrot set. There seems to be even a
|
|
116 |
dedicated Windows IDE for bf programs, though I am not sure whether
|
|
117 |
this is just an elaborate April fools' joke---judge yourself:
|
247
|
118 |
|
|
119 |
\begin{center}
|
|
120 |
\url{https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5}
|
268
|
121 |
\end{center} \bigskip
|
247
|
122 |
|
218
|
123 |
|
|
124 |
\noindent
|
336
|
125 |
As mentioned above, the original brainf*** has 8 single-character
|
|
126 |
commands. Our version of bf++ will contain the commands \texttt{'>'},
|
|
127 |
\texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'}, \texttt{'['}
|
|
128 |
and \texttt{']'} from the original, and in addition the commands
|
|
129 |
\texttt{'@'}, \texttt{'*'} and \texttt{'\#'}. Every other character
|
|
130 |
is considered a comment.
|
|
131 |
|
|
132 |
Our interpreter for bf++ operates on memory cells containing
|
218
|
133 |
integers. For this it uses a single memory pointer that points at each
|
|
134 |
stage to one memory cell. This pointer can be moved forward by one
|
|
135 |
memory cell by using the command \texttt{'>'}, and backward by using
|
|
136 |
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase,
|
|
137 |
respectively decrease, by 1 the content of the memory cell to which
|
336
|
138 |
the memory pointer currently points to. The command for output is
|
|
139 |
\texttt{'.'} whereby output works by reading the content of the memory
|
|
140 |
cell to which the memory pointer points to and printing it out as an
|
|
141 |
ASCII character.\footnote{In the originial version of bf, there is also a
|
|
142 |
command for input, but we omit it here. All our programs will be
|
|
143 |
``autonomous''.} The
|
|
144 |
commands \texttt{'['} and \texttt{']'} are looping
|
218
|
145 |
constructs. Everything in between \texttt{'['} and \texttt{']'} is
|
|
146 |
repeated until a counter (memory cell) reaches zero. A typical
|
|
147 |
program in brainf*** looks as follows:
|
|
148 |
|
|
149 |
\begin{center}
|
|
150 |
\begin{verbatim}
|
230
|
151 |
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.++
|
|
152 |
+++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
|
218
|
153 |
\end{verbatim}
|
|
154 |
\end{center}
|
|
155 |
|
|
156 |
\noindent
|
336
|
157 |
This one prints out Hello World\ldots{}obviously \texttt{;o)} We also
|
|
158 |
add 3 new commands to the bf++ langauge, whose purpose we explain later.
|
|
159 |
|
218
|
160 |
|
|
161 |
\subsubsection*{Tasks (file bf.scala)}
|
109
|
162 |
|
|
163 |
\begin{itemize}
|
237
|
164 |
\item[(1)] Write a function that takes a filename (a string) as an argument
|
229
|
165 |
and requests the corresponding file from disk. It returns the
|
237
|
166 |
content of the file as a string. If the file does not exists,
|
229
|
167 |
the function should return the empty string.\\
|
|
168 |
\mbox{}\hfill[1 Mark]
|
|
169 |
|
336
|
170 |
\item[(2)] Brainf**k++ memory is represented by a \texttt{Map} from
|
218
|
171 |
integers to integers. The empty memory is represented by
|
|
172 |
\texttt{Map()}, that is nothing is stored in the
|
229
|
173 |
memory; \texttt{Map(0 -> 1, 2 -> 3)} stores \texttt{1} at
|
|
174 |
memory location \texttt{0}, and at \texttt{2} it stores \texttt{3}. The
|
218
|
175 |
convention is that if we query the memory at a location that is
|
|
176 |
\emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write
|
268
|
177 |
a `safe-read' function, \texttt{sread}, that takes a memory (a \texttt{Map}) and
|
237
|
178 |
a memory pointer (an \texttt{Int}) as arguments, and `safely' reads the
|
218
|
179 |
corresponding memory location. If the \texttt{Map} is not defined at
|
|
180 |
the memory pointer, \texttt{sread} returns \texttt{0}.
|
|
181 |
|
|
182 |
Write another function \texttt{write}, which takes a memory, a
|
237
|
183 |
memory pointer and an integer value as arguments and updates the
|
|
184 |
\texttt{Map} with the value at the given memory location. As usual,
|
218
|
185 |
the \texttt{Map} is not updated `in-place' but a new map is created
|
237
|
186 |
with the same data, except the new value is stored at the given memory
|
218
|
187 |
pointer.\hfill[1 Mark]
|
|
188 |
|
229
|
189 |
\item[(3)] Write two functions, \texttt{jumpRight} and
|
237
|
190 |
\texttt{jumpLeft}, that are needed to implement the loop constructs
|
336
|
191 |
in brainf**k++. They take a program (a \texttt{String}) and a program
|
237
|
192 |
counter (an \texttt{Int}) as arguments and move right (respectively
|
218
|
193 |
left) in the string in order to find the \textbf{matching}
|
|
194 |
opening/closing bracket. For example, given the following program
|
|
195 |
with the program counter indicated by an arrow:
|
|
196 |
|
|
197 |
\begin{center}
|
336
|
198 |
\texttt{--[\barbelow{.}.+>--].>.++}
|
218
|
199 |
\end{center}
|
|
200 |
|
|
201 |
then the matching closing bracket is in 9th position (counting from 0) and
|
|
202 |
\texttt{jumpRight} is supposed to return the position just after this
|
109
|
203 |
|
218
|
204 |
\begin{center}
|
336
|
205 |
\texttt{--[..+>--]\barbelow{.}>.++}
|
218
|
206 |
\end{center}
|
|
207 |
|
237
|
208 |
meaning it jumps to after the loop. Similarly, if you are in 8th position,
|
218
|
209 |
then \texttt{jumpLeft} is supposed to jump to just after the opening
|
|
210 |
bracket (that is jumping to the beginning of the loop):
|
109
|
211 |
|
218
|
212 |
\begin{center}
|
336
|
213 |
\texttt{--[..+>-\barbelow{-}].>.++}
|
218
|
214 |
\qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad
|
336
|
215 |
\texttt{--[\barbelow{.}.+>--].>.++}
|
218
|
216 |
\end{center}
|
|
217 |
|
|
218 |
Unfortunately we have to take into account that there might be
|
|
219 |
other opening and closing brackets on the `way' to find the
|
336
|
220 |
matching bracket. For example in the brain*ck++ program
|
218
|
221 |
|
|
222 |
\begin{center}
|
336
|
223 |
\texttt{--[\barbelow{.}.[+>]--].>.++}
|
218
|
224 |
\end{center}
|
109
|
225 |
|
218
|
226 |
we do not want to return the index for the \texttt{'-'} in the 9th
|
336
|
227 |
position, but the program counter for \texttt{'.'} in 12th
|
218
|
228 |
position. The easiest to find out whether a bracket is matched is by
|
|
229 |
using levels (which are the third argument in \texttt{jumpLeft} and
|
|
230 |
\texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the
|
|
231 |
level by one whenever you find an opening bracket and decrease by
|
|
232 |
one for a closing bracket. Then in \texttt{jumpRight} you are looking
|
|
233 |
for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you
|
|
234 |
do the opposite. In this way you can find \textbf{matching} brackets
|
|
235 |
in strings such as
|
|
236 |
|
|
237 |
\begin{center}
|
336
|
238 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--].>.++}
|
218
|
239 |
\end{center}
|
109
|
240 |
|
218
|
241 |
for which \texttt{jumpRight} should produce the position:
|
|
242 |
|
|
243 |
\begin{center}
|
336
|
244 |
\texttt{--[..[[-]+>[.]]--]\barbelow{.}>.++}
|
218
|
245 |
\end{center}
|
|
246 |
|
|
247 |
It is also possible that the position returned by \texttt{jumpRight} or
|
|
248 |
\texttt{jumpLeft} is outside the string in cases where there are
|
|
249 |
no matching brackets. For example
|
|
250 |
|
|
251 |
\begin{center}
|
336
|
252 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--.>.++}
|
218
|
253 |
\qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad
|
336
|
254 |
\texttt{--[..[[-]+>[.]]-->.++\barbelow{\;\phantom{+}}}
|
218
|
255 |
\end{center}
|
229
|
256 |
\hfill[2 Marks]
|
109
|
257 |
|
|
258 |
|
230
|
259 |
\item[(4)] Write a recursive function \texttt{compute} that runs a
|
336
|
260 |
brain*u*k++ program. It takes a program, a program counter, a memory
|
218
|
261 |
pointer and a memory as arguments. If the program counter is outside
|
230
|
262 |
the program string, the execution stops and \texttt{compute} returns the
|
218
|
263 |
memory. If the program counter is inside the string, it reads the
|
|
264 |
corresponding character and updates the program counter \texttt{pc},
|
|
265 |
memory pointer \texttt{mp} and memory \texttt{mem} according to the
|
|
266 |
rules shown in Figure~\ref{comms}. It then calls recursively
|
230
|
267 |
\texttt{compute} with the updated data. The most convenient way to
|
336
|
268 |
implement the brainf**k++ rules in Scala is to use pattern-matching
|
230
|
269 |
and to calculate a triple consisting of the updated \texttt{pc},
|
229
|
270 |
\texttt{mp} and \texttt{mem}.
|
218
|
271 |
|
230
|
272 |
Write another function \texttt{run} that calls \texttt{compute} with a
|
336
|
273 |
given brainfu*k++ program and memory, and the program counter and memory pointer
|
230
|
274 |
set to~$0$. Like \texttt{compute}, it returns the memory after the execution
|
336
|
275 |
of the program finishes. You can test your brainf**k++ interpreter with the
|
229
|
276 |
Sierpinski triangle or the Hello world programs (they seem to be particularly
|
|
277 |
useful for debugging purposes), or have a look at
|
109
|
278 |
|
218
|
279 |
\begin{center}
|
|
280 |
\url{https://esolangs.org/wiki/Brainfuck}
|
|
281 |
\end{center}\hfill[2 Marks]
|
109
|
282 |
|
218
|
283 |
\begin{figure}[p]
|
|
284 |
\begin{center}
|
230
|
285 |
\begin{tabular}{|@{\hspace{0.5mm}}p{0.8cm}|l|}
|
218
|
286 |
\hline
|
|
287 |
\hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
288 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
289 |
$\bullet$ & $\texttt{mp} + 1$\\
|
|
290 |
$\bullet$ & \texttt{mem} unchanged
|
|
291 |
\end{tabular}\\\hline
|
|
292 |
\hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
293 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
294 |
$\bullet$ & $\texttt{mp} - 1$\\
|
|
295 |
$\bullet$ & \texttt{mem} unchanged
|
|
296 |
\end{tabular}\\\hline
|
|
297 |
\hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
298 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
299 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
300 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\
|
|
301 |
\end{tabular}\\\hline
|
|
302 |
\hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
303 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
304 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
305 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\
|
|
306 |
\end{tabular}\\\hline
|
|
307 |
\hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
308 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
309 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
|
310 |
$\bullet$ & print out \,\texttt{mem(mp)} as a character\\
|
|
311 |
\end{tabular}\\\hline
|
336
|
312 |
%\hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
313 |
% $\bullet$ & $\texttt{pc} + 1$\\
|
|
314 |
% $\bullet$ & $\texttt{mp}$ unchanged\\
|
|
315 |
% $\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
|
|
316 |
% \multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
|
|
317 |
% \end{tabular}\\\hline
|
218
|
318 |
\hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
319 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\
|
|
320 |
$\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\
|
|
321 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
|
|
322 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\
|
|
323 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
324 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
|
325 |
\end{tabular}
|
|
326 |
\\\hline
|
|
327 |
\hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
328 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\
|
|
329 |
$\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\
|
|
330 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
|
|
331 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\
|
|
332 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
333 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
|
336
|
334 |
\end{tabular}\\\hline
|
|
335 |
\hfill\texttt{'@'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
336 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
337 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
338 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
|
|
339 |
\multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
|
|
340 |
\end{tabular}\\\hline
|
|
341 |
\hfill\texttt{'*'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
342 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
343 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
344 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
|
|
345 |
\multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
|
|
346 |
\end{tabular}\\\hline
|
|
347 |
\hfill\texttt{'\#'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
348 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
349 |
$\bullet$ & $\texttt{mp}$ unchanged\\
|
|
350 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
|
|
351 |
\multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
|
|
352 |
\end{tabular}\\\hline
|
218
|
353 |
any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
|
|
354 |
$\bullet$ & $\texttt{pc} + 1$\\
|
|
355 |
$\bullet$ & \texttt{mp} and \texttt{mem} unchanged
|
|
356 |
\end{tabular}\\
|
|
357 |
\hline
|
|
358 |
\end{tabular}
|
|
359 |
\end{center}
|
|
360 |
\caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc},
|
230
|
361 |
the memory pointer \texttt{mp} and the memory \texttt{mem}.\label{comms}}
|
218
|
362 |
\end{figure}
|
|
363 |
\end{itemize}\bigskip
|
|
364 |
|
268
|
365 |
%%\newpage
|
233
|
366 |
|
307
|
367 |
\subsection*{Part B (4 Marks)}
|
218
|
368 |
|
233
|
369 |
I am sure you agree while it is fun to look at bf-programs, like the
|
|
370 |
Sierpinski triangle or the Mandelbrot program, being interpreted, it
|
|
371 |
is much more fun to write a compiler for the bf-language.
|
|
372 |
|
|
373 |
|
|
374 |
\subsubsection*{Tasks (file bfc.scala)}
|
|
375 |
|
|
376 |
\begin{itemize}
|
|
377 |
\item[(5)] Compilers in general attempt to make programs run
|
|
378 |
faster by precomputing as much information as possible
|
|
379 |
before running the program. In our case we can precompute the
|
|
380 |
addresses where we need to jump at the beginning and end of
|
|
381 |
loops.
|
|
382 |
|
|
383 |
For this write a function \texttt{jtable} that precomputes the ``jump
|
|
384 |
table'' for a bf-program. This function takes a bf-program
|
|
385 |
as an argument and returns a \texttt{Map[Int, Int]}. The
|
237
|
386 |
purpose of this Map is to record the information, in cases
|
|
387 |
a pc-position points to a '\texttt{[}' or a '\texttt{]}',
|
|
388 |
to which pc-position do we need to jump next?
|
233
|
389 |
|
|
390 |
For example for the program
|
|
391 |
|
|
392 |
\begin{center}
|
|
393 |
\texttt{+++++[->++++++++++<]>--<+++[->>++++++++++}
|
|
394 |
\texttt{<<]>>++<<----------[+>.>.<+<]}
|
|
395 |
\end{center}
|
|
396 |
|
|
397 |
we obtain the Map (note the precise numbers might differ depending on white
|
|
398 |
spaces etc.~in the bf-program):
|
|
399 |
|
|
400 |
\begin{center}
|
|
401 |
\texttt{Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)}
|
|
402 |
\end{center}
|
|
403 |
|
|
404 |
This Map states that for the '\texttt{[}' on position 5, we need to
|
|
405 |
jump to position 20, which is just after the corresponding '\texttt{]}'.
|
|
406 |
Similarly, for the '\texttt{]}' on position 19, we need to jump to
|
|
407 |
position 6, which is just after the '\texttt{[}' on position 5, and so
|
|
408 |
on. The idea is to not calculate this information each time
|
|
409 |
we hit a bracket, but just look up this information in the
|
|
410 |
\texttt{jtable}.
|
|
411 |
|
|
412 |
Then adapt the \texttt{compute} and \texttt{run} functions
|
|
413 |
from Part 1 in order to take advantage of the information
|
|
414 |
stored in the \texttt{jtable}. This means whenever \texttt{jumpLeft}
|
|
415 |
and \texttt{jumpRight} was called previously, you should look
|
|
416 |
up the jump address in the \texttt{jtable}. Feel free to reuse
|
|
417 |
the function \texttt{jumpLeft} and \texttt{jumpRight} for
|
|
418 |
calculating the \texttt{jtable}.\hfill{[1 Mark]}
|
|
419 |
|
237
|
420 |
\item[(6)] Compilers try to eliminate any ``dead'' code that could
|
|
421 |
slow down programs and also perform what is often called
|
|
422 |
\emph{peephole
|
|
423 |
optimisations}.\footnote{\url{https://en.wikipedia.org/wiki/Peephole_optimization}}
|
|
424 |
For the latter consider that it is difficult for compilers to
|
241
|
425 |
comprehend what is intended with whole programs, but they are very good
|
237
|
426 |
at finding out what small snippets of code do, and then try to
|
|
427 |
generate faster code for such snippets.
|
233
|
428 |
|
|
429 |
In our case, dead code is everything that is not a bf-command.
|
|
430 |
Therefore write a function \texttt{optimise} which deletes such
|
237
|
431 |
dead code from a bf-program. Moreover this function should replace every substring
|
233
|
432 |
of the form \pcode{[-]} by a new command \texttt{0}.
|
|
433 |
The idea is that the loop \pcode{[-]} just resets the
|
|
434 |
memory at the current location to 0. It is more efficient
|
237
|
435 |
to do this in a single step, rather than stepwise in a loop as in
|
233
|
436 |
the original bf-programs.
|
218
|
437 |
|
233
|
438 |
In the extended \texttt{compute3} and \texttt{run3} functions you should
|
|
439 |
implement this command by writing 0 to \pcode{mem(mp)}, that is use
|
|
440 |
\pcode{write(mem, mp, 0)} as the rule for the command \texttt{0}.
|
|
441 |
The easiest way to modify a string in this way is to use the regular
|
|
442 |
expression \pcode{"""[^<>+-.,\\[\\]]"""}, which recognises everything that is
|
|
443 |
not a bf-command. Similarly, the
|
237
|
444 |
regular expression \pcode{"""\\[-\\]"""} finds all occurrences of \pcode{[-]}. By using the Scala method \pcode{.replaceAll} you can replace substrings
|
|
445 |
with new strings.\\
|
|
446 |
\mbox{}\hfill{[1 Mark]}
|
233
|
447 |
|
|
448 |
\item[(7)] Finally, real compilers try to take advantage of CPUs which often
|
|
449 |
provide complex operations in hardware that can combine many smaller
|
|
450 |
instructions into a single faster instruction.
|
|
451 |
|
237
|
452 |
In our case we can optimise the several single increments performed at a
|
233
|
453 |
memory cell, for example \pcode{++++}, by a single ``increment by
|
|
454 |
4''. For this optimisation we just have to make sure these single
|
237
|
455 |
increments are all next to each other. Similar optimisations should apply
|
233
|
456 |
for the bf-commands \pcode{-}, \pcode{<} and
|
|
457 |
\pcode{>}, which can all be replaced by extended versions that take
|
|
458 |
the amount of the increment (decrement) into account. We will do
|
|
459 |
this by introducing two-character bf-commands. For example
|
|
460 |
|
|
461 |
\begin{center}
|
|
462 |
\begin{tabular}{l|l}
|
|
463 |
original bf-cmds & replacement\\
|
|
464 |
\hline
|
|
465 |
\pcode{+} & \pcode{+A}\\
|
|
466 |
\pcode{++} & \pcode{+B}\\
|
|
467 |
\pcode{+++} & \pcode{+C}\\
|
|
468 |
\ldots{} & \ldots{}\\
|
|
469 |
\pcode{+++....++} & \pcode{+Z}\\
|
|
470 |
\hspace{5mm}(these are 26 \pcode{+}'s)\\
|
|
471 |
\end{tabular}
|
|
472 |
\end{center}
|
|
473 |
|
|
474 |
|
237
|
475 |
If there are more
|
|
476 |
than 26 \pcode{+}'s in a row, then more than one ``two-character''
|
233
|
477 |
bf-commands need to be generated (the idea is that more than
|
|
478 |
26 copies of a single bf-command in a row is a rare occurrence in
|
237
|
479 |
actual bf-programs). Similar replacements apply
|
|
480 |
for \pcode{-}, \pcode{<} and \pcode{>}, but
|
|
481 |
all other bf-commands should be unaffected by this
|
233
|
482 |
change.
|
|
483 |
|
|
484 |
For this write a function \texttt{combine} which replaces sequences
|
|
485 |
of repeated increment and decrement commands by appropriate
|
237
|
486 |
two-character commands. In the functions \pcode{compute4} and
|
233
|
487 |
\pcode{run4}, the ``combine'' and the optimisation from (6) should
|
|
488 |
be performed. Make sure that when a two-character bf-command is
|
|
489 |
encountered you need to increase the \pcode{pc}-counter by two in
|
237
|
490 |
order to progress to the next command. For example
|
233
|
491 |
|
|
492 |
\begin{center}
|
|
493 |
\pcode{combine(optimise(load_bff("benchmark.bf")))}
|
|
494 |
\end{center}
|
|
495 |
|
|
496 |
generates the improved program
|
|
497 |
|
|
498 |
\begin{center}
|
|
499 |
\pcode{>A+B[<A+M>A-A]<A[[}\hspace{3mm}\ldots{}
|
|
500 |
\end{center}
|
|
501 |
|
|
502 |
for the original benchmark program
|
|
503 |
|
|
504 |
\begin{center}
|
|
505 |
\pcode{>++[<+++++++++++++>-]<[[}\hspace{3mm}\ldots
|
|
506 |
\end{center}
|
|
507 |
|
241
|
508 |
As you can see, the compiler bets on saving a lot of time on the
|
|
509 |
\pcode{+B} and \pcode{+M} steps so that the optimisations is
|
233
|
510 |
worthwhile overall (of course for the \pcode{>A}'s and so on, the compiler incurs a
|
|
511 |
penalty). Luckily, after you have performed all
|
|
512 |
optimisations in (5) - (7), you can expect that the
|
234
|
513 |
\pcode{benchmark.bf} program runs four to five times faster.
|
|
514 |
You can also test whether your compiler produces the correct result
|
|
515 |
by for example testing
|
|
516 |
|
|
517 |
\begin{center}
|
|
518 |
\pcode{run(load_bff("sierpinski.bf")) == run4(load_bff("sierpinski.bf"))}
|
|
519 |
\end{center}
|
|
520 |
|
|
521 |
which should return true for all the different compiler stages. \\
|
|
522 |
\mbox{}\hfill{[2 Marks]}
|
233
|
523 |
\end{itemize}
|
6
|
524 |
|
|
525 |
\end{document}
|
|
526 |
|
68
|
527 |
|
6
|
528 |
%%% Local Variables:
|
|
529 |
%%% mode: latex
|
|
530 |
%%% TeX-master: t
|
|
531 |
%%% End:
|