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