|
@@ -18674,6 +18674,19 @@ class InterpLdyn(InterpLlambda):
|
|
return self.tag(True)
|
|
return self.tag(True)
|
|
else:
|
|
else:
|
|
return self.interp_exp(right, env)
|
|
return self.interp_exp(right, env)
|
|
|
|
+\end{lstlisting}
|
|
|
|
+\fi}
|
|
|
|
+ \end{tcolorbox}
|
|
|
|
+
|
|
|
|
+ \caption{Interpreter for the \LangDyn{} language\python{, part 1}.}
|
|
|
|
+\label{fig:interp-Ldyn}
|
|
|
|
+\end{figure}
|
|
|
|
+
|
|
|
|
+{\if\edition\pythonEd\pythonColor
|
|
|
|
+ \begin{figure}[tbp]
|
|
|
|
+\begin{tcolorbox}[colback=white]
|
|
|
|
+\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
|
|
+ # interp_exp continued
|
|
case Compare(left, [cmp], [right]):
|
|
case Compare(left, [cmp], [right]):
|
|
l = self.interp_exp(left, env)
|
|
l = self.interp_exp(left, env)
|
|
r = self.interp_exp(right, env)
|
|
r = self.interp_exp(right, env)
|
|
@@ -18691,19 +18704,7 @@ class InterpLdyn(InterpLlambda):
|
|
return self.tag(len(self.untag(t, 'tuple', e)))
|
|
return self.tag(len(self.untag(t, 'tuple', e)))
|
|
case _:
|
|
case _:
|
|
return self.tag(super().interp_exp(e, env))
|
|
return self.tag(super().interp_exp(e, env))
|
|
-\end{lstlisting}
|
|
|
|
-\fi}
|
|
|
|
- \end{tcolorbox}
|
|
|
|
-
|
|
|
|
- \caption{Interpreter for the \LangDyn{} language\python{, part 1}.}
|
|
|
|
-\label{fig:interp-Ldyn}
|
|
|
|
-\end{figure}
|
|
|
|
|
|
|
|
-{\if\edition\pythonEd\pythonColor
|
|
|
|
- \begin{figure}[tbp]
|
|
|
|
-\begin{tcolorbox}[colback=white]
|
|
|
|
-\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
|
|
-class InterpLdyn(InterpLlambda):
|
|
|
|
def interp_stmt(self, s, env, cont):
|
|
def interp_stmt(self, s, env, cont):
|
|
match s:
|
|
match s:
|
|
case If(test, body, orelse):
|
|
case If(test, body, orelse):
|
|
@@ -18800,7 +18801,7 @@ class InterpLdyn(InterpLlambda):
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
\fi}
|
|
\fi}
|
|
{\if\edition\pythonEd\pythonColor
|
|
{\if\edition\pythonEd\pythonColor
|
|
-\begin{lstlisting}
|
|
|
|
|
|
+\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
class InterpLdyn(InterpLlambda):
|
|
class InterpLdyn(InterpLlambda):
|
|
def tag(self, v):
|
|
def tag(self, v):
|
|
if v is True or v is False:
|
|
if v is True or v is False:
|
|
@@ -18835,7 +18836,7 @@ class InterpLdyn(InterpLlambda):
|
|
\label{fig:interp-Ldyn-aux}
|
|
\label{fig:interp-Ldyn-aux}
|
|
\end{figure}
|
|
\end{figure}
|
|
|
|
|
|
-\clearpage
|
|
|
|
|
|
+%\clearpage
|
|
|
|
|
|
\section{Representation of Tagged Values}
|
|
\section{Representation of Tagged Values}
|
|
|
|
|
|
@@ -18878,7 +18879,7 @@ the rightmost 3 bits with the tag, and we can simply zero out the tag
|
|
to recover the original address.
|
|
to recover the original address.
|
|
|
|
|
|
To make tagged values into first-class entities, we can give them a
|
|
To make tagged values into first-class entities, we can give them a
|
|
-type called \racket{\code{Any}}\python{\code{AnyType()}} and define
|
|
|
|
|
|
+type called \racket{\code{Any}}\python{\code{AnyType}} and define
|
|
operations such as \code{Inject} and \code{Project} for creating and
|
|
operations such as \code{Inject} and \code{Project} for creating and
|
|
using them, yielding the statically typed \LangAny{} intermediate
|
|
using them, yielding the statically typed \LangAny{} intermediate
|
|
language. We describe how to compile \LangDyn{} to \LangAny{} in
|
|
language. We describe how to compile \LangDyn{} to \LangAny{} in
|
|
@@ -19138,7 +19139,7 @@ class TypeCheckLany(TypeCheckLlambda):
|
|
\end{figure}
|
|
\end{figure}
|
|
\fi}
|
|
\fi}
|
|
|
|
|
|
-\begin{figure}[btp]
|
|
|
|
|
|
+\begin{figure}[tbp]
|
|
\begin{tcolorbox}[colback=white]
|
|
\begin{tcolorbox}[colback=white]
|
|
{\if\edition\racketEd
|
|
{\if\edition\racketEd
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
@@ -19185,39 +19186,32 @@ class TypeCheckLany(TypeCheckLlambda):
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
\fi}
|
|
\fi}
|
|
{\if\edition\pythonEd\pythonColor
|
|
{\if\edition\pythonEd\pythonColor
|
|
-\begin{lstlisting}
|
|
|
|
|
|
+\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
class InterpLany(InterpLlambda):
|
|
class InterpLany(InterpLlambda):
|
|
def interp_exp(self, e, env):
|
|
def interp_exp(self, e, env):
|
|
match e:
|
|
match e:
|
|
case Inject(value, typ):
|
|
case Inject(value, typ):
|
|
- v = self.interp_exp(value, env)
|
|
|
|
- return Tagged(v, self.type_to_tag(typ))
|
|
|
|
|
|
+ return Tagged(self.interp_exp(value, env), self.type_to_tag(typ))
|
|
case Project(value, typ):
|
|
case Project(value, typ):
|
|
- v = self.interp_exp(value, env)
|
|
|
|
- match v:
|
|
|
|
|
|
+ match self.interp_exp(value, env):
|
|
case Tagged(val, tag) if self.type_to_tag(typ) == tag:
|
|
case Tagged(val, tag) if self.type_to_tag(typ) == tag:
|
|
return val
|
|
return val
|
|
case _:
|
|
case _:
|
|
- raise Exception('interp project to ' + repr(typ)
|
|
|
|
- + ' unexpected ' + repr(v))
|
|
|
|
|
|
+ raise Exception('failed project to ' + self.type_to_tag(typ))
|
|
case Call(Name('any_tuple_load'), [tup, index]):
|
|
case Call(Name('any_tuple_load'), [tup, index]):
|
|
- tv = self.interp_exp(tup, env)
|
|
|
|
- n = self.interp_exp(index, env)
|
|
|
|
- match tv:
|
|
|
|
|
|
+ match self.interp_exp(tup, env):
|
|
case Tagged(v, tag):
|
|
case Tagged(v, tag):
|
|
- return v[n]
|
|
|
|
|
|
+ return v[self.interp_exp(index, env)]
|
|
case _:
|
|
case _:
|
|
- raise Exception('in any_tuple_load unexpected ' + repr(tv))
|
|
|
|
|
|
+ raise Exception('in any_tuple_load untagged value')
|
|
case Call(Name('any_len'), [value]):
|
|
case Call(Name('any_len'), [value]):
|
|
- v = self.interp_exp(value, env)
|
|
|
|
- match v:
|
|
|
|
|
|
+ match self.interp_exp(value, env):
|
|
case Tagged(value, tag):
|
|
case Tagged(value, tag):
|
|
return len(value)
|
|
return len(value)
|
|
case _:
|
|
case _:
|
|
- raise Exception('interp any_len unexpected ' + repr(v))
|
|
|
|
|
|
+ raise Exception('interp any_len untagged value')
|
|
case Call(Name('arity'), [fun]):
|
|
case Call(Name('arity'), [fun]):
|
|
- f = self.interp_exp(fun, env)
|
|
|
|
- return self.arity(f)
|
|
|
|
|
|
+ return self.arity(self.interp_exp(fun, env))
|
|
case _:
|
|
case _:
|
|
return super().interp_exp(e, env)
|
|
return super().interp_exp(e, env)
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
@@ -19227,7 +19221,7 @@ class InterpLany(InterpLlambda):
|
|
\label{fig:interp-Lany}
|
|
\label{fig:interp-Lany}
|
|
\end{figure}
|
|
\end{figure}
|
|
|
|
|
|
-\begin{figure}[tbp]
|
|
|
|
|
|
+\begin{figure}[btp]
|
|
\begin{tcolorbox}[colback=white]
|
|
\begin{tcolorbox}[colback=white]
|
|
{\if\edition\racketEd
|
|
{\if\edition\racketEd
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
@@ -19260,7 +19254,7 @@ class InterpLany(InterpLlambda):
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
\fi}
|
|
\fi}
|
|
{\if\edition\pythonEd\pythonColor
|
|
{\if\edition\pythonEd\pythonColor
|
|
-\begin{lstlisting}
|
|
|
|
|
|
+\begin{lstlisting}[basicstyle=\ttfamily\footnotesize]
|
|
class InterpLany(InterpLlambda):
|
|
class InterpLany(InterpLlambda):
|
|
def type_to_tag(self, typ):
|
|
def type_to_tag(self, typ):
|
|
match typ:
|
|
match typ:
|
|
@@ -19268,23 +19262,16 @@ class InterpLany(InterpLlambda):
|
|
return 'function'
|
|
return 'function'
|
|
case TupleType(fields):
|
|
case TupleType(fields):
|
|
return 'tuple'
|
|
return 'tuple'
|
|
- case t if t == int:
|
|
|
|
- return 'int'
|
|
|
|
- case t if t == bool:
|
|
|
|
- return 'bool'
|
|
|
|
case IntType():
|
|
case IntType():
|
|
return 'int'
|
|
return 'int'
|
|
case BoolType():
|
|
case BoolType():
|
|
- return 'int'
|
|
|
|
|
|
+ return 'bool'
|
|
case _:
|
|
case _:
|
|
raise Exception('type_to_tag unexpected ' + repr(typ))
|
|
raise Exception('type_to_tag unexpected ' + repr(typ))
|
|
-
|
|
|
|
def arity(self, v):
|
|
def arity(self, v):
|
|
match v:
|
|
match v:
|
|
case Function(name, params, body, env):
|
|
case Function(name, params, body, env):
|
|
return len(params)
|
|
return len(params)
|
|
- case ClosureTuple(args, arity):
|
|
|
|
- return arity
|
|
|
|
case _:
|
|
case _:
|
|
raise Exception('Lany arity unexpected ' + repr(v))
|
|
raise Exception('Lany arity unexpected ' + repr(v))
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
@@ -20118,7 +20105,7 @@ nevertheless they should be valid \LangDyn{} programs that run to
|
|
completion without error.
|
|
completion without error.
|
|
\end{exercise}
|
|
\end{exercise}
|
|
|
|
|
|
-Figure~\ref{fig:Ldyn-passes} provides an overview of the passes needed
|
|
|
|
|
|
+Figure~\ref{fig:Ldyn-passes} gives an overview of the passes needed
|
|
for the compilation of \LangDyn{}.
|
|
for the compilation of \LangDyn{}.
|
|
|
|
|
|
\begin{figure}[bthp]
|
|
\begin{figure}[bthp]
|