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