1.1 --- a/simplified.py Sat Oct 07 01:50:03 2006 +0200
1.2 +++ b/simplified.py Sat Oct 07 19:46:21 2006 +0200
1.3 @@ -27,11 +27,16 @@
1.4 # Unique name registration.
1.5
1.6 class Naming:
1.7 +
1.8 + "Maintain records of unique names for each simple name."
1.9 +
1.10 def __init__(self):
1.11 self.obj_to_name = {}
1.12 self.names = {}
1.13 +
1.14 def get(self, obj):
1.15 return self.obj_to_name[obj]
1.16 +
1.17 def set(self, obj, name):
1.18 if self.obj_to_name.has_key(obj):
1.19 return
1.20 @@ -200,23 +205,7 @@
1.21 self._pprint(indent + 2, "| ", "when %s: %s" % (ref, attribute), stream=stream)
1.22 self._pprint(indent, "", "--------", stream=stream)
1.23
1.24 -class NamedNode(Node):
1.25 -
1.26 - "A named node."
1.27 -
1.28 - def __init__(self, *args, **kw):
1.29 - Node.__init__(self, *args, **kw)
1.30 - self.full_name = name(self, self.name or "$untitled")
1.31 -
1.32 -class Module(NamedNode): "A Python module."
1.33 -class Subprogram(NamedNode): "A subprogram: functions, methods and loops."
1.34 -
1.35 class Pass(Node): "A placeholder node corresponding to pass."
1.36 -
1.37 -class Invoke(Node): "An invocation."
1.38 -class InvokeFunction(Invoke): "A function or method invocation."
1.39 -class InvokeBlock(Invoke): "A block or loop invocation."
1.40 -
1.41 class Return(Node): "Return an evaluated expression."
1.42 class Assign(Node): "A grouping node for assignment-related operations."
1.43 class Keyword(Node): "A grouping node for keyword arguments."
1.44 @@ -237,35 +226,78 @@
1.45 class Not(Node): "A negation of an expression."
1.46 class Choice(Node): "A special node which indicates a choice of expressions."
1.47
1.48 +# Invocations involve some more work to process calculated attributes.
1.49 +
1.50 +class Invoke(Node): "An invocation."
1.51 +
1.52 +class InvokeFunction(Invoke):
1.53 +
1.54 + "A function or method invocation."
1.55 +
1.56 + def __init__(self, *args, **kw):
1.57 + Node.__init__(self, *args, **kw)
1.58 + if hasattr(self, "args"):
1.59 + self.set_args(self.args)
1.60 +
1.61 + def set_args(self, args):
1.62 +
1.63 + "Sort the 'args' into positional and keyword arguments."
1.64 +
1.65 + self.pos_args = []
1.66 + self.kw_args = []
1.67 + add_kw = 0
1.68 + for arg in args:
1.69 + if not add_kw:
1.70 + if not isinstance(arg, Keyword):
1.71 + self.pos_args.append(arg)
1.72 + else:
1.73 + add_kw = 1
1.74 + if add_kw:
1.75 + if isinstance(arg, Keyword):
1.76 + self.kw_args.append(arg)
1.77 + else:
1.78 + raise TypeError, "Positional argument appears after keyword arguments in '%s'." % self
1.79 +
1.80 +class InvokeBlock(Invoke): "A block or loop invocation."
1.81 +
1.82 +# Named nodes are those which can be referenced in some way.
1.83 +
1.84 +class WithName:
1.85 +
1.86 + "Node naming."
1.87 +
1.88 + def __init__(self):
1.89 + self.full_name = name(self, self.name or "$untitled")
1.90 +
1.91 +class Module(Node, WithName):
1.92 +
1.93 + "A Python module."
1.94 +
1.95 + def __init__(self, *args, **kw):
1.96 + Node.__init__(self, *args, **kw)
1.97 + WithName.__init__(self)
1.98 +
1.99 +class Subprogram(Node, WithName):
1.100 +
1.101 + "A subprogram: functions, methods and loops."
1.102 +
1.103 + def __init__(self, *args, **kw):
1.104 + Node.__init__(self, *args, **kw)
1.105 + WithName.__init__(self)
1.106 +
1.107 # Special non-program nodes.
1.108
1.109 -class Structure:
1.110 -
1.111 - "A non-program node containing some kind of namespace."
1.112 +class Structure(Node): "A non-program node containing some kind of namespace."
1.113
1.114 - def __init__(self, original=None, **kw):
1.115 - self.original = original
1.116 - for name, value in kw.items():
1.117 - setattr(self, name, value)
1.118 -
1.119 - def __repr__(self):
1.120 - if hasattr(self, "name"):
1.121 - return "%s '%s'" % (self.__class__.__name__, name(self, self.name))
1.122 - else:
1.123 - return "%s" % (self.__class__.__name__,)
1.124 -
1.125 -class Class(Structure):
1.126 +class Class(Structure, WithName):
1.127
1.128 "A Python class."
1.129
1.130 - pass
1.131 -
1.132 -class Instance(Structure):
1.133 -
1.134 - "An instance."
1.135 -
1.136 def __init__(self, *args, **kw):
1.137 Structure.__init__(self, *args, **kw)
1.138 + WithName.__init__(self)
1.139 +
1.140 +class Instance(Structure): "An instance."
1.141
1.142 class Constant(Instance):
1.143