cws/cw05.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 06 Dec 2018 13:52:50 +0000
changeset 230 bebe34c975a8
parent 229 5549016ab10f
child 233 38ea26f227af
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     2
\usepackage{../style}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
     3
\usepackage{../langs}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     4
\usepackage{disclaimer}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     5
\usepackage{tikz}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     6
\usepackage{pgf}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     7
\usepackage{pgfplots}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     8
\usepackage{stackengine}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     9
%% \usepackage{accents}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    10
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    11
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    15
% BF IDE
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    16
% https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    17
  
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    18
\section*{Coursework 9 (Scala)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    20
This coursework is worth 10\%. It is about a small programming
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    21
language called brainf***. The first part is due on 13 December at
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    22
11pm; the second, more advanced part, is due on 20 December at
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    23
11pm.\bigskip
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    24
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    25
\IMPORTANT{}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    26
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    27
\noindent
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    28
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
    29
maximum of 30 seconds on my laptop.
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    30
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    31
\DISCLAIMER{}
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    32
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    33
\subsection*{Reference Implementation}
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    34
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    35
As usual, this Scala assignment comes with a reference implementation in form of
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    36
two \texttt{jar}-files. You can download them from KEATS. This allows you to run any
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    37
test cases on your own computer. For example you can call Scala on the command line with the
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    38
option \texttt{-cp bf.jar} and then query any function from the
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    39
\texttt{bf.scala} template file. You have to
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    40
prefix the calls with \texttt{CW10a} and \texttt{CW10b}, respectively. For example
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    41
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    42
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    43
\begin{lstlisting}[language={},xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small]
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    44
$ scala -cp bf.jar
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    45
scala> import CW10a._  
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    46
scala> run(load_bff("sierpinski.bf"))
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    47
                               *
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    48
                              * *
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    49
                             *   *
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
                          * *     * *
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    53
                         *   *   *   *
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    54
                        * * * * * * * *
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    55
                       *               *
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
\end{lstlisting}%$
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    80
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    82
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    83
\subsection*{Part 1 (6 Marks)}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    84
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    85
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
    86
programming language.  But remember, some serious companies have built
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    87
their business on
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    88
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
    89
I claim functional programming is not a fad.  And there are far, far
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    90
more esoteric languages out there. One is called \emph{brainf***}. You
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    91
are asked in this part to implement an interpreter for
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    92
this language.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    93
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    94
Urban M\"uller developed brainf*** in 1993.  A close relative of this
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    95
language was already introduced in 1964 by Corado B\"ohm, an Italian
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    96
computer pioneer. The main feature of brainf*** is its minimalistic
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    97
set of instructions---just 8 instructions in total and all of which
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    98
are single characters. Despite the minimalism, this language has been
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    99
shown to be Turing complete\ldots{}if this doesn't ring any bell with
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   100
you: it roughly means that every algorithm we know can, in principle,
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   101
be implemented in brainf***. It just takes a lot of determination and
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   102
quite a lot of memory resources. Some relatively sophisticated sample
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   103
programs in brainf*** are given in the file \texttt{bf.scala}, including
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   104
a brainf*** program for the Sierpinski triangle and the Mandelbrot set.\bigskip
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   105
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   106
\noindent
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   107
As mentioned above, brainf*** has 8 single-character commands, namely
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   108
\texttt{'>'}, \texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'},
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   109
\texttt{','}, \texttt{'['} and \texttt{']'}. Every other character is
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   110
considered a comment.  Brainf*** operates on memory cells containing
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   111
integers. For this it uses a single memory pointer that points at each
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   112
stage to one memory cell. This pointer can be moved forward by one
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   113
memory cell by using the command \texttt{'>'}, and backward by using
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   114
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase,
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   115
respectively decrease, by 1 the content of the memory cell to which
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   116
the memory pointer currently points to. The commands for input/output
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   117
are \texttt{','} and \texttt{'.'}. Output works by reading the content
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   118
of the memory cell to which the memory pointer points to and printing
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   119
it out as an ASCII character. Input works the other way, taking some
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   120
user input and storing it in the cell to which the memory pointer
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   121
points to. The commands \texttt{'['} and \texttt{']'} are looping
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   122
constructs. Everything in between \texttt{'['} and \texttt{']'} is
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   123
repeated until a counter (memory cell) reaches zero.  A typical
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   124
program in brainf*** looks as follows:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   125
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   126
\begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   127
\begin{verbatim}
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   128
   ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.++
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   129
   +++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   130
\end{verbatim}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   131
\end{center}  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   132
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   133
\noindent
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   134
This one prints out Hello World\ldots{}obviously ;o) 
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   135
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   136
\subsubsection*{Tasks (file bf.scala)}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   137
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   138
\begin{itemize}
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   139
\item[(1)]  Write a function that takes a file name as an argument
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   140
  and requests the corresponding file from disk. It returns the
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   141
  content of the file as a String. If the file does not exists,
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   142
  the function should return the empty string.\\
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   143
  \mbox{}\hfill[1 Mark]
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   144
  
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   145
\item[(2)] Brainf*** memory is represented by a \texttt{Map} from
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   146
  integers to integers. The empty memory is represented by
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   147
  \texttt{Map()}, that is nothing is stored in the
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   148
  memory; \texttt{Map(0 -> 1, 2 -> 3)} stores \texttt{1} at
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   149
  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
   150
  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
   151
  \emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   152
  a function, \texttt{sread}, that takes a memory (a \texttt{Map}) and
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   153
  a memory pointer (an \texttt{Int}) as argument, and `safely' reads the
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   154
  corresponding memory location. If the \texttt{Map} is not defined at
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   155
  the memory pointer, \texttt{sread} returns \texttt{0}.
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   156
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   157
  Write another function \texttt{write}, which takes a memory, a
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   158
  memory pointer and an integer value as argument and updates the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   159
  \texttt{Map} with the value at the given memory location. As usual
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   160
  the \texttt{Map} is not updated `in-place' but a new map is created
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   161
  with the same data, except the value is stored at the given memory
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   162
  pointer.\hfill[1 Mark]
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   163
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   164
\item[(3)] Write two functions, \texttt{jumpRight} and
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   165
  \texttt{jumpLeft} that are needed to implement the loop constructs
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   166
  of brainf***. They take a program (a \texttt{String}) and a program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   167
  counter (an \texttt{Int}) as argument and move right (respectively
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   168
  left) in the string in order to find the \textbf{matching}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   169
  opening/closing bracket. For example, given the following program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   170
  with the program counter indicated by an arrow:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   171
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   172
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   173
  \texttt{--[\barbelow{.}.+>--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   174
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   175
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   176
  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
   177
  \texttt{jumpRight} is supposed to return the position just after this
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   178
  
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   179
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   180
  \texttt{--[..+>--]\barbelow{,}>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   181
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   182
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   183
  meaning it jumps to after the loop. Similarly, if you are in 8th position
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   184
  then \texttt{jumpLeft} is supposed to jump to just after the opening
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   185
  bracket (that is jumping to the beginning of the loop):
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   186
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   187
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   188
    \texttt{--[..+>-\barbelow{-}],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   189
    \qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   190
    \texttt{--[\barbelow{.}.+>--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   191
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   192
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   193
  Unfortunately we have to take into account that there might be
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   194
  other opening and closing brackets on the `way' to find the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   195
  matching bracket. For example in the brainf*** program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   196
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   197
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   198
  \texttt{--[\barbelow{.}.[+>]--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   199
  \end{center}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   200
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   201
  we do not want to return the index for the \texttt{'-'} in the 9th
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   202
  position, but the program counter for \texttt{','} in 12th
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   203
  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
   204
  using levels (which are the third argument in \texttt{jumpLeft} and
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   205
  \texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   206
  level by one whenever you find an opening bracket and decrease by
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   207
  one for a closing bracket. Then in \texttt{jumpRight} you are looking
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   208
  for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   209
  do the opposite. In this way you can find \textbf{matching} brackets
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   210
  in strings such as
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   211
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   212
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   213
  \texttt{--[\barbelow{.}.[[-]+>[.]]--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   214
  \end{center}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   215
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   216
  for which \texttt{jumpRight} should produce the position:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   217
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   218
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   219
  \texttt{--[..[[-]+>[.]]--]\barbelow{,}>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   220
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   221
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   222
  It is also possible that the position returned by \texttt{jumpRight} or
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   223
  \texttt{jumpLeft} is outside the string in cases where there are
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   224
  no matching brackets. For example
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   225
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   226
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   227
  \texttt{--[\barbelow{.}.[[-]+>[.]]--,>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   228
  \qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   229
  \texttt{--[..[[-]+>[.]]-->,++\barbelow{\;\phantom{+}}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   230
  \end{center}
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   231
  \hfill[2 Marks]
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   232
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   233
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   234
\item[(4)] Write a recursive function \texttt{compute} that runs a
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   235
  brainf*** program. It takes a program, a program counter, a memory
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   236
  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
   237
  the program string, the execution stops and \texttt{compute} returns the
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   238
  memory. If the program counter is inside the string, it reads the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   239
  corresponding character and updates the program counter \texttt{pc},
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   240
  memory pointer \texttt{mp} and memory \texttt{mem} according to the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   241
  rules shown in Figure~\ref{comms}. It then calls recursively
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   242
  \texttt{compute} with the updated data. The most convenient way to
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   243
  implement the brainf**k rules in Scala is to use pattern-matching
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   244
  and to calculate a triple consisting of the updated \texttt{pc},
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   245
  \texttt{mp} and \texttt{mem}.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   246
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   247
  Write another function \texttt{run} that calls \texttt{compute} with a
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   248
  given brainfu** program and memory, and the program counter and memory pointer
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   249
  set to~$0$. Like \texttt{compute}, it returns the memory after the execution
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   250
  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
   251
  Sierpinski triangle or the Hello world programs (they seem to be particularly
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   252
  useful for debugging purposes), or have a look at
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   253
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   254
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   255
  \url{https://esolangs.org/wiki/Brainfuck}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   256
  \end{center}\hfill[2 Marks]
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   257
  
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   258
  \begin{figure}[p]
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   259
  \begin{center}
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   260
    \begin{tabular}{|@{\hspace{0.5mm}}p{0.8cm}|l|}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   261
      \hline
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   262
      \hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   263
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   264
                       $\bullet$ & $\texttt{mp} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   265
                       $\bullet$ & \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   266
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   267
      \hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   268
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   269
                       $\bullet$ & $\texttt{mp} - 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   270
                       $\bullet$ & \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   271
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   272
      \hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   273
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   274
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   275
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   276
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   277
      \hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   278
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   279
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   280
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   281
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   282
      \hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   283
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   284
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   285
                       $\bullet$ & print out \,\texttt{mem(mp)} as a character\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   286
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   287
      \hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   288
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   289
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   290
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   291
                       \multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   292
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   293
      \hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   294
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   295
                       $\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   296
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   297
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   298
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   299
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   300
                     \end{tabular}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   301
                     \\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   302
      \hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   303
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   304
                       $\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   305
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   306
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   307
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   308
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   309
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   310
      any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   311
                         $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   312
                         $\bullet$ & \texttt{mp} and \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   313
                       \end{tabular}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   314
      \hline                 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   315
    \end{tabular}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   316
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   317
  \caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc},
230
bebe34c975a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   318
    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
   319
  \end{figure}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   320
\end{itemize}\bigskip  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   321
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   322
\subsection*{Part 2 (4 Marks)}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   323
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   324
While it is fun to look at bf-programs, like the Sierpinski triangle or the Mandelbrot
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   325
program, being interpreted, it is much more fun to write a compiler for the bf-language.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   326
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   330
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
%%% End: