1.1 --- a/simplify.py Sun Jul 23 23:56:11 2006 +0200
1.2 +++ b/simplify.py Sun Jul 23 23:57:13 2006 +0200
1.3 @@ -1,7 +1,9 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -Simplify AST structures for easier type propagation and analysis.
1.8 +Simplify AST structures for easier type propagation and analysis. The code in
1.9 +this module processes AST trees originating from the compiler module and
1.10 +produces a result tree consisting of instruction-oriented program nodes.
1.11
1.12 Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk>
1.13
1.14 @@ -46,7 +48,8 @@
1.15 Visitor.__init__(self)
1.16 self.result = None # The resulting tree.
1.17 self.subprograms = [] # Subprograms outside the tree.
1.18 - self.structures = [] # Structures/classes
1.19 + self.structures = [] # Structures/classes.
1.20 + self.constants = {} # Constants.
1.21 self.current_subprograms = [] # Current subprograms being processed.
1.22
1.23 def process(self, node):
1.24 @@ -115,7 +118,9 @@
1.25 return result
1.26
1.27 def visitConst(self, const):
1.28 - result = LoadConst(const, value=const.value)
1.29 + if not self.constants.has_key(const.value):
1.30 + self.constants[const.value] = Constant(name=repr(const.value), value=const.value)
1.31 + result = LoadRef(ref=self.constants[const.value])
1.32 return result
1.33
1.34 def visitReturn(self, return_):
1.35 @@ -146,16 +151,16 @@
1.36 return result
1.37
1.38 def visitTuple(self, tuple):
1.39 - return self._visitBuiltin(tuple, "Tuple")
1.40 + return self._visitBuiltin(tuple, "tuple")
1.41
1.42 def visitList(self, list):
1.43 - return self._visitBuiltin(list, "List")
1.44 + return self._visitBuiltin(list, "list")
1.45
1.46 def visitDict(self, dict):
1.47 - result = Invoke(dict, expr=LoadName(name="Dict"))
1.48 + result = Invoke(dict, expr=LoadName(name="dict"))
1.49 args = []
1.50 for key, value in dict.items:
1.51 - tuple = Invoke(expr=LoadName(name="Tuple"), star=None, dstar=None)
1.52 + tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None)
1.53 tuple.args = [self.dispatch(key), self.dispatch(value)]
1.54 args.append(tuple)
1.55 result.args = args
1.56 @@ -549,7 +554,7 @@
1.57 if len(subs) == 1:
1.58 return self.dispatch(subs[0])
1.59 else:
1.60 - return Invoke(expr=LoadName(name="Tuple"), args=self.dispatches(subs))
1.61 + return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs))
1.62
1.63 def visitSubscript(self, subscript, in_sequence=0):
1.64 value = self._visitAssNameOrAttr(subscript, in_sequence)
1.65 @@ -562,7 +567,9 @@
1.66 structure = Class(name=hex(id(class_)), bases=class_.bases)
1.67 self.structures.append(structure)
1.68
1.69 - subprogram = Subprogram(name=hex(id(class_)), acquire_locals=1, structure=structure, params=[], star=None, dstar=None)
1.70 + # Make a subprogram which initialises the class structure.
1.71 +
1.72 + subprogram = Subprogram(name=hex(id(class_)), structure=structure, params=[], star=None, dstar=None)
1.73 self.current_subprograms.append(subprogram)
1.74
1.75 subprogram.code = self.dispatch(class_.code)
1.76 @@ -585,10 +592,17 @@
1.77 else: has_dstar = 0
1.78 ndefaults = len(function.defaults)
1.79 npositional = len(function.argnames) - has_star - has_dstar
1.80 - if has_star: star = function.argnames[npositional]
1.81 - else: star = None
1.82 - if has_dstar: dstar = function.argnames[npositional + has_star]
1.83 - else: dstar = None
1.84 +
1.85 + # Produce star and dstar parameters with appropriate defaults.
1.86 +
1.87 + if has_star:
1.88 + star = (function.argnames[npositional], Invoke(expr=LoadName(name="list"), args=[], star=None, dstar=None))
1.89 + else:
1.90 + star = None
1.91 + if has_dstar:
1.92 + dstar = (function.argnames[npositional + has_star], Invoke(expr=LoadName(name="dict"), args=[], star=None, dstar=None))
1.93 + else:
1.94 + dstar = None
1.95
1.96 params = []
1.97 for i in range(0, npositional - ndefaults):