# HG changeset patch # User paulb@localhost.localdomain # Date 1171755477 -3600 # Node ID 317dbf48abd6d57be973ce5c9f1472e2e2bd8bb5 # Parent c471747c9c1eedf681ac54d5f07a46178ce1f699 Rearranged the classes in the simplified module. Added support for selective multiple instances using an __atomic__ attribute on various classes. diff -r c471747c9c1e -r 317dbf48abd6 annotate.py --- a/annotate.py Fri Feb 16 00:50:41 2007 +0100 +++ b/annotate.py Sun Feb 18 00:37:57 2007 +0100 @@ -1364,6 +1364,9 @@ __getitem__ = load + def has_key(self, name): + return self.names.has_key(name) + def revoke(self, name, type): "Revoke from the entry for the given 'name' the specified 'type'." diff -r c471747c9c1e -r 317dbf48abd6 lib/builtins.py --- a/lib/builtins.py Fri Feb 16 00:50:41 2007 +0100 +++ b/lib/builtins.py Sun Feb 18 00:37:57 2007 +0100 @@ -31,6 +31,8 @@ return self.__add__(other) class bool: + __atomic__ = 1 + def __bool__(self): return self @@ -70,6 +72,8 @@ pass class float: + __atomic__ = 1 + def __iadd__(self, other): if isinstance(other, int): return float() @@ -283,6 +287,8 @@ return self != 0 class int: + __atomic__ = 1 + def __iadd__(self, other): if isinstance(other, int): return int() @@ -477,6 +483,8 @@ return bool() class long: + __atomic__ = 1 + def __iadd__(self, other): if isinstance(other, int): return long() @@ -586,6 +594,8 @@ return self != 0 class none: + __atomic__ = 1 + def __bool__(self): return False @@ -593,6 +603,8 @@ return "None" class str: + __atomic__ = 1 + def __init__(self, x=None): x.__str__() @@ -725,19 +737,19 @@ pass class AttributeError(Exception): - pass + __atomic__ = 1 class IndexError(Exception): - pass + __atomic__ = 1 class StopIteration(Exception): - pass + __atomic__ = 1 class TypeError(Exception): - pass + __atomic__ = 1 class NotImplementedType: - pass + __atomic__ = 1 # General functions. diff -r c471747c9c1e -r 317dbf48abd6 simplified.py --- a/simplified.py Fri Feb 16 00:50:41 2007 +0100 +++ b/simplified.py Sun Feb 18 00:37:57 2007 +0100 @@ -87,6 +87,18 @@ naming.set(obj, name) return naming.get(obj) +# Named nodes are those which can be referenced in some way. + +class WithName: + + "Node naming." + + def __init__(self): + self._full_name = name(self, self.name or "$untitled") + + def full_name(self): + return self._full_name + # Elementary visitor support. class Visitor(ASTVisitor): @@ -359,6 +371,23 @@ return node +# Comparable nodes based on naming. + +class Comparable(Node): + + "Comparable nodes implementing the 'full_name' method." + + def __eq__(self, other): + # NOTE: Single instance: all instances are the same + # NOTE: Multiple instances: all instances are different + if hasattr(other, "full_name"): + return self.full_name() == other.full_name() + else: + return NotImplemented + + def __hash__(self): + return id(self) + # These are the supported "operations" described by simplified program nodes. class Pass(Node): "A placeholder node corresponding to pass." @@ -454,18 +483,6 @@ self.share_locals = 1 Invoke.__init__(self, *args, **kw) -# Named nodes are those which can be referenced in some way. - -class WithName: - - "Node naming." - - def __init__(self): - self._full_name = name(self, self.name or "$untitled") - - def full_name(self): - return self._full_name - # Program structure nodes. class Subprogram(Node, WithName): @@ -476,21 +493,6 @@ Node.__init__(self, *args, **kw) WithName.__init__(self) -class Comparable(Node): - - "Comparable nodes implementing the 'full_name' method." - - def __eq__(self, other): - # NOTE: Single instance: all instances are the same - # NOTE: Multiple instances: all instances are different - if hasattr(other, "full_name"): - return self.full_name() == other.full_name() - else: - return NotImplemented - - def __hash__(self): - return id(self) - class Module(Comparable): "A Python module." @@ -574,6 +576,16 @@ else: return attribute +class SelectiveMultipleInstanceClass(MultipleInstanceClass): + + "A Python class which provides multiple instances depending on the class." + + def _get_key(self, node): + if self.namespace.has_key("__atomic__"): + return id(self) + else: + return MultipleInstanceClass._get_key(self, node) + class Instance(Structure): "An instance." @@ -629,6 +641,8 @@ def __hash__(self): return id(self) +# Additional program and AST nodes. + class Self: """ @@ -661,4 +675,8 @@ global Class Class = MultipleInstanceClass +def set_selective_multiple_instance_mode(): + global Class + Class = SelectiveMultipleInstanceClass + # vim: tabstop=4 expandtab shiftwidth=4 diff -r c471747c9c1e -r 317dbf48abd6 test.py --- a/test.py Fri Feb 16 00:50:41 2007 +0100 +++ b/test.py Sun Feb 18 00:37:57 2007 +0100 @@ -10,6 +10,8 @@ simplified.set_single_instance_mode() elif "-m" in sys.argv: simplified.set_multiple_instance_mode() + elif "-ms" in sys.argv: + simplified.set_selective_multiple_instance_mode() import viewer from annotate import AnnotationError, Importer, load