handouts/ho07.tex
changeset 601 208b0f67a3d0
parent 600 d488a3e7b0ec
child 668 9ce78065f68d
equal deleted inserted replaced
600:d488a3e7b0ec 601:208b0f67a3d0
       
     1 % !TEX program = xelatex
     1 \documentclass{article}
     2 \documentclass{article}
     2 \usepackage{../style}
     3 \usepackage{../style}
     3 \usepackage{../langs}
     4 \usepackage{../langs}
     4 \usepackage{../grammar}
     5 \usepackage{../grammar}
     5 \usepackage{../graphics}
     6 \usepackage{../graphics}
   240 \end{lstlisting}
   241 \end{lstlisting}
   241 
   242 
   242 \noindent 
   243 \noindent 
   243 where $n_x$ is the index for the variable $x$.
   244 where $n_x$ is the index for the variable $x$.
   244 
   245 
   245 More complicated is the code for \pcode{if}-statments, say
   246 More complicated is the code for \pcode{if}-statements, say
   246 
   247 
   247 \begin{lstlisting}[mathescape,language={},numbers=none]
   248 \begin{lstlisting}[mathescape,language={},numbers=none]
   248 if $b$ then $cs_1$ else $cs_2$
   249 if $b$ then $cs_1$ else $cs_2$
   249 \end{lstlisting}
   250 \end{lstlisting}
   250 
   251 
   379 else-branch and then for the if-branch. However in the case
   380 else-branch and then for the if-branch. However in the case
   380 of while-loops this way of generating code still seems
   381 of while-loops this way of generating code still seems
   381 the most convenient.
   382 the most convenient.
   382 
   383 
   383 We are now ready to give the compile function for 
   384 We are now ready to give the compile function for 
   384 if-statments---remember this function returns for staments a 
   385 if-statements---remember this function returns for statements a 
   385 pair consisting of the code and an environment:
   386 pair consisting of the code and an environment:
   386 
   387 
   387 \begin{center}
   388 \begin{center}
   388 \begin{tabular}{lcl}
   389 \begin{tabular}{lcl}
   389 $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
   390 $\textit{compile}(\pcode{if}\;b\;\pcode{then}\; cs_1\;\pcode{else}\; cs_2, E)$ & $\dn$\\ 
   640 simple While-programs. In a real compiler, we would of course
   641 simple While-programs. In a real compiler, we would of course
   641 need to work harder and find out appropriate values for the
   642 need to work harder and find out appropriate values for the
   642 stack and local variables.
   643 stack and local variables.
   643 
   644 
   644 To sum up, in Figure~\ref{test} is the complete code generated
   645 To sum up, in Figure~\ref{test} is the complete code generated
   645 for the slightly non-sensical program
   646 for the slightly nonsensical program
   646 
   647 
   647 \begin{lstlisting}[mathescape,language=While]
   648 \begin{lstlisting}[mathescape,language=While]
   648 x := 1 + 2;
   649 x := 1 + 2;
   649 write x
   650 write x
   650 \end{lstlisting}
   651 \end{lstlisting}