|
@@ -1504,25 +1504,26 @@ $\Rightarrow$
|
|
|
We recommend implementing \key{flatten} as a structurally recursive
|
|
|
function that returns two things, 1) the newly flattened expression,
|
|
|
and 2) a list of assignment statements, one for each of the new
|
|
|
-variables introduced while flattening the expression. The newly
|
|
|
-flattened expression should be a \emph{simple} expression, that is, an
|
|
|
-integer or a variable. (There will be more kinds of simple expressions
|
|
|
-in the input languages of later Chapters.) You can return multiple
|
|
|
-things from a function using the \key{values} form and you can receive
|
|
|
-multiple things from a function call using the \key{define-values}
|
|
|
-form. If you are not familiar with these constructs, the Racket
|
|
|
-documentation will be of help. Also, the \key{map2} function
|
|
|
-(Appendix~\ref{appendix:utilities}) is useful for applying a function
|
|
|
-to each element of a list, in the case where the function returns two
|
|
|
-values. The result of \key{map2} is two lists.
|
|
|
+variables introduced during the flattening the expression. The newly
|
|
|
+flattened expression should be an $\Arg$ in the $C_0$ syntax
|
|
|
+(Figure~\ref{fig:c0-syntax}), that is, it should be an integer or a
|
|
|
+variable. You can return multiple things from a function using the
|
|
|
+\key{values} form and you can receive multiple things from a function
|
|
|
+call using the \key{define-values} form. If you are not familiar with
|
|
|
+these constructs, the Racket documentation will be of help. Also, the
|
|
|
+\key{map2} function (Appendix~\ref{appendix:utilities}) is useful for
|
|
|
+applying a function to each element of a list, in the case where the
|
|
|
+function returns two values. The result of \key{map2} is two lists.
|
|
|
|
|
|
The clause of \key{flatten} for the \key{program} node needs to
|
|
|
-recursively flatten the body of the program and also compute the list
|
|
|
-of variables used in the program. I recommend traversing the
|
|
|
-statements in the body of the program (after it has been flattened)
|
|
|
-and collect all variables that appear on the left-hand-side of an
|
|
|
-assignment. Note that each variable should only occur once in the list
|
|
|
-of variables that you place in the \key{program} form.
|
|
|
+recursively flatten the body of the program and the newly flattened
|
|
|
+expression should be placed in a \key{return} statment. The
|
|
|
+\key{flatten} pass should also compute the list of variables used in
|
|
|
+the program. I recommend traversing the statements in the body of the
|
|
|
+program (after it has been flattened) and collect all variables that
|
|
|
+appear on the left-hand-side of an assignment. Note that each variable
|
|
|
+should only occur once in the list of variables that you place in the
|
|
|
+\key{program} form.
|
|
|
|
|
|
Take special care for programs such as the following that initialize
|
|
|
variables with integers or other variables. It should be translated
|
|
@@ -2652,17 +2653,18 @@ $\Rightarrow$
|
|
|
\end{tabular} \\
|
|
|
The value of the \key{if} expression is the value of the branch that
|
|
|
is selected. Recall that in the \code{flatten} pass we need to replace
|
|
|
-complex expressions with simple expressions (variables or
|
|
|
-literals). In the translation above, on the right, we have translated
|
|
|
-the \key{if} expression into a new variable \key{if.1} and we have
|
|
|
-produced code that will assign the appropriate value to \key{if.1}.
|
|
|
-For $R_1$, the \code{flatten} pass returned a list of assignment
|
|
|
-statements. Here, for $R_2$, we return a list of statements that can
|
|
|
-include both \key{if} statements and assignment statements.
|
|
|
+arbitrary expressions with $\Arg$'s (variables or literals). In the
|
|
|
+translation above, on the right, we have translated the \key{if}
|
|
|
+expression into a new variable \key{if.1} and we have produced code
|
|
|
+that will assign the appropriate value to \key{if.1}. For $R_1$, the
|
|
|
+\code{flatten} pass returned a list of assignment statements. Here,
|
|
|
+for $R_2$, we return a list of statements that can include both
|
|
|
+\key{if} statements and assignment statements.
|
|
|
|
|
|
The next example is a bit more involved, showing what happens when
|
|
|
-there are complex expressions in the condition and branch expressions
|
|
|
-of an \key{if}, including nested \key{if} expressions.
|
|
|
+there are complex expressions (not variables or literals) in the
|
|
|
+condition and branch expressions of an \key{if}, including nested
|
|
|
+\key{if} expressions.
|
|
|
|
|
|
\begin{tabular}{lll}
|
|
|
\begin{minipage}{0.4\textwidth}
|