handouts/ho10.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 13 Oct 2020 14:29:10 +0100
changeset 779 5385c8342f02
parent 675 d665e7dd66d7
child 871 94b84d880c2b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
675
d665e7dd66d7 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
     1
% !TEX program = xelatex
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage{../style}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
\usepackage{../langs}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\usepackage{../graphics}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\usepackage{../grammar}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usepackage{multicol}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
675
d665e7dd66d7 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
     9
%%\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\fnote{\copyright{} Christian Urban, King's College London, 2014}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
%% why are shuttle flights so good with software
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
%%http://www.fastcompany.com/28121/they-write-right-stuff
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
675
d665e7dd66d7 updated
Christian Urban <urbanc@in.tum.de>
parents: 539
diff changeset
    17
\section*{Handout 10 (Static Analysis)}
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
If we want to improve the safety and security of our programs,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
we need a more principled approach to programming. Testing is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
good, but as Edsger Dijkstra famously wrote: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
\begin{quote}\it 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
``Program testing can be a very effective way to show the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
\underline{\smash{presence}} of bugs, but it is hopelessly
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
inadequate for showing their \underline{\smash{absence}}.''
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
\end{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
\noindent While such a more principled approach has been the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
subject of intense study for a long, long time, only in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
past few years some impressive results have been achieved. One
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
is the complete formalisation and (mathematical) verification
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
of a microkernel operating system called seL4.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
\url{http://sel4.systems}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
\noindent In 2011 this work was included in the MIT Technology
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
Review in the annual list of the world’s ten most important
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
emerging
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
technologies.\footnote{\url{http://www2.technologyreview.com/tr10/?year=2011}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
While this work is impressive, its technical details are too
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
enormous for an explanation here. Therefore let us look at
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
something much simpler, namely finding out properties about
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
programs using \emph{static analysis}.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
Static analysis is a technique that checks properties of a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
program without actually running the program. This should
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
raise alarm bells with you---because almost all interesting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
properties about programs  are equivalent to the halting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
problem, which we know is undecidable. For example estimating
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
the memory consumption of programs is in general undecidable,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
just like the halting problem. Static analysis circumvents
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
this undecidability-problem by essentially allowing answers
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
\emph{yes} and \emph{no}, but also \emph{don't know}. With
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
this ``trick'' even the halting problem becomes
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
decidable\ldots{}for example we could always say \emph{don't
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
know}. Of course this would be silly. The point is that we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
should be striving for a method that answers as often as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
possible either \emph{yes} or \emph{no}---just in cases when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
it is too difficult we fall back on the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
\emph{don't-know}-answer. This might sound all like abstract
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
nonsense. Therefore let us look at a concrete example.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
\subsubsection*{A Simple, Idealised Programming Language}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
Our starting point is a small, idealised programming language.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
It is idealised because we cut several corners in comparison
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
with real programming languages. The language we will study
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
contains, amongst other things, variables holding integers.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
Using static analysis, we want to find out what the sign of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
these integers (positive or negative) will be when the program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
runs. This sign-analysis seems like a very simple problem. But
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
even such simple problems, if approached naively, are in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
general undecidable, just like Turing's halting problem. I let
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
you think why?
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
Is sign-analysis of variables an interesting problem? Well,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
yes---if a compiler can find out that for example a variable
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
will never be negative and this variable is used as an index
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
for an array, then the compiler does not need to generate code
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
for an underflow-check. Remember some languages are immune to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
buffer-overflow attacks, but they need to add underflow and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
overflow checks everywhere. According to John Regher, an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
expert in the field of compilers, overflow checks can cause
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
5-10\% slowdown, in some languages even 100\% for tight
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
loops.\footnote{\url{http://blog.regehr.org/archives/1154}} If
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
the compiler can omit the underflow check, for example, then
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
this can potentially drastically speed up the generated code. 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
What do programs in our simple programming language look like?
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
The following grammar gives a first specification:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
\begin{multicols}{2}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
\begin{plstx}[rhs style=,one per line,left margin=9mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
: \meta{Stmt} ::= \meta{label} \texttt{:}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
                | \meta{var} \texttt{:=} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
                | \texttt{jmp?} \meta{Exp} \meta{label}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
                | \texttt{goto} \meta{label}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
\end{plstx}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
\columnbreak
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
\begin{plstx}[rhs style=,one per line]
: \meta{Exp} ::= \meta{Exp} \texttt{+} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
               | \meta{Exp} \texttt{*} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
               | \meta{Exp} \texttt{=} \meta{Exp} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
               | \meta{num}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
               | \meta{var}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
\end{plstx}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
\end{multicols}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
\noindent I assume you are familiar with such
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
grammars.\footnote{\url{http://en.wikipedia.org/wiki/Backus–Naur_Form}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
There are three main syntactic categories: \emph{statments}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
and \emph{expressions} as well as \emph{programs}, which are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
sequences of statements. Statements are either labels,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
variable assignments, conditional jumps (\pcode{jmp?}) and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
unconditional jumps (\pcode{goto}). Labels are just strings,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
which can be used as the target of a jump. We assume that in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
every program the labels are unique---if there is a clash,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
then we do not know where to jump to. The conditional jumps
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
and variable assignments involve (arithmetic) expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
Expressions are either numbers, variables or compound
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
expressions built up from \pcode{+}, \pcode{*} and \emph{=}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
(for simplicity reasons we do not consider any other
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
operations). We assume we have negative and positive numbers,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
\pcode{2}\ldots{} An example program that calculates the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
factorial of 5 is in our programming language as follows:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
\begin{lstlisting}[language={},xleftmargin=10mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
      a := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
      n := 5 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
top:  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
      jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
      a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
      n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
      goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
\noindent As can be seen each line of the program contains a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
statement. In the first two lines we assign values to the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
variables \pcode{a} and \pcode{n}. In line 4 we test whether
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
\pcode{n} is zero, in which case we jump to the end of the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
program marked with the label \pcode{done}. If \pcode{n} is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
not zero, we multiply the content of \pcode{a} by \pcode{n},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
decrease \pcode{n} by one and jump back to the beginning of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
the loop, marked with the label \pcode{top}. Another program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
in our language is shown in Figure~\ref{fib}. I let you think
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
what it calculates.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
\begin{figure}[t]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
\begin{lstlisting}[numbers=none,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
                   language={},xleftmargin=10mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
      n := 6
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
      m1 := 0
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
      m2 := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
loop: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
      jmp? n = 0 done
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      tmp := m2
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
      m2 := m1 + m2
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
      m1 := tmp
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
      n := n + -1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
      goto top
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
\caption{A mystery program in our idealised programming language.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
Try to find out what it calculates! \label{fib}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
\end{figure}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
Even if our language is rather small, it is still Turing
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
complete---meaning quite powerful. However, discussing this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
fact in more detail would lead us too far astray. Clearly, our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
programming is rather low-level and not very comfortable for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
writing programs. It is inspired by real machine code, which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
is the code that is executed by a CPU. So a more interesting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
question is what is missing in comparison with real machine
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
code? Well, not much\ldots{}in principle. Real machine code,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
of course, contains many more arithmetic instructions (not
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
just addition and multiplication) and many more conditional
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
jumps. We could add these to our language if we wanted, but
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
complexity is really beside the point here. Furthermore, real
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
machine code has many instructions for manipulating memory. We
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
do not have this at all. This is actually a more serious
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
simplification because we assume numbers to be arbitrary small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
or large, which is not the case with real machine code. In
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
real machine code, basic number formats have a range and might
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
over-flow or under-flow this range. Also the number of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
variables in our programs is potentially unlimited, while
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
memory in an actual computer, of course, is always limited. To
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
sum up, our language might look ridiculously simple, but it is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
not too far removed from practically relevant issues.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
\subsubsection*{An Interpreter}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
Designing a language is like playing god: you can say what
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
names for variables you allow; what programs should look like;
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
most importantly you can decide what each part of the program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
should mean and do. While our language is quite simple and the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
meaning of statements, for example, is rather straightforward,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
there are still places where we need to make real choices. For
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
example consider the conditional jumps, say the one in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
factorial program: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
\code{jmp? n = 0 done}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
\noindent How should they work? We could introduce Booleans
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
(\pcode{true} and \pcode{false}) and then jump only when the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
condition is \pcode{true}. However, since we have numbers in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
our language anyway, why not just encoding \pcode{true} as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
one, and \pcode{false} as zero? In this way we can dispense
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
with the additional concept of Booleans.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
I hope the above discussion makes it already clear we need to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
be a bit more careful with our programs. Below we shall
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
describe an interpreter for our programming language, which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
specifies exactly how programs are supposed to be
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
run\ldots{}at least we will specify this for all \emph{good}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
programs. By good programs I mean where all variables are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
initialised, for example. Our interpreter will just crash if
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
it cannot find out the value for a variable when it is not
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
initialised. Also, we will assume that labels in good programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
are unique, otherwise our programs will calculate ``garbage''.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
First we will pre-process our programs. This will simplify the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
definition of our interpreter later on. By pre-processing our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
programs we will transform programs into \emph{snippets}. A
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
snippet is a label and all the code that comes after the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
label. This essentially means a snippet is a \emph{map} from
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
labels to code.\footnote{Be sure you know what maps are. In a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
programming context they are often represented as association
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
list where some data is associated with a key.} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
Given that programs are sequences (or lists) of statements, we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
can easily calculate the snippets by just traversing this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
sequence and recursively generating the map. Suppose a program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
is of the general form
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
$stmt_1\;stmt_2\; \ldots\; stmt_n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
\noindent The idea is to go through this sequence of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
statements one by one and check whether they are a label. If
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
yes, we add the label and the remaining statements to our map.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
If no, we just continue with the next statement. To come up
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
with a recursive definition for generating snippets, let us
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
write $[]$ for the program that does not contain any
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
statement. Consider the following definition:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
$\textit{snippets}([])$ & $\dn$ & $\varnothing$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
$\textit{snippets}(stmt\;\; rest)$ & $\dn$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
$\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
   \textit{snippets}(rest)[label := rest] & \text{if}\;stmt = \textit{label:}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
   \textit{snippets}(rest)                & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
\end{cases}$   
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
\noindent In the first clause we just return the empty map for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
the program that does not contain any statement. In the second
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
clause, we have to distinguish the case where the first
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
statement is a label or not. As said before, if not, then we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
just ``throw away'' the label and recursively calculate the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
snippets for the rest of the program (the otherwise clause).
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
If yes, then we do the same, but also update the map so that
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
$label$ now points to the rest of the statements (the if
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
clause). This looks all realtively straightforward, but there
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
is one small problem we need to overcome: our two programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
shown so far have no label as \emph{entry point}---that is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
where the execution is supposed to start. We usually assume
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
that the first statement will be run first. To make this the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
default, it is convenient if we add to all our programs a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
default label, say \pcode{""} (the empty string). With this we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
can define our pre-processing of programs as follows
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
$\textit{preproc}(prog) \dn \textit{snippets}(\pcode{"":}\;\; prog)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
\end{center} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
\noindent Let us see how this pans out in practice. If we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
pre-process the factorial program shown earlier, we obtain the 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
following map:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
\small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
\lstset{numbers=none,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
        language={},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
        xleftmargin=0mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
        aboveskip=0.5mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
        belowskip=0.5mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
        frame=single,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
        framerule=0mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
        framesep=0mm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
\begin{tikzpicture}[node distance=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
\node (A1) [draw]{\pcode{""}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
\node (B1) [right=of A1] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
\node (C1) [right=of B1,anchor=north west] {
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
\begin{minipage}{3.5cm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
\begin{lstlisting}[language={},xleftmargin=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
  a := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  n := 5 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
top:  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
\end{minipage}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
\node (A2) [right=of C1.north east,draw] {\pcode{top}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
\node (B2) [right=of A2] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
\node (C2) [right=of B2, anchor=north west] {
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
\begin{minipage}{3.5cm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
\begin{lstlisting}[language={},xleftmargin=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
\end{lstlisting} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
\end{minipage}}; 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
\node (A3) [right=of C2.north east,draw] {\pcode{done}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
\node (B3) [right=of A3] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
\node (C3) [right=of B3] {$[]$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
\end{tikzpicture}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
\noindent I highlighted the \emph{keys} in this map. Since
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
there are three labels in the factorial program (remember we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
added \pcode{""}), there are three keys. When running the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
factorial program and encountering a jump, then we only have
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
to consult this snippets-map in order to find out what the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
next statements should be.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
We should now be in the position to define how a program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
should be run. In the context of interpreters, this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
``running'' of programs is often called \emph{evaluation}. Let
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
us start with the definition of how expressions are evaluated.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
A first attempt might be the following recursive function:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
$\textit{eval\_exp}(\texttt{n})$ & $\dn$ & $n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
\pcode{2}\ldots{}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) + 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) * 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  $\dn$ & $\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}) =
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
                 \textit{eval\_exp}(\texttt{e}_\texttt{2})\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  0 & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  \end{cases}$          
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
\noindent While this should look all rather intuitive`, still
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
be very careful. There is a subtlety which can be easily
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
overlooked: The function \textit{eval\_exp} takes an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
expression of our programming language as input and returns a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
number as output. Therefore whenever we encounter a number in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
our program, we just return this number---this is defined in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
the first clause above. Whenever we encounter an addition,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
well then we first evaluate the left-hand side
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
$\texttt{e}_\texttt{1}$ of the addition (this will give a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
number), then evaluate the right-hand side
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
$\texttt{e}_\texttt{2}$ (this gives another number), and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
finally add both numbers together. Here is the subtlety: on
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
the left-hand side of the $\dn$ we have a \texttt{+} (in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
teletype font) which is the symbol for addition in our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
programming language. On the right-hand side we have $+$ which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
stands for the arithmetic operation from ``mathematics'' of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
adding two numbers. These are rather different concepts---one
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
is a symbol (which we made up), and the other a mathematical
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
operation. When we will have a look at an actual
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
implementation of our interpreter, the mathematical operation
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
will be the function for addition from the programming
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
language in which we \underline{\smash{implement}} our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
interpreter. While the \texttt{+} is just a symbol that is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
used in our programming language. Clearly we have to use a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
symbol that is a good mnemonic for addition, otherwise we will
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
confuse the programmers working with our language. Therefore
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
we use $\texttt{+}$. A similar choice is made for times in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
third clause and equality in the fourth clause. 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
Remember I wrote at the beginning of this section about being
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
god when designing a programming language. You can see this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
here: we need to give meaning to symbols. At the moment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
however, we are a poor fallible god. Look again at the grammar
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
of our programming language and our definition. Clearly, an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
expression can contain variables. So far we have ignored them.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
What should our interpreter do with variables? They might
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
change during the evaluation of a program. For example the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
variable \pcode{n} in the factorial program counts down from 5
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
down to 0. How can we improve our definition above to give also
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
an answer whenever our interpreter encounters a variable in an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
expression? The solution is to add an \emph{environment},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
written $env$, as an additional input argument to our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
\textit{eval\_exp} function.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
$\textit{eval\_exp}(\texttt{n}, env)$ & $\dn$ & $n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
\pcode{2}\ldots{}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) + 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) * 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  $\dn$ & $\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) =
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
                 \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  0 & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
  \end{cases}$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
$\textit{eval\_exp}(\texttt{x}, env)$ & $\dn$ & $env(x)$      
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
\noindent This environment $env$ also acts like a map: it
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
associates variables with their current values. For example
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
after evaluating the first two lines in our factorial
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
program, such an environment might look as follows
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
\begin{tabular}{ll}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
$\fbox{\texttt{a}} \mapsto \texttt{1}$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
$\fbox{\texttt{n}} \mapsto \texttt{5}$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
\noindent Again I highlighted the keys. In the clause for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
variables, we can therefore consult this environment and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
return whatever value is currently stored for this variable.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
This is written $env(x)$---meaning we query this map with $x$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
we obtain the corresponding number. You might ask what happens
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
if an environment does not contain any value for, say, the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
variable $x$? Well, then our interpreter just ``crashes'', or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
more precisely will raise an exception. In this case we have a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
``bad'' program that tried to use a variable before it was
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
initialised. The programmer should not have done this. In a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
real programming language, we would of course try a bit harder
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
and for example give an error at compile time, or design our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
language in such a way that this can never happen. With the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
second version of \textit{eval\_exp} we completed our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
definition for evaluating expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
Next comes the evaluation function for statements. We define
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
this function in such a way that we recursively evaluate a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
whole sequence of statements. Assume a program $p$ (you want
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
to evaluate) and its pre-processed snippets $sn$. Then we can
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
define:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
$\textit{eval\_stmts}([], env)$ & $\dn$ & $env$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
$\textit{eval\_stmts}(\texttt{label:}\;rest, env)$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
  $\dn$ & $\textit{eval\_stmts}(rest, env)$ \\ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
$\textit{eval\_stmts}(\texttt{x\,:=\,e}\;rest, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
  $\dn$ & $\textit{eval\_stmts}(rest, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
           env[x := \textit{eval\_exp}(\texttt{e}, env)])$\\  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
$\textit{eval\_stmts}(\texttt{goto\,lbl}\;rest, env)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
 & $\dn$ & $\textit{eval\_stmts}(sn(\texttt{lbl}), env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
$\textit{eval\_stmts}(\texttt{jmp?\,e\,lbl}\;rest, env)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
 & $\dn$ & $\begin{cases}\begin{array}{@{}l@{\hspace{-12mm}}r@{}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
 \textit{eval\_stmts}(sn(\texttt{lbl}), env)\\ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
 & \text{if}\;\textit{eval\_exp}(\texttt{e}, env) = 1\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
 \textit{eval\_stmts}(rest, env) & \text{otherwise}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
 \end{array}\end{cases}$  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
\noindent The first clause is for the empty program, or when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
we arrived at the end of the program. In this case we just
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
return the environment. The second clause is for when the next
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
statement is a label. That means the program is of the form
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
$\texttt{label:}\;rest$ where the label is some string and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
$rest$ stands for all following statements. This case is easy,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
because our evaluation function just discards the label and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
evaluates the rest of the statements (we already extracted all
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
important information about labels when we pre-processed our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
programs and generated the snippets). The third clause is for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
variable assignments. Again we just evaluate the rest for the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
statements, but with a modified environment---since the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
variable assignment is supposed to introduce a new variable or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
change the current value of a variable. For this modification
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
of the environment we first evaluate the expression
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
$\texttt{e}$ using our evaluation function for expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
This gives us a number. Then we assign this number to the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
variable $x$ in the environment. This modified environment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
will be used to evaluate the rest of the program. The fourth
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
clause is for the unconditional jump to a label, called
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
\texttt{lbl}. That means we have to look up in our snippets
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
map $sn$ what are the next statements for this label.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
Therefore we will continue with evaluating, not with the rest
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
of the program, but with the statements stored in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
snippets-map under the label $\texttt{lbl}$. The fifth clause
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
for conditional jumps is similar, but to decide whether to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
make the jump we first need to evaluate the expression
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
$\texttt{e}$ in order to find out whether it is $1$. If yes,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
we jump, otherwise we just continue with evaluating the rest
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
of the program.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
Our interpreter works in two stages: First we pre-process our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
program generating the snippets map $sn$, say. Second we call
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
the evaluation function with the default entry point and the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
empty environment:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
$\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
\noindent It is interesting to note that our interpreter when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
it comes to the end of the program returns an environment. Our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
programming language does not contain any constructs for input
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
and output. Therefore this environment is the only effect we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
can observe when running the program (apart from that our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
interpreter might need some time before finishing the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
evaluation of the program and the CPU getting hot). Evaluating 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
the factorial program with our interpreter we receive as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
``answer''-environment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
\begin{tabular}{ll}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
$\fbox{\texttt{a}} \mapsto \texttt{120}$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
$\fbox{\texttt{n}} \mapsto \texttt{0}$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
\noindent While the discussion above should have illustrated
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
the ideas, in order to do some serious calculations, we clearly
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
need to implement the interpreter.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
\subsubsection*{Scala Code for the Interpreter}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
Functional programming languages are very convenient for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
implementations of interpreters. A good choice for a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
functional programming language is Scala, a programming
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
language that combines functional and object-oriented
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
pro\-gramming-styles. It has received in the last five years or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
so quite a bit of attention. One reason for this attention is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
that, like the Java programming language, Scala compiles to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
the Java Virtual Machine (JVM) and therefore Scala programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
can run under MacOSX, Linux and Windows.\footnote{There are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
also experimental backends for Android and JavaScript.} Unlike
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
Java, however, Scala often allows programmers to write very
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
concise and elegant code. Some therefore say Scala is the much
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
better Java. A number of companies, The Guardian, Twitter,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
Coursera, FourSquare, LinkedIn to name a few, either use Scala
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
exclusively in production code, or at least to some
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
substantial degree. If you want to try out Scala yourself, the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
Scala compiler can be downloaded from
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
\begin{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
\url{http://www.scala-lang.org}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
\end{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
Let us have a look at the Scala code shown in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
Figure~\ref{code}. It shows the entire code for the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
interpreter, though the implementation is admittedly no
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
frills.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
\begin{figure}[t]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
\small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
\lstinputlisting[language=Scala]{../progs/inter.scala}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
\caption{The entire code of the interpreter for our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
idealised programming language.\label{code}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
\end{figure}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
\subsubsection*{Static Analysis}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
Finally we can come back to our original problem, namely 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
finding out what the signs of variables are 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
\end{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
%% list of static analysers for C
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
http://spinroot.com/static/index.html
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
%% NASA coding rules for C
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
%%% Local Variables: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
%%% mode: latex
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
%%% TeX-master: t
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
%%% End: