1.1 --- a/simplify.py Sat Aug 12 01:46:35 2006 +0200
1.2 +++ b/simplify.py Sat Aug 12 18:47:05 2006 +0200
1.3 @@ -21,6 +21,17 @@
1.4 License along with this library; see the file LICENCE.txt
1.5 If not, write to the Free Software Foundation, Inc.,
1.6 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.7 +
1.8 +--------
1.9 +
1.10 +To use this module, first instantiate a Simplifier object:
1.11 +
1.12 +simplifier = Simplifier()
1.13 +
1.14 +Then, apply the simplifier to an AST tree:
1.15 +
1.16 +module = compiler.parseFile(...)
1.17 +simplifier.process(module)
1.18 """
1.19
1.20 from simplified import *
1.21 @@ -45,6 +56,13 @@
1.22 """
1.23
1.24 def __init__(self, builtins=0):
1.25 +
1.26 + """
1.27 + Initialise the simplifier with the optional 'builtins' parameter
1.28 + indicating whether the module contains the built-in classes and
1.29 + functions.
1.30 + """
1.31 +
1.32 Visitor.__init__(self)
1.33 self.result = None # The resulting tree.
1.34 self.subprograms = [] # Subprograms outside the tree.
1.35 @@ -77,6 +95,12 @@
1.36 # Relatively trivial transformations.
1.37
1.38 def visitModule(self, module):
1.39 +
1.40 + """
1.41 + Process the given 'module', producing a Module object which contains the
1.42 + resulting program nodes.
1.43 + """
1.44 +
1.45 self.result = Module(module)
1.46 module_code = self.dispatch(module.node)
1.47
1.48 @@ -709,10 +733,25 @@
1.49 return result
1.50
1.51 def _visitFunction(self, function, subprogram):
1.52 - if function.flags & 4 != 0: has_star = 1
1.53 - else: has_star = 0
1.54 - if function.flags & 8 != 0: has_dstar = 1
1.55 - else: has_dstar = 0
1.56 +
1.57 + """
1.58 + A common function generator which transforms the given 'function' node
1.59 + and initialises the given 'subprogram' appropriately.
1.60 + """
1.61 +
1.62 + # Discover star and dstar parameters.
1.63 +
1.64 + if function.flags & 4 != 0:
1.65 + has_star = 1
1.66 + else:
1.67 + has_star = 0
1.68 + if function.flags & 8 != 0:
1.69 + has_dstar = 1
1.70 + else:
1.71 + has_dstar = 0
1.72 +
1.73 + # Discover the number of defaults and positional parameters.
1.74 +
1.75 ndefaults = len(function.defaults)
1.76 npositional = len(function.argnames) - has_star - has_dstar
1.77