equal
deleted
inserted
replaced
|
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} |
18 |
19 |
19 \section*{Handout 8 (A Functional Language)} |
20 \section*{Handout 8 (A Functional Language)} |
20 |
21 |
21 |
22 |
22 The language we looked at in the previous lecture was rather |
23 The language we looked at in the previous lecture was rather |
23 primitive and the estimater rather crude---everything was |
24 primitive and the compiler rather crude---everything was |
24 essentially estimated into a big monolithic chunk of code |
25 essentially estimated into a big monolithic chunk of code |
25 inside the main function. In this handout we like to have a |
26 inside the main function. In this handout we like to have a |
26 look at a slightly more comfortable language, which I call |
27 look at a slightly more comfortable language, which I call |
27 Fun-language, and a tiny-teeny bit more realistic estimater. |
28 Fun-language, and a tiny-teeny bit more realistic compiler. |
28 The Fun-language is a functional programming language. A small |
29 The Fun-language is a functional programming language. A small |
29 collection of programs we want to be able to write and estimate |
30 collection of programs we want to be able to write and estimate |
30 is as follows: |
31 is as follows: |
31 |
32 |
32 \begin{lstlisting}[numbers=none] |
33 \begin{lstlisting}[numbers=none] |
122 Let us first look at some clauses for compiling expressions. |
123 Let us first look at some clauses for compiling expressions. |
123 The compilation of arithmetic and boolean expressions is just |
124 The compilation of arithmetic and boolean expressions is just |
124 like for the While-language and do not need any modification. |
125 like for the While-language and do not need any modification. |
125 (recall that the \textit{estimate}-function for boolean |
126 (recall that the \textit{estimate}-function for boolean |
126 expression takes a third argument for the label where the |
127 expression takes a third argument for the label where the |
127 contro-flow should jump when the boolean expression is |
128 control-flow should jump when the boolean expression is |
128 \emph{not} true---this is needed for compiling \pcode{if}s). |
129 \emph{not} true---this is needed for compiling \pcode{if}s). |
129 One additional feature in the Fun-language are sequences. |
130 One additional feature in the Fun-language are sequences. |
130 Their purpose is to do one calculation after another. The |
131 Their purpose is to do one calculation after another. The |
131 reason why we need to be careful however is the convention |
132 reason why we need to be careful however is the convention |
132 that every expression can only produce s single result |
133 that every expression can only produce s single result |
182 |
183 |
183 \noindent We also need to first generate code for the |
184 \noindent We also need to first generate code for the |
184 argument-expression of write, which in the While-language was |
185 argument-expression of write, which in the While-language was |
185 only allowed to be a single variable. |
186 only allowed to be a single variable. |
186 |
187 |
187 Most of the new code in the estimater for the Fun-language |
188 Most of the new code in the compiler for the Fun-language |
188 comes from function definitions and function calls. For this |
189 comes from function definitions and function calls. For this |
189 have a look again at the helper function in |
190 have a look again at the helper function in |
190 Figure~\ref{write}. Assuming we have a function definition |
191 Figure~\ref{write}. Assuming we have a function definition |
191 |
192 |
192 \begin{lstlisting}[numbers=none,mathescape] |
193 \begin{lstlisting}[numbers=none,mathescape] |
266 |
267 |
267 def add(x, y) = if x == 0 then y |
268 def add(x, y) = if x == 0 then y |
268 else suc(add(x - 1, y)); |
269 else suc(add(x - 1, y)); |
269 \end{lstlisting} |
270 \end{lstlisting} |
270 |
271 |
271 \noindent The succesor function is a simple loading of the |
272 \noindent The successor function is a simple loading of the |
272 argument $x$ (index $0$) onto the stack, as well as the number |
273 argument $x$ (index $0$) onto the stack, as well as the number |
273 $1$. Then we add both elements leaving the result of the |
274 $1$. Then we add both elements leaving the result of the |
274 addition on top of the stack. This value will be returned by |
275 addition on top of the stack. This value will be returned by |
275 the \pcode{suc}-function. See below: |
276 the \pcode{suc}-function. See below: |
276 |
277 |