1.1 --- a/simplify.py Mon Jul 17 00:10:35 2006 +0200
1.2 +++ b/simplify.py Tue Jul 18 00:34:32 2006 +0200
1.3 @@ -21,11 +21,10 @@
1.4 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.5 """
1.6
1.7 -from compiler.visitor import ASTVisitor
1.8 +from simplified import *
1.9 import compiler.ast
1.10 -from simplified import *
1.11
1.12 -class Simplifier(ASTVisitor):
1.13 +class Simplifier(Visitor):
1.14
1.15 """
1.16 A simplifying visitor for AST nodes.
1.17 @@ -44,25 +43,12 @@
1.18 """
1.19
1.20 def __init__(self):
1.21 - ASTVisitor.__init__(self)
1.22 + Visitor.__init__(self)
1.23 self.result = None # The resulting tree.
1.24 self.subprograms = [] # Subprograms outside the tree.
1.25 + self.structures = [] # Structures/classes
1.26 self.current_subprograms = [] # Current subprograms being processed.
1.27
1.28 - # Generic visitor methods.
1.29 -
1.30 - def default(self, node, *args):
1.31 - raise ValueError, node.__class__
1.32 -
1.33 - def dispatch(self, node, *args):
1.34 - return ASTVisitor.dispatch(self, node, *args)
1.35 -
1.36 - def dispatches(self, nodes, *args):
1.37 - results = []
1.38 - for node in nodes:
1.39 - results.append(self.dispatch(node, *args))
1.40 - return results
1.41 -
1.42 def dispatch_or_none(self, node, *args):
1.43 if node is not None:
1.44 return self.dispatch(node, *args)
1.45 @@ -87,26 +73,6 @@
1.46 self.result.code = self.dispatch(module.node)
1.47 return self.result
1.48
1.49 - def visitClass(self, class_):
1.50 - structure = Class(name=hex(id(class_)), bases=class_.bases)
1.51 -
1.52 - subprogram = Subprogram(name=hex(id(class_)), acquire_locals=1, structure=structure, params=[], star=None, dstar=None)
1.53 - self.current_subprograms.append(subprogram)
1.54 -
1.55 - subprogram.code = self.dispatch(class_.code)
1.56 -
1.57 - self.current_subprograms.pop()
1.58 - self.subprograms.append(subprogram)
1.59 -
1.60 - # Make a definition of the class associating it with a name.
1.61 -
1.62 - result = Assign(class_)
1.63 - init = Invoke(expr=LoadRef(ref=subprogram), args=[], star=None, dstar=None)
1.64 - load = LoadRef(ref=structure)
1.65 - store = StoreName(name=class_.name)
1.66 - result.code = [init, load, store]
1.67 - return result
1.68 -
1.69 def visitGetattr(self, getattr):
1.70 result = LoadAttr(getattr, name=getattr.attrname)
1.71 result.expr = self.dispatch(getattr.expr)
1.72 @@ -588,6 +554,26 @@
1.73
1.74 # Invocation and subprogram transformations.
1.75
1.76 + def visitClass(self, class_):
1.77 + structure = Class(name=hex(id(class_)), bases=class_.bases)
1.78 + self.structures.append(structure)
1.79 +
1.80 + subprogram = Subprogram(name=hex(id(class_)), acquire_locals=1, structure=structure, params=[], star=None, dstar=None)
1.81 + self.current_subprograms.append(subprogram)
1.82 +
1.83 + subprogram.code = self.dispatch(class_.code)
1.84 +
1.85 + self.current_subprograms.pop()
1.86 + self.subprograms.append(subprogram)
1.87 +
1.88 + # Make a definition of the class associating it with a name.
1.89 +
1.90 + result = Assign(class_)
1.91 + init = Invoke(expr=LoadRef(ref=subprogram), args=[], star=None, dstar=None)
1.92 + store = StoreName(name=class_.name, expr=LoadRef(ref=structure))
1.93 + result.code = [init, store]
1.94 + return result
1.95 +
1.96 def _visitFunction(self, function, subprogram):
1.97 if function.flags & 4 != 0: has_star = 1
1.98 else: has_star = 0
1.99 @@ -631,10 +617,7 @@
1.100
1.101 # Make a definition of the function associating it with a name.
1.102
1.103 - result = Assign(function)
1.104 - load = LoadRef(ref=subprogram)
1.105 - store = StoreName(name=function.name)
1.106 - result.code = [load, store]
1.107 + result = StoreName(name=function.name, expr=LoadRef(ref=subprogram))
1.108 return result
1.109
1.110 def visitLambda(self, lambda_):