浏览代码

python, syntax for Pif

Jeremy Siek 3 年之前
父节点
当前提交
eb193114f1
共有 2 个文件被更改,包括 77 次插入25 次删除
  1. 53 15
      book.tex
  2. 24 10
      defs.tex

+ 53 - 15
book.tex

@@ -6109,12 +6109,11 @@ and the linear scan of \citet{Poletto:1999uq} may be more appropriate.
 The \LangInt{} and \LangVar{} languages only have a single kind of
 The \LangInt{} and \LangVar{} languages only have a single kind of
 value, integers. In this chapter we add a second kind of value, the
 value, integers. In this chapter we add a second kind of value, the
 Booleans, to create the \LangIf{} language. The Boolean values
 Booleans, to create the \LangIf{} language. The Boolean values
-\emph{true} and \emph{false} are written \racket{\key{\#t}}\python{\key{True}}
-and \racket{\key{\#f}}\python{\key{False}}
+\emph{true} and \emph{false} are written \TRUE{} and \FALSE{}
 respectively in \racket{Racket}\python{Python}.
 respectively in \racket{Racket}\python{Python}.
 The \LangIf{} language includes several
 The \LangIf{} language includes several
-operations that involve Booleans (\key{and}, \key{not}, \racket{\key{eq?}}\python{==}, \key{<}, etc.) and the conditional \key{if} expression. With the
-addition of \key{if}, programs can have non-trivial control flow which
+operations that involve Booleans (\key{and}, \key{not}, \racket{\key{eq?}}\python{==}, \key{<}, etc.) and the \key{if} expression \python{and statement}.
+With the addition of \key{if}, programs can have non-trivial control flow which
 \racket{impacts \code{explicate\_control} and liveness analysis}
 \racket{impacts \code{explicate\_control} and liveness analysis}
 \python{impacts liveness analysis and motivates a new pass named
 \python{impacts liveness analysis and motivates a new pass named
   \code{explicate\_control}}. Also, because
   \code{explicate\_control}}. Also, because
@@ -6169,7 +6168,7 @@ checking and build a type checker for \LangIf{}
 %
 %
 The remaining sections of this chapter discuss how our compiler passes
 The remaining sections of this chapter discuss how our compiler passes
 change to accommodate Booleans and conditional control flow. There is
 change to accommodate Booleans and conditional control flow. There is
-one new pass, named \code{shrink}, that translates some operators into
+a new pass, named \code{shrink}, that translates some operators into
 others, thereby reducing the number of operators that need to be
 others, thereby reducing the number of operators that need to be
 handled in later passes.
 handled in later passes.
 %
 %
@@ -6193,30 +6192,33 @@ to handle conditional jumps.
 The concrete syntax of the \LangIf{} language is defined in
 The concrete syntax of the \LangIf{} language is defined in
 Figure~\ref{fig:Rif-concrete-syntax} and the abstract syntax is defined
 Figure~\ref{fig:Rif-concrete-syntax} and the abstract syntax is defined
 in Figure~\ref{fig:Rif-syntax}. The \LangIf{} language includes all of
 in Figure~\ref{fig:Rif-syntax}. The \LangIf{} language includes all of
-\LangVar{} (shown in gray), the Boolean literals \code{\#t} and
-\code{\#f}, and the conditional \code{if} expression. We expand the
+\LangVar{}\racket{(shown in gray)}, the Boolean literals \TRUE{} and
+\FALSE{}, and the \code{if} expression \python{and statement}. We expand the
 operators to include
 operators to include
 \begin{enumerate}
 \begin{enumerate}
 \item subtraction on integers,
 \item subtraction on integers,
 \item the logical operators \key{and}, \key{or} and \key{not},
 \item the logical operators \key{and}, \key{or} and \key{not},
-\item the \key{eq?} operation for comparing two integers or two Booleans, and
+\item the \racket{\key{eq?} operation}\python{\key{==} and \key{!=} operations}
+  for comparing two integers or two Booleans for equality, and
 \item the \key{<}, \key{<=}, \key{>}, and \key{>=} operations for
 \item the \key{<}, \key{<=}, \key{>}, and \key{>=} operations for
   comparing integers.
   comparing integers.
 \end{enumerate}
 \end{enumerate}
-We reorganize the abstract syntax for the primitive operations in
-Figure~\ref{fig:Rif-syntax}, using only one grammar rule for all of
-them. This means that the grammar no longer checks whether the arity
-of an operators matches the number of arguments. That responsibility
-is moved to the type checker for \LangIf{}, which we introduce in
-Section~\ref{sec:type-check-Rif}.
+
+\racket{We reorganize the abstract syntax for the primitive
+  operations in Figure~\ref{fig:Rif-syntax}, using only one grammar
+  rule for all of them. This means that the grammar no longer checks
+  whether the arity of an operators matches the number of
+  arguments. That responsibility is moved to the type checker for
+  \LangIf{}, which we introduce in Section~\ref{sec:type-check-Rif}.}
 
 
 \begin{figure}[tp]
 \begin{figure}[tp]
 \centering
 \centering
 \fbox{
 \fbox{
 \begin{minipage}{0.96\textwidth}
 \begin{minipage}{0.96\textwidth}
+{\if\edition\racketEd\color{olive}    
 \[
 \[
 \begin{array}{lcl}
 \begin{array}{lcl}
-  \itm{bool} &::=& \key{\#t} \MID \key{\#f} \\  
+  \itm{bool} &::=& \TRUE \MID \FALSE \\  
   \itm{cmp} &::= & \key{eq?} \MID \key{<} \MID \key{<=} \MID \key{>} \MID \key{>=} \\
   \itm{cmp} &::= & \key{eq?} \MID \key{<} \MID \key{<=} \MID \key{>} \MID \key{>=} \\
   \Exp &::=& \gray{ \Int \MID \CREAD{} \MID \CNEG{\Exp} \MID \CADD{\Exp}{\Exp} }  \MID \CSUB{\Exp}{\Exp} \\
   \Exp &::=& \gray{ \Int \MID \CREAD{} \MID \CNEG{\Exp} \MID \CADD{\Exp}{\Exp} }  \MID \CSUB{\Exp}{\Exp} \\
      &\MID&  \gray{ \Var \MID \CLET{\Var}{\Exp}{\Exp} } \\
      &\MID&  \gray{ \Var \MID \CLET{\Var}{\Exp}{\Exp} } \\
@@ -6227,6 +6229,20 @@ Section~\ref{sec:type-check-Rif}.
   \LangIfM{} &::=& \Exp
   \LangIfM{} &::=& \Exp
 \end{array}
 \end{array}
 \]
 \]
+\fi}
+{\if\edition\pythonEd\color{purple}
+\[
+\begin{array}{rcl}
+  \itm{binop} &::= & \key{+} \MID \key{-} \MID \key{==} \MID \key{!=} \MID \key{<} \MID \key{<=} \MID \key{>} \MID \key{>=} \\
+  \itm{uniop} &::= & \key{-} \MID \key{not} \\
+  \Exp &::=& \Int \MID \key{input\_int}\LP\RP \MID \itm{uniop}\;\Exp \MID \Exp \; \itm{binop} \; \Exp \MID \Var{} \\
+  &\MID&  \TRUE \MID \FALSE \MID \Exp\;\key{if}\;\Exp\;\key{else}\;\Exp\\
+  \Stmt &::=& \key{print}\LP \Exp \RP \MID \Exp \MID \Var\mathop{\key{=}}\Exp
+      \MID \key{if}\; \Exp \;\key{:}\; \Stmt^{+} \;\key{else:}\; \Stmt^{+}\\
+  \LangVarM{} &::=& \Stmt^{*}
+\end{array}
+\]
+\fi}
 \end{minipage}
 \end{minipage}
 }
 }
 \caption{The concrete syntax of \LangIf{}, extending \LangVar{}
 \caption{The concrete syntax of \LangIf{}, extending \LangVar{}
@@ -6238,6 +6254,7 @@ Section~\ref{sec:type-check-Rif}.
 \centering
 \centering
 \fbox{
 \fbox{
 \begin{minipage}{0.96\textwidth}
 \begin{minipage}{0.96\textwidth}
+{\if\edition\racketEd\color{olive}    
 \[
 \[
 \begin{array}{lcl}
 \begin{array}{lcl}
   \itm{bool} &::=& \code{\#t} \MID \code{\#f} \\
   \itm{bool} &::=& \code{\#t} \MID \code{\#f} \\
@@ -6250,6 +6267,27 @@ Section~\ref{sec:type-check-Rif}.
   \LangIfM{} &::=& \PROGRAM{\code{'()}}{\Exp}
   \LangIfM{} &::=& \PROGRAM{\code{'()}}{\Exp}
 \end{array}
 \end{array}
 \]
 \]
+\fi}
+{\if\edition\pythonEd\color{purple}
+\[
+\begin{array}{lcl}
+\itm{binop} &::=& \code{Add()} \MID \code{Sub()} \\
+\itm{uniop} &::=& \code{USub()} \MID \code{Not()} \\
+\itm{bool} &::=& \code{True} \MID \code{False} \\
+\itm{boolop} &::=& \code{And()} \MID \code{Or()} \\
+\itm{cmp} &::= & \code{Eq()} \MID \code{NotEq()} \MID \code{Lt()} \MID \code{LtE()} \MID \code{Gt()} \MID \code{GtE()} \\
+\Exp &::=& \INT{\Int} \MID \READ{} \MID \VAR{\Var} \\
+     &\MID& \BINOP{\Exp}{\itm{binop}}{\Exp}
+     \MID \UNIOP{\itm{uniop}}{\Exp}\\
+     &\MID& \CMP{\Exp}{\itm{cmp}}{\Exp} 
+     \MID \BOOLOP{\itm{boolop}}{\Exp}{\Exp}\\
+     &\MID& \BOOL{\itm{bool}} \MID \IF{\Exp}{\Exp}{\Exp} \\
+\Stmt{} &::=& \PRINT{\Exp} \MID \EXPR{\Exp} \\
+        &\MID& \ASSIGN{\VAR{\Var}}{\Exp} \MID \IFSTMT{\Exp}{\Stmt^{*}}{\Stmt^{*}}\\
+\LangIfM{} &::=& \PROGRAM{\code{'()}}{\Stmt^{*}}
+\end{array}
+\]
+\fi}
 \end{minipage}
 \end{minipage}
 }
 }
 \caption{The abstract syntax of \LangIf{}.}
 \caption{The abstract syntax of \LangIf{}.}

+ 24 - 10
defs.tex

@@ -13,6 +13,7 @@
 \newcommand{\LangVarANFM}{R^{\mathsf{ANF}}_{\mathsf{Var}}}
 \newcommand{\LangVarANFM}{R^{\mathsf{ANF}}_{\mathsf{Var}}}
 
 
 \newcommand{\LangIf}{$R_{\mathsf{If}}$} %R2
 \newcommand{\LangIf}{$R_{\mathsf{If}}$} %R2
+\newcommand{\LangIfM}{\ensuremath{R_{\mathsf{If}}}} %R2
 \fi
 \fi
 
 
 \if\edition\pythonEd
 \if\edition\pythonEd
@@ -25,12 +26,12 @@
 \newcommand{\LangVarANFM}{P^{\mathsf{ANF}}_{\mathsf{Var}}}
 \newcommand{\LangVarANFM}{P^{\mathsf{ANF}}_{\mathsf{Var}}}
 
 
 \newcommand{\LangIf}{$P_{\mathsf{If}}$} %R2
 \newcommand{\LangIf}{$P_{\mathsf{If}}$} %R2
+\newcommand{\LangIfM}{\ensuremath{P_{\mathsf{If}}}} %R2
 \fi
 \fi
 
 
 
 
 \newcommand{\LangCVar}{$C_{\mathsf{Var}}$} % C0
 \newcommand{\LangCVar}{$C_{\mathsf{Var}}$} % C0
 \newcommand{\LangCVarM}{C_{\mathsf{Var}}} % C0
 \newcommand{\LangCVarM}{C_{\mathsf{Var}}} % C0
-\newcommand{\LangIfM}{\ensuremath{R_{\mathsf{If}}}} %R2
 \newcommand{\LangCIf}{$C_{\mathsf{If}}$} %C1
 \newcommand{\LangCIf}{$C_{\mathsf{If}}$} %C1
 \newcommand{\LangCIfM}{\ensuremath{C_{\mathsf{If}}}} %C1
 \newcommand{\LangCIfM}{\ensuremath{C_{\mathsf{If}}}} %C1
 \newcommand{\LangIfANF}{\ensuremath{R^{\mathsf{ANF}}_{\mathsf{if}}}} %R2
 \newcommand{\LangIfANF}{\ensuremath{R^{\mathsf{ANF}}_{\mathsf{if}}}} %R2
@@ -132,6 +133,14 @@
 \newcommand{\ADD}[2]{{\color{olive}\key{(Prim}~\code{+}~\code{(}#1~#2\code{))}}}
 \newcommand{\ADD}[2]{{\color{olive}\key{(Prim}~\code{+}~\code{(}#1~#2\code{))}}}
 \newcommand{\PROGRAM}[2]{\LP\code{Program}~#1~#2\RP}
 \newcommand{\PROGRAM}[2]{\LP\code{Program}~#1~#2\RP}
 \newcommand{\VAR}[1]{\key{(Var}~#1\key{)}}
 \newcommand{\VAR}[1]{\key{(Var}~#1\key{)}}
+\newcommand{\BOOL}[1]{\key{(Bool}~#1\key{)}}
+\newcommand{\TRUE}{\key{\#t}}
+\newcommand{\FALSE}{\key{\#f}}
+\newcommand{\UNIOP}[2]{\key{(Prim}~#1~\code{(}#2\code{))}}
+\newcommand{\CUNIOP}[2]{\LP #1~#2 \RP}
+\newcommand{\BINOP}[3]{\key{(Prim}~#1~\code{(}#2~#3\code{))}}
+\newcommand{\CBINOP}[3]{\LP #1~#2~#3\RP}
+\newcommand{\IF}[3]{\key{(If}\,#1~#2~#3\key{)}}
 \fi
 \fi
 
 
 \if\edition\pythonEd
 \if\edition\pythonEd
@@ -139,14 +148,23 @@
 \newcommand{\READOP}{{\color{purple}\key{input\_int}}}
 \newcommand{\READOP}{{\color{purple}\key{input\_int}}}
 \newcommand{\READ}{{\color{purple}\key{Call(Name('input\_int'),[])}}}
 \newcommand{\READ}{{\color{purple}\key{Call(Name('input\_int'),[])}}}
 \newcommand{\NEG}[1]{{\color{purple}\key{UnaryOp(USub(),} #1\code{)}}}
 \newcommand{\NEG}[1]{{\color{purple}\key{UnaryOp(USub(),} #1\code{)}}}
-\newcommand{\ADD}[2]{{\color{purple}\key{BinOp(Add()}\key{,}#1\code{,}#2\code{)}}}
+\newcommand{\ADD}[2]{{\color{purple}\key{BinOp}\LP \key{Add()}\key{,}#1\code{,}#2\code{)}}}
 \newcommand{\PRINT}[1]{{\color{purple}\key{Expr}\LP\key{Call}\LP\key{Name}\LP\key{'print'}\RP\key{,}\LS#1\RS\RP\RP}}
 \newcommand{\PRINT}[1]{{\color{purple}\key{Expr}\LP\key{Call}\LP\key{Name}\LP\key{'print'}\RP\key{,}\LS#1\RS\RP\RP}}
 \newcommand{\EXPR}[1]{{\color{purple}\key{Expr}\LP #1\RP}}
 \newcommand{\EXPR}[1]{{\color{purple}\key{Expr}\LP #1\RP}}
 \newcommand{\PROGRAM}[2]{\code{Module}\LP #2\RP}
 \newcommand{\PROGRAM}[2]{\code{Module}\LP #2\RP}
 \newcommand{\VAR}[1]{\key{Name}\LP #1\RP}
 \newcommand{\VAR}[1]{\key{Name}\LP #1\RP}
+\newcommand{\BOOL}[1]{\key{Constant}\LP #1 \RP}
+\newcommand{\UNIOP}[2]{\key{UnaryOp}\LP #1 \code{,} #2 \RP}
+\newcommand{\CUNIOP}[2]{#1~#2}
+\newcommand{\BINOP}[3]{\key{BinOp}\LP #1 \code{,} #2 \code{,} #3 \RP}
+\newcommand{\BOOLOP}[3]{\key{BoolOp}\LP #1 \code{,} #2 \code{,} #3 \RP}
+\newcommand{\CMP}[3]{\key{Compare}\LP #1\code{,}\LS #2 \RS \code{,} #3\RP}
+\newcommand{\CBINOP}[3]{#1 #2 #3}
+\newcommand{\TRUE}{\key{True}}
+\newcommand{\FALSE}{\key{False}}
+\newcommand{\IF}[3]{\key{IfExp}\LP #1 \code{,} #2 \code{,} #3 \RP}
 \fi
 \fi
 
 
-\newcommand{\BOOL}[1]{\key{(Bool}~#1\key{)}}
 \newcommand{\PRIM}[2]{\LP\key{Prim}~#1~\LP #2\RP\RP}
 \newcommand{\PRIM}[2]{\LP\key{Prim}~#1~\LP #2\RP\RP}
 \newcommand{\CREAD}{\key{(read)}}
 \newcommand{\CREAD}{\key{(read)}}
 \newcommand{\CNEG}[1]{\LP\key{-}~#1\RP}
 \newcommand{\CNEG}[1]{\LP\key{-}~#1\RP}
@@ -167,14 +185,9 @@
 \newcommand{\AND}[2]{\key{(Prim}~\code{and}~\code{(}#1~#2\code{))}}
 \newcommand{\AND}[2]{\key{(Prim}~\code{and}~\code{(}#1~#2\code{))}}
 \newcommand{\OR}[2]{\key{(Prim}~\code{or}~\code{(}#1~#2\code{))}}
 \newcommand{\OR}[2]{\key{(Prim}~\code{or}~\code{(}#1~#2\code{))}}
 \newcommand{\NOT}[1]{\key{(Prim}~\code{not}~\code{(}#1~\code{))}}
 \newcommand{\NOT}[1]{\key{(Prim}~\code{not}~\code{(}#1~\code{))}}
-\newcommand{\UNIOP}[2]{\key{(Prim}~#1~\code{(}#2\code{))}}
-\newcommand{\CUNIOP}[2]{\LP #1~#2 \RP}
-\newcommand{\BINOP}[3]{\key{(Prim}~#1~\code{(}#2~#3\code{))}}
-\newcommand{\CBINOP}[3]{\LP #1~#2~#3\RP}
+\newcommand{\LET}[3]{\key{(Let}~#1~#2~#3\key{)}}
 \newcommand{\CLET}[3]{\LP\key{let}~\LP\LS#1~#2\RS\RP~#3\RP}
 \newcommand{\CLET}[3]{\LP\key{let}~\LP\LS#1~#2\RS\RP~#3\RP}
 \newcommand{\CIF}[3]{\LP\key{if}~#1~#2~#3\RP}
 \newcommand{\CIF}[3]{\LP\key{if}~#1~#2~#3\RP}
-\newcommand{\LET}[3]{\key{(Let}~#1~#2~#3\key{)}}
-\newcommand{\IF}[3]{\key{(If}\,#1~#2~#3\key{)}}
 \newcommand{\CAST}[3]{\LP\key{Cast}~#1~#2~#3\RP}
 \newcommand{\CAST}[3]{\LP\key{Cast}~#1~#2~#3\RP}
 \newcommand{\VECTOR}[1]{\LP\key{Prim}~\code{vector}~\LP~#1\RP\RP}
 \newcommand{\VECTOR}[1]{\LP\key{Prim}~\code{vector}~\LP~#1\RP\RP}
 \newcommand{\VECREF}[2]{\LP\key{Prim}~\code{vector-ref}~\LP~#1~#2\RP\RP}
 \newcommand{\VECREF}[2]{\LP\key{Prim}~\code{vector-ref}~\LP~#1~#2\RP\RP}
@@ -207,15 +220,16 @@
 
 
 \if\edition\racketEd
 \if\edition\racketEd
 \newcommand{\ASSIGN}[2]{\key{(Assign}~#1~#2\key{)}}
 \newcommand{\ASSIGN}[2]{\key{(Assign}~#1~#2\key{)}}
+\newcommand{\IFSTMT}[3]{\key{(IfStmt}\,#1~#2~#3\key{)}}
 \fi
 \fi
 \if\edition\pythonEd
 \if\edition\pythonEd
 \newcommand{\ASSIGN}[2]{\key{Assign}\LP\LS #1\RS\key{,}#2\RP}
 \newcommand{\ASSIGN}[2]{\key{Assign}\LP\LS #1\RS\key{,}#2\RP}
+\newcommand{\IFSTMT}[3]{\key{If}\LP #1 \code{,} #2 \code{,} #3 \RP}
 \fi
 \fi
 
 
 \newcommand{\RETURN}[1]{\key{(Return}~#1\key{)}}
 \newcommand{\RETURN}[1]{\key{(Return}~#1\key{)}}
 \newcommand{\SEQ}[2]{\key{(Seq}~#1~#2\key{)}}
 \newcommand{\SEQ}[2]{\key{(Seq}~#1~#2\key{)}}
 \newcommand{\GOTO}[1]{\key{(Goto}~#1\key{)}}
 \newcommand{\GOTO}[1]{\key{(Goto}~#1\key{)}}
-\newcommand{\IFSTMT}[3]{\key{(IfStmt}\,#1~#2~#3\key{)}}
 
 
 \if\edition\racketEd
 \if\edition\racketEd
 \newcommand{\IMM}[1]{\key{(Imm}~#1\key{)}}
 \newcommand{\IMM}[1]{\key{(Imm}~#1\key{)}}