paulb@213 | 1 | #!/usr/bin/env python |
paulb@213 | 2 | |
paulb@213 | 3 | """ |
paulb@213 | 4 | Simplified program utilities. |
paulb@213 | 5 | |
paulb@213 | 6 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@213 | 7 | |
paulb@213 | 8 | This software is free software; you can redistribute it and/or |
paulb@213 | 9 | modify it under the terms of the GNU General Public License as |
paulb@213 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@213 | 11 | the License, or (at your option) any later version. |
paulb@213 | 12 | |
paulb@213 | 13 | This software is distributed in the hope that it will be useful, |
paulb@213 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@213 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@213 | 16 | GNU General Public License for more details. |
paulb@213 | 17 | |
paulb@213 | 18 | You should have received a copy of the GNU General Public |
paulb@213 | 19 | License along with this library; see the file LICENCE.txt |
paulb@213 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@213 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@213 | 22 | """ |
paulb@213 | 23 | |
paulb@213 | 24 | from compiler.visitor import ASTVisitor |
paulb@213 | 25 | |
paulb@213 | 26 | # Exceptions. |
paulb@213 | 27 | |
paulb@213 | 28 | class SimplifiedError(Exception): |
paulb@213 | 29 | |
paulb@213 | 30 | "An error in the annotation process." |
paulb@213 | 31 | |
paulb@213 | 32 | def __init__(self, exc, node, *args): |
paulb@213 | 33 | |
paulb@213 | 34 | """ |
paulb@213 | 35 | Initialise the error with an existing exception 'exc', the 'node' at |
paulb@213 | 36 | which this error occurs, along with additional optional arguments. |
paulb@213 | 37 | """ |
paulb@213 | 38 | |
paulb@213 | 39 | Exception.__init__(self, *args) |
paulb@213 | 40 | self.nodes = [node] |
paulb@213 | 41 | self.exc = exc |
paulb@213 | 42 | |
paulb@213 | 43 | def add(self, node): |
paulb@213 | 44 | |
paulb@213 | 45 | "Add the given 'node' to the path of nodes leading from the exception." |
paulb@213 | 46 | |
paulb@213 | 47 | self.nodes.append(node) |
paulb@213 | 48 | |
paulb@213 | 49 | def __str__(self): |
paulb@213 | 50 | |
paulb@213 | 51 | "Return a string showing the principal exception details." |
paulb@213 | 52 | |
paulb@213 | 53 | return "%s, %s" % (self.exc, self.nodes) |
paulb@213 | 54 | |
paulb@213 | 55 | # Elementary visitor support. |
paulb@213 | 56 | |
paulb@213 | 57 | class Visitor(ASTVisitor): |
paulb@213 | 58 | |
paulb@213 | 59 | "A visitor base class." |
paulb@213 | 60 | |
paulb@213 | 61 | def __init__(self): |
paulb@213 | 62 | ASTVisitor.__init__(self) |
paulb@213 | 63 | |
paulb@213 | 64 | def default(self, node, *args): |
paulb@213 | 65 | raise SimplifiedError, (None, node) |
paulb@213 | 66 | |
paulb@213 | 67 | def dispatch(self, node, *args): |
paulb@213 | 68 | return ASTVisitor.dispatch(self, node, *args) |
paulb@213 | 69 | |
paulb@213 | 70 | def dispatches(self, nodes, *args): |
paulb@213 | 71 | results = [] |
paulb@213 | 72 | for node in nodes: |
paulb@213 | 73 | results.append(self.dispatch(node, *args)) |
paulb@213 | 74 | return results |
paulb@213 | 75 | |
paulb@213 | 76 | def dispatch_dict(self, d, *args): |
paulb@213 | 77 | results = {} |
paulb@213 | 78 | for name, node in d.items(): |
paulb@213 | 79 | results[name] = self.dispatch(node, *args) |
paulb@213 | 80 | return results |
paulb@213 | 81 | |
paulb@213 | 82 | # Unique name registration. |
paulb@213 | 83 | |
paulb@213 | 84 | class Naming: |
paulb@213 | 85 | |
paulb@213 | 86 | "Maintain records of unique names for each simple name." |
paulb@213 | 87 | |
paulb@213 | 88 | index_separator = "-" |
paulb@213 | 89 | |
paulb@213 | 90 | def __init__(self): |
paulb@213 | 91 | self.names = {} |
paulb@213 | 92 | |
paulb@213 | 93 | def get(self, obj): |
paulb@213 | 94 | return obj._unique_name |
paulb@213 | 95 | |
paulb@213 | 96 | def set(self, obj, name): |
paulb@213 | 97 | if hasattr(obj, "_unique_name"): |
paulb@213 | 98 | return |
paulb@213 | 99 | if not self.names.has_key(name): |
paulb@213 | 100 | self.names[name] = 0 |
paulb@213 | 101 | n = self.names[name] + 1 |
paulb@213 | 102 | self.names[name] = n |
paulb@213 | 103 | obj._unique_name = "%s%s%d" % (name, self.index_separator, n) |
paulb@213 | 104 | |
paulb@213 | 105 | def name(obj, name): |
paulb@213 | 106 | |
paulb@213 | 107 | "Return a unique name for the given 'obj', indicating the base 'name'." |
paulb@213 | 108 | |
paulb@213 | 109 | naming.set(obj, name) |
paulb@213 | 110 | return naming.get(obj) |
paulb@213 | 111 | |
paulb@213 | 112 | # Naming singleton. |
paulb@213 | 113 | |
paulb@213 | 114 | naming = Naming() |
paulb@213 | 115 | |
paulb@213 | 116 | # Named nodes are those which can be referenced in some way. |
paulb@213 | 117 | |
paulb@213 | 118 | class WithName: |
paulb@213 | 119 | |
paulb@213 | 120 | "Node naming." |
paulb@213 | 121 | |
paulb@213 | 122 | def __init__(self): |
paulb@213 | 123 | |
paulb@213 | 124 | "Initialise the object's full name." |
paulb@213 | 125 | |
paulb@213 | 126 | self._full_name = name(self, self.name or "$untitled") |
paulb@213 | 127 | |
paulb@213 | 128 | def full_name(self): |
paulb@213 | 129 | |
paulb@213 | 130 | "Return the object's full name." |
paulb@213 | 131 | |
paulb@213 | 132 | return self._full_name |
paulb@213 | 133 | |
paulb@213 | 134 | # Comparable nodes based on naming. |
paulb@213 | 135 | |
paulb@213 | 136 | class Comparable: |
paulb@213 | 137 | |
paulb@213 | 138 | "Comparable nodes implementing the 'full_name' method." |
paulb@213 | 139 | |
paulb@213 | 140 | def __eq__(self, other): |
paulb@213 | 141 | |
paulb@213 | 142 | "This object is equal to 'other' if the full names are the same." |
paulb@213 | 143 | |
paulb@213 | 144 | # NOTE: Single instance: all instances are the same |
paulb@213 | 145 | # NOTE: Multiple instances: all instances are different |
paulb@213 | 146 | if hasattr(other, "full_name"): |
paulb@213 | 147 | return self.full_name() == other.full_name() |
paulb@213 | 148 | else: |
paulb@213 | 149 | return NotImplemented |
paulb@213 | 150 | |
paulb@213 | 151 | def __hash__(self): |
paulb@213 | 152 | |
paulb@213 | 153 | "The hash of this object is based on its full name." |
paulb@213 | 154 | |
paulb@213 | 155 | return hash(self.full_name()) |
paulb@213 | 156 | |
paulb@213 | 157 | # Structure nodes indicating namespace-bearing objects. |
paulb@213 | 158 | |
paulb@213 | 159 | class Structure(Comparable): |
paulb@213 | 160 | |
paulb@213 | 161 | "A non-program node containing some kind of namespace." |
paulb@213 | 162 | |
paulb@213 | 163 | def __init__(self, **kw): |
paulb@213 | 164 | for name, value in kw.items(): |
paulb@213 | 165 | setattr(self, name, value) |
paulb@213 | 166 | |
paulb@224 | 167 | def __repr__(self): |
paulb@224 | 168 | return "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@224 | 169 | |
paulb@244 | 170 | # Namespace classes. |
paulb@244 | 171 | |
paulb@244 | 172 | class Namespace: |
paulb@244 | 173 | |
paulb@244 | 174 | """ |
paulb@244 | 175 | A local namespace which may either relate to a genuine set of function |
paulb@244 | 176 | locals or the initialisation of a structure or module. |
paulb@244 | 177 | """ |
paulb@244 | 178 | |
paulb@244 | 179 | def __init__(self): |
paulb@244 | 180 | |
paulb@244 | 181 | """ |
paulb@244 | 182 | Initialise the namespace with a mapping of local names to possible |
paulb@244 | 183 | types, a list of return values and of possible returned local |
paulb@244 | 184 | namespaces. The namespace also tracks the "current" types and a mapping |
paulb@244 | 185 | of temporary value names to types. |
paulb@244 | 186 | """ |
paulb@244 | 187 | |
paulb@244 | 188 | self.names = {} |
paulb@244 | 189 | self.returns = set() |
paulb@244 | 190 | self.return_locals = set() |
paulb@244 | 191 | self.raises = set() |
paulb@244 | 192 | self.temp = {} |
paulb@244 | 193 | self.types = set() |
paulb@244 | 194 | |
paulb@244 | 195 | def set_types(self, types): |
paulb@244 | 196 | |
paulb@244 | 197 | "Set the current collection of 'types'." |
paulb@244 | 198 | |
paulb@244 | 199 | self.types = types.copy() |
paulb@244 | 200 | |
paulb@244 | 201 | def add(self, name, types): |
paulb@244 | 202 | |
paulb@244 | 203 | "Add to the entry with the given 'name' the specified 'types'." |
paulb@244 | 204 | |
paulb@244 | 205 | if self.names.has_key(name): |
paulb@244 | 206 | self.names[name].update(types) |
paulb@244 | 207 | else: |
paulb@244 | 208 | self.store(name, types) |
paulb@244 | 209 | |
paulb@244 | 210 | def store(self, name, types): |
paulb@244 | 211 | |
paulb@244 | 212 | "Store in (or associate with) the given 'name' the specified 'types'." |
paulb@244 | 213 | |
paulb@244 | 214 | self.names[name] = types.copy() |
paulb@244 | 215 | |
paulb@244 | 216 | __setitem__ = store |
paulb@244 | 217 | |
paulb@244 | 218 | def load(self, name): |
paulb@244 | 219 | |
paulb@244 | 220 | "Load the types associated with the given 'name'." |
paulb@244 | 221 | |
paulb@244 | 222 | return self.names[name] |
paulb@244 | 223 | |
paulb@244 | 224 | __getitem__ = load |
paulb@244 | 225 | |
paulb@244 | 226 | def has_key(self, name): |
paulb@244 | 227 | return self.names.has_key(name) |
paulb@244 | 228 | |
paulb@244 | 229 | def keys(self): |
paulb@244 | 230 | return self.names.keys() |
paulb@244 | 231 | |
paulb@244 | 232 | def values(self): |
paulb@244 | 233 | return self.names.values() |
paulb@244 | 234 | |
paulb@244 | 235 | def items(self): |
paulb@244 | 236 | return self.names.items() |
paulb@244 | 237 | |
paulb@244 | 238 | def get(self, name, default=None): |
paulb@244 | 239 | return self.names.get(name, default) |
paulb@244 | 240 | |
paulb@244 | 241 | def revoke(self, name, type): |
paulb@244 | 242 | |
paulb@244 | 243 | "Revoke from the entry for the given 'name' the specified 'type'." |
paulb@244 | 244 | |
paulb@244 | 245 | new_types = self.names[name].copy() |
paulb@244 | 246 | new_types.remove(type) |
paulb@244 | 247 | self.names[name] = new_types |
paulb@244 | 248 | |
paulb@244 | 249 | def revoke_exception_type(self, type): |
paulb@244 | 250 | |
paulb@244 | 251 | "Revoke the given 'type' from the collection of exception types." |
paulb@244 | 252 | |
paulb@244 | 253 | if type in self.raises: |
paulb@244 | 254 | self.raises.remove(type) |
paulb@244 | 255 | |
paulb@244 | 256 | def revoke_temp_type(self, index, type): |
paulb@244 | 257 | |
paulb@244 | 258 | "Revoke from the temporary variable 'index' the given 'type'." |
paulb@244 | 259 | |
paulb@244 | 260 | new_types = self.temp[index][-1].copy() |
paulb@244 | 261 | new_types.remove(type) |
paulb@244 | 262 | self.temp[index][-1] = new_types |
paulb@244 | 263 | |
paulb@244 | 264 | def merge_namespace(self, namespace, everything=1): |
paulb@244 | 265 | |
paulb@244 | 266 | """ |
paulb@244 | 267 | Merge items from the given 'namespace' with this namespace. When the |
paulb@244 | 268 | optional 'everything' parameter is set to a false value (unlike the |
paulb@244 | 269 | default), return values and locals snapshots will not be copied to this |
paulb@244 | 270 | namespace. |
paulb@244 | 271 | """ |
paulb@244 | 272 | |
paulb@244 | 273 | self.merge_items(namespace.names.items()) |
paulb@244 | 274 | self.raises.update(namespace.raises) |
paulb@244 | 275 | if everything: |
paulb@244 | 276 | self.returns.update(namespace.returns) |
paulb@244 | 277 | self.return_locals.update(namespace.return_locals) |
paulb@244 | 278 | for name, values in namespace.temp.items(): |
paulb@244 | 279 | if values: |
paulb@244 | 280 | if not self.temp.has_key(name) or not self.temp[name]: |
paulb@244 | 281 | self.temp[name] = [set()] |
paulb@244 | 282 | self.temp[name][-1].update(values[-1]) |
paulb@244 | 283 | |
paulb@244 | 284 | def merge_items(self, items): |
paulb@244 | 285 | |
paulb@244 | 286 | "Merge the given 'items' with this namespace." |
paulb@244 | 287 | |
paulb@244 | 288 | for name, types in items: |
paulb@244 | 289 | self.merge(name, types) |
paulb@244 | 290 | |
paulb@244 | 291 | def merge(self, name, types): |
paulb@244 | 292 | |
paulb@244 | 293 | "Merge the entry for the given 'name' and 'types' with this namespace." |
paulb@244 | 294 | |
paulb@244 | 295 | if not self.names.has_key(name): |
paulb@244 | 296 | self.names[name] = types.copy() |
paulb@244 | 297 | else: |
paulb@244 | 298 | existing = self.names[name] |
paulb@244 | 299 | existing.update(types) |
paulb@244 | 300 | |
paulb@244 | 301 | def snapshot(self): |
paulb@244 | 302 | |
paulb@244 | 303 | "Make a snapshot of the locals and remember them." |
paulb@244 | 304 | |
paulb@244 | 305 | namespace = Namespace() |
paulb@244 | 306 | namespace.merge_namespace(self) |
paulb@244 | 307 | self.return_locals.add(namespace) |
paulb@244 | 308 | |
paulb@244 | 309 | def reset(self): |
paulb@244 | 310 | |
paulb@244 | 311 | "Reset a namespace in preparation for merging with returned locals." |
paulb@244 | 312 | |
paulb@244 | 313 | self.names = {} |
paulb@244 | 314 | |
paulb@244 | 315 | def __repr__(self): |
paulb@244 | 316 | return repr(self.names) + " (temp) " + repr(self.temp) |
paulb@244 | 317 | |
paulb@213 | 318 | # vim: tabstop=4 expandtab shiftwidth=4 |