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@36 | 111 | self.attr_position = 0 |
paul@23 | 112 | |
paul@3 | 113 | def __getitem__(self, name): |
paul@3 | 114 | return self.namespace[name] |
paul@3 | 115 | |
paul@15 | 116 | def get(self, name, default=None): |
paul@15 | 117 | return self.namespace.get(name, default) |
paul@15 | 118 | |
paul@12 | 119 | def __setitem__(self, name, value): |
paul@37 | 120 | self.set(name, value) |
paul@31 | 121 | |
paul@34 | 122 | def set(self, name, value, single_assignment=1): |
paul@37 | 123 | if name in self.globals: |
paul@37 | 124 | self.global_namespace.set(name, value, 0) |
paul@37 | 125 | else: |
paul@37 | 126 | attr = self._set(name, value) |
paul@12 | 127 | |
paul@37 | 128 | # NOTE: Insist on assignments with known values. |
paul@23 | 129 | |
paul@37 | 130 | if value is not None: |
paul@37 | 131 | attr.update(value, single_assignment) |
paul@32 | 132 | |
paul@32 | 133 | def set_module(self, name, value): |
paul@36 | 134 | attr = self._set(name, value) |
paul@36 | 135 | if attr.assignments is None: |
paul@36 | 136 | attr.assignments = 1 |
paul@36 | 137 | attr.assignment_values.add(value) |
paul@36 | 138 | |
paul@36 | 139 | def _set(self, name, value): |
paul@36 | 140 | if not self.namespace.has_key(name): |
paul@36 | 141 | self.namespace[name] = Attr(self.attr_position, self, name, value) |
paul@36 | 142 | self.attr_position += 1 |
paul@36 | 143 | return self.namespace[name] |
paul@23 | 144 | |
paul@12 | 145 | def __delitem__(self, name): |
paul@12 | 146 | del self.namespace[name] |
paul@12 | 147 | |
paul@13 | 148 | def has_key(self, name): |
paul@13 | 149 | return self.namespace.has_key(name) |
paul@13 | 150 | |
paul@3 | 151 | def keys(self): |
paul@3 | 152 | return self.namespace.keys() |
paul@3 | 153 | |
paul@7 | 154 | def values(self): |
paul@7 | 155 | return self.namespace.values() |
paul@7 | 156 | |
paul@7 | 157 | def items(self): |
paul@7 | 158 | return self.namespace.items() |
paul@7 | 159 | |
paul@12 | 160 | def make_global(self, name): |
paul@12 | 161 | if not self.namespace.has_key(name): |
paul@12 | 162 | self.globals.add(name) |
paul@12 | 163 | else: |
paul@43 | 164 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@12 | 165 | |
paul@32 | 166 | def get_assignments(self, name): |
paul@32 | 167 | if self.assignments.has_key(name): |
paul@32 | 168 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@32 | 169 | else: |
paul@32 | 170 | return None |
paul@32 | 171 | |
paul@37 | 172 | def to_list(self, d): |
paul@37 | 173 | l = [None] * len(d.keys()) |
paul@37 | 174 | for attr in d.values(): |
paul@37 | 175 | l[attr.position] = attr |
paul@37 | 176 | return l |
paul@37 | 177 | |
paul@4 | 178 | class Naming: |
paul@4 | 179 | |
paul@4 | 180 | "A mix-in providing naming conveniences." |
paul@4 | 181 | |
paul@4 | 182 | def full_name(self): |
paul@4 | 183 | if self.name is not None: |
paul@7 | 184 | return self.parent_name + "." + self.name |
paul@4 | 185 | else: |
paul@7 | 186 | return self.parent_name |
paul@4 | 187 | |
paul@12 | 188 | # Program data structures. |
paul@12 | 189 | |
paul@10 | 190 | class Attr: |
paul@10 | 191 | |
paul@10 | 192 | "An attribute entry." |
paul@10 | 193 | |
paul@36 | 194 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 195 | self.position = position |
paul@10 | 196 | self.parent = parent |
paul@36 | 197 | self.name = name |
paul@10 | 198 | self.value = value |
paul@10 | 199 | |
paul@36 | 200 | # Number of assignments per name. |
paul@36 | 201 | |
paul@36 | 202 | self.assignments = assignments |
paul@36 | 203 | self.assignment_values = set() |
paul@36 | 204 | |
paul@36 | 205 | def update(self, value, single_assignment): |
paul@36 | 206 | if self.assignments is None: |
paul@36 | 207 | if single_assignment: |
paul@36 | 208 | self.assignments = 1 |
paul@36 | 209 | else: |
paul@36 | 210 | self.assignments = AtLeast(1) |
paul@36 | 211 | else: |
paul@36 | 212 | if single_assignment: |
paul@36 | 213 | self.assignments += 1 |
paul@36 | 214 | else: |
paul@36 | 215 | self.assignments += AtLeast(1) |
paul@36 | 216 | self.assignment_values.add(value) |
paul@36 | 217 | |
paul@10 | 218 | def __repr__(self): |
paul@36 | 219 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 220 | |
paul@21 | 221 | class Const: |
paul@21 | 222 | |
paul@21 | 223 | "A constant object." |
paul@21 | 224 | |
paul@21 | 225 | def __init__(self, value): |
paul@21 | 226 | self.value = value |
paul@21 | 227 | |
paul@21 | 228 | # Image generation details. |
paul@21 | 229 | |
paul@21 | 230 | self.location = None |
paul@21 | 231 | |
paul@21 | 232 | def __repr__(self): |
paul@37 | 233 | if self.location is not None: |
paul@37 | 234 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 235 | else: |
paul@37 | 236 | return "Const(%r)" % self.value |
paul@21 | 237 | |
paul@21 | 238 | def __eq__(self, other): |
paul@21 | 239 | return self.value == other.value |
paul@21 | 240 | |
paul@21 | 241 | def __hash__(self): |
paul@21 | 242 | return hash(self.value) |
paul@21 | 243 | |
paul@4 | 244 | class Class(NamespaceDict, Naming): |
paul@0 | 245 | |
paul@0 | 246 | "An inspected class." |
paul@0 | 247 | |
paul@24 | 248 | def __init__(self, name, parent_name, global_namespace=None, node=None): |
paul@24 | 249 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 250 | self.name = name |
paul@7 | 251 | self.parent_name = parent_name |
paul@17 | 252 | self.node = node |
paul@12 | 253 | |
paul@0 | 254 | self.bases = [] |
paul@46 | 255 | self.instattr = set() # instance attributes |
paul@46 | 256 | self.instattr_relocated = set() # instance attributes which do not have |
paul@46 | 257 | # the same position as those of the same |
paul@46 | 258 | # name in some superclasses |
paul@12 | 259 | |
paul@46 | 260 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 261 | self.all_instattr_names = None # from all_instattr |
paul@46 | 262 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 263 | self.all_classattr_names = None # from all_classattr |
paul@46 | 264 | self.allattr = None # cache for all_attributes |
paul@46 | 265 | self.allattr_names = None # from allattr |
paul@9 | 266 | |
paul@9 | 267 | # Image generation details. |
paul@9 | 268 | |
paul@9 | 269 | self.location = None |
paul@19 | 270 | self.code_location = None |
paul@0 | 271 | |
paul@12 | 272 | def __repr__(self): |
paul@37 | 273 | if self.location is not None: |
paul@37 | 274 | return "Class(%r, %r, location=%r)" % (self.name, self.parent_name, self.location) |
paul@37 | 275 | else: |
paul@37 | 276 | return "Class(%r, %r)" % (self.name, self.parent_name) |
paul@12 | 277 | |
paul@0 | 278 | def add_base(self, base): |
paul@0 | 279 | self.bases.append(base) |
paul@0 | 280 | |
paul@12 | 281 | def add_instance_attribute(self, name): |
paul@12 | 282 | self.instattr.add(name) |
paul@9 | 283 | |
paul@36 | 284 | "Return the attribute names provided by this class only." |
paul@9 | 285 | |
paul@36 | 286 | class_attribute_names = NamespaceDict.keys |
paul@4 | 287 | |
paul@4 | 288 | def class_attributes(self): |
paul@4 | 289 | |
paul@12 | 290 | "Return class attributes provided by this class only." |
paul@4 | 291 | |
paul@36 | 292 | return self |
paul@12 | 293 | |
paul@12 | 294 | def all_class_attribute_names(self): |
paul@12 | 295 | |
paul@12 | 296 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 297 | |
paul@12 | 298 | if self.all_classattr_names is None: |
paul@12 | 299 | self.all_class_attributes() |
paul@12 | 300 | return self.all_classattr_names |
paul@12 | 301 | |
paul@12 | 302 | def all_class_attributes(self): |
paul@12 | 303 | |
paul@12 | 304 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 305 | |
paul@12 | 306 | if self.all_classattr is None: |
paul@12 | 307 | self.all_classattr = {} |
paul@12 | 308 | |
paul@9 | 309 | reversed_bases = self.bases[:] |
paul@9 | 310 | reversed_bases.reverse() |
paul@9 | 311 | for cls in reversed_bases: |
paul@12 | 312 | self.all_classattr.update(cls.all_class_attributes()) |
paul@10 | 313 | |
paul@10 | 314 | # Record attributes provided by this class, along with their |
paul@10 | 315 | # positions. |
paul@10 | 316 | |
paul@12 | 317 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 318 | |
paul@12 | 319 | return self.all_classattr |
paul@4 | 320 | |
paul@7 | 321 | def instance_attribute_names(self): |
paul@7 | 322 | |
paul@7 | 323 | "Return the instance attribute names provided by the class." |
paul@7 | 324 | |
paul@12 | 325 | if self.all_instattr_names is None: |
paul@12 | 326 | self.instance_attributes() |
paul@12 | 327 | return self.all_instattr_names |
paul@7 | 328 | |
paul@4 | 329 | def instance_attributes(self): |
paul@4 | 330 | |
paul@9 | 331 | "Return instance-only attributes for instances of this class." |
paul@9 | 332 | |
paul@12 | 333 | if self.all_instattr is None: |
paul@12 | 334 | self.all_instattr = {} |
paul@46 | 335 | instattr = {} |
paul@12 | 336 | |
paul@12 | 337 | reversed_bases = self.bases[:] |
paul@12 | 338 | reversed_bases.reverse() |
paul@46 | 339 | |
paul@46 | 340 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 341 | # details. |
paul@46 | 342 | |
paul@12 | 343 | for cls in reversed_bases: |
paul@46 | 344 | for name, attr in cls.instance_attributes().items(): |
paul@46 | 345 | if not instattr.has_key(name): |
paul@46 | 346 | instattr[name] = set() |
paul@46 | 347 | instattr[name].add(attr.position) |
paul@12 | 348 | |
paul@12 | 349 | # Record instance attributes provided by this class and its bases, |
paul@12 | 350 | # along with their positions. |
paul@12 | 351 | |
paul@46 | 352 | for name in self.instattr: |
paul@46 | 353 | if not instattr.has_key(name): |
paul@46 | 354 | instattr[name] = set([-1]) # position not yet defined |
paul@46 | 355 | |
paul@46 | 356 | if not instattr: |
paul@46 | 357 | self.all_instattr = {} |
paul@46 | 358 | else: |
paul@46 | 359 | positions = instattr.items() |
paul@46 | 360 | instarray = [None] * len(positions) |
paul@46 | 361 | |
paul@46 | 362 | # Get the positions in ascending order of list size, with lists |
paul@46 | 363 | # of the same size ordered according to their smallest position |
paul@46 | 364 | # value. |
paul@46 | 365 | |
paul@46 | 366 | positions.sort(self._cmp_positions) |
paul@46 | 367 | #print self.name, positions |
paul@12 | 368 | |
paul@46 | 369 | # Get the names in position order. |
paul@46 | 370 | |
paul@46 | 371 | held = [] |
paul@46 | 372 | |
paul@46 | 373 | for name, pos in positions: |
paul@46 | 374 | pos = list(pos) |
paul@46 | 375 | if pos[0] != -1 and instarray[pos[0]] is None: |
paul@46 | 376 | instarray[pos[0]] = name, Attr(pos[0], None, name) |
paul@46 | 377 | else: |
paul@46 | 378 | if pos[0] != -1 or len(pos) > 1: |
paul@46 | 379 | self.instattr_relocated.add(name) |
paul@46 | 380 | held.append((name, pos)) |
paul@46 | 381 | |
paul@46 | 382 | for i, attr in enumerate(instarray): |
paul@46 | 383 | if attr is None: |
paul@46 | 384 | name, pos = held.pop() |
paul@46 | 385 | instarray[i] = name, Attr(i, None, name) |
paul@46 | 386 | |
paul@46 | 387 | self.all_instattr = dict(instarray) |
paul@12 | 388 | |
paul@12 | 389 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 390 | |
paul@12 | 391 | return self.all_instattr |
paul@9 | 392 | |
paul@46 | 393 | def _cmp_positions(self, a, b): |
paul@46 | 394 | name_a, list_a = a |
paul@46 | 395 | name_b, list_b = b |
paul@46 | 396 | if len(list_a) < len(list_b): |
paul@46 | 397 | return -1 |
paul@46 | 398 | elif len(list_a) > len(list_b): |
paul@46 | 399 | return 1 |
paul@46 | 400 | else: |
paul@46 | 401 | return cmp(min(list_a), min(list_b)) |
paul@46 | 402 | |
paul@9 | 403 | def all_attribute_names(self): |
paul@9 | 404 | |
paul@9 | 405 | """ |
paul@9 | 406 | Return the names of all attributes provided by instances of this class. |
paul@9 | 407 | """ |
paul@9 | 408 | |
paul@9 | 409 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 410 | return self.allattr_names |
paul@9 | 411 | |
paul@9 | 412 | def all_attributes(self): |
paul@9 | 413 | |
paul@4 | 414 | """ |
paul@4 | 415 | Return all attributes for an instance, indicating either the class which |
paul@4 | 416 | provides them or that the instance itself provides them. |
paul@4 | 417 | """ |
paul@4 | 418 | |
paul@9 | 419 | if self.allattr is None: |
paul@9 | 420 | self.allattr = {} |
paul@12 | 421 | self.allattr.update(self.all_class_attributes()) |
paul@9 | 422 | for i, name in enumerate(self.instance_attribute_names()): |
paul@9 | 423 | if self.allattr.has_key(name): |
paul@9 | 424 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@36 | 425 | self.allattr[name] = Attr(i, None, name) |
paul@9 | 426 | return self.allattr |
paul@4 | 427 | |
paul@12 | 428 | class Function(NamespaceDict, Naming): |
paul@0 | 429 | |
paul@0 | 430 | "An inspected function." |
paul@0 | 431 | |
paul@24 | 432 | def __init__(self, name, parent_name, argnames, has_star, has_dstar, global_namespace=None, node=None): |
paul@24 | 433 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 434 | self.name = name |
paul@7 | 435 | self.parent_name = parent_name |
paul@3 | 436 | self.argnames = argnames |
paul@3 | 437 | self.has_star = has_star |
paul@3 | 438 | self.has_dstar = has_dstar |
paul@17 | 439 | self.node = node |
paul@3 | 440 | |
paul@36 | 441 | # Caches. |
paul@36 | 442 | |
paul@12 | 443 | self.localnames = None # cache for locals |
paul@12 | 444 | |
paul@17 | 445 | # Add parameters to the namespace. |
paul@17 | 446 | |
paul@25 | 447 | self._add_parameters(argnames) |
paul@17 | 448 | |
paul@9 | 449 | # Image generation details. |
paul@9 | 450 | |
paul@9 | 451 | self.location = None |
paul@19 | 452 | self.code_location = None |
paul@9 | 453 | |
paul@25 | 454 | def _add_parameters(self, argnames): |
paul@25 | 455 | for name in argnames: |
paul@25 | 456 | if isinstance(name, tuple): |
paul@25 | 457 | self._add_parameters(name) |
paul@25 | 458 | else: |
paul@25 | 459 | self[name] = None |
paul@25 | 460 | |
paul@4 | 461 | def __repr__(self): |
paul@37 | 462 | if self.location is not None: |
paul@37 | 463 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@37 | 464 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar, self.location |
paul@37 | 465 | ) |
paul@37 | 466 | else: |
paul@37 | 467 | return "Function(%r, %r, %r, %r, %r)" % ( |
paul@37 | 468 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar |
paul@37 | 469 | ) |
paul@4 | 470 | |
paul@12 | 471 | def make_global(self, name): |
paul@13 | 472 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 473 | self.globals.add(name) |
paul@12 | 474 | else: |
paul@40 | 475 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 476 | |
paul@7 | 477 | def parameters(self): |
paul@7 | 478 | |
paul@7 | 479 | """ |
paul@7 | 480 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 481 | parameter list. |
paul@7 | 482 | """ |
paul@7 | 483 | |
paul@7 | 484 | parameters = {} |
paul@7 | 485 | for i, name in enumerate(self.argnames): |
paul@7 | 486 | parameters[name] = i |
paul@7 | 487 | return parameters |
paul@7 | 488 | |
paul@36 | 489 | def all_locals(self): |
paul@36 | 490 | |
paul@36 | 491 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 492 | |
paul@36 | 493 | return self |
paul@36 | 494 | |
paul@12 | 495 | def locals(self): |
paul@12 | 496 | |
paul@12 | 497 | "Return a dictionary mapping names to local details." |
paul@12 | 498 | |
paul@12 | 499 | if self.localnames is None: |
paul@12 | 500 | self.localnames = {} |
paul@36 | 501 | self.localnames.update(self.all_locals()) |
paul@36 | 502 | for name in self.argnames: |
paul@36 | 503 | del self.localnames[name] |
paul@12 | 504 | return self.localnames |
paul@12 | 505 | |
paul@4 | 506 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 507 | |
paul@3 | 508 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 509 | |
paul@24 | 510 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 511 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 512 | self.name = name |
paul@7 | 513 | self.parent_name = parent_name |
paul@0 | 514 | |
paul@12 | 515 | def all_class_attributes(self): |
paul@7 | 516 | return {} |
paul@7 | 517 | |
paul@13 | 518 | def instance_attributes(self): |
paul@13 | 519 | return {} |
paul@13 | 520 | |
paul@4 | 521 | def __repr__(self): |
paul@7 | 522 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 523 | |
paul@15 | 524 | class Module(NamespaceDict): |
paul@0 | 525 | |
paul@20 | 526 | "An inspected module's core details." |
paul@20 | 527 | |
paul@15 | 528 | def __init__(self, name): |
paul@24 | 529 | NamespaceDict.__init__(self, self) |
paul@1 | 530 | self.name = name |
paul@0 | 531 | |
paul@7 | 532 | # Complete lists of classes and functions. |
paul@7 | 533 | |
paul@33 | 534 | self.all_objects = set() |
paul@0 | 535 | |
paul@22 | 536 | # Constant records. |
paul@22 | 537 | |
paul@22 | 538 | self.constant_values = {} |
paul@22 | 539 | self.constant_list = None # cache for constants |
paul@22 | 540 | |
paul@15 | 541 | # Image generation details. |
paul@15 | 542 | |
paul@15 | 543 | self.location = None |
paul@19 | 544 | self.code_location = None |
paul@15 | 545 | |
paul@20 | 546 | # Original location details. |
paul@20 | 547 | |
paul@20 | 548 | self.node = None |
paul@20 | 549 | |
paul@15 | 550 | def full_name(self): |
paul@15 | 551 | return self.name |
paul@15 | 552 | |
paul@15 | 553 | def __repr__(self): |
paul@37 | 554 | if self.location is not None: |
paul@37 | 555 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 556 | else: |
paul@37 | 557 | return "Module(%r)" % self.name |
paul@15 | 558 | |
paul@15 | 559 | # Attribute methods. |
paul@15 | 560 | |
paul@36 | 561 | "Return the module attribute names provided by the module." |
paul@15 | 562 | |
paul@36 | 563 | module_attribute_names = NamespaceDict.keys |
paul@15 | 564 | |
paul@15 | 565 | def module_attributes(self): |
paul@15 | 566 | |
paul@15 | 567 | "Return a dictionary mapping names to module attributes." |
paul@15 | 568 | |
paul@36 | 569 | return self |
paul@15 | 570 | |
paul@22 | 571 | def constants(self): |
paul@22 | 572 | |
paul@22 | 573 | "Return a list of constants." |
paul@22 | 574 | |
paul@22 | 575 | if self.constant_list is None: |
paul@22 | 576 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 577 | |
paul@22 | 578 | return self.constant_list |
paul@22 | 579 | |
paul@21 | 580 | # Program visitors. |
paul@21 | 581 | |
paul@15 | 582 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 583 | |
paul@20 | 584 | """ |
paul@20 | 585 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 586 | capable of being used as an AST visitor. |
paul@20 | 587 | """ |
paul@15 | 588 | |
paul@15 | 589 | def __init__(self, name, importer=None): |
paul@15 | 590 | ASTVisitor.__init__(self) |
paul@15 | 591 | Module.__init__(self, name) |
paul@15 | 592 | self.visitor = self |
paul@15 | 593 | |
paul@15 | 594 | self.importer = importer |
paul@15 | 595 | self.loaded = 0 |
paul@15 | 596 | |
paul@1 | 597 | # Current expression state. |
paul@1 | 598 | |
paul@1 | 599 | self.expr = None |
paul@1 | 600 | |
paul@1 | 601 | # Namespace state. |
paul@1 | 602 | |
paul@34 | 603 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 604 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 605 | self.namespaces = [] |
paul@0 | 606 | self.module = None |
paul@0 | 607 | |
paul@0 | 608 | def parse(self, filename): |
paul@9 | 609 | |
paul@9 | 610 | "Parse the file having the given 'filename'." |
paul@9 | 611 | |
paul@0 | 612 | module = compiler.parseFile(filename) |
paul@0 | 613 | self.process(module) |
paul@0 | 614 | |
paul@0 | 615 | def process(self, module): |
paul@9 | 616 | |
paul@9 | 617 | "Process the given 'module'." |
paul@9 | 618 | |
paul@20 | 619 | self.node = self.module = module |
paul@1 | 620 | processed = self.dispatch(module) |
paul@13 | 621 | if self.has_key("__all__"): |
paul@12 | 622 | all = self["__all__"] |
paul@1 | 623 | if isinstance(all, compiler.ast.List): |
paul@1 | 624 | for n in all.nodes: |
paul@12 | 625 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 626 | return processed |
paul@0 | 627 | |
paul@3 | 628 | def vacuum(self): |
paul@9 | 629 | |
paul@9 | 630 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 631 | |
paul@13 | 632 | for name, value in self.items(): |
paul@3 | 633 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 634 | del self[name] |
paul@0 | 635 | |
paul@16 | 636 | # Complain about globals not initialised at the module level. |
paul@16 | 637 | |
paul@16 | 638 | if isinstance(value, Global): |
paul@16 | 639 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 640 | |
paul@3 | 641 | # Namespace methods. |
paul@0 | 642 | |
paul@0 | 643 | def store(self, name, obj): |
paul@12 | 644 | |
paul@12 | 645 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 646 | |
paul@12 | 647 | if not self.namespaces: |
paul@34 | 648 | self.set(name, obj, not self.in_loop) |
paul@12 | 649 | else: |
paul@34 | 650 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 651 | |
paul@12 | 652 | # Record all non-local objects. |
paul@12 | 653 | |
paul@12 | 654 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 655 | self.all_objects.add(obj) |
paul@0 | 656 | |
paul@34 | 657 | def store_instance_attr(self, name): |
paul@12 | 658 | |
paul@12 | 659 | "Record instance attribute 'name' in the current class." |
paul@12 | 660 | |
paul@0 | 661 | if self.in_init: |
paul@15 | 662 | |
paul@12 | 663 | # Current namespace is the function. |
paul@12 | 664 | # Previous namespace is the class. |
paul@15 | 665 | |
paul@12 | 666 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 667 | |
paul@7 | 668 | def get_parent(self): |
paul@12 | 669 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 670 | |
paul@3 | 671 | # Visitor methods. |
paul@3 | 672 | |
paul@3 | 673 | def default(self, node, *args): |
paul@39 | 674 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 675 | |
paul@3 | 676 | def dispatch(self, node, *args): |
paul@3 | 677 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 678 | |
paul@0 | 679 | def NOP(self, node): |
paul@21 | 680 | for n in node.getChildNodes(): |
paul@21 | 681 | self.dispatch(n) |
paul@19 | 682 | return None |
paul@0 | 683 | |
paul@1 | 684 | visitAdd = NOP |
paul@1 | 685 | |
paul@0 | 686 | visitAnd = NOP |
paul@0 | 687 | |
paul@4 | 688 | visitAssert = NOP |
paul@4 | 689 | |
paul@0 | 690 | def visitAssign(self, node): |
paul@0 | 691 | self.expr = self.dispatch(node.expr) |
paul@0 | 692 | for n in node.nodes: |
paul@0 | 693 | self.dispatch(n) |
paul@0 | 694 | return None |
paul@0 | 695 | |
paul@0 | 696 | def visitAssAttr(self, node): |
paul@0 | 697 | expr = self.dispatch(node.expr) |
paul@36 | 698 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 699 | self.store_instance_attr(node.attrname) |
paul@0 | 700 | return None |
paul@0 | 701 | |
paul@0 | 702 | def visitAssList(self, node): |
paul@0 | 703 | for n in node.nodes: |
paul@0 | 704 | self.dispatch(n) |
paul@0 | 705 | return None |
paul@0 | 706 | |
paul@0 | 707 | def visitAssName(self, node): |
paul@36 | 708 | if isinstance(self.expr, Attr): |
paul@36 | 709 | self.store(node.name, self.expr.value) |
paul@36 | 710 | else: |
paul@36 | 711 | self.store(node.name, self.expr) |
paul@0 | 712 | return None |
paul@0 | 713 | |
paul@0 | 714 | visitAssTuple = visitAssList |
paul@0 | 715 | |
paul@2 | 716 | visitAugAssign = NOP |
paul@2 | 717 | |
paul@4 | 718 | visitBackquote = NOP |
paul@4 | 719 | |
paul@2 | 720 | visitBitand = NOP |
paul@2 | 721 | |
paul@2 | 722 | visitBitor = NOP |
paul@2 | 723 | |
paul@4 | 724 | visitBitxor = NOP |
paul@4 | 725 | |
paul@4 | 726 | visitBreak = NOP |
paul@4 | 727 | |
paul@0 | 728 | visitCallFunc = NOP |
paul@0 | 729 | |
paul@0 | 730 | def visitClass(self, node): |
paul@12 | 731 | if self.namespaces: |
paul@41 | 732 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 733 | else: |
paul@24 | 734 | cls = Class(node.name, self.get_parent().full_name(), self, node) |
paul@37 | 735 | |
paul@37 | 736 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 737 | # definitions of classes. |
paul@37 | 738 | |
paul@0 | 739 | for base in node.bases: |
paul@36 | 740 | expr = self.dispatch(base) |
paul@36 | 741 | if isinstance(expr, Attr): |
paul@36 | 742 | if expr.assignments != 1: |
paul@39 | 743 | raise InspectError(self.full_name(), node, |
paul@40 | 744 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 745 | else: |
paul@36 | 746 | cls.add_base(expr.value) |
paul@36 | 747 | else: # if expr is None: |
paul@39 | 748 | raise InspectError(self.full_name(), node, |
paul@40 | 749 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 750 | |
paul@21 | 751 | # Make a back reference from the node for code generation. |
paul@21 | 752 | |
paul@39 | 753 | node.unit = cls |
paul@21 | 754 | |
paul@0 | 755 | # Make an entry for the class. |
paul@0 | 756 | |
paul@0 | 757 | self.store(node.name, cls) |
paul@0 | 758 | |
paul@12 | 759 | self.namespaces.append(cls) |
paul@0 | 760 | self.dispatch(node.code) |
paul@12 | 761 | self.namespaces.pop() |
paul@0 | 762 | |
paul@19 | 763 | return None |
paul@0 | 764 | |
paul@4 | 765 | visitCompare = NOP |
paul@4 | 766 | |
paul@21 | 767 | def visitConst(self, node): |
paul@21 | 768 | const = Const(node.value) |
paul@22 | 769 | self.constant_values[node.value] = const |
paul@24 | 770 | return const |
paul@0 | 771 | |
paul@4 | 772 | visitContinue = NOP |
paul@4 | 773 | |
paul@4 | 774 | visitDecorators = NOP |
paul@4 | 775 | |
paul@0 | 776 | visitDict = NOP |
paul@0 | 777 | |
paul@0 | 778 | visitDiscard = NOP |
paul@0 | 779 | |
paul@4 | 780 | visitDiv = NOP |
paul@4 | 781 | |
paul@4 | 782 | visitEllipsis = NOP |
paul@4 | 783 | |
paul@4 | 784 | visitExec = NOP |
paul@4 | 785 | |
paul@4 | 786 | visitExpression = NOP |
paul@4 | 787 | |
paul@4 | 788 | visitFloorDiv = NOP |
paul@4 | 789 | |
paul@34 | 790 | def visitFor(self, node): |
paul@34 | 791 | self.in_loop = 1 |
paul@34 | 792 | self.NOP(node) |
paul@34 | 793 | self.in_loop = 0 |
paul@0 | 794 | |
paul@0 | 795 | def visitFrom(self, node): |
paul@0 | 796 | if self.importer is None: |
paul@39 | 797 | raise InspectError(self.full_name(), node, |
paul@39 | 798 | "Please use the micropython.Importer class for code which uses the 'from' statement.") |
paul@0 | 799 | |
paul@0 | 800 | module = self.importer.load(node.modname, 1) |
paul@0 | 801 | |
paul@35 | 802 | #if module is None: |
paul@35 | 803 | # print "Warning:", node.modname, "not imported." |
paul@0 | 804 | |
paul@0 | 805 | for name, alias in node.names: |
paul@0 | 806 | if name != "*": |
paul@4 | 807 | if module is not None and module.namespace.has_key(name): |
paul@24 | 808 | attr = module[name] |
paul@36 | 809 | self.store(alias or name, attr.value) |
paul@2 | 810 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 811 | self.importer.load(attr.name) |
paul@24 | 812 | |
paul@24 | 813 | # Support the import of names from missing modules. |
paul@24 | 814 | |
paul@3 | 815 | else: |
paul@24 | 816 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 817 | else: |
paul@3 | 818 | if module is not None: |
paul@3 | 819 | for n in module.namespace.keys(): |
paul@24 | 820 | attr = module[n] |
paul@36 | 821 | self.store(n, attr.value) |
paul@3 | 822 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 823 | self.importer.load(attr.name) |
paul@0 | 824 | |
paul@0 | 825 | return None |
paul@0 | 826 | |
paul@0 | 827 | def visitFunction(self, node): |
paul@3 | 828 | function = Function( |
paul@3 | 829 | node.name, |
paul@7 | 830 | self.get_parent().full_name(), |
paul@3 | 831 | node.argnames, |
paul@17 | 832 | (node.flags & 4 != 0), |
paul@17 | 833 | (node.flags & 8 != 0), |
paul@24 | 834 | self, |
paul@17 | 835 | node |
paul@3 | 836 | ) |
paul@0 | 837 | |
paul@21 | 838 | # Make a back reference from the node for code generation. |
paul@21 | 839 | |
paul@39 | 840 | node.unit = function |
paul@21 | 841 | |
paul@12 | 842 | self.namespaces.append(function) |
paul@15 | 843 | |
paul@12 | 844 | # Current namespace is the function. |
paul@12 | 845 | # Previous namespace is the class. |
paul@15 | 846 | |
paul@12 | 847 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 848 | self.in_init = 1 |
paul@15 | 849 | |
paul@12 | 850 | self.dispatch(node.code) |
paul@12 | 851 | self.in_init = 0 |
paul@12 | 852 | self.namespaces.pop() |
paul@3 | 853 | |
paul@3 | 854 | self.store(node.name, function) |
paul@0 | 855 | return None |
paul@0 | 856 | |
paul@4 | 857 | visitGenExpr = NOP |
paul@4 | 858 | |
paul@4 | 859 | visitGenExprFor = NOP |
paul@4 | 860 | |
paul@4 | 861 | visitGenExprIf = NOP |
paul@4 | 862 | |
paul@4 | 863 | visitGenExprInner = NOP |
paul@4 | 864 | |
paul@0 | 865 | def visitGetattr(self, node): |
paul@0 | 866 | expr = self.dispatch(node.expr) |
paul@36 | 867 | if isinstance(expr, Attr): |
paul@36 | 868 | value = expr.value |
paul@36 | 869 | if isinstance(value, Module): |
paul@36 | 870 | return value.namespace.get(node.attrname) |
paul@36 | 871 | elif isinstance(value, UnresolvedName): |
paul@36 | 872 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@15 | 873 | return builtins.get(node.attrname) |
paul@4 | 874 | |
paul@12 | 875 | def visitGlobal(self, node): |
paul@12 | 876 | if self.namespaces: |
paul@12 | 877 | for name in node.names: |
paul@12 | 878 | self.namespaces[-1].make_global(name) |
paul@37 | 879 | |
paul@37 | 880 | # Record a global entry for the name in the module. |
paul@37 | 881 | |
paul@15 | 882 | if not self.has_key(name): |
paul@16 | 883 | self[name] = Global() |
paul@0 | 884 | |
paul@0 | 885 | def visitIf(self, node): |
paul@0 | 886 | for test, body in node.tests: |
paul@0 | 887 | self.dispatch(body) |
paul@0 | 888 | if node.else_ is not None: |
paul@0 | 889 | self.dispatch(node.else_) |
paul@0 | 890 | return None |
paul@0 | 891 | |
paul@12 | 892 | visitIfExp = NOP |
paul@12 | 893 | |
paul@0 | 894 | def visitImport(self, node): |
paul@0 | 895 | if self.importer is None: |
paul@39 | 896 | raise InspectError(self.full_name(), node, |
paul@39 | 897 | "Please use the micropython.Importer class for code which uses the 'import' statement.") |
paul@0 | 898 | |
paul@0 | 899 | for name, alias in node.names: |
paul@2 | 900 | if alias is not None: |
paul@24 | 901 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 902 | else: |
paul@24 | 903 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 904 | |
paul@0 | 905 | return None |
paul@0 | 906 | |
paul@4 | 907 | visitInvert = NOP |
paul@4 | 908 | |
paul@4 | 909 | visitKeyword = NOP |
paul@4 | 910 | |
paul@4 | 911 | visitLambda = NOP |
paul@4 | 912 | |
paul@1 | 913 | visitLeftShift = NOP |
paul@1 | 914 | |
paul@0 | 915 | visitList = NOP |
paul@0 | 916 | |
paul@4 | 917 | visitListComp = NOP |
paul@4 | 918 | |
paul@4 | 919 | visitListCompFor = NOP |
paul@4 | 920 | |
paul@4 | 921 | visitListCompIf = NOP |
paul@4 | 922 | |
paul@4 | 923 | visitMod = NOP |
paul@4 | 924 | |
paul@0 | 925 | def visitModule(self, node): |
paul@0 | 926 | return self.dispatch(node.node) |
paul@0 | 927 | |
paul@2 | 928 | visitMul = NOP |
paul@2 | 929 | |
paul@0 | 930 | def visitName(self, node): |
paul@0 | 931 | name = node.name |
paul@36 | 932 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 933 | return self.namespaces[-1][name] |
paul@13 | 934 | elif self.has_key(name): |
paul@12 | 935 | return self[name] |
paul@15 | 936 | elif builtins.has_key(name): |
paul@15 | 937 | return builtins[name] |
paul@0 | 938 | else: |
paul@0 | 939 | return None |
paul@0 | 940 | |
paul@0 | 941 | visitNot = NOP |
paul@0 | 942 | |
paul@0 | 943 | visitOr = NOP |
paul@0 | 944 | |
paul@0 | 945 | visitPass = NOP |
paul@0 | 946 | |
paul@4 | 947 | visitPower = NOP |
paul@4 | 948 | |
paul@4 | 949 | visitPrint = NOP |
paul@4 | 950 | |
paul@4 | 951 | visitPrintnl = NOP |
paul@4 | 952 | |
paul@0 | 953 | visitRaise = NOP |
paul@0 | 954 | |
paul@4 | 955 | visitReturn = NOP |
paul@4 | 956 | |
paul@1 | 957 | visitRightShift = NOP |
paul@1 | 958 | |
paul@4 | 959 | visitSlice = NOP |
paul@4 | 960 | |
paul@23 | 961 | visitSliceobj = NOP |
paul@23 | 962 | |
paul@0 | 963 | def visitStmt(self, node): |
paul@0 | 964 | for n in node.nodes: |
paul@0 | 965 | self.dispatch(n) |
paul@0 | 966 | return None |
paul@0 | 967 | |
paul@4 | 968 | visitSub = NOP |
paul@4 | 969 | |
paul@0 | 970 | visitSubscript = NOP |
paul@0 | 971 | |
paul@0 | 972 | def visitTryExcept(self, node): |
paul@0 | 973 | self.dispatch(node.body) |
paul@0 | 974 | for name, var, n in node.handlers: |
paul@0 | 975 | self.dispatch(n) |
paul@0 | 976 | if node.else_ is not None: |
paul@0 | 977 | self.dispatch(node.else_) |
paul@0 | 978 | return None |
paul@0 | 979 | |
paul@4 | 980 | visitTryFinally = NOP |
paul@4 | 981 | |
paul@1 | 982 | visitTuple = NOP |
paul@1 | 983 | |
paul@4 | 984 | visitUnaryAdd = NOP |
paul@4 | 985 | |
paul@4 | 986 | visitUnarySub = NOP |
paul@4 | 987 | |
paul@34 | 988 | def visitWhile(self, node): |
paul@34 | 989 | self.in_loop = 1 |
paul@34 | 990 | self.NOP(node) |
paul@34 | 991 | self.in_loop = 0 |
paul@4 | 992 | |
paul@4 | 993 | visitWith = NOP |
paul@4 | 994 | |
paul@4 | 995 | visitYield = NOP |
paul@4 | 996 | |
paul@16 | 997 | class Global: |
paul@16 | 998 | |
paul@16 | 999 | """ |
paul@16 | 1000 | A reference to an object assigned to a global from outside the module |
paul@16 | 1001 | top-level. |
paul@16 | 1002 | """ |
paul@16 | 1003 | |
paul@16 | 1004 | pass |
paul@16 | 1005 | |
paul@4 | 1006 | # Built-in types initialisation. |
paul@4 | 1007 | |
paul@15 | 1008 | class Builtins(Module): |
paul@15 | 1009 | |
paul@15 | 1010 | "The special built-in types module." |
paul@15 | 1011 | |
paul@15 | 1012 | def __init__(self): |
paul@15 | 1013 | Module.__init__(self, "__builtins__") |
paul@44 | 1014 | self.loaded = 1 |
paul@44 | 1015 | self.module = None |
paul@15 | 1016 | |
paul@15 | 1017 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@15 | 1018 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@15 | 1019 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@15 | 1020 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@15 | 1021 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@15 | 1022 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@15 | 1023 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@15 | 1024 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@15 | 1025 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@15 | 1026 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@15 | 1027 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@15 | 1028 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@15 | 1029 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@15 | 1030 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@25 | 1031 | 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float', |
paul@44 | 1032 | 'frozenset', 'int', # 'list', |
paul@44 | 1033 | 'long', 'object', 'set', 'slice', 'str', |
paul@25 | 1034 | 'tuple', 'type', 'unicode', 'xrange']: |
paul@44 | 1035 | self.store(key, Class(key, self.full_name(), self)) |
paul@44 | 1036 | |
paul@44 | 1037 | # NOTE: Temporary measure - provide detailed built-ins. |
paul@44 | 1038 | |
paul@44 | 1039 | cls = Class("list", self.full_name(), self) |
paul@44 | 1040 | cls.set("__iter__", Function("__iter__", cls.full_name(), [], 0, 0, cls)) |
paul@44 | 1041 | self.store("list", cls) |
paul@44 | 1042 | |
paul@44 | 1043 | cls = Class("listiterator", self.full_name(), self) |
paul@44 | 1044 | cls.set("next", Function("next", cls.full_name(), [], 0, 0, cls)) |
paul@44 | 1045 | self.store("listiterator", cls) |
paul@15 | 1046 | |
paul@25 | 1047 | # NOTE: Incomplete: some functions have more than one parameter. |
paul@25 | 1048 | |
paul@25 | 1049 | for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr', |
paul@25 | 1050 | 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod', |
paul@25 | 1051 | 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals', |
paul@25 | 1052 | 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance', |
paul@25 | 1053 | 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct', |
paul@25 | 1054 | 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce', |
paul@25 | 1055 | 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted', |
paul@25 | 1056 | 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']: |
paul@24 | 1057 | self[key] = Function(key, self.full_name(), ['arg'], 0, 0, self) |
paul@16 | 1058 | |
paul@44 | 1059 | def store(self, name, obj): |
paul@44 | 1060 | self.set(name, obj) |
paul@44 | 1061 | self.all_objects.add(obj) |
paul@44 | 1062 | |
paul@44 | 1063 | def vacuum(self): |
paul@44 | 1064 | pass |
paul@44 | 1065 | |
paul@15 | 1066 | builtins = Builtins() |
paul@0 | 1067 | |
paul@0 | 1068 | # vim: tabstop=4 expandtab shiftwidth=4 |