1.1 --- a/simplify.py Sat Sep 30 18:54:06 2006 +0200
1.2 +++ b/simplify.py Tue Oct 03 00:50:03 2006 +0200
1.3 @@ -24,13 +24,17 @@
1.4
1.5 --------
1.6
1.7 -To use this module, first instantiate a Simplifier object:
1.8 +To use this module, the easiest approach is to use the simplify function:
1.9 +
1.10 +simplify(filename)
1.11 +
1.12 +The more complicated approach involves first instantiating a Simplifier object:
1.13
1.14 simplifier = Simplifier()
1.15
1.16 -Then, apply the simplifier to an AST tree:
1.17 +Then, applying the simplifier to an AST tree:
1.18
1.19 -module = compiler.parseFile(...)
1.20 +module = compiler.parseFile(filename)
1.21 simplifier.process(module)
1.22 """
1.23
1.24 @@ -64,7 +68,6 @@
1.25 """
1.26
1.27 Visitor.__init__(self)
1.28 - self.result = None # The resulting tree.
1.29 self.subprograms = [] # Subprograms outside the tree.
1.30 self.structures = [] # Structures/classes.
1.31 self.constants = {} # Constants.
1.32 @@ -76,7 +79,9 @@
1.33 self.visitor = self
1.34
1.35 def process(self, node):
1.36 - return self.dispatch(node)
1.37 + result = self.dispatch(node)
1.38 + result.simplifier = self
1.39 + return result
1.40
1.41 def dispatch_or_none(self, node, *args):
1.42 if node is not None:
1.43 @@ -104,7 +109,7 @@
1.44 resulting program nodes.
1.45 """
1.46
1.47 - self.result = Module(module)
1.48 + result = Module(module)
1.49 module_code = self.dispatch(module.node)
1.50
1.51 # NOTE: Constant initialisation necessary for annotation but perhaps
1.52 @@ -117,10 +122,10 @@
1.53 # NOTE: Hack to ensure correct initialisation of constants.
1.54
1.55 if self.builtins:
1.56 - self.result.code = module_code + init_code
1.57 + result.code = module_code + init_code
1.58 else:
1.59 - self.result.code = init_code + module_code
1.60 - return self.result
1.61 + result.code = init_code + module_code
1.62 + return result
1.63
1.64 def visitGetattr(self, getattr):
1.65 result = LoadAttr(getattr,
1.66 @@ -182,9 +187,7 @@
1.67 return result
1.68
1.69 def visitContinue(self, continue_):
1.70 - result = Invoke(continue_,
1.71 - same_frame=1, produces_result=0,
1.72 - star=None, dstar=None, args=[],
1.73 + result = InvokeBlock(continue_,
1.74 expr=LoadRef(ref=self.current_subprograms[-1])
1.75 )
1.76 return result
1.77 @@ -397,7 +400,7 @@
1.78
1.79 # Make an invocation of the subprogram.
1.80
1.81 - result = Invoke(compare, same_frame=1, star=None, dstar=None, args=[])
1.82 + result = InvokeBlock(compare, produces_result=1)
1.83 result.expr = LoadRef(ref=subprogram)
1.84 return result
1.85
1.86 @@ -457,7 +460,7 @@
1.87
1.88 # Make an invocation of the subprogram.
1.89
1.90 - result = Invoke(and_, same_frame=1, star=None, dstar=None, args=[])
1.91 + result = InvokeBlock(and_, produces_result=1)
1.92 result.expr = LoadRef(ref=subprogram)
1.93 return result
1.94
1.95 @@ -517,7 +520,7 @@
1.96
1.97 # Make an invocation of the subprogram.
1.98
1.99 - result = Invoke(or_, same_frame=1, star=None, dstar=None, args=[])
1.100 + result = InvokeBlock(or_, produces_result=1)
1.101 result.expr = LoadRef(ref=subprogram)
1.102 return result
1.103
1.104 @@ -908,7 +911,7 @@
1.105 return LoadRef(lambda_, ref=subprogram)
1.106
1.107 def visitCallFunc(self, callfunc):
1.108 - result = Invoke(callfunc, same_frame=0, star=None, dstar=None)
1.109 + result = InvokeFunction(callfunc, star=None, dstar=None)
1.110 result.args = self.dispatches(callfunc.args)
1.111 if callfunc.star_args is not None:
1.112 result.star = self.dispatch(callfunc.star_args)
1.113 @@ -944,7 +947,7 @@
1.114 # Inside the conditional, add a recursive invocation to the subprogram
1.115 # if the test condition was satisfied.
1.116
1.117 - continuation = Invoke(same_frame=1, star=None, dstar=None, args=[])
1.118 + continuation = InvokeBlock()
1.119 continuation.expr = LoadRef(ref=subprogram)
1.120
1.121 # Return within the main section of the loop.
1.122 @@ -965,7 +968,7 @@
1.123
1.124 # Make an invocation of the subprogram.
1.125
1.126 - result = Invoke(while_, same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.127 + result = InvokeBlock(while_)
1.128 result.expr = LoadRef(ref=subprogram)
1.129 return result
1.130
1.131 @@ -1018,7 +1021,7 @@
1.132 # Inside the conditional, add a recursive invocation to the subprogram
1.133 # if the test condition was satisfied.
1.134
1.135 - continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.136 + continuation = InvokeBlock()
1.137 continuation.expr = LoadRef(ref=subprogram)
1.138 try_except.body = [assign] + self.dispatch(for_.body) + [continuation]
1.139 subprogram.code = [try_except, Return()]
1.140 @@ -1034,7 +1037,7 @@
1.141 result = Assign(for_)
1.142 result.code = [
1.143 StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)), args=[], star=None, dstar=None)),
1.144 - Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]),
1.145 + InvokeBlock(expr=LoadRef(ref=subprogram)),
1.146 ReleaseTemp()
1.147 ]
1.148 return result
1.149 @@ -1049,4 +1052,20 @@
1.150 result.finally_ = self.dispatch(tryfinally.final)
1.151 return result
1.152
1.153 +# Convenience functions.
1.154 +
1.155 +def simplify(filename, builtins=0):
1.156 +
1.157 + """
1.158 + Simplify the module stored in the file with the given 'filename'. If the
1.159 + optional 'builtins' parameter is set to a true value (the default being a
1.160 + false value), then the module is considered as the builtins module.
1.161 + """
1.162 +
1.163 + simplifier = Simplifier(builtins)
1.164 + module = compiler.parseFile(filename)
1.165 + simplified = simplifier.process(module)
1.166 + compiler.misc.set_filename(filename, module)
1.167 + return simplified
1.168 +
1.169 # vim: tabstop=4 expandtab shiftwidth=4