Browse Source

Changes from sharelatex

Jeremy G. Siek 9 năm trước cách đây
mục cha
commit
aa47723206
1 tập tin đã thay đổi với 20 bổ sung6 xóa
  1. 20 6
      book.tex

+ 20 - 6
book.tex

@@ -3019,14 +3019,28 @@ use of a variable, it can lookup its type in the association list.
           [else (error 'typecheck-R2 "'not' expects a Boolean" e)])]
           [else (error 'typecheck-R2 "'not' expects a Boolean" e)])]
        ...
        ...
        [`(program ,body)
        [`(program ,body)
-         (typecheck-R2 '() body)
-         `(program ,body)]
+         (let ([ty (typecheck-R2 '() body)])
+           `(program (type ,ty) ,body))]
        ))
        ))
 \end{lstlisting}
 \end{lstlisting}
 \caption{Skeleton of a type checker for the $R_2$ language.}
 \caption{Skeleton of a type checker for the $R_2$ language.}
 \label{fig:type-check-R2}
 \label{fig:type-check-R2}
 \end{figure}
 \end{figure}
 
 
+In order to print the resulting value correctly, the overall type of the program
+has to be threaded through the remainder of the passes. We can store the type
+within the \key{program} form as a new node. This is shown in Figure \ref{fig:type-check-R2}. The syntax for post-typechecking $R_2$ programs is below:
+
+\fbox{
+\begin{minipage}{0.96\textwidth}
+\[
+\begin{array}{lcl}
+  R_2 &::=& (\key{program}\;(\key{type}\;\textt{type})\; \Exp)
+\end{array}
+\]
+\end{minipage}
+}
+
 \begin{exercise}\normalfont
 \begin{exercise}\normalfont
 Complete the implementation of \code{typecheck-R2} and test it on 10
 Complete the implementation of \code{typecheck-R2} and test it on 10
 new example programs in $R_2$ that you choose based on how thoroughly
 new example programs in $R_2$ that you choose based on how thoroughly
@@ -3067,7 +3081,7 @@ because it is not needed in the translation of the \key{and} of $R_2$.
       \mid (\key{not}\;\Arg) \mid (\key{eq?}\;\Arg\;\Arg) \\
       \mid (\key{not}\;\Arg) \mid (\key{eq?}\;\Arg\;\Arg) \\
 \Stmt &::=& \gray{\ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg}} \\
 \Stmt &::=& \gray{\ASSIGN{\Var}{\Exp} \mid \RETURN{\Arg}} \\
       &\mid& \IF{(\key{eq?}\, \Arg\,\Arg)}{\Stmt^{*}}{\Stmt^{*}} \\
       &\mid& \IF{(\key{eq?}\, \Arg\,\Arg)}{\Stmt^{*}}{\Stmt^{*}} \\
-C_1 & ::= & (\key{program}\;(\Var^{*})\;\Stmt^{+})
+C_1 & ::= & (\key{program}\;(\Var^{*})\;(\key{type}\;\textit{type})\;\Stmt^{+})
 \end{array}
 \end{array}
 \]
 \]
 \end{minipage}
 \end{minipage}
@@ -3211,7 +3225,7 @@ So $0011 \mathrel{\mathrm{XOR}} 0101 = 0110$.
               \mid (\key{movzbq}\;\Arg\;\Arg) \\
               \mid (\key{movzbq}\;\Arg\;\Arg) \\
       &\mid&  (\key{jmp} \; \itm{label}) \mid (\key{je} \; \itm{label}) \mid
       &\mid&  (\key{jmp} \; \itm{label}) \mid (\key{je} \; \itm{label}) \mid
              (\key{label} \; \itm{label}) \\
              (\key{label} \; \itm{label}) \\
-x86_1 &::= & (\key{program} \;\itm{info} \; \Instr^{+})
+x86_1 &::= & (\key{program} \;\itm{info} \;(\key{type}\;\itm{type})\; \Instr^{+})
 \end{array}
 \end{array}
 \]
 \]
 \end{minipage}
 \end{minipage}
@@ -3736,7 +3750,7 @@ short-circuiting behavior in the order of evaluation of its arguments.
     (\key{vector-ref}\;\Exp\;\Int) \\
     (\key{vector-ref}\;\Exp\;\Int) \\
   &\mid& (\key{vector-set!}\;\Exp\;\Int\;\Exp)\\
   &\mid& (\key{vector-set!}\;\Exp\;\Int\;\Exp)\\
   &\mid& (\key{void}) \\
   &\mid& (\key{void}) \\
-  R_3 &::=& (\key{program} \; \Exp)
+  R_3 &::=& (\key{program} \;(\key{type}\;\itm{type})\; \Exp)
 \end{array}
 \end{array}
 \]
 \]
 \end{minipage}
 \end{minipage}
@@ -4727,7 +4741,7 @@ function references from just a symbol $f$ to \code{(function-ref
 good idea, because it will make sure that there are no local variables
 good idea, because it will make sure that there are no local variables
 and functions that share the same name. On the other hand,
 and functions that share the same name. On the other hand,
 \code{reveal-functions} needs to come before the \code{flatten} pass
 \code{reveal-functions} needs to come before the \code{flatten} pass
-because \code{flatten} will help us compiler \code{function-ref}.
+because \code{flatten} will help us compile \code{function-ref}.
 
 
 Because each \code{function-ref} needs to eventually become an
 Because each \code{function-ref} needs to eventually become an
 \code{leaq} instruction, it first needs to become an assignment
 \code{leaq} instruction, it first needs to become an assignment