cws/cw05.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 06 Dec 2018 13:15:28 +0000
changeset 229 5549016ab10f
parent 221 9e7897f25e13
child 230 bebe34c975a8
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
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    34
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    35
\subsection*{Part 1 (6 Marks)}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    36
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    37
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
    38
programming language.  But remember, some serious companies have built
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    39
their business on
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    40
Scala.\footnote{\url{https://en.wikipedia.org/wiki/Scala_(programming_language)\#Companies}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    41
And there are far, far more esoteric languages out there. One is
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    42
called \emph{brainf***}. You are asked in this part to implement an
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    43
interpreter and compiler for this language.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    44
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    45
Urban M\"uller developed brainf*** in 1993.  A close relative of this
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    46
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
    47
computer pioneer. The main feature of brainf*** is its minimalistic
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    48
set of instructions---just 8 instructions in total and all of which
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    49
are single characters. Despite the minimalism, this language has been
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    50
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
    51
you: it roughly means that every algorithm we know can, in principle,
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    52
be implemented in brainf***. It just takes a lot of determination and
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    53
quite a lot of memory resources. Some relatively sophisticated sample
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    54
programs in brainf*** are given in the file \texttt{bf.scala}, including
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    55
a brainf*** program for the Sierpinski triangle and Mandelbot set.\bigskip
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    56
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    57
\noindent
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    58
As mentioned above, brainf*** has 8 single-character commands, namely
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    59
\texttt{'>'}, \texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'},
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    60
\texttt{','}, \texttt{'['} and \texttt{']'}. Every other character is
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    61
considered a comment.  Brainf*** operates on memory cells containing
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    62
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
    63
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
    64
memory cell by using the command \texttt{'>'}, and backward by using
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    65
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase,
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    66
respectively decrease, by 1 the content of the memory cell to which
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    67
the memory pointer currently points to. The commands for input/output
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    68
are \texttt{','} and \texttt{'.'}. Output works by reading the content
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    69
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
    70
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
    71
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
    72
points to. The commands \texttt{'['} and \texttt{']'} are looping
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    73
constructs. Everything in between \texttt{'['} and \texttt{']'} is
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    74
repeated until a counter (memory cell) reaches zero.  A typical
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    75
program in brainf*** looks as follows:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    76
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    77
\begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    78
\begin{verbatim}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    79
 ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    80
 ..+++.>>.<-.<.+++.------.--------.>>+.>++.
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    81
\end{verbatim}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    82
\end{center}  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    83
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    84
\noindent
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    85
This one prints out Hello World\ldots{}obviously. 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    86
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    87
\subsubsection*{Tasks (file bf.scala)}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    88
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    89
\begin{itemize}
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    90
\item[(1)]  Write a function that takes a file name as argument and
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    91
  and requests the corresponding file from disk. It returns the
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    92
  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
    93
  the function should return the empty string.\\
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    94
  \mbox{}\hfill[1 Mark]
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    95
  
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    96
\item[(2)] Brainf*** memory is represented by a \texttt{Map} from
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    97
  integers to integers. The empty memory is represented by
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    98
  \texttt{Map()}, that is nothing is stored in the
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    99
  memory; \texttt{Map(0 -> 1, 2 -> 3)} stores \texttt{1} at
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   100
  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
   101
  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
   102
  \emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   103
  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
   104
  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
   105
  corresponding memory location. If the \texttt{Map} is not defined at
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   106
  the memory pointer, \texttt{sread} returns \texttt{0}.
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   107
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   108
  Write another function \texttt{write}, which takes a memory, a
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   109
  memory pointer and an integer value as argument and updates the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   110
  \texttt{Map} with the value at the given memory location. As usual
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   111
  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
   112
  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
   113
  pointer.\hfill[1 Mark]
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   114
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   115
\item[(3)] Write two functions, \texttt{jumpRight} and
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   116
  \texttt{jumpLeft} that are needed to implement the loop constructs
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   117
  of brainf***. They take a program (a \texttt{String}) and a program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   118
  counter (an \texttt{Int}) as argument and move right (respectively
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   119
  left) in the string in order to find the \textbf{matching}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   120
  opening/closing bracket. For example, given the following program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   121
  with the program counter indicated by an arrow:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   122
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   123
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   124
  \texttt{--[\barbelow{.}.+>--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   125
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   126
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   127
  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
   128
  \texttt{jumpRight} is supposed to return the position just after this
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   129
  
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   130
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   131
  \texttt{--[..+>--]\barbelow{,}>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   132
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   133
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   134
  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
   135
  then \texttt{jumpLeft} is supposed to jump to just after the opening
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   136
  bracket (that is jumping to the beginning of the loop):
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   137
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   138
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   139
    \texttt{--[..+>-\barbelow{-}],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   140
    \qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   141
    \texttt{--[\barbelow{.}.+>--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   142
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   143
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   144
  Unfortunately we have to take into account that there might be
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   145
  other opening and closing brackets on the `way' to find the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   146
  matching bracket. For example in the brainf*** program
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   147
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   148
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   149
  \texttt{--[\barbelow{.}.[+>]--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   150
  \end{center}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   151
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   152
  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
   153
  position, but the program counter for \texttt{','} in 12th
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   154
  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
   155
  using levels (which are the third argument in \texttt{jumpLeft} and
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   156
  \texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   157
  level by one whenever you find an opening bracket and decrease by
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   158
  one for a closing bracket. Then in \texttt{jumpRight} you are looking
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   159
  for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   160
  do the opposite. In this way you can find \textbf{matching} brackets
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   161
  in strings such as
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   162
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   163
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   164
  \texttt{--[\barbelow{.}.[[-]+>[.]]--],>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   165
  \end{center}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   166
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   167
  for which \texttt{jumpRight} should produce the position:
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   168
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   169
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   170
  \texttt{--[..[[-]+>[.]]--]\barbelow{,}>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   171
  \end{center}
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
  It is also possible that the position returned by \texttt{jumpRight} or
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   174
  \texttt{jumpLeft} is outside the string in cases where there are
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   175
  no matching brackets. For example
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   176
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   177
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   178
  \texttt{--[\barbelow{.}.[[-]+>[.]]--,>,++}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   179
  \qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   180
  \texttt{--[..[[-]+>[.]]-->,++\barbelow{\;\phantom{+}}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   181
  \end{center}
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   182
  \hfill[2 Marks]
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   183
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   184
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   185
\item[(4)] Write a recursive function \texttt{run} that executes a
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   186
  brainf*** program. It takes a program, a program counter, a memory
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   187
  pointer and a memory as arguments. If the program counter is outside
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   188
  the program string, the execution stops and \texttt{run} returns the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   189
  memory. If the program counter is inside the string, it reads the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   190
  corresponding character and updates the program counter \texttt{pc},
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   191
  memory pointer \texttt{mp} and memory \texttt{mem} according to the
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   192
  rules shown in Figure~\ref{comms}. It then calls recursively
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   193
  \texttt{run} with the updated data. The most convenient way to
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   194
  implement the rules in \texttt{run} is to use pattern-matching
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   195
  and calculating a triple consisting of the new \texttt{pc},
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   196
  \texttt{mp} and \texttt{mem}.
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   197
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   198
  Write another function \texttt{start} that calls \texttt{run} with a
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   199
  given brainfu** program and memory, and the program counter and memory pointer
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   200
  set to~$0$. Like \texttt{run} it returns the memory after the execution
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   201
  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
   202
  Sierpinski triangle or the Hello world programs (they seem to be particularly
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   203
  useful for debugging purposes), or have a look at
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   204
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   205
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   206
  \url{https://esolangs.org/wiki/Brainfuck}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   207
  \end{center}\hfill[2 Marks]
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   208
  
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   209
  \begin{figure}[p]
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   210
  \begin{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   211
    \begin{tabular}{|@{}p{0.8cm}|l|}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   212
      \hline
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   213
      \hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   214
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   215
                       $\bullet$ & $\texttt{mp} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   216
                       $\bullet$ & \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   217
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   218
      \hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   219
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   220
                       $\bullet$ & $\texttt{mp} - 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   221
                       $\bullet$ & \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   222
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   223
      \hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   224
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   225
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   226
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   227
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   228
      \hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   229
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   230
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   231
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   232
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   233
      \hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   234
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   235
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   236
                       $\bullet$ & print out \,\texttt{mem(mp)} as a character\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   237
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   238
      \hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   239
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   240
                       $\bullet$ & $\texttt{mp}$ unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   241
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   242
                       \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
   243
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   244
      \hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   245
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   246
                       $\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   247
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   248
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   249
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   250
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   251
                     \end{tabular}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   252
                     \\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   253
      \hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   254
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   255
                       $\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   256
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   257
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   258
                       $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   259
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   260
                     \end{tabular}\\\hline   
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   261
      any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   262
                         $\bullet$ & $\texttt{pc} + 1$\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   263
                         $\bullet$ & \texttt{mp} and \texttt{mem} unchanged
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   264
                       \end{tabular}\\
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   265
      \hline                 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   266
    \end{tabular}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   267
  \end{center}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   268
  \caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc},
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   269
    memory pointer \texttt{mp} and memory \texttt{mem}.\label{comms}}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   270
  \end{figure}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   271
\end{itemize}\bigskip  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   272
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   273
\subsection*{Part 2 (4 Marks)}
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   274
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   275
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
   276
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
   277
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   281
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
%%% End: