simplify

Changeset

7:bdffcb65c94e
2006-07-13 paulb raw files shortlog changelog graph Added produces_result to some Invoke nodes, returns_value to some Subprogram nodes. Introduced full sequences of nodes in subprograms for And and Or operations.
simplified.py (file) simplify.py (file)
     1.1 --- a/simplified.py	Tue Jul 11 00:53:56 2006 +0200
     1.2 +++ b/simplified.py	Thu Jul 13 23:15:20 2006 +0200
     1.3 @@ -113,8 +113,6 @@
     1.4  class Try(Node): "A try...except...else...finally grouping node."
     1.5  class Except(Node): "An exception handler node."
     1.6  class Raise(Node): "An exception raising node."
     1.7 -class And(Node): "A conjunction of expressions."
     1.8 -class Or(Node): "A disjunction of expressions."
     1.9  class Not(Node): "A negation of an expression."
    1.10  
    1.11  # vim: tabstop=4 expandtab shiftwidth=4
     2.1 --- a/simplify.py	Tue Jul 11 00:53:56 2006 +0200
     2.2 +++ b/simplify.py	Thu Jul 13 23:15:20 2006 +0200
     2.3 @@ -142,7 +142,7 @@
     2.4          return result
     2.5  
     2.6      def visitContinue(self, continue_):
     2.7 -        result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[])
     2.8 +        result = Invoke(continue_, same_frame=1, produces_result=0, star=None, dstar=None, args=[])
     2.9          result.expr = LoadRef(ref=self.current_subprograms[-1])
    2.10          return result
    2.11  
    2.12 @@ -194,21 +194,61 @@
    2.13      # Logical operators.
    2.14  
    2.15      def visitAnd(self, and_):
    2.16 -        result = And(and_)
    2.17 +
    2.18 +        # Make a subprogram for the expression and record it outside the main tree.
    2.19 +
    2.20 +        subprogram = Subprogram(and_, name=hex(id(and_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None)
    2.21 +        self.current_subprograms.append(subprogram)
    2.22 +
    2.23          nodes = []
    2.24 +        last = and_.nodes[-1]
    2.25          for node in and_.nodes:
    2.26 -            nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
    2.27 -                params=[], star=None, dstar=None))
    2.28 -        result.nodes = nodes
    2.29 +            expr = self.dispatch(node)
    2.30 +            if node is not last:
    2.31 +                invocation = Not(expr=Invoke(expr=LoadAttr(expr=StoreTemp(expr=expr), name="__true__"),
    2.32 +                    params=[], star=None, dstar=None))
    2.33 +                nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())]))
    2.34 +                nodes.append(ReleaseTemp())
    2.35 +            else:
    2.36 +                nodes.append(Return(expr=expr))
    2.37 +        subprogram.code = nodes
    2.38 +
    2.39 +        self.current_subprograms.pop()
    2.40 +        self.subprograms.append(subprogram)
    2.41 +
    2.42 +        # Make an invocation of the subprogram.
    2.43 +
    2.44 +        result = Invoke(and_, same_frame=1, star=None, dstar=None, args=[])
    2.45 +        result.expr = LoadRef(ref=subprogram)
    2.46          return result
    2.47  
    2.48      def visitOr(self, or_):
    2.49 -        result = Or(or_)
    2.50 +
    2.51 +        # Make a subprogram for the expression and record it outside the main tree.
    2.52 +
    2.53 +        subprogram = Subprogram(or_, name=hex(id(or_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None)
    2.54 +        self.current_subprograms.append(subprogram)
    2.55 +
    2.56          nodes = []
    2.57 +        last = or_.nodes[-1]
    2.58          for node in or_.nodes:
    2.59 -            nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
    2.60 -                params=[], star=None, dstar=None))
    2.61 -        result.nodes = nodes
    2.62 +            expr = self.dispatch(node)
    2.63 +            if node is not last:
    2.64 +                invocation = Invoke(expr=LoadAttr(expr=StoreTemp(expr=expr), name="__true__"),
    2.65 +                    params=[], star=None, dstar=None)
    2.66 +                nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())]))
    2.67 +                nodes.append(ReleaseTemp())
    2.68 +            else:
    2.69 +                nodes.append(Return(expr=expr))
    2.70 +        subprogram.code = nodes
    2.71 +
    2.72 +        self.current_subprograms.pop()
    2.73 +        self.subprograms.append(subprogram)
    2.74 +
    2.75 +        # Make an invocation of the subprogram.
    2.76 +
    2.77 +        result = Invoke(or_, same_frame=1, star=None, dstar=None, args=[])
    2.78 +        result.expr = LoadRef(ref=subprogram)
    2.79          return result
    2.80  
    2.81      def visitNot(self, not_):
    2.82 @@ -379,7 +419,7 @@
    2.83          # Make a subprogram for the function and record it outside the main
    2.84          # tree.
    2.85  
    2.86 -        subprogram = Subprogram(function, name=function.name, star=None, dstar=None)
    2.87 +        subprogram = Subprogram(function, name=function.name, returns_value=1, star=None, dstar=None)
    2.88          self.current_subprograms.append(subprogram)
    2.89          subprogram.code = self.dispatch(function.code)
    2.90          self.current_subprograms.pop()
    2.91 @@ -398,7 +438,7 @@
    2.92          # Make a subprogram for the function and record it outside the main
    2.93          # tree.
    2.94  
    2.95 -        subprogram = Subprogram(lambda_, name=hex(id(lambda_)), star=None, dstar=None)
    2.96 +        subprogram = Subprogram(lambda_, name=hex(id(lambda_)), returns_value=1, star=None, dstar=None)
    2.97          self.current_subprograms.append(subprogram)
    2.98          subprogram.code = [Return(expr=self.dispatch(lambda_.code))]
    2.99          self.current_subprograms.pop()
   2.100 @@ -422,7 +462,7 @@
   2.101  
   2.102          # Make a subprogram for the block and record it outside the main tree.
   2.103  
   2.104 -        subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, params=[], star=None, dstar=None)
   2.105 +        subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
   2.106          self.current_subprograms.append(subprogram)
   2.107  
   2.108          # Include a conditional statement in the subprogram.
   2.109 @@ -446,7 +486,7 @@
   2.110  
   2.111          # Make an invocation of the subprogram.
   2.112  
   2.113 -        result = Invoke(while_, same_frame=1, star=None, dstar=None, args=[])
   2.114 +        result = Invoke(while_, same_frame=1, produces_result=0, star=None, dstar=None, args=[])
   2.115          result.expr = LoadRef(ref=subprogram)
   2.116          return result
   2.117  
   2.118 @@ -454,7 +494,7 @@
   2.119  
   2.120          # Make a subprogram for the block and record it outside the main tree.
   2.121  
   2.122 -        subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, params=[], star=None, dstar=None)
   2.123 +        subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
   2.124          self.current_subprograms.append(subprogram)
   2.125  
   2.126          # Wrap the assignment in a try...except statement.
   2.127 @@ -475,7 +515,7 @@
   2.128          # Inside the conditional, add a recursive invocation to the subprogram
   2.129          # if the test condition was satisfied.
   2.130  
   2.131 -        continuation = Invoke(same_frame=1, star=None, dstar=None, args=[])
   2.132 +        continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[])
   2.133          continuation.expr = LoadRef(ref=subprogram)
   2.134          try_except.body = [assign] + self.dispatch(for_.body) + [continuation]
   2.135          subprogram.code = [try_except]
   2.136 @@ -489,7 +529,7 @@
   2.137          result = Assign(for_)
   2.138          result.code = [
   2.139              StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))),
   2.140 -            Invoke(expr=LoadRef(ref=subprogram), same_frame=1, star=None, dstar=None, args=[]),
   2.141 +            Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]),
   2.142              ReleaseTemp()
   2.143              ]
   2.144          return result