| 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 | 
 | 
| 233 |    322 | \newpage
 | 
|  |    323 | 
 | 
| 229 |    324 | \subsection*{Part 2 (4 Marks)}
 | 
| 218 |    325 | 
 | 
| 233 |    326 | I am sure you agree while it is fun to look at bf-programs, like the
 | 
|  |    327 | Sierpinski triangle or the Mandelbrot program, being interpreted, it
 | 
|  |    328 | is much more fun to write a compiler for the bf-language.
 | 
|  |    329 | 
 | 
|  |    330 | 
 | 
|  |    331 | \subsubsection*{Tasks (file bfc.scala)}
 | 
|  |    332 | 
 | 
|  |    333 | \begin{itemize}
 | 
|  |    334 | \item[(5)] Compilers in general attempt to make programs run
 | 
|  |    335 |   faster by precomputing as much information as possible
 | 
|  |    336 |   before running the program. In our case we can precompute the
 | 
|  |    337 |   addresses where we need to jump at the beginning and end of
 | 
|  |    338 |   loops. 
 | 
|  |    339 | 
 | 
|  |    340 |   For this write a function \texttt{jtable} that precomputes the ``jump
 | 
|  |    341 |   table'' for a bf-program. This function takes a bf-program 
 | 
|  |    342 |   as an argument and returns a \texttt{Map[Int, Int]}. The 
 | 
|  |    343 |   purpose of this Map is to record the information
 | 
|  |    344 |   that given on the pc-position $pc$ is a '\texttt{[}' or a '\texttt{]}',
 | 
|  |    345 |   then to which pc-position do we need to jump next?
 | 
|  |    346 |  
 | 
|  |    347 |   For example for the program
 | 
|  |    348 |     
 | 
|  |    349 |   \begin{center}
 | 
|  |    350 |     \texttt{+++++[->++++++++++<]>--<+++[->>++++++++++}
 | 
|  |    351 |     \texttt{<<]>>++<<----------[+>.>.<+<]}
 | 
|  |    352 |   \end{center}
 | 
|  |    353 | 
 | 
|  |    354 |   we obtain the Map (note the precise numbers might differ depending on white
 | 
|  |    355 |   spaces etc.~in the bf-program):
 | 
|  |    356 | 
 | 
|  |    357 |   \begin{center}
 | 
|  |    358 |   \texttt{Map(69 -> 61, 5 -> 20, 60 -> 70, 27 -> 44, 43 -> 28, 19 -> 6)}
 | 
|  |    359 |   \end{center}  
 | 
|  |    360 |   
 | 
|  |    361 |   This Map states that for the '\texttt{[}' on position 5, we need to
 | 
|  |    362 |   jump to position 20, which is just after the corresponding '\texttt{]}'.
 | 
|  |    363 |   Similarly, for the '\texttt{]}' on position 19, we need to jump to
 | 
|  |    364 |   position 6, which is just after the '\texttt{[}' on position 5, and so
 | 
|  |    365 |   on. The idea is to not calculate this information each time
 | 
|  |    366 |   we hit a bracket, but just look up this information in the 
 | 
|  |    367 |   \texttt{jtable}. 
 | 
|  |    368 | 
 | 
|  |    369 |   Then adapt the \texttt{compute} and \texttt{run} functions
 | 
|  |    370 |   from Part 1 in order to take advantage of the information
 | 
|  |    371 |   stored in the \texttt{jtable}. This means whenever \texttt{jumpLeft}
 | 
|  |    372 |   and \texttt{jumpRight} was called previously, you should look
 | 
|  |    373 |   up the jump address in the \texttt{jtable}. Feel free to reuse
 | 
|  |    374 |   the function \texttt{jumpLeft} and \texttt{jumpRight} for
 | 
|  |    375 |   calculating the \texttt{jtable}.\hfill{[1 Mark]}
 | 
|  |    376 | 
 | 
|  |    377 | \item[(6)] Compilers try to eliminate any ``dead'' code that could slow
 | 
|  |    378 |   down programs and also perform what is often called
 | 
|  |    379 |   \emph{peephole optimisations}.\footnote{\url{https://en.wikipedia.org/wiki/Peephole_optimization}} While it is difficult for compilers to comprehend what
 | 
|  |    380 |   is intended with whole programs, they are very good at finding out
 | 
|  |    381 |   what small snippets of code do, and then try to generate
 | 
|  |    382 |   faster code for such snippets.
 | 
|  |    383 | 
 | 
|  |    384 |   In our case, dead code is everything that is not a bf-command.
 | 
|  |    385 |   Therefore write a function \texttt{optimise} which deletes such
 | 
|  |    386 |   dead code. Moreover this function should replace every substring
 | 
|  |    387 |   of the form \pcode{[-]} by a new command \texttt{0}. 
 | 
|  |    388 |   The idea is that the loop \pcode{[-]} just resets the
 | 
|  |    389 |   memory at the current location to 0. It is more efficient
 | 
|  |    390 |   to do this in a single step, rather than incrementally as in
 | 
|  |    391 |   the original bf-programs.
 | 
| 218 |    392 | 
 | 
| 233 |    393 |   In the extended \texttt{compute3} and \texttt{run3} functions you should
 | 
|  |    394 |   implement this command by writing 0 to \pcode{mem(mp)}, that is use
 | 
|  |    395 |   \pcode{write(mem, mp, 0)} as the rule for the command \texttt{0}.
 | 
|  |    396 |   The easiest way to modify a string in this way is to use the regular
 | 
|  |    397 |   expression \pcode{"""[^<>+-.,\\[\\]]"""}, which recognises everything that is 
 | 
|  |    398 |   not a bf-command. Similarly, the
 | 
|  |    399 |   regular expression \pcode{"""\\[-\\]"""} finds all occurrences of \pcode{[-]} and 
 | 
|  |    400 |   by using the Scala method \pcode{.replaceAll} you can replace it with the 
 | 
|  |    401 |   string \pcode{"0"} standing for the new bf-command.\hfill{[1 Mark]}
 | 
|  |    402 | 
 | 
|  |    403 | \item[(7)] Finally, real compilers try to take advantage of CPUs which often
 | 
|  |    404 |   provide complex operations in hardware that can combine many smaller
 | 
|  |    405 |   instructions into a single faster instruction.
 | 
|  |    406 | 
 | 
|  |    407 |   In our case we can optimise the several single increments of a
 | 
|  |    408 |   memory cell, for example \pcode{++++}, by a single ``increment by
 | 
|  |    409 |   4''. For this optimisation we just have to make sure these single
 | 
|  |    410 |   increments are all next to each other. Similarly optimisations should apply
 | 
|  |    411 |   for the bf-commands \pcode{-}, \pcode{<} and
 | 
|  |    412 |   \pcode{>}, which can all be replaced by extended versions that take
 | 
|  |    413 |   the amount of the increment (decrement) into account. We will do
 | 
|  |    414 |   this by introducing two-character bf-commands. For example
 | 
|  |    415 | 
 | 
|  |    416 |   \begin{center}
 | 
|  |    417 |     \begin{tabular}{l|l}
 | 
|  |    418 |       original bf-cmds & replacement\\
 | 
|  |    419 |       \hline
 | 
|  |    420 |       \pcode{+} & \pcode{+A}\\
 | 
|  |    421 |       \pcode{++} & \pcode{+B}\\
 | 
|  |    422 |       \pcode{+++} & \pcode{+C}\\
 | 
|  |    423 |       \ldots{} & \ldots{}\\
 | 
|  |    424 |       \pcode{+++....++} & \pcode{+Z}\\
 | 
|  |    425 |       \hspace{5mm}(these are 26 \pcode{+}'s)\\
 | 
|  |    426 |     \end{tabular} 
 | 
|  |    427 |   \end{center}  
 | 
|  |    428 | 
 | 
|  |    429 | 
 | 
|  |    430 |   Similarly for \pcode{-}, \pcode{<} and \pcode{>}. If there are more
 | 
|  |    431 |   than 26 \pcode{+}'s in a row, then more than on ``two-character''
 | 
|  |    432 |   bf-commands need to be generated (the idea is that more than
 | 
|  |    433 |   26 copies of a single bf-command in a row is a rare occurrence in
 | 
|  |    434 |   actual bf-programs). All other bf-commands should be unaffected by this
 | 
|  |    435 |   change. 
 | 
|  |    436 | 
 | 
|  |    437 |   For this write a function \texttt{combine} which replaces sequences
 | 
|  |    438 |   of repeated increment and decrement commands by appropriate
 | 
|  |    439 |   two-character commands.  In the functions \pcode{compute4} and
 | 
|  |    440 |   \pcode{run4}, the ``combine'' and the optimisation from (6) should
 | 
|  |    441 |   be performed. Make sure that when a two-character bf-command is
 | 
|  |    442 |   encountered you need to increase the \pcode{pc}-counter by two in
 | 
|  |    443 |   order to process the next command. For example
 | 
|  |    444 | 
 | 
|  |    445 |   \begin{center}
 | 
|  |    446 |   \pcode{combine(optimise(load_bff("benchmark.bf")))}  
 | 
|  |    447 |   \end{center}  
 | 
|  |    448 | 
 | 
|  |    449 |   generates the improved program
 | 
|  |    450 | 
 | 
|  |    451 |   \begin{center}
 | 
|  |    452 |   \pcode{>A+B[<A+M>A-A]<A[[}\hspace{3mm}\ldots{}
 | 
|  |    453 |   \end{center}  
 | 
|  |    454 | 
 | 
|  |    455 |   for the original benchmark program
 | 
|  |    456 | 
 | 
|  |    457 |   \begin{center}
 | 
|  |    458 |     \pcode{>++[<+++++++++++++>-]<[[}\hspace{3mm}\ldots
 | 
|  |    459 |   \end{center}    
 | 
|  |    460 | 
 | 
|  |    461 |   As you can see, the compiler bets on saving so much time on the
 | 
|  |    462 |   \pcode{+B} and \pcode{+M} steps so that the optimisations is
 | 
|  |    463 |   worthwhile overall (of course for the \pcode{>A}'s and so on, the compiler incurs a
 | 
|  |    464 |   penalty). Luckily, after you have performed all
 | 
|  |    465 |   optimisations in (5) - (7), you can expect that the
 | 
| 234 |    466 |   \pcode{benchmark.bf} program runs four to five times faster.
 | 
|  |    467 |   You can also test whether your compiler produces the correct result
 | 
|  |    468 |   by for example testing
 | 
|  |    469 | 
 | 
|  |    470 |   \begin{center}
 | 
|  |    471 |   \pcode{run(load_bff("sierpinski.bf")) == run4(load_bff("sierpinski.bf"))}
 | 
|  |    472 |   \end{center}
 | 
|  |    473 | 
 | 
|  |    474 |   which should return true for all the different compiler stages. \\ 
 | 
|  |    475 |   \mbox{}\hfill{[2 Marks]}
 | 
| 233 |    476 | \end{itemize}  
 | 
| 6 |    477 | 
 | 
|  |    478 | \end{document}
 | 
|  |    479 | 
 | 
| 68 |    480 | 
 | 
| 6 |    481 | %%% Local Variables: 
 | 
|  |    482 | %%% mode: latex
 | 
|  |    483 | %%% TeX-master: t
 | 
|  |    484 | %%% End: 
 |