1.1 --- a/simplified.py Fri Feb 16 00:50:41 2007 +0100
1.2 +++ b/simplified.py Sun Feb 18 00:37:57 2007 +0100
1.3 @@ -87,6 +87,18 @@
1.4 naming.set(obj, name)
1.5 return naming.get(obj)
1.6
1.7 +# Named nodes are those which can be referenced in some way.
1.8 +
1.9 +class WithName:
1.10 +
1.11 + "Node naming."
1.12 +
1.13 + def __init__(self):
1.14 + self._full_name = name(self, self.name or "$untitled")
1.15 +
1.16 + def full_name(self):
1.17 + return self._full_name
1.18 +
1.19 # Elementary visitor support.
1.20
1.21 class Visitor(ASTVisitor):
1.22 @@ -359,6 +371,23 @@
1.23
1.24 return node
1.25
1.26 +# Comparable nodes based on naming.
1.27 +
1.28 +class Comparable(Node):
1.29 +
1.30 + "Comparable nodes implementing the 'full_name' method."
1.31 +
1.32 + def __eq__(self, other):
1.33 + # NOTE: Single instance: all instances are the same
1.34 + # NOTE: Multiple instances: all instances are different
1.35 + if hasattr(other, "full_name"):
1.36 + return self.full_name() == other.full_name()
1.37 + else:
1.38 + return NotImplemented
1.39 +
1.40 + def __hash__(self):
1.41 + return id(self)
1.42 +
1.43 # These are the supported "operations" described by simplified program nodes.
1.44
1.45 class Pass(Node): "A placeholder node corresponding to pass."
1.46 @@ -454,18 +483,6 @@
1.47 self.share_locals = 1
1.48 Invoke.__init__(self, *args, **kw)
1.49
1.50 -# Named nodes are those which can be referenced in some way.
1.51 -
1.52 -class WithName:
1.53 -
1.54 - "Node naming."
1.55 -
1.56 - def __init__(self):
1.57 - self._full_name = name(self, self.name or "$untitled")
1.58 -
1.59 - def full_name(self):
1.60 - return self._full_name
1.61 -
1.62 # Program structure nodes.
1.63
1.64 class Subprogram(Node, WithName):
1.65 @@ -476,21 +493,6 @@
1.66 Node.__init__(self, *args, **kw)
1.67 WithName.__init__(self)
1.68
1.69 -class Comparable(Node):
1.70 -
1.71 - "Comparable nodes implementing the 'full_name' method."
1.72 -
1.73 - def __eq__(self, other):
1.74 - # NOTE: Single instance: all instances are the same
1.75 - # NOTE: Multiple instances: all instances are different
1.76 - if hasattr(other, "full_name"):
1.77 - return self.full_name() == other.full_name()
1.78 - else:
1.79 - return NotImplemented
1.80 -
1.81 - def __hash__(self):
1.82 - return id(self)
1.83 -
1.84 class Module(Comparable):
1.85
1.86 "A Python module."
1.87 @@ -574,6 +576,16 @@
1.88 else:
1.89 return attribute
1.90
1.91 +class SelectiveMultipleInstanceClass(MultipleInstanceClass):
1.92 +
1.93 + "A Python class which provides multiple instances depending on the class."
1.94 +
1.95 + def _get_key(self, node):
1.96 + if self.namespace.has_key("__atomic__"):
1.97 + return id(self)
1.98 + else:
1.99 + return MultipleInstanceClass._get_key(self, node)
1.100 +
1.101 class Instance(Structure):
1.102
1.103 "An instance."
1.104 @@ -629,6 +641,8 @@
1.105 def __hash__(self):
1.106 return id(self)
1.107
1.108 +# Additional program and AST nodes.
1.109 +
1.110 class Self:
1.111
1.112 """
1.113 @@ -661,4 +675,8 @@
1.114 global Class
1.115 Class = MultipleInstanceClass
1.116
1.117 +def set_selective_multiple_instance_mode():
1.118 + global Class
1.119 + Class = SelectiveMultipleInstanceClass
1.120 +
1.121 # vim: tabstop=4 expandtab shiftwidth=4