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