paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@305 | 4 | Program data structures. There are two separate kinds of structures: those with |
paul@305 | 5 | context, which are the values manipulated by programs, and those without |
paul@305 | 6 | context, which are typically constant things which are stored alongside the |
paul@305 | 7 | program but which are wrapped in context-dependent structures in the running |
paul@305 | 8 | program. |
paul@83 | 9 | |
paul@647 | 10 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 11 | |
paul@83 | 12 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 13 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 14 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 15 | version. |
paul@83 | 16 | |
paul@83 | 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 19 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 20 | details. |
paul@83 | 21 | |
paul@83 | 22 | You should have received a copy of the GNU General Public License along with |
paul@83 | 23 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 24 | |
paul@83 | 25 | -------- |
paul@83 | 26 | |
paul@305 | 27 | The principal value structure class in this module is the Attr class which |
paul@305 | 28 | represents attributes of objects and retains the context of each reference to an |
paul@305 | 29 | attribute. This class models program behaviour at run-time. |
paul@305 | 30 | |
paul@190 | 31 | The central data structure classes in this module are the following: |
paul@83 | 32 | |
paul@83 | 33 | * Class |
paul@83 | 34 | * Function |
paul@83 | 35 | * Module |
paul@83 | 36 | |
paul@83 | 37 | All of the above support the Naming interface either explicitly or through |
paul@83 | 38 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 39 | using the method of that name. |
paul@83 | 40 | |
paul@83 | 41 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 42 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 43 | to distinguish between certain kinds of attributes: |
paul@83 | 44 | |
paul@190 | 45 | * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes |
paul@83 | 46 | * Function: parameters, locals, all_locals |
paul@83 | 47 | * Module: module_attributes |
paul@83 | 48 | |
paul@83 | 49 | These specific methods are useful in certain situations. |
paul@83 | 50 | |
paul@92 | 51 | The above classes also provide an 'astnode' attribute, indicating the AST node |
paul@92 | 52 | where each such object is defined. |
paul@83 | 53 | """ |
paul@83 | 54 | |
paul@428 | 55 | from micropython.program import ReplaceableContext, PlaceholderContext |
paul@554 | 56 | from micropython.basicdata import * |
paul@622 | 57 | from micropython.branch import BranchTracking |
paul@507 | 58 | import sys |
paul@83 | 59 | |
paul@555 | 60 | try: |
paul@555 | 61 | set |
paul@555 | 62 | except NameError: |
paul@555 | 63 | from sets import Set as set |
paul@555 | 64 | |
paul@622 | 65 | class NamespaceDict(Namespace, BranchTracking): |
paul@83 | 66 | |
paul@83 | 67 | "A mix-in providing dictionary methods." |
paul@83 | 68 | |
paul@180 | 69 | def __init__(self, module=None): |
paul@622 | 70 | BranchTracking.__init__(self) |
paul@622 | 71 | |
paul@343 | 72 | self.module = module |
paul@83 | 73 | self.namespace = {} |
paul@83 | 74 | self.globals = set() |
paul@351 | 75 | self.lambdas = {} # only really useful for functions |
paul@591 | 76 | self.finalised = False |
paul@83 | 77 | |
paul@272 | 78 | # Attribute/name definition and access. |
paul@272 | 79 | |
paul@167 | 80 | def __delitem__(self, name): |
paul@167 | 81 | del self.namespace[name] |
paul@167 | 82 | |
paul@167 | 83 | def has_key(self, name): |
paul@167 | 84 | return self.namespace.has_key(name) |
paul@167 | 85 | |
paul@167 | 86 | def keys(self): |
paul@167 | 87 | return self.namespace.keys() |
paul@167 | 88 | |
paul@167 | 89 | def values(self): |
paul@167 | 90 | return self.namespace.values() |
paul@167 | 91 | |
paul@167 | 92 | def items(self): |
paul@167 | 93 | return self.namespace.items() |
paul@167 | 94 | |
paul@83 | 95 | def __getitem__(self, name): |
paul@83 | 96 | return self.namespace[name] |
paul@83 | 97 | |
paul@348 | 98 | def get(self, name, default=None): |
paul@348 | 99 | return self.namespace.get(name, default) |
paul@348 | 100 | |
paul@622 | 101 | # Introspection methods. |
paul@622 | 102 | |
paul@622 | 103 | def is_method(self): |
paul@622 | 104 | |
paul@622 | 105 | """ |
paul@622 | 106 | Return whether this function is a method explicitly defined in a class. |
paul@622 | 107 | """ |
paul@622 | 108 | |
paul@622 | 109 | return False |
paul@622 | 110 | |
paul@351 | 111 | # Administrative methods. |
paul@351 | 112 | |
paul@437 | 113 | def finalise(self, objtable): |
paul@437 | 114 | self.finalise_attributes() |
paul@495 | 115 | self.finalise_users(objtable) |
paul@437 | 116 | |
paul@351 | 117 | def items_for_vacuum(self): |
paul@351 | 118 | return self.items() + self.lambdas.items() |
paul@351 | 119 | |
paul@351 | 120 | def vacuum_item(self, name): |
paul@363 | 121 | if self.has_key(name): |
paul@363 | 122 | del self[name] |
paul@591 | 123 | return True |
paul@394 | 124 | else: |
paul@591 | 125 | return False |
paul@351 | 126 | |
paul@351 | 127 | def add_lambda(self, obj): |
paul@351 | 128 | attr = Attr(None, self, obj.name) |
paul@351 | 129 | attr.update([self.get_context_and_value(obj)], single_assignment=1) |
paul@351 | 130 | self.lambdas[obj.name] = attr |
paul@351 | 131 | |
paul@348 | 132 | # Specialised access methods. |
paul@348 | 133 | |
paul@343 | 134 | def get_using_node(self, name, node): |
paul@343 | 135 | |
paul@343 | 136 | """ |
paul@343 | 137 | Access the given 'name' through this namespace, making use of the module |
paul@343 | 138 | and builtins namespaces if necessary, annotating the given 'node' with |
paul@343 | 139 | the scope involved. |
paul@343 | 140 | """ |
paul@343 | 141 | |
paul@343 | 142 | attr, scope, full_name = self._get_with_scope(name) |
paul@343 | 143 | |
paul@343 | 144 | if scope is not None: |
paul@343 | 145 | node._scope = scope |
paul@343 | 146 | self.note_scope(name, scope) |
paul@343 | 147 | |
paul@551 | 148 | if full_name is not None and (scope != "local" or isinstance(self, (Class, Module))): |
paul@343 | 149 | self.use_specific_attribute(full_name, name) |
paul@343 | 150 | |
paul@343 | 151 | return attr |
paul@343 | 152 | |
paul@343 | 153 | def _get_with_scope(self, name, external=0): |
paul@343 | 154 | |
paul@348 | 155 | """ |
paul@348 | 156 | Find the source of the given 'name', returning the attribute object, |
paul@348 | 157 | scope (constant, local, global or builtins), and the full name of the |
paul@348 | 158 | source namespace (or None for constants). |
paul@348 | 159 | |
paul@348 | 160 | If the optional 'external' flag is set to a true value, only external |
paul@348 | 161 | (non-local) namespaces will be involved in the search. |
paul@348 | 162 | """ |
paul@348 | 163 | |
paul@343 | 164 | module = self.module |
paul@343 | 165 | builtins = module and module.builtins or None |
paul@343 | 166 | importer = module and module.importer or None |
paul@343 | 167 | |
paul@343 | 168 | # Constants. |
paul@343 | 169 | |
paul@343 | 170 | if importer is not None and importer.predefined_constants.has_key(name): |
paul@343 | 171 | return importer.get_predefined_constant(name), "constant", None |
paul@343 | 172 | |
paul@343 | 173 | # Locals. |
paul@343 | 174 | |
paul@343 | 175 | elif not external and self.has_key(name): |
paul@343 | 176 | return self[name], "local", self.full_name() |
paul@343 | 177 | |
paul@343 | 178 | # Globals. |
paul@343 | 179 | |
paul@344 | 180 | elif module is not None and module is not self and module.has_key(name): |
paul@343 | 181 | return module[name], "global", module.full_name() |
paul@343 | 182 | |
paul@343 | 183 | # Builtins. |
paul@343 | 184 | |
paul@343 | 185 | elif builtins is not None and builtins.has_key(name): |
paul@343 | 186 | return builtins[name], "builtins", builtins.full_name() |
paul@343 | 187 | |
paul@343 | 188 | # Unknown. |
paul@343 | 189 | |
paul@343 | 190 | else: |
paul@343 | 191 | return None, None, None |
paul@343 | 192 | |
paul@348 | 193 | # Attribute definition methods. |
paul@83 | 194 | |
paul@83 | 195 | def __setitem__(self, name, value): |
paul@83 | 196 | self.set(name, value) |
paul@83 | 197 | |
paul@83 | 198 | def set(self, name, value, single_assignment=1): |
paul@83 | 199 | |
paul@83 | 200 | """ |
paul@83 | 201 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 202 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 203 | this operation (or whether the operation covers potentially many |
paul@83 | 204 | assignments in the lifetime of a program). |
paul@83 | 205 | """ |
paul@83 | 206 | |
paul@442 | 207 | if value is None: |
paul@507 | 208 | print >>sys.stderr, "Warning: name %r in namespace %r has an unknown value (evaluated to None)." % (name, self.full_name()) |
paul@448 | 209 | value = make_instance() |
paul@442 | 210 | |
paul@83 | 211 | if name in self.globals: |
paul@180 | 212 | self.module.set(name, value, 0) |
paul@83 | 213 | else: |
paul@192 | 214 | self._set(name, value, single_assignment) |
paul@362 | 215 | self.define_scope(name, "local") |
paul@83 | 216 | |
paul@83 | 217 | def set_module(self, name, value): |
paul@83 | 218 | |
paul@83 | 219 | """ |
paul@83 | 220 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 221 | context of making a module reference available in association with |
paul@83 | 222 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 223 | module. |
paul@83 | 224 | """ |
paul@83 | 225 | |
paul@192 | 226 | self._set(name, value, 1) |
paul@83 | 227 | |
paul@192 | 228 | def _set(self, name, attr_or_value, single_assignment=1): |
paul@83 | 229 | |
paul@187 | 230 | """ |
paul@192 | 231 | The underlying set operation associating 'name' with the given |
paul@192 | 232 | 'attr_or_value'. |
paul@187 | 233 | See: docs/assignment.txt |
paul@187 | 234 | """ |
paul@83 | 235 | |
paul@192 | 236 | # Add and/or obtain the namespace entry. |
paul@87 | 237 | |
paul@192 | 238 | if not self.namespace.has_key(name): |
paul@192 | 239 | self.namespace[name] = Attr(None, self, name) |
paul@120 | 240 | |
paul@192 | 241 | attr = self.namespace[name] |
paul@329 | 242 | self._set_using_attr(attr, attr_or_value, single_assignment) |
paul@329 | 243 | |
paul@329 | 244 | def _set_using_attr(self, attr, attr_or_value, single_assignment=1): |
paul@187 | 245 | |
paul@192 | 246 | # Handle attribute assignment as well as assignment of basic objects. |
paul@237 | 247 | # Attempt to fix the context if not explicitly defined. |
paul@192 | 248 | |
paul@192 | 249 | if isinstance(attr_or_value, Attr): |
paul@237 | 250 | context_values = self.get_updated_context_values(attr_or_value.context_values) |
paul@237 | 251 | else: |
paul@237 | 252 | context_values = self.get_updated_context_values([self.get_context_and_value(attr_or_value)]) |
paul@187 | 253 | |
paul@237 | 254 | attr.update(context_values, single_assignment) |
paul@237 | 255 | |
paul@237 | 256 | def get_context_and_value(self, value): |
paul@237 | 257 | |
paul@237 | 258 | "Return a context, value tuple for the given 'value'." |
paul@237 | 259 | |
paul@237 | 260 | # Functions have a replaceable context. |
paul@120 | 261 | |
paul@237 | 262 | if isinstance(value, Function): |
paul@237 | 263 | return (ReplaceableContext, value) |
paul@237 | 264 | |
paul@237 | 265 | # Classes use placeholder contexts which cannot be replaced but which |
paul@237 | 266 | # do not communicate useful contextual information. |
paul@237 | 267 | |
paul@237 | 268 | elif isinstance(value, Class): |
paul@237 | 269 | return (PlaceholderContext, value) |
paul@237 | 270 | |
paul@237 | 271 | # Other values employ themselves as the context. |
paul@87 | 272 | |
paul@192 | 273 | else: |
paul@237 | 274 | return (value, value) |
paul@83 | 275 | |
paul@192 | 276 | def get_updated_context_values(self, context_values): |
paul@87 | 277 | |
paul@119 | 278 | """ |
paul@192 | 279 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 280 | set. |
paul@187 | 281 | See: docs/assignment.txt |
paul@119 | 282 | """ |
paul@87 | 283 | |
paul@192 | 284 | results = set() |
paul@192 | 285 | |
paul@192 | 286 | for context, value in context_values: |
paul@192 | 287 | |
paul@192 | 288 | # Set the context of instances to themselves. |
paul@187 | 289 | |
paul@192 | 290 | if isinstance(value, Instance): |
paul@192 | 291 | results.add((value, value)) |
paul@192 | 292 | else: |
paul@192 | 293 | results.add((context, value)) |
paul@192 | 294 | |
paul@192 | 295 | return results |
paul@87 | 296 | |
paul@83 | 297 | def make_global(self, name): |
paul@306 | 298 | |
paul@306 | 299 | "Declare 'name' as a global in the current namespace." |
paul@306 | 300 | |
paul@83 | 301 | if not self.namespace.has_key(name): |
paul@83 | 302 | self.globals.add(name) |
paul@362 | 303 | self.define_scope(name, "global") |
paul@591 | 304 | return True |
paul@83 | 305 | else: |
paul@591 | 306 | return False |
paul@83 | 307 | |
paul@272 | 308 | # Attribute positioning. |
paul@272 | 309 | |
paul@83 | 310 | def attributes_as_list(self): |
paul@92 | 311 | |
paul@92 | 312 | "Return the attributes in a list." |
paul@92 | 313 | |
paul@83 | 314 | self.finalise_attributes() |
paul@83 | 315 | l = [None] * len(self.keys()) |
paul@83 | 316 | for attr in self.values(): |
paul@83 | 317 | l[attr.position] = attr |
paul@83 | 318 | return l |
paul@83 | 319 | |
paul@287 | 320 | def finalise_attributes(self): |
paul@83 | 321 | |
paul@83 | 322 | "Make sure all attributes are fully defined." |
paul@83 | 323 | |
paul@92 | 324 | if self.finalised: |
paul@92 | 325 | return |
paul@92 | 326 | |
paul@83 | 327 | # The default action is to assign attribute positions sequentially. |
paul@83 | 328 | |
paul@83 | 329 | for i, attr in enumerate(self.values()): |
paul@83 | 330 | attr.position = i |
paul@83 | 331 | |
paul@591 | 332 | self.finalised = True |
paul@92 | 333 | |
paul@282 | 334 | def unfinalise_attributes(self): |
paul@282 | 335 | |
paul@282 | 336 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 337 | |
paul@591 | 338 | self.finalised = False |
paul@282 | 339 | |
paul@305 | 340 | # Program data structures. |
paul@83 | 341 | |
paul@83 | 342 | class Attr: |
paul@83 | 343 | |
paul@87 | 344 | "An attribute entry having a context." |
paul@83 | 345 | |
paul@647 | 346 | def __init__(self, position, parent, name, parent_type=None): |
paul@119 | 347 | |
paul@119 | 348 | """ |
paul@119 | 349 | Initialise the attribute with the given 'position' within the collection |
paul@647 | 350 | of attributes of its 'parent', indicating its 'name'. If the |
paul@647 | 351 | 'parent_type' is specified, it will contain the type of any instance |
paul@647 | 352 | parent. |
paul@119 | 353 | """ |
paul@119 | 354 | |
paul@83 | 355 | self.position = position |
paul@83 | 356 | self.parent = parent |
paul@83 | 357 | self.name = name |
paul@647 | 358 | self.parent_type = parent_type |
paul@192 | 359 | |
paul@272 | 360 | # Possible values. |
paul@272 | 361 | |
paul@192 | 362 | self.context_values = set() |
paul@83 | 363 | |
paul@83 | 364 | # Number of assignments per name. |
paul@83 | 365 | |
paul@192 | 366 | self.assignments = None |
paul@192 | 367 | |
paul@268 | 368 | # Value-related methods. |
paul@268 | 369 | |
paul@192 | 370 | def get_contexts(self): |
paul@192 | 371 | return [c for (c, v) in self.context_values] |
paul@167 | 372 | |
paul@192 | 373 | def get_values(self): |
paul@192 | 374 | return [v for (c, v) in self.context_values] |
paul@192 | 375 | |
paul@192 | 376 | def get_context(self): |
paul@578 | 377 | |
paul@578 | 378 | "Get the context referenced by the attribute." |
paul@578 | 379 | |
paul@578 | 380 | if self.assignments == 1 and len(self.context_values) == 1: |
paul@237 | 381 | return self.get_contexts()[0] |
paul@237 | 382 | else: |
paul@237 | 383 | return None |
paul@192 | 384 | |
paul@192 | 385 | def get_value(self): |
paul@578 | 386 | |
paul@578 | 387 | "Get the value referenced by the attribute." |
paul@578 | 388 | |
paul@578 | 389 | if self.assignments == 1 and len(self.context_values) == 1: |
paul@237 | 390 | return self.get_values()[0] |
paul@237 | 391 | else: |
paul@237 | 392 | return None |
paul@192 | 393 | |
paul@508 | 394 | __call__ = get_value # convenient access to any single value |
paul@508 | 395 | |
paul@192 | 396 | def update(self, context_values, single_assignment): |
paul@83 | 397 | |
paul@83 | 398 | """ |
paul@192 | 399 | Update the attribute, adding the 'context_values' provided to the |
paul@192 | 400 | known details associated with the attribute, changing the number of |
paul@192 | 401 | assignments according to the 'single_assignment' status of the |
paul@192 | 402 | operation, where a true value indicates that only one assignment is |
paul@192 | 403 | associated with the update, and a false value indicates that potentially |
paul@192 | 404 | many assignments may be involved. |
paul@83 | 405 | """ |
paul@83 | 406 | |
paul@442 | 407 | if self.context_values.issuperset(context_values) and \ |
paul@448 | 408 | not (make_instance(), make_instance()) in context_values: |
paul@442 | 409 | return |
paul@442 | 410 | |
paul@578 | 411 | self.update_assignments(len(set(context_values)), single_assignment) |
paul@578 | 412 | self.context_values.update(context_values) |
paul@578 | 413 | |
paul@578 | 414 | def update_assignments(self, n, single_assignment): |
paul@83 | 415 | if self.assignments is None: |
paul@83 | 416 | if single_assignment: |
paul@578 | 417 | self.assignments = n |
paul@83 | 418 | else: |
paul@578 | 419 | self.assignments = AtLeast(n) |
paul@83 | 420 | else: |
paul@83 | 421 | if single_assignment: |
paul@578 | 422 | self.assignments += n |
paul@83 | 423 | else: |
paul@578 | 424 | self.assignments += AtLeast(n) |
paul@87 | 425 | |
paul@331 | 426 | def is_constant(self): |
paul@331 | 427 | |
paul@331 | 428 | """ |
paul@331 | 429 | Return whether this attribute references something that can be regarded |
paul@331 | 430 | as being constant within a particular scope. |
paul@331 | 431 | """ |
paul@331 | 432 | |
paul@331 | 433 | return self.assignments == 1 |
paul@331 | 434 | |
paul@330 | 435 | def is_strict_constant(self): |
paul@330 | 436 | |
paul@330 | 437 | """ |
paul@330 | 438 | Return whether this attribute references something that can be regarded |
paul@330 | 439 | as being constant. |
paul@330 | 440 | """ |
paul@330 | 441 | |
paul@330 | 442 | value = self.get_value() |
paul@468 | 443 | return not (value is None or (isinstance(value, Instance) and not isinstance(value, Constant))) |
paul@330 | 444 | |
paul@242 | 445 | def is_static_attribute(self): |
paul@242 | 446 | |
paul@242 | 447 | """ |
paul@242 | 448 | Return whether this attribute is defined on a fixed/static object such |
paul@242 | 449 | as a class or a module. |
paul@242 | 450 | """ |
paul@242 | 451 | |
paul@401 | 452 | return isinstance(self.parent, (Class, Module)) |
paul@127 | 453 | |
paul@243 | 454 | def defines_ambiguous_class(self): |
paul@243 | 455 | |
paul@243 | 456 | "Return whether this attribute defines more than one class." |
paul@243 | 457 | |
paul@243 | 458 | if self.assignments > 1: |
paul@591 | 459 | have_class = False |
paul@243 | 460 | for obj in self.get_values(): |
paul@243 | 461 | if isinstance(obj, Class): |
paul@243 | 462 | if have_class: |
paul@591 | 463 | return True |
paul@591 | 464 | have_class = True |
paul@591 | 465 | |
paul@591 | 466 | return False |
paul@243 | 467 | |
paul@119 | 468 | def defined_within_hierarchy(self): |
paul@119 | 469 | |
paul@119 | 470 | """ |
paul@119 | 471 | Return whether the parent and context of the attribute belong to the |
paul@119 | 472 | same class hierarchy. |
paul@119 | 473 | """ |
paul@119 | 474 | |
paul@192 | 475 | # Must be defined within a class. |
paul@192 | 476 | |
paul@192 | 477 | if isinstance(self.parent, Class): |
paul@192 | 478 | |
paul@192 | 479 | # To be sure, all contexts must be classes and be the same as the |
paul@192 | 480 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@192 | 481 | # parent. |
paul@192 | 482 | |
paul@192 | 483 | for context in self.get_contexts(): |
paul@192 | 484 | if not ( |
paul@192 | 485 | isinstance(context, Class) and ( |
paul@192 | 486 | context is self.parent or |
paul@192 | 487 | context.has_subclass(self.parent) or |
paul@192 | 488 | self.parent.has_subclass(context)) |
paul@192 | 489 | ): |
paul@591 | 490 | return False |
paul@591 | 491 | |
paul@591 | 492 | return True |
paul@192 | 493 | |
paul@192 | 494 | # Instance attributes are not defined within a hierarchy. |
paul@192 | 495 | |
paul@192 | 496 | else: |
paul@591 | 497 | return False |
paul@119 | 498 | |
paul@194 | 499 | def defined_outside_hierarchy(self): |
paul@194 | 500 | |
paul@194 | 501 | """ |
paul@194 | 502 | Return whether the parent and context of the attribute never belong to |
paul@194 | 503 | the same class hierarchy. |
paul@194 | 504 | """ |
paul@194 | 505 | |
paul@194 | 506 | # Must be defined within a class. |
paul@194 | 507 | |
paul@194 | 508 | if isinstance(self.parent, Class): |
paul@194 | 509 | |
paul@194 | 510 | # To be sure, all contexts must be classes and be the same as the |
paul@194 | 511 | # parent, or be a superclass of the parent, or be a subclass of the |
paul@194 | 512 | # parent. |
paul@194 | 513 | |
paul@194 | 514 | for context in self.get_contexts(): |
paul@640 | 515 | if ( |
paul@194 | 516 | isinstance(context, Class) and not ( |
paul@194 | 517 | context is self.parent or |
paul@194 | 518 | context.has_subclass(self.parent) or |
paul@194 | 519 | self.parent.has_subclass(context)) |
paul@194 | 520 | ): |
paul@591 | 521 | return False |
paul@591 | 522 | |
paul@591 | 523 | return True |
paul@194 | 524 | |
paul@194 | 525 | # Instance attributes are not defined within a hierarchy. |
paul@194 | 526 | |
paul@194 | 527 | else: |
paul@640 | 528 | return True |
paul@194 | 529 | |
paul@83 | 530 | def __repr__(self): |
paul@494 | 531 | if self.position is not None: |
paul@494 | 532 | position = "at %r, " % self.position |
paul@494 | 533 | else: |
paul@494 | 534 | position = "" |
paul@647 | 535 | if self.parent_type is not None: |
paul@647 | 536 | parent_type = "parent %s, " % shortrepr(self.parent_type) |
paul@647 | 537 | else: |
paul@647 | 538 | parent_type = "" |
paul@647 | 539 | return "<attribute %s.%s (%s%sassigned %r)>" % ( |
paul@494 | 540 | shortrepr(self.parent), self.name, |
paul@647 | 541 | parent_type, position, self.assignments |
paul@87 | 542 | ) |
paul@83 | 543 | |
paul@329 | 544 | def __shortrepr__(self): |
paul@494 | 545 | return "%s.%s (at %r)" % (shortrepr(self.parent), self.name, self.position) |
paul@329 | 546 | |
paul@192 | 547 | def _context_values_str(self): |
paul@192 | 548 | l = [] |
paul@192 | 549 | for (c, v) in self.context_values: |
paul@192 | 550 | l.append("(c=%s, v=%s)" % (shortrepr(c), shortrepr(v))) |
paul@192 | 551 | return ", ".join(l) |
paul@192 | 552 | |
paul@85 | 553 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 554 | |
paul@394 | 555 | "A base class for common/normal classes and the type class." |
paul@394 | 556 | |
paul@665 | 557 | def __init__(self, name, parent=None, module=None, node=None, original_name=None): |
paul@83 | 558 | |
paul@83 | 559 | """ |
paul@394 | 560 | Initialise the class with the given 'name', optional 'parent' object, |
paul@394 | 561 | 'module' and AST 'node'. The optional information must be set at a later |
paul@394 | 562 | point using the 'set_context' method if omitted. |
paul@83 | 563 | """ |
paul@83 | 564 | |
paul@180 | 565 | NamespaceDict.__init__(self, module) |
paul@83 | 566 | self.name = name |
paul@83 | 567 | self.parent = parent |
paul@91 | 568 | self.astnode = node |
paul@665 | 569 | self.original_name = original_name or name |
paul@83 | 570 | |
paul@83 | 571 | # Superclasses, descendants and attributes. |
paul@83 | 572 | |
paul@83 | 573 | self.bases = [] |
paul@83 | 574 | self.descendants = set() |
paul@83 | 575 | self.instattr = set() # instance attributes |
paul@527 | 576 | self.instattr_tentative = set() # tentative/suspected instance attributes |
paul@83 | 577 | self.relocated = set() # attributes which do not have the same |
paul@83 | 578 | # position as those of the same name in |
paul@83 | 579 | # some superclasses |
paul@83 | 580 | |
paul@83 | 581 | # Caches. |
paul@83 | 582 | |
paul@282 | 583 | self.reset_caches() |
paul@83 | 584 | |
paul@667 | 585 | # Program-related details. |
paul@240 | 586 | |
paul@83 | 587 | self.instantiator = None |
paul@173 | 588 | self.blocks = None |
paul@116 | 589 | self.temp_usage = 0 |
paul@116 | 590 | self.local_usage = 0 |
paul@133 | 591 | self.all_local_usage = 0 |
paul@83 | 592 | |
paul@401 | 593 | # Add an attribute to this class for use by instances. |
paul@401 | 594 | |
paul@401 | 595 | self.set("__class__", self) |
paul@394 | 596 | |
paul@394 | 597 | def set_context(self, parent, module, node): |
paul@401 | 598 | |
paul@401 | 599 | "Set the 'parent', 'module' and 'node' of a class created in advance." |
paul@401 | 600 | |
paul@394 | 601 | self.parent = parent |
paul@394 | 602 | self.module = module |
paul@394 | 603 | self.astnode = node |
paul@394 | 604 | |
paul@282 | 605 | def reset_caches(self): |
paul@282 | 606 | |
paul@282 | 607 | "Reset the caches." |
paul@282 | 608 | |
paul@282 | 609 | self.all_instattr = None # cache for instance_attributes |
paul@282 | 610 | self.all_instattr_names = None # from all_instattr |
paul@282 | 611 | self.all_classattr = None # cache for all_class_attributes |
paul@282 | 612 | self.all_classattr_names = None # from all_classattr |
paul@282 | 613 | self.allattr = None # cache for all_attributes |
paul@282 | 614 | self.allattr_names = None # from allattr |
paul@282 | 615 | |
paul@83 | 616 | def __repr__(self): |
paul@667 | 617 | return "<class %s>" % shortrepr(self) |
paul@87 | 618 | |
paul@87 | 619 | def __shortrepr__(self): |
paul@494 | 620 | return "%s.%s" % (shortrepr(self.parent), self.name) |
paul@87 | 621 | |
paul@227 | 622 | def get_body_block(self): |
paul@227 | 623 | return self.get_instantiator().blocks[0] |
paul@215 | 624 | |
paul@189 | 625 | # Namespace-related methods. |
paul@189 | 626 | |
paul@192 | 627 | def get_updated_context_values(self, context_values): |
paul@87 | 628 | |
paul@119 | 629 | """ |
paul@192 | 630 | Adapt the contexts found in the given 'context_values', returning a new |
paul@192 | 631 | set. |
paul@187 | 632 | See: docs/assignment.txt |
paul@119 | 633 | """ |
paul@87 | 634 | |
paul@192 | 635 | results = set() |
paul@192 | 636 | |
paul@192 | 637 | for context, value in context_values: |
paul@187 | 638 | |
paul@187 | 639 | # Change the ownership of functions. |
paul@187 | 640 | |
paul@237 | 641 | if context is ReplaceableContext and value is not None and isinstance(value, Function): |
paul@192 | 642 | results.add((self, value)) |
paul@192 | 643 | else: |
paul@192 | 644 | results.add((context, value)) |
paul@187 | 645 | |
paul@192 | 646 | return NamespaceDict.get_updated_context_values(self, results) |
paul@83 | 647 | |
paul@351 | 648 | # Administrative methods. |
paul@351 | 649 | |
paul@394 | 650 | def items_for_vacuum(self): |
paul@405 | 651 | |
paul@405 | 652 | "Consider both class and instance attributes for vacuuming." |
paul@405 | 653 | |
paul@394 | 654 | items = [] |
paul@394 | 655 | for name in self.instattr: |
paul@394 | 656 | items.append((name, None)) |
paul@394 | 657 | return NamespaceDict.items_for_vacuum(self) + items |
paul@394 | 658 | |
paul@394 | 659 | def vacuum_item(self, name): |
paul@405 | 660 | |
paul@405 | 661 | "Vacuum 'name' from the class or instance attribute collections." |
paul@405 | 662 | |
paul@405 | 663 | # NOTE: Hack to prevent damage to exceptions. |
paul@405 | 664 | |
paul@405 | 665 | if name == "_pc": |
paul@591 | 666 | return False |
paul@405 | 667 | |
paul@394 | 668 | if not NamespaceDict.vacuum_item(self, name): |
paul@394 | 669 | self.instattr.remove(name) |
paul@591 | 670 | return True |
paul@394 | 671 | |
paul@287 | 672 | def finalise_attributes(self): |
paul@83 | 673 | |
paul@83 | 674 | "Make sure that all attributes are fully defined." |
paul@83 | 675 | |
paul@92 | 676 | if self.finalised: |
paul@92 | 677 | return |
paul@92 | 678 | |
paul@83 | 679 | self.finalise_class_attributes() |
paul@83 | 680 | self.finalise_instance_attributes() |
paul@591 | 681 | self.finalised = True |
paul@83 | 682 | |
paul@282 | 683 | def unfinalise_attributes(self): |
paul@282 | 684 | |
paul@282 | 685 | "Open attribute definitions to editing and subsequent finalisation." |
paul@282 | 686 | |
paul@282 | 687 | self.reset_caches() |
paul@591 | 688 | self.finalised = False |
paul@282 | 689 | |
paul@184 | 690 | # Convenience methods for accessing functions and methods. |
paul@184 | 691 | |
paul@83 | 692 | def get_instantiator(self): |
paul@83 | 693 | |
paul@83 | 694 | "Return a function which can be used to instantiate the class." |
paul@83 | 695 | |
paul@83 | 696 | if self.instantiator is None: |
paul@212 | 697 | self.instantiator = self.get_init_method().as_instantiator() |
paul@83 | 698 | return self.instantiator |
paul@83 | 699 | |
paul@137 | 700 | def get_init_method(self): |
paul@192 | 701 | return self.all_class_attributes()["__init__"].get_value() |
paul@137 | 702 | |
paul@83 | 703 | # Class-specific methods. |
paul@83 | 704 | |
paul@83 | 705 | def add_base(self, base): |
paul@83 | 706 | self.bases.append(base) |
paul@83 | 707 | base.add_descendant(self) |
paul@83 | 708 | |
paul@527 | 709 | def add_instance_attribute(self, name, tentative=False): |
paul@527 | 710 | if tentative: |
paul@527 | 711 | self.instattr_tentative.add(name) |
paul@527 | 712 | else: |
paul@527 | 713 | self.instattr.add(name) |
paul@83 | 714 | |
paul@83 | 715 | def add_descendant(self, cls): |
paul@83 | 716 | self.descendants.add(cls) |
paul@83 | 717 | for base in self.bases: |
paul@83 | 718 | base.add_descendant(cls) |
paul@83 | 719 | |
paul@122 | 720 | def has_subclass(self, other): |
paul@122 | 721 | return other in self.descendants |
paul@122 | 722 | |
paul@135 | 723 | def all_descendants(self): |
paul@135 | 724 | d = {} |
paul@135 | 725 | for cls in self.descendants: |
paul@135 | 726 | d[cls.full_name()] = cls |
paul@135 | 727 | return d |
paul@135 | 728 | |
paul@83 | 729 | "Return the attribute names provided by this class only." |
paul@83 | 730 | |
paul@83 | 731 | class_attribute_names = NamespaceDict.keys |
paul@83 | 732 | |
paul@83 | 733 | def class_attributes(self): |
paul@83 | 734 | |
paul@83 | 735 | "Return class attributes provided by this class only." |
paul@83 | 736 | |
paul@83 | 737 | return dict(self) |
paul@83 | 738 | |
paul@83 | 739 | def all_class_attribute_names(self): |
paul@83 | 740 | |
paul@83 | 741 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 742 | |
paul@83 | 743 | if self.all_classattr_names is None: |
paul@83 | 744 | self.all_class_attributes() |
paul@282 | 745 | self.all_classattr_names = self.all_classattr.keys() |
paul@83 | 746 | return self.all_classattr_names |
paul@83 | 747 | |
paul@83 | 748 | def all_class_attributes(self): |
paul@83 | 749 | |
paul@83 | 750 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 751 | |
paul@83 | 752 | self.finalise_class_attributes() |
paul@83 | 753 | return self.all_classattr |
paul@83 | 754 | |
paul@83 | 755 | def finalise_class_attributes(self): |
paul@83 | 756 | |
paul@83 | 757 | "Make sure that the class attributes are fully defined." |
paul@83 | 758 | |
paul@83 | 759 | if self.all_classattr is None: |
paul@83 | 760 | self.all_classattr = {} |
paul@83 | 761 | clsattr = {} |
paul@83 | 762 | |
paul@83 | 763 | # Record provisional position information for attributes of this |
paul@83 | 764 | # class. |
paul@83 | 765 | |
paul@83 | 766 | for name in self.class_attributes().keys(): |
paul@396 | 767 | |
paul@396 | 768 | # Special case: __class__ has to be at position 0. |
paul@396 | 769 | |
paul@396 | 770 | if name == "__class__": |
paul@396 | 771 | clsattr[name] = set([0]) |
paul@396 | 772 | else: |
paul@396 | 773 | clsattr[name] = set() # position not yet defined |
paul@83 | 774 | |
paul@83 | 775 | reversed_bases = self.bases[:] |
paul@83 | 776 | reversed_bases.reverse() |
paul@83 | 777 | |
paul@83 | 778 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 779 | |
paul@83 | 780 | for cls in reversed_bases: |
paul@83 | 781 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 782 | self.all_classattr[name] = attr |
paul@83 | 783 | |
paul@83 | 784 | # Record previous attribute information. |
paul@83 | 785 | |
paul@83 | 786 | if clsattr.has_key(name): |
paul@83 | 787 | clsattr[name].add(attr.position) |
paul@83 | 788 | |
paul@83 | 789 | # Record class attributes provided by this class and its bases, |
paul@83 | 790 | # along with their positions. |
paul@83 | 791 | |
paul@83 | 792 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 793 | |
paul@83 | 794 | if clsattr: |
paul@83 | 795 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 796 | self.all_classattr[name].position = i |
paul@83 | 797 | |
paul@83 | 798 | return self.all_classattr |
paul@83 | 799 | |
paul@83 | 800 | def instance_attribute_names(self): |
paul@83 | 801 | |
paul@83 | 802 | "Return the instance attribute names provided by the class." |
paul@83 | 803 | |
paul@83 | 804 | if self.all_instattr_names is None: |
paul@83 | 805 | self.instance_attributes() |
paul@83 | 806 | return self.all_instattr_names |
paul@83 | 807 | |
paul@83 | 808 | def instance_attributes(self): |
paul@83 | 809 | |
paul@83 | 810 | "Return instance-only attributes for instances of this class." |
paul@83 | 811 | |
paul@83 | 812 | self.finalise_instance_attributes() |
paul@83 | 813 | return self.all_instattr |
paul@83 | 814 | |
paul@667 | 815 | def instance_attributes_as_list(self): |
paul@667 | 816 | |
paul@667 | 817 | "Return instance-only attributes in a list ordered by position." |
paul@667 | 818 | |
paul@667 | 819 | attrs = self.instance_attributes().values() |
paul@667 | 820 | attrs.sort(cmp=lambda x, y: cmp(x.position, y.position)) |
paul@667 | 821 | return attrs |
paul@667 | 822 | |
paul@83 | 823 | def finalise_instance_attributes(self): |
paul@83 | 824 | |
paul@83 | 825 | "Make sure that the instance attributes are fully defined." |
paul@83 | 826 | |
paul@527 | 827 | # Eliminate tentative instance attributes that are actually class |
paul@527 | 828 | # attributes. |
paul@527 | 829 | |
paul@527 | 830 | for attrname in self.all_class_attributes().keys(): |
paul@527 | 831 | if attrname in self.instattr_tentative: |
paul@527 | 832 | self.instattr_tentative.remove(attrname) |
paul@527 | 833 | |
paul@527 | 834 | for cls in self.descendants: |
paul@527 | 835 | for attrname in cls.class_attribute_names(): |
paul@527 | 836 | if attrname in self.instattr_tentative: |
paul@527 | 837 | self.instattr_tentative.remove(attrname) |
paul@527 | 838 | |
paul@527 | 839 | for attrname in self.instattr_tentative: |
paul@527 | 840 | self.instattr.add(attrname) |
paul@527 | 841 | |
paul@210 | 842 | # Cache the attributes by converting the positioned attributes into a |
paul@210 | 843 | # dictionary. |
paul@83 | 844 | |
paul@210 | 845 | if self.all_instattr is None: |
paul@210 | 846 | self.all_instattr = self._get_attributes() |
paul@83 | 847 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 848 | |
paul@83 | 849 | return self.all_instattr |
paul@83 | 850 | |
paul@210 | 851 | def _get_attributes(self): |
paul@210 | 852 | |
paul@210 | 853 | """ |
paul@210 | 854 | Return a dictionary mapping names to Attr instances incorporating |
paul@210 | 855 | information about their positions in the final instance structure. |
paul@210 | 856 | """ |
paul@210 | 857 | |
paul@210 | 858 | instattr = {} |
paul@210 | 859 | |
paul@210 | 860 | # Record provisional position information for attributes of this |
paul@210 | 861 | # instance. |
paul@210 | 862 | |
paul@210 | 863 | for name in self.instattr: |
paul@401 | 864 | instattr[name] = set() # position not yet defined |
paul@210 | 865 | |
paul@210 | 866 | reversed_bases = self.bases[:] |
paul@210 | 867 | reversed_bases.reverse() |
paul@210 | 868 | |
paul@210 | 869 | # For the bases in reverse order, acquire instance attribute |
paul@210 | 870 | # details. |
paul@210 | 871 | |
paul@210 | 872 | for cls in reversed_bases: |
paul@210 | 873 | for name, attr in cls.instance_attributes().items(): |
paul@210 | 874 | |
paul@210 | 875 | # Record previous attribute information. |
paul@210 | 876 | |
paul@210 | 877 | if instattr.has_key(name): |
paul@210 | 878 | instattr[name].add(attr.position) |
paul@235 | 879 | else: |
paul@235 | 880 | instattr[name] = set([attr.position]) |
paul@210 | 881 | |
paul@210 | 882 | # Build the dictionary of attributes using the existing positions known |
paul@210 | 883 | # for each name. |
paul@210 | 884 | |
paul@210 | 885 | d = {} |
paul@210 | 886 | for i, name in enumerate(self._get_position_list(instattr)): |
paul@647 | 887 | d[name] = Attr(i, make_instance(), name, self) |
paul@210 | 888 | return d |
paul@210 | 889 | |
paul@83 | 890 | def _get_position_list(self, positions): |
paul@83 | 891 | |
paul@83 | 892 | """ |
paul@83 | 893 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 894 | names to positions, indicating the positions of the attributes in the |
paul@83 | 895 | final instance structure. |
paul@83 | 896 | """ |
paul@83 | 897 | |
paul@83 | 898 | position_items = positions.items() |
paul@83 | 899 | namearray = [None] * len(position_items) |
paul@83 | 900 | |
paul@83 | 901 | # Get the positions in ascending order of list size, with lists |
paul@83 | 902 | # of the same size ordered according to their smallest position |
paul@83 | 903 | # value. |
paul@83 | 904 | |
paul@83 | 905 | position_items.sort(self._cmp_positions) |
paul@83 | 906 | |
paul@83 | 907 | # Get the names in position order. |
paul@83 | 908 | |
paul@83 | 909 | held = [] |
paul@83 | 910 | |
paul@83 | 911 | for name, pos in position_items: |
paul@83 | 912 | pos = list(pos) |
paul@83 | 913 | pos.sort() |
paul@83 | 914 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 915 | namearray[pos[0]] = name |
paul@83 | 916 | else: |
paul@83 | 917 | if pos: |
paul@83 | 918 | self.relocated.add(name) |
paul@83 | 919 | held.append((name, pos)) |
paul@83 | 920 | |
paul@83 | 921 | for i, attr in enumerate(namearray): |
paul@83 | 922 | if attr is None: |
paul@83 | 923 | name, pos = held.pop() |
paul@83 | 924 | namearray[i] = name |
paul@83 | 925 | |
paul@83 | 926 | return namearray |
paul@83 | 927 | |
paul@83 | 928 | def _cmp_positions(self, a, b): |
paul@83 | 929 | |
paul@83 | 930 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 931 | |
paul@83 | 932 | name_a, list_a = a |
paul@83 | 933 | name_b, list_b = b |
paul@83 | 934 | if len(list_a) < len(list_b): |
paul@83 | 935 | return -1 |
paul@83 | 936 | elif len(list_a) > len(list_b): |
paul@83 | 937 | return 1 |
paul@83 | 938 | elif not list_a: |
paul@83 | 939 | return 0 |
paul@83 | 940 | else: |
paul@83 | 941 | return cmp(min(list_a), min(list_b)) |
paul@83 | 942 | |
paul@83 | 943 | def all_attribute_names(self): |
paul@83 | 944 | |
paul@83 | 945 | """ |
paul@83 | 946 | Return the names of all attributes provided by instances of this class. |
paul@83 | 947 | """ |
paul@83 | 948 | |
paul@83 | 949 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 950 | return self.allattr_names |
paul@83 | 951 | |
paul@83 | 952 | def all_attributes(self): |
paul@83 | 953 | |
paul@83 | 954 | """ |
paul@83 | 955 | Return all attributes for an instance, indicating either the class which |
paul@83 | 956 | provides them or that the instance itself provides them. |
paul@394 | 957 | |
paul@401 | 958 | Note that __class__ acts like a class attribute for both instances and |
paul@401 | 959 | classes, and must be able to convey distinct values. |
paul@83 | 960 | """ |
paul@83 | 961 | |
paul@83 | 962 | if self.allattr is None: |
paul@83 | 963 | self.allattr = {} |
paul@83 | 964 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 965 | for name, attr in self.instance_attributes().items(): |
paul@394 | 966 | if self.allattr.has_key(name) and name != "__class__": |
paul@507 | 967 | print >>sys.stderr, "Warning: instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 968 | self.allattr[name] = attr |
paul@83 | 969 | return self.allattr |
paul@83 | 970 | |
paul@85 | 971 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 972 | |
paul@83 | 973 | "An inspected function." |
paul@83 | 974 | |
paul@331 | 975 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 976 | dynamic_def=0, module=None, node=None, original_name=None): |
paul@83 | 977 | |
paul@83 | 978 | """ |
paul@83 | 979 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 980 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 981 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@351 | 982 | presence of a ** parameter), optional 'dynamic_def' (indicating that the |
paul@351 | 983 | function must be handled dynamically), optional 'module', and optional |
paul@351 | 984 | AST 'node'. |
paul@83 | 985 | """ |
paul@83 | 986 | |
paul@180 | 987 | NamespaceDict.__init__(self, module) |
paul@351 | 988 | |
paul@351 | 989 | if name is None: |
paul@351 | 990 | self.name = "lambda#%d" % new_lambda() |
paul@591 | 991 | self._is_lambda = True |
paul@351 | 992 | else: |
paul@351 | 993 | self.name = name |
paul@591 | 994 | self._is_lambda = False |
paul@351 | 995 | |
paul@665 | 996 | self.original_name = original_name or name |
paul@665 | 997 | |
paul@83 | 998 | self.parent = parent |
paul@83 | 999 | self.argnames = argnames |
paul@83 | 1000 | self.defaults = defaults |
paul@83 | 1001 | self.has_star = has_star |
paul@83 | 1002 | self.has_dstar = has_dstar |
paul@331 | 1003 | self.dynamic_def = dynamic_def |
paul@91 | 1004 | self.astnode = node |
paul@83 | 1005 | |
paul@83 | 1006 | # Initialise the positional names. |
paul@83 | 1007 | |
paul@83 | 1008 | self.positional_names = self.argnames[:] |
paul@83 | 1009 | if has_dstar: |
paul@83 | 1010 | self.dstar_name = self.positional_names[-1] |
paul@83 | 1011 | del self.positional_names[-1] |
paul@83 | 1012 | if has_star: |
paul@83 | 1013 | self.star_name = self.positional_names[-1] |
paul@83 | 1014 | del self.positional_names[-1] |
paul@83 | 1015 | |
paul@83 | 1016 | # Initialise default storage. |
paul@83 | 1017 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 1018 | # NOTE: visiting. |
paul@83 | 1019 | |
paul@83 | 1020 | self.default_attrs = [] |
paul@83 | 1021 | |
paul@272 | 1022 | # Initialise attribute usage. |
paul@272 | 1023 | |
paul@358 | 1024 | if node is not None: |
paul@358 | 1025 | for arg in argnames: |
paul@279 | 1026 | |
paul@358 | 1027 | # Define attribute users. |
paul@279 | 1028 | |
paul@279 | 1029 | self._define_attribute_user_for_name(node, arg) |
paul@279 | 1030 | |
paul@83 | 1031 | # Caches. |
paul@83 | 1032 | |
paul@83 | 1033 | self.localnames = None # cache for locals |
paul@83 | 1034 | |
paul@83 | 1035 | # Add parameters to the namespace. |
paul@83 | 1036 | |
paul@83 | 1037 | self._add_parameters(argnames) |
paul@83 | 1038 | |
paul@83 | 1039 | # Image generation details. |
paul@83 | 1040 | |
paul@330 | 1041 | self.dynamic = None |
paul@83 | 1042 | |
paul@83 | 1043 | # Program-related details. |
paul@83 | 1044 | |
paul@173 | 1045 | self.blocks = None |
paul@225 | 1046 | self.body_block = None |
paul@225 | 1047 | |
paul@116 | 1048 | self.temp_usage = 0 |
paul@116 | 1049 | self.local_usage = 0 |
paul@133 | 1050 | self.all_local_usage = 0 |
paul@83 | 1051 | |
paul@83 | 1052 | def _add_parameters(self, argnames): |
paul@368 | 1053 | |
paul@368 | 1054 | "Add 'argnames' to the namespace." |
paul@368 | 1055 | |
paul@368 | 1056 | for name in argnames: |
paul@448 | 1057 | self.set(name, make_instance()) |
paul@368 | 1058 | |
paul@368 | 1059 | for name, top_level in self._flattened_parameters(argnames): |
paul@368 | 1060 | if not top_level: |
paul@448 | 1061 | self.set(name, make_instance()) |
paul@368 | 1062 | |
paul@368 | 1063 | def _flattened_parameters(self, argnames, top_level=1): |
paul@368 | 1064 | l = [] |
paul@83 | 1065 | for name in argnames: |
paul@83 | 1066 | if isinstance(name, tuple): |
paul@368 | 1067 | l += self._flattened_parameters(name, 0) |
paul@83 | 1068 | else: |
paul@368 | 1069 | l.append((name, top_level)) |
paul@368 | 1070 | return l |
paul@83 | 1071 | |
paul@83 | 1072 | def __repr__(self): |
paul@667 | 1073 | return "<function %s>" % shortrepr(self) |
paul@83 | 1074 | |
paul@87 | 1075 | def __shortrepr__(self): |
paul@494 | 1076 | return "%s.%s(%s)" % (shortrepr(self.parent), self.name, ", ".join(self.argnames)) |
paul@87 | 1077 | |
paul@227 | 1078 | def get_body_block(self): |
paul@227 | 1079 | return self.body_block |
paul@215 | 1080 | |
paul@351 | 1081 | def is_lambda(self): |
paul@351 | 1082 | return self._is_lambda |
paul@351 | 1083 | |
paul@330 | 1084 | # Defaults-related methods. |
paul@189 | 1085 | |
paul@329 | 1086 | def store_default(self, attr_or_value): |
paul@329 | 1087 | |
paul@329 | 1088 | """ |
paul@329 | 1089 | Reserve space for defaults, set outside the function, potentially on a |
paul@329 | 1090 | dynamic basis, using the 'attr_or_value'. |
paul@329 | 1091 | """ |
paul@329 | 1092 | |
paul@192 | 1093 | attr = Attr(None, self, None) |
paul@329 | 1094 | self._set_using_attr(attr, attr_or_value) |
paul@83 | 1095 | self.default_attrs.append(attr) |
paul@83 | 1096 | |
paul@330 | 1097 | def make_dynamic(self): |
paul@330 | 1098 | |
paul@330 | 1099 | "Return whether this function must be handled using a dynamic object." |
paul@330 | 1100 | |
paul@330 | 1101 | if self.dynamic is None: |
paul@330 | 1102 | for attr in self.default_attrs: |
paul@331 | 1103 | if not attr.is_strict_constant() and self.dynamic_def: |
paul@591 | 1104 | self.dynamic = True |
paul@330 | 1105 | self._make_dynamic() |
paul@330 | 1106 | break |
paul@330 | 1107 | else: |
paul@591 | 1108 | self.dynamic = False |
paul@330 | 1109 | |
paul@330 | 1110 | return self.dynamic |
paul@330 | 1111 | |
paul@330 | 1112 | is_dynamic = make_dynamic |
paul@330 | 1113 | |
paul@330 | 1114 | def _make_dynamic(self): |
paul@330 | 1115 | |
paul@330 | 1116 | "Where functions have dynamic defaults, add a context argument." |
paul@330 | 1117 | |
paul@330 | 1118 | name = "<context>" |
paul@330 | 1119 | self.argnames.insert(0, name) |
paul@330 | 1120 | self.positional_names.insert(0, name) |
paul@448 | 1121 | self.set(name, make_instance()) |
paul@330 | 1122 | |
paul@330 | 1123 | # Namespace-related methods. |
paul@330 | 1124 | |
paul@83 | 1125 | def make_global(self, name): |
paul@339 | 1126 | |
paul@339 | 1127 | "Declare 'name' as a global in the current namespace." |
paul@339 | 1128 | |
paul@83 | 1129 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 1130 | self.globals.add(name) |
paul@591 | 1131 | return True |
paul@83 | 1132 | else: |
paul@591 | 1133 | return False |
paul@83 | 1134 | |
paul@83 | 1135 | def parameters(self): |
paul@83 | 1136 | |
paul@83 | 1137 | """ |
paul@83 | 1138 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 1139 | parameter list. |
paul@83 | 1140 | """ |
paul@83 | 1141 | |
paul@83 | 1142 | parameters = {} |
paul@83 | 1143 | for i, name in enumerate(self.argnames): |
paul@83 | 1144 | parameters[name] = i |
paul@83 | 1145 | return parameters |
paul@83 | 1146 | |
paul@368 | 1147 | def tuple_parameters(self, argnames=None): |
paul@368 | 1148 | |
paul@368 | 1149 | """ |
paul@368 | 1150 | Return a list of (position, parameter) entries corresponding to tuple |
paul@368 | 1151 | parameters, where each parameter may either be a string or another such |
paul@368 | 1152 | list of entries. |
paul@368 | 1153 | """ |
paul@368 | 1154 | |
paul@368 | 1155 | names = argnames or self.argnames |
paul@368 | 1156 | |
paul@368 | 1157 | l = [] |
paul@368 | 1158 | for i, name in enumerate(names): |
paul@368 | 1159 | if isinstance(name, tuple): |
paul@368 | 1160 | l.append((i, self.tuple_parameters(name))) |
paul@368 | 1161 | elif argnames: |
paul@368 | 1162 | l.append((i, name)) |
paul@368 | 1163 | return l |
paul@368 | 1164 | |
paul@83 | 1165 | def all_locals(self): |
paul@83 | 1166 | |
paul@83 | 1167 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 1168 | |
paul@83 | 1169 | return dict(self) |
paul@83 | 1170 | |
paul@83 | 1171 | def locals(self): |
paul@83 | 1172 | |
paul@83 | 1173 | "Return a dictionary mapping names to local details." |
paul@83 | 1174 | |
paul@83 | 1175 | if self.localnames is None: |
paul@83 | 1176 | self.localnames = {} |
paul@83 | 1177 | self.localnames.update(self.all_locals()) |
paul@83 | 1178 | for name in self.argnames: |
paul@83 | 1179 | del self.localnames[name] |
paul@83 | 1180 | return self.localnames |
paul@83 | 1181 | |
paul@83 | 1182 | def is_method(self): |
paul@83 | 1183 | |
paul@241 | 1184 | """ |
paul@241 | 1185 | Return whether this function is a method explicitly defined in a class. |
paul@241 | 1186 | """ |
paul@83 | 1187 | |
paul@83 | 1188 | return isinstance(self.parent, Class) |
paul@83 | 1189 | |
paul@83 | 1190 | def is_relocated(self, name): |
paul@83 | 1191 | |
paul@83 | 1192 | """ |
paul@83 | 1193 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 1194 | having this function as a method. |
paul@83 | 1195 | """ |
paul@83 | 1196 | |
paul@83 | 1197 | for cls in self.parent.descendants: |
paul@83 | 1198 | if name in cls.relocated: |
paul@591 | 1199 | return True |
paul@591 | 1200 | return False |
paul@83 | 1201 | |
paul@351 | 1202 | # Administrative methods. |
paul@351 | 1203 | |
paul@351 | 1204 | def items_for_vacuum(self): |
paul@351 | 1205 | return self.lambdas.items() |
paul@351 | 1206 | |
paul@351 | 1207 | def vacuum_item(self, name): |
paul@351 | 1208 | del self.lambdas[name] |
paul@591 | 1209 | return True |
paul@351 | 1210 | |
paul@287 | 1211 | def finalise_attributes(self): |
paul@83 | 1212 | |
paul@83 | 1213 | """ |
paul@83 | 1214 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 1215 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 1216 | Defaults are also finalised by this method. |
paul@83 | 1217 | """ |
paul@83 | 1218 | |
paul@210 | 1219 | if self.finalised: |
paul@210 | 1220 | return |
paul@210 | 1221 | |
paul@210 | 1222 | # Defaults. |
paul@210 | 1223 | |
paul@83 | 1224 | for i, default in enumerate(self.default_attrs): |
paul@83 | 1225 | default.position = i |
paul@83 | 1226 | |
paul@368 | 1227 | # Parameters. |
paul@210 | 1228 | |
paul@368 | 1229 | i = self._finalise_parameters() |
paul@83 | 1230 | |
paul@83 | 1231 | if i is not None: |
paul@144 | 1232 | nparams = i + 1 |
paul@83 | 1233 | else: |
paul@144 | 1234 | nparams = 0 |
paul@144 | 1235 | |
paul@368 | 1236 | # Locals (and tuple parameter names). |
paul@368 | 1237 | |
paul@144 | 1238 | i = None |
paul@144 | 1239 | for i, attr in enumerate(self.locals().values()): |
paul@144 | 1240 | attr.position = i + nparams |
paul@83 | 1241 | |
paul@144 | 1242 | if i is not None: |
paul@144 | 1243 | nothers = i + 1 |
paul@144 | 1244 | else: |
paul@144 | 1245 | nothers = 0 |
paul@83 | 1246 | |
paul@144 | 1247 | self.local_usage = nothers |
paul@144 | 1248 | self.all_local_usage = nparams + nothers |
paul@591 | 1249 | self.finalised = True |
paul@83 | 1250 | |
paul@368 | 1251 | def _finalise_parameters(self): |
paul@368 | 1252 | if not self.argnames: |
paul@368 | 1253 | return None |
paul@368 | 1254 | |
paul@368 | 1255 | for i, name in enumerate(self.argnames): |
paul@368 | 1256 | self[name].position = i |
paul@368 | 1257 | |
paul@368 | 1258 | return i |
paul@368 | 1259 | |
paul@212 | 1260 | def as_instantiator(self): |
paul@83 | 1261 | |
paul@230 | 1262 | "Make an instantiator function from a method, keeping all arguments." |
paul@83 | 1263 | |
paul@230 | 1264 | function = Function(self.parent.name, self.parent.parent, self.argnames, self.defaults, |
paul@358 | 1265 | self.has_star, self.has_dstar, self.dynamic_def, self.module) |
paul@83 | 1266 | function.default_attrs = self.default_attrs |
paul@83 | 1267 | return function |
paul@83 | 1268 | |
paul@85 | 1269 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 1270 | |
paul@83 | 1271 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 1272 | |
paul@180 | 1273 | def __init__(self, name, parent_name, module=None): |
paul@180 | 1274 | NamespaceDict.__init__(self, module) |
paul@83 | 1275 | self.name = name |
paul@83 | 1276 | self.parent_name = parent_name |
paul@88 | 1277 | self.parent = None |
paul@167 | 1278 | |
paul@167 | 1279 | self.descendants = set() |
paul@167 | 1280 | |
paul@167 | 1281 | def add_descendant(self, cls): |
paul@167 | 1282 | self.descendants.add(cls) |
paul@83 | 1283 | |
paul@363 | 1284 | def all_attributes(self): |
paul@83 | 1285 | return {} |
paul@83 | 1286 | |
paul@363 | 1287 | def all_attribute_names(self): |
paul@363 | 1288 | return [] |
paul@363 | 1289 | |
paul@363 | 1290 | all_class_attributes = class_attributes = instance_attributes = all_attributes |
paul@363 | 1291 | all_class_attribute_names = class_attribute_names = instance_attribute_names = all_attribute_names |
paul@83 | 1292 | |
paul@83 | 1293 | def __repr__(self): |
paul@494 | 1294 | return "<unknown %s>" % shortrepr(self) |
paul@494 | 1295 | |
paul@494 | 1296 | def __shortrepr__(self): |
paul@607 | 1297 | if self.name is not None: |
paul@607 | 1298 | return "%s.%s" % (self.parent_name, self.name) |
paul@607 | 1299 | else: |
paul@607 | 1300 | return self.parent_name |
paul@87 | 1301 | |
paul@83 | 1302 | def full_name(self): |
paul@83 | 1303 | if self.name is not None: |
paul@83 | 1304 | return self.parent_name + "." + self.name |
paul@83 | 1305 | else: |
paul@83 | 1306 | return self.parent_name |
paul@83 | 1307 | |
paul@85 | 1308 | class Module(NamespaceDict, Constant): |
paul@83 | 1309 | |
paul@83 | 1310 | "An inspected module's core details." |
paul@83 | 1311 | |
paul@343 | 1312 | def __init__(self, name, importer): |
paul@83 | 1313 | NamespaceDict.__init__(self, self) |
paul@83 | 1314 | self.name = name |
paul@343 | 1315 | self.importer = importer |
paul@88 | 1316 | self.parent = None |
paul@83 | 1317 | |
paul@83 | 1318 | # Original location details. |
paul@83 | 1319 | |
paul@91 | 1320 | self.astnode = None |
paul@83 | 1321 | |
paul@83 | 1322 | # Complete lists of classes and functions. |
paul@83 | 1323 | |
paul@83 | 1324 | self.all_objects = set() |
paul@83 | 1325 | |
paul@581 | 1326 | # Whether the module is imported in a circular fashion, exposing the |
paul@581 | 1327 | # unfinished namespace to external modification. |
paul@581 | 1328 | |
paul@581 | 1329 | self.circular_import = self in importer.circular_imports |
paul@581 | 1330 | |
paul@576 | 1331 | # A set of global names that cannot support combined attribute usage |
paul@576 | 1332 | # observations because they may be modified within functions during |
paul@576 | 1333 | # initialisation. |
paul@576 | 1334 | |
paul@576 | 1335 | self.modified_names = set() |
paul@575 | 1336 | |
paul@83 | 1337 | # Keyword records. |
paul@83 | 1338 | |
paul@83 | 1339 | self.keyword_names = set() |
paul@83 | 1340 | |
paul@83 | 1341 | # Program-related details. |
paul@83 | 1342 | |
paul@173 | 1343 | self.blocks = None |
paul@116 | 1344 | self.temp_usage = 0 |
paul@116 | 1345 | self.local_usage = 0 |
paul@133 | 1346 | self.all_local_usage = 0 |
paul@83 | 1347 | |
paul@83 | 1348 | def full_name(self): |
paul@83 | 1349 | return self.name |
paul@83 | 1350 | |
paul@83 | 1351 | def __repr__(self): |
paul@667 | 1352 | return "<module %s>" % shortrepr(self) |
paul@83 | 1353 | |
paul@87 | 1354 | def __shortrepr__(self): |
paul@494 | 1355 | return self.name |
paul@87 | 1356 | |
paul@83 | 1357 | # Attribute methods. |
paul@83 | 1358 | |
paul@83 | 1359 | "Return the module attribute names provided by the module." |
paul@83 | 1360 | |
paul@83 | 1361 | module_attribute_names = NamespaceDict.keys |
paul@83 | 1362 | |
paul@83 | 1363 | def module_attributes(self): |
paul@83 | 1364 | |
paul@83 | 1365 | "Return a dictionary mapping names to module attributes." |
paul@83 | 1366 | |
paul@83 | 1367 | return dict(self) |
paul@83 | 1368 | |
paul@612 | 1369 | all_attributes = module_attributes |
paul@612 | 1370 | |
paul@609 | 1371 | def get_static_attribute(self, name): |
paul@609 | 1372 | |
paul@609 | 1373 | """ |
paul@609 | 1374 | Return a static attribute for the given 'name' or None if no such |
paul@609 | 1375 | attribute exists. |
paul@609 | 1376 | """ |
paul@609 | 1377 | |
paul@609 | 1378 | return self.get(name) |
paul@609 | 1379 | |
paul@578 | 1380 | def modify_name(self, name): |
paul@578 | 1381 | |
paul@578 | 1382 | """ |
paul@578 | 1383 | Modify a global 'name' invalidating various assumptions about its |
paul@578 | 1384 | behaviour based on the module namespace being "safe" and suitable for |
paul@578 | 1385 | attribute usage and constant value observations. |
paul@578 | 1386 | """ |
paul@578 | 1387 | |
paul@578 | 1388 | self.modified_names.add(name) |
paul@578 | 1389 | |
paul@578 | 1390 | # Establish an attribute directly in the namespace if not present. |
paul@578 | 1391 | |
paul@578 | 1392 | if not self.namespace.has_key(name): |
paul@578 | 1393 | self.namespace[name] = Attr(None, self, name) |
paul@578 | 1394 | |
paul@581 | 1395 | # Indicate that there is at least one assignment to the name, although |
paul@581 | 1396 | # no actual values are recorded. |
paul@581 | 1397 | |
paul@578 | 1398 | attr = self.namespace[name] |
paul@578 | 1399 | attr.update_assignments(1, False) |
paul@578 | 1400 | |
paul@581 | 1401 | def set(self, name, value, single_assignment=1): |
paul@581 | 1402 | |
paul@581 | 1403 | """ |
paul@581 | 1404 | Set the module attribute with the given 'name', having the given 'value' |
paul@581 | 1405 | that is assigned once if 'single_assignment' is omitted to given as a |
paul@581 | 1406 | true value. |
paul@581 | 1407 | |
paul@581 | 1408 | Where the module is imported before it is completely initialised, all |
paul@581 | 1409 | assignments will be regarded as multiple assignments since it is |
paul@581 | 1410 | possible that an external assignment to the name may occur. |
paul@581 | 1411 | """ |
paul@581 | 1412 | |
paul@581 | 1413 | NamespaceDict.set(self, name, value, not self.circular_import and single_assignment) |
paul@581 | 1414 | |
paul@575 | 1415 | # Attribute usage methods that apply to module globals in certain |
paul@575 | 1416 | # circumstances. |
paul@575 | 1417 | |
paul@634 | 1418 | def can_use_name_for_usage(self, name): |
paul@634 | 1419 | return name not in self.modified_names and not self.circular_import |
paul@634 | 1420 | |
paul@576 | 1421 | def _use_attribute(self, name, attrname, value=None): |
paul@634 | 1422 | if self.can_use_name_for_usage(name): |
paul@576 | 1423 | return NamespaceDict._use_attribute(self, name, attrname, value) |
paul@576 | 1424 | else: |
paul@577 | 1425 | self.importer.use_name(attrname, self.full_name(), value) |
paul@576 | 1426 | return [] |
paul@576 | 1427 | |
paul@576 | 1428 | def _define_attribute_user_for_name(self, node, name): |
paul@634 | 1429 | if self.can_use_name_for_usage(name): |
paul@576 | 1430 | NamespaceDict._define_attribute_user_for_name(self, node, name) |
paul@576 | 1431 | |
paul@576 | 1432 | def _init_attribute_user_for_name(self, node, name): |
paul@634 | 1433 | if self.can_use_name_for_usage(name): |
paul@576 | 1434 | NamespaceDict._init_attribute_user_for_name(self, node, name) |
paul@576 | 1435 | |
paul@576 | 1436 | def _define_attribute_accessor(self, name, attrname, node, value): |
paul@634 | 1437 | if self.can_use_name_for_usage(name): |
paul@576 | 1438 | NamespaceDict._define_attribute_accessor(self, name, attrname, node, value) |
paul@577 | 1439 | else: |
paul@577 | 1440 | self.importer.use_name(attrname, self.full_name(), value) |
paul@575 | 1441 | |
paul@617 | 1442 | # Pre-made class instances. |
paul@617 | 1443 | # For each of these, their details will be filled in later. |
paul@617 | 1444 | |
paul@617 | 1445 | premade = { |
paul@617 | 1446 | "bool" : Class("bool"), |
paul@617 | 1447 | "float" : Class("float"), |
paul@617 | 1448 | "int" : Class("int"), |
paul@617 | 1449 | "long" : Class("long"), |
paul@617 | 1450 | "str" : Class("str"), |
paul@617 | 1451 | "unicode" : Class("unicode"), |
paul@617 | 1452 | "type" : Class("type"), |
paul@617 | 1453 | } |
paul@394 | 1454 | |
paul@618 | 1455 | def get_constant_class(name): |
paul@618 | 1456 | return premade[name] |
paul@618 | 1457 | |
paul@394 | 1458 | # Class construction. |
paul@394 | 1459 | |
paul@394 | 1460 | def get_class(name, parent, module, node): |
paul@394 | 1461 | |
paul@394 | 1462 | """ |
paul@394 | 1463 | Return a Class instance for the class with the given 'name', 'parent', |
paul@394 | 1464 | 'module' and 'node'. |
paul@394 | 1465 | """ |
paul@394 | 1466 | |
paul@617 | 1467 | if premade.has_key(name) and module.full_name() == "__builtins__": |
paul@617 | 1468 | cls = premade[name] |
paul@617 | 1469 | cls.set_context(parent, module, node) |
paul@394 | 1470 | else: |
paul@635 | 1471 | # Where names are reused in a namespace, differentiate between classes |
paul@635 | 1472 | # using a name index. |
paul@635 | 1473 | |
paul@665 | 1474 | original_name = name |
paul@665 | 1475 | |
paul@635 | 1476 | if parent.has_key(name): |
paul@635 | 1477 | name = "%s#%d" % (name, parent[name].assignments + 1) |
paul@635 | 1478 | |
paul@665 | 1479 | cls = Class(name, parent, module, node, original_name) |
paul@635 | 1480 | |
paul@635 | 1481 | # Add a reference for the class's "shadow" name. |
paul@635 | 1482 | |
paul@635 | 1483 | parent.use_specific_attribute(parent.full_name(), name) |
paul@635 | 1484 | |
paul@635 | 1485 | return cls |
paul@394 | 1486 | |
paul@665 | 1487 | # Function construction. |
paul@665 | 1488 | |
paul@665 | 1489 | def get_function(name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 1490 | dynamic_def=0, module=None, node=None): |
paul@665 | 1491 | |
paul@665 | 1492 | """ |
paul@665 | 1493 | Return a Function instance for the class with the given 'name', 'parent', |
paul@665 | 1494 | and other details. |
paul@665 | 1495 | """ |
paul@665 | 1496 | |
paul@665 | 1497 | original_name = name |
paul@665 | 1498 | |
paul@665 | 1499 | if parent.has_key(name): |
paul@665 | 1500 | name = "%s#%d" % (name, parent[name].assignments + 1) |
paul@665 | 1501 | |
paul@665 | 1502 | fn = Function(name, parent, argnames, defaults, has_star, has_dstar, |
paul@665 | 1503 | dynamic_def, module, node, original_name) |
paul@665 | 1504 | |
paul@665 | 1505 | # Add a reference for the function's "shadow" name. |
paul@665 | 1506 | |
paul@665 | 1507 | if name: |
paul@665 | 1508 | parent.use_specific_attribute(parent.full_name(), name) |
paul@665 | 1509 | |
paul@665 | 1510 | return fn |
paul@665 | 1511 | |
paul@554 | 1512 | # Lambda sequence numbering. |
paul@554 | 1513 | |
paul@554 | 1514 | lambda_index = 0 |
paul@554 | 1515 | |
paul@554 | 1516 | def new_lambda(): |
paul@554 | 1517 | |
paul@554 | 1518 | "Return a new sequence number for a lambda definition." |
paul@554 | 1519 | |
paul@554 | 1520 | global lambda_index |
paul@554 | 1521 | lambda_index += 1 |
paul@554 | 1522 | return lambda_index |
paul@554 | 1523 | |
paul@555 | 1524 | # Special representations. |
paul@555 | 1525 | |
paul@555 | 1526 | class AtLeast: |
paul@555 | 1527 | |
paul@555 | 1528 | "A special representation for numbers of a given value or greater." |
paul@555 | 1529 | |
paul@555 | 1530 | def __init__(self, count): |
paul@555 | 1531 | self.count = count |
paul@555 | 1532 | |
paul@555 | 1533 | def __eq__(self, other): |
paul@591 | 1534 | return False |
paul@555 | 1535 | |
paul@555 | 1536 | __lt__ = __le__ = __eq__ |
paul@555 | 1537 | |
paul@555 | 1538 | def __ne__(self, other): |
paul@591 | 1539 | return True |
paul@555 | 1540 | |
paul@555 | 1541 | def __gt__(self, other): |
paul@555 | 1542 | if isinstance(other, AtLeast): |
paul@591 | 1543 | return False |
paul@555 | 1544 | else: |
paul@555 | 1545 | return self.count > other |
paul@555 | 1546 | |
paul@555 | 1547 | def __ge__(self, other): |
paul@555 | 1548 | if isinstance(other, AtLeast): |
paul@591 | 1549 | return False |
paul@555 | 1550 | else: |
paul@555 | 1551 | return self.count >= other |
paul@555 | 1552 | |
paul@555 | 1553 | def __iadd__(self, other): |
paul@555 | 1554 | if isinstance(other, AtLeast): |
paul@555 | 1555 | self.count += other.count |
paul@555 | 1556 | else: |
paul@555 | 1557 | self.count += other |
paul@555 | 1558 | return self |
paul@555 | 1559 | |
paul@555 | 1560 | def __radd__(self, other): |
paul@555 | 1561 | if isinstance(other, AtLeast): |
paul@555 | 1562 | return AtLeast(self.count + other.count) |
paul@555 | 1563 | else: |
paul@555 | 1564 | return AtLeast(self.count + other) |
paul@555 | 1565 | |
paul@555 | 1566 | def __repr__(self): |
paul@555 | 1567 | return "AtLeast(%r)" % self.count |
paul@555 | 1568 | |
paul@83 | 1569 | # vim: tabstop=4 expandtab shiftwidth=4 |