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