paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@31 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@20 | 20 | |
paul@20 | 21 | -------- |
paul@20 | 22 | |
paul@20 | 23 | The central classes in this module are the following: |
paul@20 | 24 | |
paul@20 | 25 | * Class |
paul@20 | 26 | * Function |
paul@20 | 27 | * Module |
paul@20 | 28 | * InspectedModule (derived from Module) |
paul@20 | 29 | |
paul@20 | 30 | All of the above support the Naming interface either explicitly or through |
paul@20 | 31 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@20 | 32 | using the method of that name. |
paul@20 | 33 | |
paul@20 | 34 | Additionally, all of the above also support a dictionary interface in order to |
paul@20 | 35 | access names within their defined scopes. Specific methods also exist in order |
paul@20 | 36 | to distinguish between certain kinds of attributes: |
paul@20 | 37 | |
paul@20 | 38 | * Class: (class|all_class|instance|all)_attributes |
paul@31 | 39 | * Function: parameters, locals, all_locals |
paul@20 | 40 | * Module: module_attributes |
paul@20 | 41 | |
paul@20 | 42 | These specific methods are useful in certain situations. |
paul@20 | 43 | |
paul@20 | 44 | The above classes also provide a 'node' attribute, indicating the AST node where |
paul@20 | 45 | each such object is defined. |
paul@0 | 46 | """ |
paul@0 | 47 | |
paul@39 | 48 | from micropython.common import * |
paul@0 | 49 | import compiler.ast |
paul@0 | 50 | from compiler.visitor import ASTVisitor |
paul@0 | 51 | try: |
paul@0 | 52 | set |
paul@0 | 53 | except NameError: |
paul@0 | 54 | from sets import Set as set |
paul@0 | 55 | |
paul@39 | 56 | class InspectError(ProcessingError): pass |
paul@0 | 57 | |
paul@31 | 58 | class AtLeast: |
paul@31 | 59 | |
paul@31 | 60 | "A special representation for numbers of a given value or greater." |
paul@31 | 61 | |
paul@31 | 62 | def __init__(self, count): |
paul@31 | 63 | self.count = count |
paul@31 | 64 | |
paul@31 | 65 | def __eq__(self, other): |
paul@31 | 66 | return 0 |
paul@31 | 67 | |
paul@31 | 68 | __lt__ = __le__ = __eq__ |
paul@31 | 69 | |
paul@31 | 70 | def __ne__(self, other): |
paul@31 | 71 | return 1 |
paul@31 | 72 | |
paul@31 | 73 | def __gt__(self, other): |
paul@31 | 74 | if isinstance(other, AtLeast): |
paul@31 | 75 | return 0 |
paul@31 | 76 | else: |
paul@31 | 77 | return self.count > other |
paul@31 | 78 | |
paul@31 | 79 | def __ge__(self, other): |
paul@31 | 80 | if isinstance(other, AtLeast): |
paul@31 | 81 | return 0 |
paul@31 | 82 | else: |
paul@31 | 83 | return self.count >= other |
paul@31 | 84 | |
paul@31 | 85 | def __iadd__(self, other): |
paul@31 | 86 | if isinstance(other, AtLeast): |
paul@31 | 87 | self.count += other.count |
paul@31 | 88 | else: |
paul@31 | 89 | self.count += other |
paul@31 | 90 | return self |
paul@31 | 91 | |
paul@31 | 92 | def __radd__(self, other): |
paul@31 | 93 | if isinstance(other, AtLeast): |
paul@31 | 94 | return AtLeast(self.count + other.count) |
paul@31 | 95 | else: |
paul@31 | 96 | return AtLeast(self.count + other) |
paul@31 | 97 | |
paul@31 | 98 | def __repr__(self): |
paul@31 | 99 | return "AtLeast(%r)" % self.count |
paul@31 | 100 | |
paul@12 | 101 | # Mix-ins and abstract classes. |
paul@12 | 102 | |
paul@3 | 103 | class NamespaceDict: |
paul@3 | 104 | |
paul@3 | 105 | "A mix-in providing dictionary methods." |
paul@3 | 106 | |
paul@24 | 107 | def __init__(self, global_namespace=None): |
paul@12 | 108 | self.namespace = {} |
paul@12 | 109 | self.globals = set() |
paul@24 | 110 | self.global_namespace = global_namespace |
paul@23 | 111 | |
paul@3 | 112 | def __getitem__(self, name): |
paul@3 | 113 | return self.namespace[name] |
paul@3 | 114 | |
paul@15 | 115 | def get(self, name, default=None): |
paul@15 | 116 | return self.namespace.get(name, default) |
paul@15 | 117 | |
paul@12 | 118 | def __setitem__(self, name, value): |
paul@37 | 119 | self.set(name, value) |
paul@31 | 120 | |
paul@34 | 121 | def set(self, name, value, single_assignment=1): |
paul@47 | 122 | |
paul@47 | 123 | """ |
paul@47 | 124 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@47 | 125 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@47 | 126 | this operation (or whether the operation covers potentially many |
paul@47 | 127 | assignments in the lifetime of a program). |
paul@47 | 128 | """ |
paul@47 | 129 | |
paul@37 | 130 | if name in self.globals: |
paul@37 | 131 | self.global_namespace.set(name, value, 0) |
paul@37 | 132 | else: |
paul@37 | 133 | attr = self._set(name, value) |
paul@12 | 134 | |
paul@37 | 135 | # NOTE: Insist on assignments with known values. |
paul@23 | 136 | |
paul@37 | 137 | if value is not None: |
paul@37 | 138 | attr.update(value, single_assignment) |
paul@32 | 139 | |
paul@32 | 140 | def set_module(self, name, value): |
paul@47 | 141 | |
paul@47 | 142 | """ |
paul@47 | 143 | A specialised set operation, making 'name' refer to 'value' in the |
paul@47 | 144 | context of making a module reference available in association with |
paul@47 | 145 | 'name' as part of the import of that module or a submodule of that |
paul@47 | 146 | module. |
paul@47 | 147 | """ |
paul@47 | 148 | |
paul@36 | 149 | attr = self._set(name, value) |
paul@36 | 150 | if attr.assignments is None: |
paul@36 | 151 | attr.assignments = 1 |
paul@36 | 152 | attr.assignment_values.add(value) |
paul@36 | 153 | |
paul@53 | 154 | "A specialised set operation for parameters." |
paul@53 | 155 | |
paul@53 | 156 | set_parameter = set_module |
paul@53 | 157 | |
paul@36 | 158 | def _set(self, name, value): |
paul@47 | 159 | |
paul@47 | 160 | "The underlying set operation associating 'name' with 'value'." |
paul@47 | 161 | |
paul@36 | 162 | if not self.namespace.has_key(name): |
paul@49 | 163 | self.namespace[name] = Attr(None, self, name, value) |
paul@36 | 164 | return self.namespace[name] |
paul@23 | 165 | |
paul@12 | 166 | def __delitem__(self, name): |
paul@12 | 167 | del self.namespace[name] |
paul@12 | 168 | |
paul@13 | 169 | def has_key(self, name): |
paul@13 | 170 | return self.namespace.has_key(name) |
paul@13 | 171 | |
paul@3 | 172 | def keys(self): |
paul@3 | 173 | return self.namespace.keys() |
paul@3 | 174 | |
paul@7 | 175 | def values(self): |
paul@7 | 176 | return self.namespace.values() |
paul@7 | 177 | |
paul@7 | 178 | def items(self): |
paul@7 | 179 | return self.namespace.items() |
paul@7 | 180 | |
paul@12 | 181 | def make_global(self, name): |
paul@12 | 182 | if not self.namespace.has_key(name): |
paul@12 | 183 | self.globals.add(name) |
paul@12 | 184 | else: |
paul@43 | 185 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@12 | 186 | |
paul@32 | 187 | def get_assignments(self, name): |
paul@32 | 188 | if self.assignments.has_key(name): |
paul@32 | 189 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@32 | 190 | else: |
paul@32 | 191 | return None |
paul@32 | 192 | |
paul@49 | 193 | def attributes_as_list(self): |
paul@49 | 194 | self.finalise_attributes() |
paul@49 | 195 | l = [None] * len(self.keys()) |
paul@49 | 196 | for attr in self.values(): |
paul@37 | 197 | l[attr.position] = attr |
paul@37 | 198 | return l |
paul@37 | 199 | |
paul@49 | 200 | def finalise_attributes(self): |
paul@49 | 201 | |
paul@49 | 202 | "Make sure all attributes are fully defined." |
paul@49 | 203 | |
paul@49 | 204 | # The default action is to assign attribute positions sequentially. |
paul@49 | 205 | |
paul@49 | 206 | for i, attr in enumerate(self.values()): |
paul@49 | 207 | attr.position = i |
paul@49 | 208 | |
paul@4 | 209 | class Naming: |
paul@4 | 210 | |
paul@4 | 211 | "A mix-in providing naming conveniences." |
paul@4 | 212 | |
paul@4 | 213 | def full_name(self): |
paul@4 | 214 | if self.name is not None: |
paul@53 | 215 | return self.parent.full_name() + "." + self.name |
paul@4 | 216 | else: |
paul@53 | 217 | return self.parent.full_name() |
paul@4 | 218 | |
paul@12 | 219 | # Program data structures. |
paul@12 | 220 | |
paul@10 | 221 | class Attr: |
paul@10 | 222 | |
paul@10 | 223 | "An attribute entry." |
paul@10 | 224 | |
paul@36 | 225 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 226 | self.position = position |
paul@10 | 227 | self.parent = parent |
paul@36 | 228 | self.name = name |
paul@10 | 229 | self.value = value |
paul@10 | 230 | |
paul@36 | 231 | # Number of assignments per name. |
paul@36 | 232 | |
paul@36 | 233 | self.assignments = assignments |
paul@36 | 234 | self.assignment_values = set() |
paul@36 | 235 | |
paul@36 | 236 | def update(self, value, single_assignment): |
paul@47 | 237 | |
paul@47 | 238 | """ |
paul@47 | 239 | Update the attribute, adding the 'value' provided to the known values |
paul@47 | 240 | associated with the attribute, changing the number of assignments |
paul@47 | 241 | according to the 'single_assignment' status of the operation, where |
paul@47 | 242 | a true value indicates that only one assignment is associated with the |
paul@47 | 243 | update, and a false value indicates that potentially many assignments |
paul@47 | 244 | may be involved. |
paul@47 | 245 | """ |
paul@47 | 246 | |
paul@36 | 247 | if self.assignments is None: |
paul@36 | 248 | if single_assignment: |
paul@36 | 249 | self.assignments = 1 |
paul@36 | 250 | else: |
paul@36 | 251 | self.assignments = AtLeast(1) |
paul@36 | 252 | else: |
paul@36 | 253 | if single_assignment: |
paul@36 | 254 | self.assignments += 1 |
paul@36 | 255 | else: |
paul@36 | 256 | self.assignments += AtLeast(1) |
paul@36 | 257 | self.assignment_values.add(value) |
paul@36 | 258 | |
paul@10 | 259 | def __repr__(self): |
paul@36 | 260 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 261 | |
paul@21 | 262 | class Const: |
paul@21 | 263 | |
paul@21 | 264 | "A constant object." |
paul@21 | 265 | |
paul@21 | 266 | def __init__(self, value): |
paul@21 | 267 | self.value = value |
paul@21 | 268 | |
paul@21 | 269 | # Image generation details. |
paul@21 | 270 | |
paul@21 | 271 | self.location = None |
paul@21 | 272 | |
paul@21 | 273 | def __repr__(self): |
paul@37 | 274 | if self.location is not None: |
paul@37 | 275 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 276 | else: |
paul@37 | 277 | return "Const(%r)" % self.value |
paul@21 | 278 | |
paul@21 | 279 | def __eq__(self, other): |
paul@21 | 280 | return self.value == other.value |
paul@21 | 281 | |
paul@21 | 282 | def __hash__(self): |
paul@21 | 283 | return hash(self.value) |
paul@21 | 284 | |
paul@4 | 285 | class Class(NamespaceDict, Naming): |
paul@0 | 286 | |
paul@0 | 287 | "An inspected class." |
paul@0 | 288 | |
paul@53 | 289 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@24 | 290 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 291 | self.name = name |
paul@53 | 292 | self.parent = parent |
paul@17 | 293 | self.node = node |
paul@12 | 294 | |
paul@53 | 295 | # Superclasses, descendants and attributes. |
paul@47 | 296 | |
paul@0 | 297 | self.bases = [] |
paul@53 | 298 | self.descendants = set() |
paul@46 | 299 | self.instattr = set() # instance attributes |
paul@49 | 300 | self.relocated = set() # attributes which do not have the same |
paul@49 | 301 | # position as those of the same name in |
paul@49 | 302 | # some superclasses |
paul@12 | 303 | |
paul@47 | 304 | # Caches. |
paul@47 | 305 | |
paul@46 | 306 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 307 | self.all_instattr_names = None # from all_instattr |
paul@46 | 308 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 309 | self.all_classattr_names = None # from all_classattr |
paul@46 | 310 | self.allattr = None # cache for all_attributes |
paul@46 | 311 | self.allattr_names = None # from allattr |
paul@9 | 312 | |
paul@9 | 313 | # Image generation details. |
paul@9 | 314 | |
paul@9 | 315 | self.location = None |
paul@19 | 316 | self.code_location = None |
paul@0 | 317 | |
paul@12 | 318 | def __repr__(self): |
paul@37 | 319 | if self.location is not None: |
paul@53 | 320 | return "Class(%r, %r, location=%r)" % (self.name, self.parent, self.location) |
paul@37 | 321 | else: |
paul@53 | 322 | return "Class(%r, %r)" % (self.name, self.parent) |
paul@12 | 323 | |
paul@49 | 324 | def finalise_attributes(self): |
paul@49 | 325 | |
paul@49 | 326 | "Make sure that all attributes are fully defined." |
paul@49 | 327 | |
paul@49 | 328 | self.finalise_class_attributes() |
paul@49 | 329 | self.finalise_instance_attributes() |
paul@49 | 330 | |
paul@49 | 331 | # Class-specific methods. |
paul@49 | 332 | |
paul@0 | 333 | def add_base(self, base): |
paul@0 | 334 | self.bases.append(base) |
paul@53 | 335 | base.add_descendant(self) |
paul@0 | 336 | |
paul@12 | 337 | def add_instance_attribute(self, name): |
paul@12 | 338 | self.instattr.add(name) |
paul@9 | 339 | |
paul@53 | 340 | def add_descendant(self, cls): |
paul@53 | 341 | self.descendants.add(cls) |
paul@53 | 342 | for base in self.bases: |
paul@53 | 343 | base.add_descendant(cls) |
paul@53 | 344 | |
paul@36 | 345 | "Return the attribute names provided by this class only." |
paul@9 | 346 | |
paul@36 | 347 | class_attribute_names = NamespaceDict.keys |
paul@4 | 348 | |
paul@4 | 349 | def class_attributes(self): |
paul@4 | 350 | |
paul@12 | 351 | "Return class attributes provided by this class only." |
paul@4 | 352 | |
paul@49 | 353 | self.finalise_class_attributes() |
paul@53 | 354 | return dict(self) |
paul@12 | 355 | |
paul@12 | 356 | def all_class_attribute_names(self): |
paul@12 | 357 | |
paul@12 | 358 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 359 | |
paul@12 | 360 | if self.all_classattr_names is None: |
paul@12 | 361 | self.all_class_attributes() |
paul@12 | 362 | return self.all_classattr_names |
paul@12 | 363 | |
paul@12 | 364 | def all_class_attributes(self): |
paul@12 | 365 | |
paul@12 | 366 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 367 | |
paul@49 | 368 | self.finalise_class_attributes() |
paul@49 | 369 | return self.all_classattr |
paul@49 | 370 | |
paul@49 | 371 | def finalise_class_attributes(self): |
paul@49 | 372 | |
paul@49 | 373 | "Make sure that the class attributes are fully defined." |
paul@49 | 374 | |
paul@12 | 375 | if self.all_classattr is None: |
paul@12 | 376 | self.all_classattr = {} |
paul@49 | 377 | clsattr = {} |
paul@49 | 378 | |
paul@49 | 379 | # Record provisional position information for attributes of this |
paul@49 | 380 | # class. |
paul@49 | 381 | |
paul@49 | 382 | for name in self.class_attributes().keys(): |
paul@49 | 383 | clsattr[name] = set() # position not yet defined |
paul@12 | 384 | |
paul@9 | 385 | reversed_bases = self.bases[:] |
paul@9 | 386 | reversed_bases.reverse() |
paul@49 | 387 | |
paul@49 | 388 | # For the bases in reverse order, acquire class attribute details. |
paul@49 | 389 | |
paul@9 | 390 | for cls in reversed_bases: |
paul@49 | 391 | for name, attr in cls.all_class_attributes().items(): |
paul@49 | 392 | self.all_classattr[name] = attr |
paul@10 | 393 | |
paul@49 | 394 | # Record previous attribute information. |
paul@49 | 395 | |
paul@49 | 396 | if clsattr.has_key(name): |
paul@49 | 397 | clsattr[name].add(attr.position) |
paul@49 | 398 | |
paul@49 | 399 | # Record class attributes provided by this class and its bases, |
paul@49 | 400 | # along with their positions. |
paul@10 | 401 | |
paul@12 | 402 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 403 | |
paul@49 | 404 | if clsattr: |
paul@49 | 405 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@49 | 406 | self.all_classattr[name].position = i |
paul@49 | 407 | |
paul@12 | 408 | return self.all_classattr |
paul@4 | 409 | |
paul@7 | 410 | def instance_attribute_names(self): |
paul@7 | 411 | |
paul@7 | 412 | "Return the instance attribute names provided by the class." |
paul@7 | 413 | |
paul@12 | 414 | if self.all_instattr_names is None: |
paul@12 | 415 | self.instance_attributes() |
paul@12 | 416 | return self.all_instattr_names |
paul@7 | 417 | |
paul@4 | 418 | def instance_attributes(self): |
paul@4 | 419 | |
paul@9 | 420 | "Return instance-only attributes for instances of this class." |
paul@9 | 421 | |
paul@49 | 422 | self.finalise_instance_attributes() |
paul@49 | 423 | return self.all_instattr |
paul@49 | 424 | |
paul@49 | 425 | def finalise_instance_attributes(self): |
paul@49 | 426 | |
paul@49 | 427 | "Make sure that the instance attributes are fully defined." |
paul@49 | 428 | |
paul@12 | 429 | if self.all_instattr is None: |
paul@12 | 430 | self.all_instattr = {} |
paul@46 | 431 | instattr = {} |
paul@12 | 432 | |
paul@49 | 433 | # Record provisional position information for attributes of this |
paul@49 | 434 | # instance. |
paul@49 | 435 | |
paul@49 | 436 | for name in self.instattr: |
paul@49 | 437 | instattr[name] = set() # position not yet defined |
paul@49 | 438 | |
paul@12 | 439 | reversed_bases = self.bases[:] |
paul@12 | 440 | reversed_bases.reverse() |
paul@46 | 441 | |
paul@46 | 442 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 443 | # details. |
paul@46 | 444 | |
paul@12 | 445 | for cls in reversed_bases: |
paul@46 | 446 | for name, attr in cls.instance_attributes().items(): |
paul@49 | 447 | |
paul@49 | 448 | # Record previous attribute information. |
paul@12 | 449 | |
paul@49 | 450 | if instattr.has_key(name): |
paul@49 | 451 | instattr[name].add(attr.position) |
paul@46 | 452 | |
paul@48 | 453 | # Cache the attributes by converting the positioned attributes into |
paul@48 | 454 | # a dictionary. |
paul@48 | 455 | |
paul@46 | 456 | if not instattr: |
paul@46 | 457 | self.all_instattr = {} |
paul@46 | 458 | else: |
paul@49 | 459 | self.all_instattr = self._get_attributes(instattr) |
paul@12 | 460 | |
paul@12 | 461 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 462 | |
paul@12 | 463 | return self.all_instattr |
paul@9 | 464 | |
paul@49 | 465 | def _get_position_list(self, positions): |
paul@48 | 466 | |
paul@48 | 467 | """ |
paul@49 | 468 | Return a list of attribute names for the given 'positions' mapping from |
paul@49 | 469 | names to positions, indicating the positions of the attributes in the |
paul@49 | 470 | final instance structure. |
paul@48 | 471 | """ |
paul@48 | 472 | |
paul@49 | 473 | position_items = positions.items() |
paul@49 | 474 | namearray = [None] * len(position_items) |
paul@48 | 475 | |
paul@48 | 476 | # Get the positions in ascending order of list size, with lists |
paul@48 | 477 | # of the same size ordered according to their smallest position |
paul@48 | 478 | # value. |
paul@48 | 479 | |
paul@49 | 480 | position_items.sort(self._cmp_positions) |
paul@48 | 481 | |
paul@48 | 482 | # Get the names in position order. |
paul@48 | 483 | |
paul@48 | 484 | held = [] |
paul@48 | 485 | |
paul@49 | 486 | for name, pos in position_items: |
paul@48 | 487 | pos = list(pos) |
paul@51 | 488 | pos.sort() |
paul@51 | 489 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@49 | 490 | namearray[pos[0]] = name |
paul@48 | 491 | else: |
paul@49 | 492 | if pos: |
paul@49 | 493 | self.relocated.add(name) |
paul@48 | 494 | held.append((name, pos)) |
paul@48 | 495 | |
paul@49 | 496 | for i, attr in enumerate(namearray): |
paul@48 | 497 | if attr is None: |
paul@48 | 498 | name, pos = held.pop() |
paul@49 | 499 | namearray[i] = name |
paul@49 | 500 | |
paul@49 | 501 | #print self.name, positions |
paul@49 | 502 | #print "->", namearray |
paul@49 | 503 | return namearray |
paul@49 | 504 | |
paul@49 | 505 | def _get_attributes(self, positions): |
paul@48 | 506 | |
paul@49 | 507 | """ |
paul@49 | 508 | For the given 'positions' mapping from names to positions, return a |
paul@49 | 509 | dictionary mapping names to Attr instances incorporating information |
paul@49 | 510 | about their positions in the final instance structure. |
paul@49 | 511 | """ |
paul@49 | 512 | |
paul@49 | 513 | d = {} |
paul@49 | 514 | for i, name in enumerate(self._get_position_list(positions)): |
paul@49 | 515 | d[name] = Attr(i, None, None, name) |
paul@49 | 516 | return d |
paul@48 | 517 | |
paul@46 | 518 | def _cmp_positions(self, a, b): |
paul@48 | 519 | |
paul@48 | 520 | "Compare name plus position list operands 'a' and 'b'." |
paul@48 | 521 | |
paul@46 | 522 | name_a, list_a = a |
paul@46 | 523 | name_b, list_b = b |
paul@46 | 524 | if len(list_a) < len(list_b): |
paul@46 | 525 | return -1 |
paul@46 | 526 | elif len(list_a) > len(list_b): |
paul@46 | 527 | return 1 |
paul@49 | 528 | elif not list_a: |
paul@49 | 529 | return 0 |
paul@46 | 530 | else: |
paul@46 | 531 | return cmp(min(list_a), min(list_b)) |
paul@46 | 532 | |
paul@9 | 533 | def all_attribute_names(self): |
paul@9 | 534 | |
paul@9 | 535 | """ |
paul@9 | 536 | Return the names of all attributes provided by instances of this class. |
paul@9 | 537 | """ |
paul@9 | 538 | |
paul@9 | 539 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 540 | return self.allattr_names |
paul@9 | 541 | |
paul@9 | 542 | def all_attributes(self): |
paul@9 | 543 | |
paul@4 | 544 | """ |
paul@4 | 545 | Return all attributes for an instance, indicating either the class which |
paul@4 | 546 | provides them or that the instance itself provides them. |
paul@4 | 547 | """ |
paul@4 | 548 | |
paul@9 | 549 | if self.allattr is None: |
paul@9 | 550 | self.allattr = {} |
paul@12 | 551 | self.allattr.update(self.all_class_attributes()) |
paul@47 | 552 | for name, attr in self.instance_attributes().items(): |
paul@9 | 553 | if self.allattr.has_key(name): |
paul@9 | 554 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@47 | 555 | self.allattr[name] = attr |
paul@9 | 556 | return self.allattr |
paul@4 | 557 | |
paul@12 | 558 | class Function(NamespaceDict, Naming): |
paul@0 | 559 | |
paul@0 | 560 | "An inspected function." |
paul@0 | 561 | |
paul@53 | 562 | def __init__(self, name, parent, argnames, has_star, has_dstar, global_namespace=None, node=None): |
paul@24 | 563 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 564 | self.name = name |
paul@53 | 565 | self.parent = parent |
paul@3 | 566 | self.argnames = argnames |
paul@3 | 567 | self.has_star = has_star |
paul@3 | 568 | self.has_dstar = has_dstar |
paul@17 | 569 | self.node = node |
paul@3 | 570 | |
paul@36 | 571 | # Caches. |
paul@36 | 572 | |
paul@12 | 573 | self.localnames = None # cache for locals |
paul@12 | 574 | |
paul@17 | 575 | # Add parameters to the namespace. |
paul@17 | 576 | |
paul@25 | 577 | self._add_parameters(argnames) |
paul@17 | 578 | |
paul@9 | 579 | # Image generation details. |
paul@9 | 580 | |
paul@9 | 581 | self.location = None |
paul@19 | 582 | self.code_location = None |
paul@9 | 583 | |
paul@25 | 584 | def _add_parameters(self, argnames): |
paul@25 | 585 | for name in argnames: |
paul@25 | 586 | if isinstance(name, tuple): |
paul@25 | 587 | self._add_parameters(name) |
paul@25 | 588 | else: |
paul@53 | 589 | self.set_parameter(name, None) |
paul@25 | 590 | |
paul@4 | 591 | def __repr__(self): |
paul@37 | 592 | if self.location is not None: |
paul@37 | 593 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@53 | 594 | self.name, self.parent, self.argnames, self.has_star, self.has_dstar, self.location |
paul@37 | 595 | ) |
paul@37 | 596 | else: |
paul@37 | 597 | return "Function(%r, %r, %r, %r, %r)" % ( |
paul@53 | 598 | self.name, self.parent, self.argnames, self.has_star, self.has_dstar |
paul@37 | 599 | ) |
paul@4 | 600 | |
paul@12 | 601 | def make_global(self, name): |
paul@13 | 602 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 603 | self.globals.add(name) |
paul@12 | 604 | else: |
paul@40 | 605 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 606 | |
paul@7 | 607 | def parameters(self): |
paul@7 | 608 | |
paul@7 | 609 | """ |
paul@7 | 610 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 611 | parameter list. |
paul@7 | 612 | """ |
paul@7 | 613 | |
paul@7 | 614 | parameters = {} |
paul@7 | 615 | for i, name in enumerate(self.argnames): |
paul@7 | 616 | parameters[name] = i |
paul@7 | 617 | return parameters |
paul@7 | 618 | |
paul@36 | 619 | def all_locals(self): |
paul@36 | 620 | |
paul@36 | 621 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 622 | |
paul@53 | 623 | return dict(self) |
paul@36 | 624 | |
paul@12 | 625 | def locals(self): |
paul@12 | 626 | |
paul@12 | 627 | "Return a dictionary mapping names to local details." |
paul@12 | 628 | |
paul@12 | 629 | if self.localnames is None: |
paul@12 | 630 | self.localnames = {} |
paul@36 | 631 | self.localnames.update(self.all_locals()) |
paul@36 | 632 | for name in self.argnames: |
paul@36 | 633 | del self.localnames[name] |
paul@12 | 634 | return self.localnames |
paul@12 | 635 | |
paul@53 | 636 | class UnresolvedName(NamespaceDict): |
paul@3 | 637 | |
paul@3 | 638 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 639 | |
paul@24 | 640 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 641 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 642 | self.name = name |
paul@7 | 643 | self.parent_name = parent_name |
paul@0 | 644 | |
paul@12 | 645 | def all_class_attributes(self): |
paul@7 | 646 | return {} |
paul@7 | 647 | |
paul@13 | 648 | def instance_attributes(self): |
paul@13 | 649 | return {} |
paul@13 | 650 | |
paul@4 | 651 | def __repr__(self): |
paul@7 | 652 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 653 | |
paul@53 | 654 | def full_name(self): |
paul@53 | 655 | if self.name is not None: |
paul@53 | 656 | return self.parent_name + "." + self.name |
paul@53 | 657 | else: |
paul@53 | 658 | return self.parent_name |
paul@53 | 659 | |
paul@15 | 660 | class Module(NamespaceDict): |
paul@0 | 661 | |
paul@20 | 662 | "An inspected module's core details." |
paul@20 | 663 | |
paul@15 | 664 | def __init__(self, name): |
paul@24 | 665 | NamespaceDict.__init__(self, self) |
paul@1 | 666 | self.name = name |
paul@0 | 667 | |
paul@7 | 668 | # Complete lists of classes and functions. |
paul@7 | 669 | |
paul@33 | 670 | self.all_objects = set() |
paul@0 | 671 | |
paul@22 | 672 | # Constant records. |
paul@22 | 673 | |
paul@22 | 674 | self.constant_values = {} |
paul@22 | 675 | self.constant_list = None # cache for constants |
paul@22 | 676 | |
paul@15 | 677 | # Image generation details. |
paul@15 | 678 | |
paul@15 | 679 | self.location = None |
paul@19 | 680 | self.code_location = None |
paul@15 | 681 | |
paul@20 | 682 | # Original location details. |
paul@20 | 683 | |
paul@20 | 684 | self.node = None |
paul@20 | 685 | |
paul@15 | 686 | def full_name(self): |
paul@15 | 687 | return self.name |
paul@15 | 688 | |
paul@15 | 689 | def __repr__(self): |
paul@37 | 690 | if self.location is not None: |
paul@37 | 691 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 692 | else: |
paul@37 | 693 | return "Module(%r)" % self.name |
paul@15 | 694 | |
paul@15 | 695 | # Attribute methods. |
paul@15 | 696 | |
paul@36 | 697 | "Return the module attribute names provided by the module." |
paul@15 | 698 | |
paul@36 | 699 | module_attribute_names = NamespaceDict.keys |
paul@15 | 700 | |
paul@15 | 701 | def module_attributes(self): |
paul@15 | 702 | |
paul@15 | 703 | "Return a dictionary mapping names to module attributes." |
paul@15 | 704 | |
paul@53 | 705 | return dict(self) |
paul@15 | 706 | |
paul@22 | 707 | def constants(self): |
paul@22 | 708 | |
paul@22 | 709 | "Return a list of constants." |
paul@22 | 710 | |
paul@22 | 711 | if self.constant_list is None: |
paul@22 | 712 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 713 | |
paul@22 | 714 | return self.constant_list |
paul@22 | 715 | |
paul@21 | 716 | # Program visitors. |
paul@21 | 717 | |
paul@15 | 718 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 719 | |
paul@20 | 720 | """ |
paul@20 | 721 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 722 | capable of being used as an AST visitor. |
paul@20 | 723 | """ |
paul@15 | 724 | |
paul@15 | 725 | def __init__(self, name, importer=None): |
paul@15 | 726 | ASTVisitor.__init__(self) |
paul@15 | 727 | Module.__init__(self, name) |
paul@15 | 728 | self.visitor = self |
paul@15 | 729 | |
paul@15 | 730 | self.importer = importer |
paul@15 | 731 | self.loaded = 0 |
paul@15 | 732 | |
paul@1 | 733 | # Current expression state. |
paul@1 | 734 | |
paul@1 | 735 | self.expr = None |
paul@1 | 736 | |
paul@1 | 737 | # Namespace state. |
paul@1 | 738 | |
paul@34 | 739 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 740 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 741 | self.namespaces = [] |
paul@0 | 742 | self.module = None |
paul@0 | 743 | |
paul@0 | 744 | def parse(self, filename): |
paul@9 | 745 | |
paul@9 | 746 | "Parse the file having the given 'filename'." |
paul@9 | 747 | |
paul@0 | 748 | module = compiler.parseFile(filename) |
paul@0 | 749 | self.process(module) |
paul@0 | 750 | |
paul@0 | 751 | def process(self, module): |
paul@9 | 752 | |
paul@9 | 753 | "Process the given 'module'." |
paul@9 | 754 | |
paul@20 | 755 | self.node = self.module = module |
paul@1 | 756 | processed = self.dispatch(module) |
paul@13 | 757 | if self.has_key("__all__"): |
paul@12 | 758 | all = self["__all__"] |
paul@1 | 759 | if isinstance(all, compiler.ast.List): |
paul@1 | 760 | for n in all.nodes: |
paul@12 | 761 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 762 | return processed |
paul@0 | 763 | |
paul@3 | 764 | def vacuum(self): |
paul@9 | 765 | |
paul@9 | 766 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 767 | |
paul@13 | 768 | for name, value in self.items(): |
paul@3 | 769 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 770 | del self[name] |
paul@0 | 771 | |
paul@16 | 772 | # Complain about globals not initialised at the module level. |
paul@16 | 773 | |
paul@16 | 774 | if isinstance(value, Global): |
paul@16 | 775 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 776 | |
paul@3 | 777 | # Namespace methods. |
paul@0 | 778 | |
paul@0 | 779 | def store(self, name, obj): |
paul@12 | 780 | |
paul@12 | 781 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 782 | |
paul@12 | 783 | if not self.namespaces: |
paul@34 | 784 | self.set(name, obj, not self.in_loop) |
paul@12 | 785 | else: |
paul@34 | 786 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 787 | |
paul@12 | 788 | # Record all non-local objects. |
paul@12 | 789 | |
paul@12 | 790 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 791 | self.all_objects.add(obj) |
paul@0 | 792 | |
paul@34 | 793 | def store_instance_attr(self, name): |
paul@12 | 794 | |
paul@12 | 795 | "Record instance attribute 'name' in the current class." |
paul@12 | 796 | |
paul@0 | 797 | if self.in_init: |
paul@15 | 798 | |
paul@12 | 799 | # Current namespace is the function. |
paul@12 | 800 | # Previous namespace is the class. |
paul@15 | 801 | |
paul@12 | 802 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 803 | |
paul@7 | 804 | def get_parent(self): |
paul@12 | 805 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 806 | |
paul@3 | 807 | # Visitor methods. |
paul@3 | 808 | |
paul@3 | 809 | def default(self, node, *args): |
paul@39 | 810 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 811 | |
paul@3 | 812 | def dispatch(self, node, *args): |
paul@3 | 813 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 814 | |
paul@0 | 815 | def NOP(self, node): |
paul@21 | 816 | for n in node.getChildNodes(): |
paul@21 | 817 | self.dispatch(n) |
paul@19 | 818 | return None |
paul@0 | 819 | |
paul@1 | 820 | visitAdd = NOP |
paul@1 | 821 | |
paul@0 | 822 | visitAnd = NOP |
paul@0 | 823 | |
paul@4 | 824 | visitAssert = NOP |
paul@4 | 825 | |
paul@0 | 826 | def visitAssign(self, node): |
paul@0 | 827 | self.expr = self.dispatch(node.expr) |
paul@0 | 828 | for n in node.nodes: |
paul@0 | 829 | self.dispatch(n) |
paul@0 | 830 | return None |
paul@0 | 831 | |
paul@0 | 832 | def visitAssAttr(self, node): |
paul@0 | 833 | expr = self.dispatch(node.expr) |
paul@36 | 834 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 835 | self.store_instance_attr(node.attrname) |
paul@0 | 836 | return None |
paul@0 | 837 | |
paul@0 | 838 | def visitAssList(self, node): |
paul@0 | 839 | for n in node.nodes: |
paul@0 | 840 | self.dispatch(n) |
paul@0 | 841 | return None |
paul@0 | 842 | |
paul@0 | 843 | def visitAssName(self, node): |
paul@36 | 844 | if isinstance(self.expr, Attr): |
paul@36 | 845 | self.store(node.name, self.expr.value) |
paul@36 | 846 | else: |
paul@36 | 847 | self.store(node.name, self.expr) |
paul@0 | 848 | return None |
paul@0 | 849 | |
paul@0 | 850 | visitAssTuple = visitAssList |
paul@0 | 851 | |
paul@2 | 852 | visitAugAssign = NOP |
paul@2 | 853 | |
paul@4 | 854 | visitBackquote = NOP |
paul@4 | 855 | |
paul@2 | 856 | visitBitand = NOP |
paul@2 | 857 | |
paul@2 | 858 | visitBitor = NOP |
paul@2 | 859 | |
paul@4 | 860 | visitBitxor = NOP |
paul@4 | 861 | |
paul@4 | 862 | visitBreak = NOP |
paul@4 | 863 | |
paul@0 | 864 | visitCallFunc = NOP |
paul@0 | 865 | |
paul@0 | 866 | def visitClass(self, node): |
paul@12 | 867 | if self.namespaces: |
paul@41 | 868 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 869 | else: |
paul@53 | 870 | cls = Class(node.name, self.get_parent(), self, node) |
paul@37 | 871 | |
paul@37 | 872 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 873 | # definitions of classes. |
paul@37 | 874 | |
paul@0 | 875 | for base in node.bases: |
paul@36 | 876 | expr = self.dispatch(base) |
paul@36 | 877 | if isinstance(expr, Attr): |
paul@36 | 878 | if expr.assignments != 1: |
paul@39 | 879 | raise InspectError(self.full_name(), node, |
paul@40 | 880 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 881 | else: |
paul@36 | 882 | cls.add_base(expr.value) |
paul@36 | 883 | else: # if expr is None: |
paul@39 | 884 | raise InspectError(self.full_name(), node, |
paul@40 | 885 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 886 | |
paul@21 | 887 | # Make a back reference from the node for code generation. |
paul@21 | 888 | |
paul@39 | 889 | node.unit = cls |
paul@21 | 890 | |
paul@0 | 891 | # Make an entry for the class. |
paul@0 | 892 | |
paul@0 | 893 | self.store(node.name, cls) |
paul@0 | 894 | |
paul@12 | 895 | self.namespaces.append(cls) |
paul@0 | 896 | self.dispatch(node.code) |
paul@12 | 897 | self.namespaces.pop() |
paul@0 | 898 | |
paul@19 | 899 | return None |
paul@0 | 900 | |
paul@4 | 901 | visitCompare = NOP |
paul@4 | 902 | |
paul@21 | 903 | def visitConst(self, node): |
paul@21 | 904 | const = Const(node.value) |
paul@22 | 905 | self.constant_values[node.value] = const |
paul@24 | 906 | return const |
paul@0 | 907 | |
paul@4 | 908 | visitContinue = NOP |
paul@4 | 909 | |
paul@4 | 910 | visitDecorators = NOP |
paul@4 | 911 | |
paul@0 | 912 | visitDict = NOP |
paul@0 | 913 | |
paul@0 | 914 | visitDiscard = NOP |
paul@0 | 915 | |
paul@4 | 916 | visitDiv = NOP |
paul@4 | 917 | |
paul@4 | 918 | visitEllipsis = NOP |
paul@4 | 919 | |
paul@4 | 920 | visitExec = NOP |
paul@4 | 921 | |
paul@4 | 922 | visitExpression = NOP |
paul@4 | 923 | |
paul@4 | 924 | visitFloorDiv = NOP |
paul@4 | 925 | |
paul@34 | 926 | def visitFor(self, node): |
paul@34 | 927 | self.in_loop = 1 |
paul@34 | 928 | self.NOP(node) |
paul@34 | 929 | self.in_loop = 0 |
paul@0 | 930 | |
paul@0 | 931 | def visitFrom(self, node): |
paul@0 | 932 | if self.importer is None: |
paul@39 | 933 | raise InspectError(self.full_name(), node, |
paul@39 | 934 | "Please use the micropython.Importer class for code which uses the 'from' statement.") |
paul@0 | 935 | |
paul@0 | 936 | module = self.importer.load(node.modname, 1) |
paul@0 | 937 | |
paul@35 | 938 | #if module is None: |
paul@35 | 939 | # print "Warning:", node.modname, "not imported." |
paul@0 | 940 | |
paul@0 | 941 | for name, alias in node.names: |
paul@0 | 942 | if name != "*": |
paul@4 | 943 | if module is not None and module.namespace.has_key(name): |
paul@24 | 944 | attr = module[name] |
paul@36 | 945 | self.store(alias or name, attr.value) |
paul@2 | 946 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 947 | self.importer.load(attr.name) |
paul@24 | 948 | |
paul@24 | 949 | # Support the import of names from missing modules. |
paul@24 | 950 | |
paul@3 | 951 | else: |
paul@24 | 952 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 953 | else: |
paul@3 | 954 | if module is not None: |
paul@3 | 955 | for n in module.namespace.keys(): |
paul@24 | 956 | attr = module[n] |
paul@36 | 957 | self.store(n, attr.value) |
paul@3 | 958 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 959 | self.importer.load(attr.name) |
paul@0 | 960 | |
paul@0 | 961 | return None |
paul@0 | 962 | |
paul@0 | 963 | def visitFunction(self, node): |
paul@3 | 964 | function = Function( |
paul@3 | 965 | node.name, |
paul@53 | 966 | self.get_parent(), |
paul@3 | 967 | node.argnames, |
paul@17 | 968 | (node.flags & 4 != 0), |
paul@17 | 969 | (node.flags & 8 != 0), |
paul@24 | 970 | self, |
paul@17 | 971 | node |
paul@3 | 972 | ) |
paul@0 | 973 | |
paul@21 | 974 | # Make a back reference from the node for code generation. |
paul@21 | 975 | |
paul@39 | 976 | node.unit = function |
paul@21 | 977 | |
paul@12 | 978 | self.namespaces.append(function) |
paul@15 | 979 | |
paul@12 | 980 | # Current namespace is the function. |
paul@12 | 981 | # Previous namespace is the class. |
paul@15 | 982 | |
paul@12 | 983 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 984 | self.in_init = 1 |
paul@15 | 985 | |
paul@12 | 986 | self.dispatch(node.code) |
paul@12 | 987 | self.in_init = 0 |
paul@12 | 988 | self.namespaces.pop() |
paul@3 | 989 | |
paul@3 | 990 | self.store(node.name, function) |
paul@0 | 991 | return None |
paul@0 | 992 | |
paul@4 | 993 | visitGenExpr = NOP |
paul@4 | 994 | |
paul@4 | 995 | visitGenExprFor = NOP |
paul@4 | 996 | |
paul@4 | 997 | visitGenExprIf = NOP |
paul@4 | 998 | |
paul@4 | 999 | visitGenExprInner = NOP |
paul@4 | 1000 | |
paul@0 | 1001 | def visitGetattr(self, node): |
paul@0 | 1002 | expr = self.dispatch(node.expr) |
paul@36 | 1003 | if isinstance(expr, Attr): |
paul@36 | 1004 | value = expr.value |
paul@36 | 1005 | if isinstance(value, Module): |
paul@36 | 1006 | return value.namespace.get(node.attrname) |
paul@36 | 1007 | elif isinstance(value, UnresolvedName): |
paul@36 | 1008 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@15 | 1009 | return builtins.get(node.attrname) |
paul@4 | 1010 | |
paul@12 | 1011 | def visitGlobal(self, node): |
paul@12 | 1012 | if self.namespaces: |
paul@12 | 1013 | for name in node.names: |
paul@12 | 1014 | self.namespaces[-1].make_global(name) |
paul@37 | 1015 | |
paul@37 | 1016 | # Record a global entry for the name in the module. |
paul@37 | 1017 | |
paul@15 | 1018 | if not self.has_key(name): |
paul@16 | 1019 | self[name] = Global() |
paul@0 | 1020 | |
paul@0 | 1021 | def visitIf(self, node): |
paul@0 | 1022 | for test, body in node.tests: |
paul@0 | 1023 | self.dispatch(body) |
paul@0 | 1024 | if node.else_ is not None: |
paul@0 | 1025 | self.dispatch(node.else_) |
paul@0 | 1026 | return None |
paul@0 | 1027 | |
paul@12 | 1028 | visitIfExp = NOP |
paul@12 | 1029 | |
paul@0 | 1030 | def visitImport(self, node): |
paul@0 | 1031 | if self.importer is None: |
paul@39 | 1032 | raise InspectError(self.full_name(), node, |
paul@39 | 1033 | "Please use the micropython.Importer class for code which uses the 'import' statement.") |
paul@0 | 1034 | |
paul@0 | 1035 | for name, alias in node.names: |
paul@2 | 1036 | if alias is not None: |
paul@24 | 1037 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 1038 | else: |
paul@24 | 1039 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 1040 | |
paul@0 | 1041 | return None |
paul@0 | 1042 | |
paul@4 | 1043 | visitInvert = NOP |
paul@4 | 1044 | |
paul@4 | 1045 | visitKeyword = NOP |
paul@4 | 1046 | |
paul@4 | 1047 | visitLambda = NOP |
paul@4 | 1048 | |
paul@1 | 1049 | visitLeftShift = NOP |
paul@1 | 1050 | |
paul@0 | 1051 | visitList = NOP |
paul@0 | 1052 | |
paul@4 | 1053 | visitListComp = NOP |
paul@4 | 1054 | |
paul@4 | 1055 | visitListCompFor = NOP |
paul@4 | 1056 | |
paul@4 | 1057 | visitListCompIf = NOP |
paul@4 | 1058 | |
paul@4 | 1059 | visitMod = NOP |
paul@4 | 1060 | |
paul@0 | 1061 | def visitModule(self, node): |
paul@0 | 1062 | return self.dispatch(node.node) |
paul@0 | 1063 | |
paul@2 | 1064 | visitMul = NOP |
paul@2 | 1065 | |
paul@0 | 1066 | def visitName(self, node): |
paul@0 | 1067 | name = node.name |
paul@36 | 1068 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 1069 | return self.namespaces[-1][name] |
paul@13 | 1070 | elif self.has_key(name): |
paul@12 | 1071 | return self[name] |
paul@15 | 1072 | elif builtins.has_key(name): |
paul@15 | 1073 | return builtins[name] |
paul@0 | 1074 | else: |
paul@0 | 1075 | return None |
paul@0 | 1076 | |
paul@0 | 1077 | visitNot = NOP |
paul@0 | 1078 | |
paul@0 | 1079 | visitOr = NOP |
paul@0 | 1080 | |
paul@0 | 1081 | visitPass = NOP |
paul@0 | 1082 | |
paul@4 | 1083 | visitPower = NOP |
paul@4 | 1084 | |
paul@4 | 1085 | visitPrint = NOP |
paul@4 | 1086 | |
paul@4 | 1087 | visitPrintnl = NOP |
paul@4 | 1088 | |
paul@0 | 1089 | visitRaise = NOP |
paul@0 | 1090 | |
paul@4 | 1091 | visitReturn = NOP |
paul@4 | 1092 | |
paul@1 | 1093 | visitRightShift = NOP |
paul@1 | 1094 | |
paul@4 | 1095 | visitSlice = NOP |
paul@4 | 1096 | |
paul@23 | 1097 | visitSliceobj = NOP |
paul@23 | 1098 | |
paul@0 | 1099 | def visitStmt(self, node): |
paul@0 | 1100 | for n in node.nodes: |
paul@0 | 1101 | self.dispatch(n) |
paul@0 | 1102 | return None |
paul@0 | 1103 | |
paul@4 | 1104 | visitSub = NOP |
paul@4 | 1105 | |
paul@0 | 1106 | visitSubscript = NOP |
paul@0 | 1107 | |
paul@0 | 1108 | def visitTryExcept(self, node): |
paul@0 | 1109 | self.dispatch(node.body) |
paul@0 | 1110 | for name, var, n in node.handlers: |
paul@0 | 1111 | self.dispatch(n) |
paul@0 | 1112 | if node.else_ is not None: |
paul@0 | 1113 | self.dispatch(node.else_) |
paul@0 | 1114 | return None |
paul@0 | 1115 | |
paul@4 | 1116 | visitTryFinally = NOP |
paul@4 | 1117 | |
paul@1 | 1118 | visitTuple = NOP |
paul@1 | 1119 | |
paul@4 | 1120 | visitUnaryAdd = NOP |
paul@4 | 1121 | |
paul@4 | 1122 | visitUnarySub = NOP |
paul@4 | 1123 | |
paul@34 | 1124 | def visitWhile(self, node): |
paul@34 | 1125 | self.in_loop = 1 |
paul@34 | 1126 | self.NOP(node) |
paul@34 | 1127 | self.in_loop = 0 |
paul@4 | 1128 | |
paul@4 | 1129 | visitWith = NOP |
paul@4 | 1130 | |
paul@4 | 1131 | visitYield = NOP |
paul@4 | 1132 | |
paul@16 | 1133 | class Global: |
paul@16 | 1134 | |
paul@16 | 1135 | """ |
paul@16 | 1136 | A reference to an object assigned to a global from outside the module |
paul@16 | 1137 | top-level. |
paul@16 | 1138 | """ |
paul@16 | 1139 | |
paul@16 | 1140 | pass |
paul@16 | 1141 | |
paul@4 | 1142 | # Built-in types initialisation. |
paul@4 | 1143 | |
paul@15 | 1144 | class Builtins(Module): |
paul@15 | 1145 | |
paul@15 | 1146 | "The special built-in types module." |
paul@15 | 1147 | |
paul@15 | 1148 | def __init__(self): |
paul@15 | 1149 | Module.__init__(self, "__builtins__") |
paul@44 | 1150 | self.loaded = 1 |
paul@44 | 1151 | self.module = None |
paul@15 | 1152 | |
paul@15 | 1153 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@15 | 1154 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@15 | 1155 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@15 | 1156 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@15 | 1157 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@15 | 1158 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@15 | 1159 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@15 | 1160 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@15 | 1161 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@15 | 1162 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@15 | 1163 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@15 | 1164 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@15 | 1165 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@15 | 1166 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@25 | 1167 | 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float', |
paul@44 | 1168 | 'frozenset', 'int', # 'list', |
paul@44 | 1169 | 'long', 'object', 'set', 'slice', 'str', |
paul@25 | 1170 | 'tuple', 'type', 'unicode', 'xrange']: |
paul@53 | 1171 | self.store(key, Class(key, self, self)) |
paul@44 | 1172 | |
paul@44 | 1173 | # NOTE: Temporary measure - provide detailed built-ins. |
paul@44 | 1174 | |
paul@53 | 1175 | cls = Class("list", self, self) |
paul@53 | 1176 | cls.set("__iter__", Function("__iter__", cls, [], 0, 0, cls)) |
paul@44 | 1177 | self.store("list", cls) |
paul@44 | 1178 | |
paul@53 | 1179 | cls = Class("listiterator", self, self) |
paul@53 | 1180 | cls.set("next", Function("next", cls, [], 0, 0, cls)) |
paul@44 | 1181 | self.store("listiterator", cls) |
paul@15 | 1182 | |
paul@25 | 1183 | # NOTE: Incomplete: some functions have more than one parameter. |
paul@25 | 1184 | |
paul@25 | 1185 | for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr', |
paul@25 | 1186 | 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod', |
paul@25 | 1187 | 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals', |
paul@25 | 1188 | 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance', |
paul@25 | 1189 | 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct', |
paul@25 | 1190 | 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce', |
paul@25 | 1191 | 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted', |
paul@25 | 1192 | 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']: |
paul@53 | 1193 | self[key] = Function(key, self, ['arg'], 0, 0, self) |
paul@16 | 1194 | |
paul@44 | 1195 | def store(self, name, obj): |
paul@44 | 1196 | self.set(name, obj) |
paul@44 | 1197 | self.all_objects.add(obj) |
paul@44 | 1198 | |
paul@44 | 1199 | def vacuum(self): |
paul@44 | 1200 | pass |
paul@44 | 1201 | |
paul@15 | 1202 | builtins = Builtins() |
paul@0 | 1203 | |
paul@0 | 1204 | # vim: tabstop=4 expandtab shiftwidth=4 |