handouts/ho09.tex
changeset 360 eb2004430215
parent 359 c90f803dc7ea
child 366 34a8f73b2c94
equal deleted inserted replaced
359:c90f803dc7ea 360:eb2004430215
   193 \subsubsection*{An Interpreter}
   193 \subsubsection*{An Interpreter}
   194 
   194 
   195 Designing a language is like playing god: you can say what
   195 Designing a language is like playing god: you can say what
   196 names for variables you allow; what programs should look like;
   196 names for variables you allow; what programs should look like;
   197 most importantly you can decide what each part of the program
   197 most importantly you can decide what each part of the program
   198 should mean and do. While our language is rather simple and
   198 should mean and do. While our language is quite simple and the
   199 the meaning of statements, for example, is rather
   199 meaning of statements, for example, is rather straightforward,
   200 straightforward, there are still places where we need to make
   200 there are still places where we need to make real choices. For
   201 real choices. For example consider the conditional jumps, say
   201 example consider the conditional jumps, say the one in the
   202 the one in the factorial program: 
   202 factorial program: 
   203 
   203 
   204 \begin{center}
   204 \begin{center}
   205 \code{jmp? n = 0 done}
   205 \code{jmp? n = 0 done}
   206 \end{center}
   206 \end{center}
   207 
   207 
   223 initialised. Also, we will assume that labels in good programs
   223 initialised. Also, we will assume that labels in good programs
   224 are unique, otherwise our programs will calculate ``garbage''.
   224 are unique, otherwise our programs will calculate ``garbage''.
   225 
   225 
   226 First we will pre-process our programs. This will simplify the
   226 First we will pre-process our programs. This will simplify the
   227 definition of our interpreter later on. By pre-processing our
   227 definition of our interpreter later on. By pre-processing our
   228 programs we will transform programs into \emph{snippets}.
   228 programs we will transform programs into \emph{snippets}. A
   229 Their purpose is to simplify the definition of what the
   229 snippet is a label and all the code that comes after the
   230 interpreter should do in case of a jump. A snippet is a label
   230 label. This essentially means a snippet is a \emph{map} from
   231 and all the code that comes after the label. This essentially
   231 labels to code.\footnote{Be sure you know what maps are. In a
   232 means a snippet is a \emph{map} from labels to
       
   233 code.\footnote{Be sure you know what maps are. In a
       
   234 programming context they are often represented as association
   232 programming context they are often represented as association
   235 list where some data is associated with a key.} 
   233 list where some data is associated with a key.} 
   236 
   234 
   237 Given that programs are sequences (or lists) of statements, we
   235 Given that programs are sequences (or lists) of statements, we
   238 can easily calculate the snippets by just traversing this
   236 can easily calculate the snippets by just traversing this
   526 
   524 
   527 \begin{center}
   525 \begin{center}
   528 $\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
   526 $\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
   529 \end{center}
   527 \end{center}
   530  
   528  
   531 \noindent It is interesting to not that our interpreter when
   529 \noindent It is interesting to note that our interpreter when
   532 it comes to the end of the program returns an environment. Our
   530 it comes to the end of the program returns an environment. Our
   533 programming language does not contain any constructs for input
   531 programming language does not contain any constructs for input
   534 and output. Therefore this environment is the only effect we
   532 and output. Therefore this environment is the only effect we
   535 can observe when running the program (apart from that our
   533 can observe when running the program (apart from that our
   536 interpreter might need some time before finishing the
   534 interpreter might need some time before finishing the
   537 evaluation of the program)
   535 evaluation of the program and the CPU getting hot). Evaluating 
   538 
   536 the factorial program with our interpreter we receive as
   539 
   537 ``answer''-environment
   540 
   538 
       
   539 \begin{center}
       
   540 \begin{tabular}{ll}
       
   541 $\fbox{\texttt{a}} \mapsto \texttt{120}$ &
       
   542 $\fbox{\texttt{n}} \mapsto \texttt{0}$
       
   543 \end{tabular}
       
   544 \end{center}
       
   545 
       
   546 \noindent While the discussion above should have illustrated
       
   547 the ideas, in order to do some serious calculation we clearly
       
   548 need to implement the interpreter.
       
   549 
       
   550 
       
   551 \subsubsection*{Code of the Interpreter}
       
   552 
       
   553 Functional programming languages are very convenient for
       
   554 implementations of interpreters. A convenient choice for a
       
   555 functional programming language is Scala. This is a
       
   556 programming language that combines functional and
       
   557 object-oriented programming-styles. It has received in the
       
   558 last five years or so quite a bit of attention. One reason for
       
   559 this attention is that, like the Java programming language,
       
   560 Scala compiles to the Java Virtual Machine (JVM) and therefore
       
   561 Scala programs can run under MacOSX, Linux and
       
   562 Windows.\footnote{There are also experimental backends for
       
   563 Android and JavaScript.} Unlike Java, however, Scala often
       
   564 allows programmers to write very concise and elegant code.
       
   565 Some therefore say Scala is the much better Java. A number of
       
   566 companies, The Guardian, Twitter, Coursera, FourSquare,
       
   567 LinkedIn to name a few, either use Scala exclusively in
       
   568 production code, or at least to some substantial degree. If
       
   569 you want to try out Scala yourself, the Scala compiler can be
       
   570 downloaded from
       
   571 
       
   572 \begin{quote}
       
   573 \url{http://www.scala-lang.org}
       
   574 \end{quote}
       
   575 
       
   576 
       
   577 \begin{figure}[t]
       
   578 \small
       
   579 \lstinputlisting[language=Scala]{../progs/inter.scala}
       
   580 \caption{Bla}
       
   581 \end{figure}
       
   582 
       
   583 
       
   584 \subsubsection*{Static Analysis}
       
   585 
       
   586 Finally we can come back to our original problem, namely 
       
   587 finding out what the signs of variables are 
       
   588 
       
   589 \begin{center}
       
   590 
       
   591 
       
   592 \end{center}
   541  
   593  
   542 \end{document}
   594 \end{document}
   543 
   595 
   544 %%% Local Variables: 
   596 %%% Local Variables: 
   545 %%% mode: latex
   597 %%% mode: latex