1.1 --- a/simplified.py Sat Oct 07 01:49:01 2006 +0200
1.2 +++ b/simplified.py Sat Oct 07 01:49:50 2006 +0200
1.3 @@ -24,6 +24,29 @@
1.4 from compiler.visitor import ASTVisitor
1.5 import sys
1.6
1.7 +# Unique name registration.
1.8 +
1.9 +class Naming:
1.10 + def __init__(self):
1.11 + self.obj_to_name = {}
1.12 + self.names = {}
1.13 + def get(self, obj):
1.14 + return self.obj_to_name[obj]
1.15 + def set(self, obj, name):
1.16 + if self.obj_to_name.has_key(obj):
1.17 + return
1.18 + if not self.names.has_key(name):
1.19 + self.names[name] = 0
1.20 + n = self.names[name] + 1
1.21 + self.names[name] = n
1.22 + self.obj_to_name[obj] = "%s#%d" % (name, n)
1.23 +
1.24 +naming = Naming()
1.25 +
1.26 +def name(obj, name):
1.27 + naming.set(obj, name)
1.28 + return naming.get(obj)
1.29 +
1.30 # Elementary visitor support.
1.31
1.32 class Visitor(ASTVisitor):
1.33 @@ -94,14 +117,16 @@
1.34 setattr(self, name, value)
1.35
1.36 def __repr__(self):
1.37 - if hasattr(self, "name"):
1.38 - return "%s '%s' (at %x)" % (self.__class__.__name__, self.name, id(self))
1.39 - if hasattr(self, "index"):
1.40 + if hasattr(self, "full_name"):
1.41 + return "%s '%s'" % (self.__class__.__name__, self.full_name)
1.42 + elif hasattr(self, "name"):
1.43 + return "%s '%s'" % (self.__class__.__name__, self.name)
1.44 + elif hasattr(self, "index"):
1.45 return "%s (%s)" % (self.__class__.__name__, self.index)
1.46 elif hasattr(self, "value"):
1.47 return "%s %s" % (self.__class__.__name__, repr(self.value))
1.48 elif hasattr(self, "ref"):
1.49 - return "%s '%x' (at %x)" % (self.__class__.__name__, id(self.ref), id(self))
1.50 + return "%s '%s'" % (self.__class__.__name__, name(self.ref, self.ref.name))
1.51 else:
1.52 return "%s" % (self.__class__.__name__,)
1.53
1.54 @@ -175,8 +200,17 @@
1.55 self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream)
1.56 self._pprint(indent, "", "--------", stream=stream)
1.57
1.58 -class Module(Node): "A Python module."
1.59 -class Subprogram(Node): "A subprogram: functions, methods and loops."
1.60 +class NamedNode(Node):
1.61 +
1.62 + "A named node."
1.63 +
1.64 + def __init__(self, *args, **kw):
1.65 + Node.__init__(self, *args, **kw)
1.66 + self.full_name = name(self, self.name or "$untitled")
1.67 +
1.68 +class Module(NamedNode): "A Python module."
1.69 +class Subprogram(NamedNode): "A subprogram: functions, methods and loops."
1.70 +
1.71 class Pass(Node): "A placeholder node corresponding to pass."
1.72
1.73 class Invoke(Node): "An invocation."
1.74 @@ -216,9 +250,9 @@
1.75
1.76 def __repr__(self):
1.77 if hasattr(self, "name"):
1.78 - return "%s '%s' (at %x)" % (self.__class__.__name__, self.name, id(self))
1.79 + return "%s '%s'" % (self.__class__.__name__, name(self, self.name))
1.80 else:
1.81 - return "%s (at %x)" % (self.__class__.__name__, id(self))
1.82 + return "%s" % (self.__class__.__name__,)
1.83
1.84 class Class(Structure):
1.85
2.1 --- a/simplify.py Sat Oct 07 01:49:01 2006 +0200
2.2 +++ b/simplify.py Sat Oct 07 01:49:50 2006 +0200
2.3 @@ -74,6 +74,10 @@
2.4 self.current_subprograms = [] # Current subprograms being processed.
2.5 self.builtins = builtins # Whether the builtins are being processed.
2.6
2.7 + # Convenience attributes.
2.8 +
2.9 + self.subnames = {}
2.10 +
2.11 # For compiler package mechanisms.
2.12
2.13 self.visitor = self
2.14 @@ -109,7 +113,7 @@
2.15 resulting program nodes.
2.16 """
2.17
2.18 - result = Module(module)
2.19 + result = Module(module, name="module")
2.20 module_code = self.dispatch(module.node)
2.21
2.22 # NOTE: Constant initialisation necessary for annotation but perhaps
2.23 @@ -431,7 +435,7 @@
2.24 subprogram.code = results
2.25
2.26 self.current_subprograms.pop()
2.27 - self.subprograms.append(subprogram)
2.28 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.29
2.30 # Make an invocation of the subprogram.
2.31
2.32 @@ -491,7 +495,7 @@
2.33 subprogram.code = results
2.34
2.35 self.current_subprograms.pop()
2.36 - self.subprograms.append(subprogram)
2.37 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.38
2.39 # Make an invocation of the subprogram.
2.40
2.41 @@ -553,7 +557,7 @@
2.42 subprogram.code = results
2.43
2.44 self.current_subprograms.pop()
2.45 - self.subprograms.append(subprogram)
2.46 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.47
2.48 # Make an invocation of the subprogram.
2.49
2.50 @@ -883,7 +887,7 @@
2.51 subprogram.code = self.dispatch(class_.code) + [Return(class_)]
2.52
2.53 self.current_subprograms.pop()
2.54 - self.subprograms.append(subprogram)
2.55 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.56
2.57 # Make a definition of the class associating it with a name.
2.58
2.59 @@ -951,7 +955,7 @@
2.60 subprogram.params = params
2.61 subprogram.star = star
2.62 subprogram.dstar = dstar
2.63 - self.subprograms.append(subprogram)
2.64 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.65
2.66 def visitFunction(self, function):
2.67
2.68 @@ -1038,7 +1042,7 @@
2.69 subprogram.code = [test]
2.70
2.71 self.current_subprograms.pop()
2.72 - self.subprograms.append(subprogram)
2.73 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.74
2.75 # Make an invocation of the subprogram.
2.76
2.77 @@ -1102,8 +1106,8 @@
2.78
2.79 # Finish the subprogram definition.
2.80
2.81 - self.subprograms.append(subprogram)
2.82 self.current_subprograms.pop()
2.83 + self.subprograms.append(subprogram); self.subnames[subprogram.full_name] = subprogram
2.84
2.85 # Obtain an iterator for the sequence involved.
2.86 # Then, make an invocation of the subprogram.