1.1 --- a/simplify.py Tue Jul 11 00:53:56 2006 +0200
1.2 +++ b/simplify.py Thu Jul 13 23:15:20 2006 +0200
1.3 @@ -142,7 +142,7 @@
1.4 return result
1.5
1.6 def visitContinue(self, continue_):
1.7 - result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[])
1.8 + result = Invoke(continue_, same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.9 result.expr = LoadRef(ref=self.current_subprograms[-1])
1.10 return result
1.11
1.12 @@ -194,21 +194,61 @@
1.13 # Logical operators.
1.14
1.15 def visitAnd(self, and_):
1.16 - result = And(and_)
1.17 +
1.18 + # Make a subprogram for the expression and record it outside the main tree.
1.19 +
1.20 + subprogram = Subprogram(and_, name=hex(id(and_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None)
1.21 + self.current_subprograms.append(subprogram)
1.22 +
1.23 nodes = []
1.24 + last = and_.nodes[-1]
1.25 for node in and_.nodes:
1.26 - nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
1.27 - params=[], star=None, dstar=None))
1.28 - result.nodes = nodes
1.29 + expr = self.dispatch(node)
1.30 + if node is not last:
1.31 + invocation = Not(expr=Invoke(expr=LoadAttr(expr=StoreTemp(expr=expr), name="__true__"),
1.32 + params=[], star=None, dstar=None))
1.33 + nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())]))
1.34 + nodes.append(ReleaseTemp())
1.35 + else:
1.36 + nodes.append(Return(expr=expr))
1.37 + subprogram.code = nodes
1.38 +
1.39 + self.current_subprograms.pop()
1.40 + self.subprograms.append(subprogram)
1.41 +
1.42 + # Make an invocation of the subprogram.
1.43 +
1.44 + result = Invoke(and_, same_frame=1, star=None, dstar=None, args=[])
1.45 + result.expr = LoadRef(ref=subprogram)
1.46 return result
1.47
1.48 def visitOr(self, or_):
1.49 - result = Or(or_)
1.50 +
1.51 + # Make a subprogram for the expression and record it outside the main tree.
1.52 +
1.53 + subprogram = Subprogram(or_, name=hex(id(or_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None)
1.54 + self.current_subprograms.append(subprogram)
1.55 +
1.56 nodes = []
1.57 + last = or_.nodes[-1]
1.58 for node in or_.nodes:
1.59 - nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
1.60 - params=[], star=None, dstar=None))
1.61 - result.nodes = nodes
1.62 + expr = self.dispatch(node)
1.63 + if node is not last:
1.64 + invocation = Invoke(expr=LoadAttr(expr=StoreTemp(expr=expr), name="__true__"),
1.65 + params=[], star=None, dstar=None)
1.66 + nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())]))
1.67 + nodes.append(ReleaseTemp())
1.68 + else:
1.69 + nodes.append(Return(expr=expr))
1.70 + subprogram.code = nodes
1.71 +
1.72 + self.current_subprograms.pop()
1.73 + self.subprograms.append(subprogram)
1.74 +
1.75 + # Make an invocation of the subprogram.
1.76 +
1.77 + result = Invoke(or_, same_frame=1, star=None, dstar=None, args=[])
1.78 + result.expr = LoadRef(ref=subprogram)
1.79 return result
1.80
1.81 def visitNot(self, not_):
1.82 @@ -379,7 +419,7 @@
1.83 # Make a subprogram for the function and record it outside the main
1.84 # tree.
1.85
1.86 - subprogram = Subprogram(function, name=function.name, star=None, dstar=None)
1.87 + subprogram = Subprogram(function, name=function.name, returns_value=1, star=None, dstar=None)
1.88 self.current_subprograms.append(subprogram)
1.89 subprogram.code = self.dispatch(function.code)
1.90 self.current_subprograms.pop()
1.91 @@ -398,7 +438,7 @@
1.92 # Make a subprogram for the function and record it outside the main
1.93 # tree.
1.94
1.95 - subprogram = Subprogram(lambda_, name=hex(id(lambda_)), star=None, dstar=None)
1.96 + subprogram = Subprogram(lambda_, name=hex(id(lambda_)), returns_value=1, star=None, dstar=None)
1.97 self.current_subprograms.append(subprogram)
1.98 subprogram.code = [Return(expr=self.dispatch(lambda_.code))]
1.99 self.current_subprograms.pop()
1.100 @@ -422,7 +462,7 @@
1.101
1.102 # Make a subprogram for the block and record it outside the main tree.
1.103
1.104 - subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, params=[], star=None, dstar=None)
1.105 + subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.106 self.current_subprograms.append(subprogram)
1.107
1.108 # Include a conditional statement in the subprogram.
1.109 @@ -446,7 +486,7 @@
1.110
1.111 # Make an invocation of the subprogram.
1.112
1.113 - result = Invoke(while_, same_frame=1, star=None, dstar=None, args=[])
1.114 + result = Invoke(while_, same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.115 result.expr = LoadRef(ref=subprogram)
1.116 return result
1.117
1.118 @@ -454,7 +494,7 @@
1.119
1.120 # Make a subprogram for the block and record it outside the main tree.
1.121
1.122 - subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, params=[], star=None, dstar=None)
1.123 + subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.124 self.current_subprograms.append(subprogram)
1.125
1.126 # Wrap the assignment in a try...except statement.
1.127 @@ -475,7 +515,7 @@
1.128 # Inside the conditional, add a recursive invocation to the subprogram
1.129 # if the test condition was satisfied.
1.130
1.131 - continuation = Invoke(same_frame=1, star=None, dstar=None, args=[])
1.132 + continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.133 continuation.expr = LoadRef(ref=subprogram)
1.134 try_except.body = [assign] + self.dispatch(for_.body) + [continuation]
1.135 subprogram.code = [try_except]
1.136 @@ -489,7 +529,7 @@
1.137 result = Assign(for_)
1.138 result.code = [
1.139 StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))),
1.140 - Invoke(expr=LoadRef(ref=subprogram), same_frame=1, star=None, dstar=None, args=[]),
1.141 + Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]),
1.142 ReleaseTemp()
1.143 ]
1.144 return result