1.1 --- a/simplify.py Mon Jul 24 00:21:01 2006 +0200
1.2 +++ b/simplify.py Mon Jul 24 22:33:47 2006 +0200
1.3 @@ -170,6 +170,17 @@
1.4
1.5 def visitIf(self, if_):
1.6
1.7 + """
1.8 + Convert If(tests=..., else_=...) to:
1.9 +
1.10 + Invoke -> Subprogram
1.11 + Conditional (test) -> Invoke -> Subprogram (body)
1.12 + Return
1.13 + Conditional (test) -> Invoke -> Subprogram (body)
1.14 + Return
1.15 + ...
1.16 + """
1.17 +
1.18 # Make a subprogram for the statement and record it outside the main tree.
1.19
1.20 subprogram = Subprogram(name=hex(id(if_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.21 @@ -185,13 +196,30 @@
1.22 test = Conditional(else_=[], test=Invoke(
1.23 expr=LoadAttr(expr=self.dispatch(compare), name="__true__"),
1.24 args=[], star=None, dstar=None))
1.25 - test.body = self.dispatch(stmt) + [Return()]
1.26 +
1.27 + body_subprogram = Subprogram(name=hex(id(stmt)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.28 + self.current_subprograms.append(body_subprogram)
1.29 +
1.30 + body_subprogram.code = self.dispatch(stmt) + [Return()]
1.31 +
1.32 + self.current_subprograms.pop()
1.33 + self.subprograms.append(body_subprogram)
1.34 +
1.35 + test.body = [Invoke(stmt, expr=LoadRef(ref=body_subprogram), same_frame=1, star=None, dstar=None, args=[]), Return()]
1.36 nodes.append(test)
1.37
1.38 # Add the compound statement from any else clause to the end.
1.39
1.40 if if_.else_ is not None:
1.41 - nodes += self.dispatch(if_.else_)
1.42 + else_subprogram = Subprogram(name=hex(id(if_.else_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.43 + self.current_subprograms.append(else_subprogram)
1.44 +
1.45 + else_subprogram.code = self.dispatch(if_.else_) + [Return()]
1.46 +
1.47 + self.current_subprograms.pop()
1.48 + self.subprograms.append(else_subprogram)
1.49 +
1.50 + nodes.append(Invoke(stmt, expr=LoadRef(ref=else_subprogram), same_frame=1, star=None, dstar=None, args=[]))
1.51
1.52 subprogram.code = nodes
1.53