paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@17 | 5 | |
paul@39 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@17 | 7 | |
paul@17 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@17 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@17 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@17 | 11 | version. |
paul@17 | 12 | |
paul@17 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@17 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@17 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@17 | 16 | details. |
paul@17 | 17 | |
paul@17 | 18 | You should have received a copy of the GNU General Public License along with |
paul@17 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@17 | 20 | """ |
paul@17 | 21 | |
paul@17 | 22 | import micropython.inspect |
paul@17 | 23 | from micropython.rsvp import * |
paul@39 | 24 | from micropython.common import * |
paul@17 | 25 | import compiler.ast |
paul@17 | 26 | from compiler.visitor import ASTVisitor |
paul@17 | 27 | try: |
paul@17 | 28 | set |
paul@17 | 29 | except NameError: |
paul@17 | 30 | from sets import Set as set |
paul@17 | 31 | |
paul@39 | 32 | class TranslateError(ProcessingError): pass |
paul@17 | 33 | |
paul@19 | 34 | class Label: |
paul@19 | 35 | |
paul@19 | 36 | "A reference to a location." |
paul@19 | 37 | |
paul@19 | 38 | def __init__(self, number, location=None): |
paul@19 | 39 | self.number = number |
paul@19 | 40 | self.location = location |
paul@19 | 41 | |
paul@19 | 42 | def __repr__(self): |
paul@19 | 43 | return "Label(%r, location=%r)" % (self.number, self.location) |
paul@19 | 44 | |
paul@17 | 45 | # Program visitors. |
paul@17 | 46 | |
paul@17 | 47 | class Translation(ASTVisitor): |
paul@17 | 48 | |
paul@17 | 49 | "A translated module." |
paul@17 | 50 | |
paul@21 | 51 | def __init__(self, module, objtable, paramtable): |
paul@18 | 52 | |
paul@21 | 53 | """ |
paul@21 | 54 | Initialise the translation with an inspected 'module' and an attribute |
paul@21 | 55 | table 'objtable' and parameter table 'paramtable'. |
paul@21 | 56 | """ |
paul@18 | 57 | |
paul@17 | 58 | ASTVisitor.__init__(self) |
paul@17 | 59 | self.visitor = self |
paul@17 | 60 | self.module = module |
paul@21 | 61 | self.objtable = objtable |
paul@21 | 62 | self.paramtable = paramtable |
paul@17 | 63 | self.unit = None |
paul@17 | 64 | |
paul@19 | 65 | # Wiring within the code. |
paul@19 | 66 | |
paul@19 | 67 | self.labels = {} |
paul@19 | 68 | self.label_number = 0 |
paul@41 | 69 | self.loop_labels = [] |
paul@41 | 70 | |
paul@41 | 71 | # The code itself. |
paul@41 | 72 | |
paul@19 | 73 | self.code = None |
paul@19 | 74 | |
paul@17 | 75 | def get_module_code(self): |
paul@17 | 76 | |
paul@17 | 77 | "Return the top-level module code." |
paul@17 | 78 | |
paul@17 | 79 | self.unit = self.module |
paul@19 | 80 | self.code = [] |
paul@44 | 81 | if self.module.module is not None: |
paul@44 | 82 | self.dispatch(self.module.module) |
paul@19 | 83 | return self.code |
paul@17 | 84 | |
paul@17 | 85 | def get_code(self, unit): |
paul@17 | 86 | |
paul@17 | 87 | "Return the code for the given 'unit'." |
paul@17 | 88 | |
paul@17 | 89 | self.unit = unit |
paul@19 | 90 | self.code = [] |
paul@25 | 91 | if unit.node is not None: |
paul@25 | 92 | self.dispatch(unit.node) |
paul@19 | 93 | return self.code |
paul@17 | 94 | |
paul@17 | 95 | def __repr__(self): |
paul@17 | 96 | return "Translation(%r)" % self.module |
paul@17 | 97 | |
paul@17 | 98 | def get_scope(self, name): |
paul@17 | 99 | if self.unit.has_key(name): |
paul@17 | 100 | return "local" |
paul@17 | 101 | elif self.module.has_key(name): |
paul@17 | 102 | return "global" |
paul@17 | 103 | else: |
paul@17 | 104 | return "builtins" |
paul@17 | 105 | |
paul@19 | 106 | # Code writing methods. |
paul@19 | 107 | |
paul@19 | 108 | def new_label(self): |
paul@44 | 109 | |
paul@44 | 110 | "Return a new label object for use with set_label." |
paul@44 | 111 | |
paul@19 | 112 | number = self.label_number |
paul@19 | 113 | label = Label(number) |
paul@19 | 114 | self.labels[label] = label |
paul@19 | 115 | self.label_number += 1 |
paul@19 | 116 | return label |
paul@19 | 117 | |
paul@19 | 118 | def set_label(self, label): |
paul@19 | 119 | |
paul@19 | 120 | """ |
paul@19 | 121 | Set the location of 'label' to that within the entire image: the |
paul@19 | 122 | location within the code combined with location of the code unit. |
paul@19 | 123 | """ |
paul@19 | 124 | |
paul@19 | 125 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 126 | |
paul@19 | 127 | def new_op(self, op): |
paul@44 | 128 | |
paul@44 | 129 | "Add 'op' to the generated code." |
paul@44 | 130 | |
paul@19 | 131 | self.code.append(op) |
paul@19 | 132 | |
paul@43 | 133 | def replace_op(self, op): |
paul@44 | 134 | |
paul@44 | 135 | "Replace the last added instruction with 'op'." |
paul@44 | 136 | |
paul@43 | 137 | self.code[-1] = op |
paul@43 | 138 | |
paul@26 | 139 | def last_op(self): |
paul@44 | 140 | |
paul@44 | 141 | "Return the last added instruction." |
paul@44 | 142 | |
paul@26 | 143 | return self.code[-1] |
paul@26 | 144 | |
paul@17 | 145 | # Visitor methods. |
paul@17 | 146 | |
paul@17 | 147 | def default(self, node, *args): |
paul@39 | 148 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@17 | 149 | |
paul@17 | 150 | def dispatch(self, node, *args): |
paul@17 | 151 | return ASTVisitor.dispatch(self, node, *args) |
paul@17 | 152 | |
paul@43 | 153 | def _visitAttr(self, node, classes): |
paul@44 | 154 | |
paul@44 | 155 | """ |
paul@44 | 156 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 157 | given 'classes'. |
paul@44 | 158 | """ |
paul@44 | 159 | |
paul@43 | 160 | self.dispatch(node.expr) |
paul@44 | 161 | self._generateAttr(node.attrname, classes) |
paul@43 | 162 | |
paul@44 | 163 | def _generateAttr(self, attrname, classes): |
paul@44 | 164 | |
paul@44 | 165 | """ |
paul@44 | 166 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 167 | """ |
paul@44 | 168 | |
paul@44 | 169 | AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 170 | # NOTE: Only simple cases are used for optimisations. |
paul@43 | 171 | |
paul@43 | 172 | last = self.last_op() |
paul@43 | 173 | if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1: |
paul@43 | 174 | |
paul@43 | 175 | # Get the details of the access. |
paul@43 | 176 | |
paul@43 | 177 | target = last.attr.value |
paul@43 | 178 | target_name = target.full_name() |
paul@43 | 179 | table_entry = self.objtable.table[target_name] |
paul@44 | 180 | pos = table_entry[attrname] |
paul@43 | 181 | self.replace_op(AttrInstruction(pos)) |
paul@43 | 182 | |
paul@43 | 183 | else: |
paul@44 | 184 | index = self.objtable.get_index(attrname) |
paul@43 | 185 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 186 | |
paul@17 | 187 | def _visitName(self, node, classes): |
paul@44 | 188 | |
paul@44 | 189 | """ |
paul@44 | 190 | Visit the name-related 'node', generating instructions based on the |
paul@44 | 191 | given 'classes'. |
paul@44 | 192 | """ |
paul@44 | 193 | |
paul@17 | 194 | name = node.name |
paul@17 | 195 | scope = self.get_scope(name) |
paul@37 | 196 | #print self.module.name, node.lineno, name, scope |
paul@44 | 197 | self._generateName(name, scope, classes, node) |
paul@44 | 198 | |
paul@44 | 199 | def _generateName(self, name, scope, classes, node): |
paul@44 | 200 | |
paul@44 | 201 | """ |
paul@44 | 202 | Generate code for the access to 'name' in 'scope' using the given |
paul@44 | 203 | 'classes', and using the given 'node' as the source of the access. |
paul@44 | 204 | """ |
paul@44 | 205 | |
paul@17 | 206 | NameInstruction, AttrInstruction = classes |
paul@17 | 207 | |
paul@17 | 208 | if scope == "local": |
paul@17 | 209 | unit = self.unit |
paul@17 | 210 | if isinstance(unit, micropython.inspect.Function): |
paul@38 | 211 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@17 | 212 | elif isinstance(unit, micropython.inspect.Class): |
paul@19 | 213 | self.new_op(AttrInstruction(unit.all_class_attributes()[name])) |
paul@17 | 214 | elif isinstance(unit, micropython.inspect.Module): |
paul@19 | 215 | self.new_op(AttrInstruction(unit.module_attributes()[name])) |
paul@17 | 216 | else: |
paul@39 | 217 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r" % (unit, name)) |
paul@17 | 218 | |
paul@17 | 219 | elif scope == "global": |
paul@17 | 220 | globals = self.module.module_attributes() |
paul@17 | 221 | if globals.has_key(name): |
paul@19 | 222 | self.new_op(AttrInstruction(globals[name])) |
paul@17 | 223 | else: |
paul@39 | 224 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r" % (self.module, name)) |
paul@17 | 225 | |
paul@17 | 226 | else: |
paul@17 | 227 | builtins = micropython.inspect.builtins.module_attributes() |
paul@19 | 228 | self.new_op(AttrInstruction(builtins[name])) |
paul@17 | 229 | |
paul@21 | 230 | def visitAdd(self, node): |
paul@17 | 231 | |
paul@21 | 232 | """ |
paul@21 | 233 | _t1 = node.left |
paul@21 | 234 | _t2 = node.right |
paul@21 | 235 | try: |
paul@21 | 236 | _t1.__add__(_t2) |
paul@21 | 237 | except AttributeError: |
paul@21 | 238 | _t2.__radd__(_t1) |
paul@21 | 239 | """ |
paul@17 | 240 | |
paul@19 | 241 | def visitAnd(self, node): pass |
paul@17 | 242 | |
paul@19 | 243 | def visitAssert(self, node): pass |
paul@17 | 244 | |
paul@17 | 245 | def visitAssign(self, node): |
paul@19 | 246 | self.dispatch(node.expr) |
paul@17 | 247 | for n in node.nodes: |
paul@19 | 248 | self.dispatch(n) |
paul@17 | 249 | |
paul@43 | 250 | def visitAssAttr(self, node): |
paul@43 | 251 | self._visitAttr(node, (StoreAttr, StoreAttrIndex)) |
paul@17 | 252 | |
paul@19 | 253 | def visitAssList(self, node): pass |
paul@17 | 254 | |
paul@17 | 255 | def visitAssName(self, node): |
paul@19 | 256 | self._visitName(node, (StoreName, StoreAttr)) |
paul@17 | 257 | |
paul@17 | 258 | visitAssTuple = visitAssList |
paul@17 | 259 | |
paul@19 | 260 | def visitAugAssign(self, node): pass |
paul@17 | 261 | |
paul@19 | 262 | def visitBackquote(self, node): pass |
paul@17 | 263 | |
paul@19 | 264 | def visitBitand(self, node): pass |
paul@17 | 265 | |
paul@19 | 266 | def visitBitor(self, node): pass |
paul@17 | 267 | |
paul@19 | 268 | def visitBitxor(self, node): pass |
paul@17 | 269 | |
paul@41 | 270 | def visitBreak(self, node): |
paul@41 | 271 | next_label, exit_label = self.loop_labels[-1] |
paul@41 | 272 | self.new_op(Jump(exit_label)) |
paul@17 | 273 | |
paul@21 | 274 | def visitCallFunc(self, node): |
paul@21 | 275 | |
paul@21 | 276 | """ |
paul@21 | 277 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@21 | 278 | the correct location, then invoke the function. |
paul@21 | 279 | """ |
paul@21 | 280 | |
paul@45 | 281 | # Mark the frame, evaluate the target, generate the call. |
paul@45 | 282 | |
paul@45 | 283 | self._startCallFunc() |
paul@45 | 284 | self.dispatch(node.node) |
paul@45 | 285 | self._generateCallFunc(node.args, node) |
paul@45 | 286 | |
paul@45 | 287 | def _startCallFunc(self): |
paul@45 | 288 | |
paul@45 | 289 | "Record the location of the invocation." |
paul@21 | 290 | |
paul@21 | 291 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 292 | |
paul@44 | 293 | def _generateCallFunc(self, args, node): |
paul@44 | 294 | |
paul@26 | 295 | # NOTE: Only simple cases are used for optimisations. |
paul@26 | 296 | |
paul@26 | 297 | last = self.last_op() |
paul@26 | 298 | if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1: |
paul@26 | 299 | target = last.attr.value |
paul@45 | 300 | context = last.attr.parent |
paul@26 | 301 | else: |
paul@26 | 302 | target = None |
paul@45 | 303 | context = None |
paul@45 | 304 | |
paul@45 | 305 | # Where a target is known and has a known context, avoid generating any |
paul@45 | 306 | # first argument. |
paul@45 | 307 | |
paul@45 | 308 | if context is not None: |
paul@45 | 309 | pass # NOTE: Class methods should be supported. |
paul@45 | 310 | else: |
paul@45 | 311 | continue_label = self.new_label() |
paul@45 | 312 | self.new_op(LoadContext()) |
paul@45 | 313 | self.new_op(CheckContext()) |
paul@45 | 314 | self.new_op(JumpIfTrue(continue_label)) |
paul@45 | 315 | self.new_op(LoadConst("TypeError")) # NOTE: Do this properly! |
paul@45 | 316 | self.new_op(RaiseException()) |
paul@45 | 317 | self.set_label(continue_label) |
paul@26 | 318 | |
paul@21 | 319 | # Evaluate the arguments. |
paul@21 | 320 | |
paul@22 | 321 | positional = 1 |
paul@22 | 322 | |
paul@44 | 323 | for i, arg in enumerate(args): |
paul@21 | 324 | if isinstance(arg, compiler.ast.Keyword): |
paul@22 | 325 | if positional: |
paul@44 | 326 | self.new_op(ReserveFrame(len(args) - i)) |
paul@22 | 327 | positional = 0 |
paul@22 | 328 | |
paul@21 | 329 | self.dispatch(arg.expr) |
paul@21 | 330 | |
paul@26 | 331 | # Optimise where the target is known now. |
paul@26 | 332 | |
paul@26 | 333 | if target is not None: |
paul@39 | 334 | |
paul@39 | 335 | # Find the parameter table entry for the target. |
paul@39 | 336 | |
paul@39 | 337 | target_name = target.full_name() |
paul@39 | 338 | |
paul@39 | 339 | # Look for a callable with the precise target name. |
paul@39 | 340 | |
paul@39 | 341 | try: |
paul@39 | 342 | table_entry = self.paramtable.table[target_name] |
paul@39 | 343 | |
paul@39 | 344 | # Where no callable is present, check to see if it is a |
paul@39 | 345 | # class name and find the initialiser instead. |
paul@39 | 346 | |
paul@39 | 347 | except KeyError: |
paul@39 | 348 | if self.objtable.table.has_key(target_name): |
paul@39 | 349 | table_entry = self.paramtable.table[target_name + ".__init__"] |
paul@39 | 350 | else: |
paul@39 | 351 | raise |
paul@39 | 352 | |
paul@39 | 353 | pos = table_entry[arg.name] |
paul@26 | 354 | self.new_op(StoreFrame(pos)) |
paul@26 | 355 | |
paul@26 | 356 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 357 | # the parameter location. |
paul@26 | 358 | |
paul@26 | 359 | else: |
paul@21 | 360 | |
paul@26 | 361 | # Combine the target details with the name to get the location. |
paul@26 | 362 | # See the access method on the List class. |
paul@26 | 363 | |
paul@39 | 364 | try: |
paul@39 | 365 | paramindex = self.paramtable.get_index(arg.name) |
paul@39 | 366 | except ValueError: |
paul@39 | 367 | raise TranslateError(self.module.full_name(), node, "No parameter definition exists for %r." % arg.name) |
paul@21 | 368 | |
paul@26 | 369 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 370 | |
paul@26 | 371 | # use (callable+0)+paramindex+table |
paul@26 | 372 | # checks embedded offset against (callable+0) |
paul@26 | 373 | # moves the top of stack to frame+position |
paul@21 | 374 | |
paul@21 | 375 | else: |
paul@21 | 376 | self.dispatch(arg) |
paul@21 | 377 | |
paul@21 | 378 | self.new_op(LoadCallable()) # uses the start of the frame to get the callable |
paul@21 | 379 | self.new_op(Jump()) |
paul@21 | 380 | |
paul@21 | 381 | # NOTE: Exception handling required. |
paul@21 | 382 | |
paul@21 | 383 | self.new_op(DropFrame()) |
paul@17 | 384 | |
paul@38 | 385 | def visitClass(self, node): |
paul@38 | 386 | unit = self.unit |
paul@39 | 387 | self.unit = node.unit |
paul@44 | 388 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 389 | self.dispatch(node.code) |
paul@38 | 390 | self.unit = unit |
paul@17 | 391 | |
paul@21 | 392 | def visitCompare(self, node): |
paul@21 | 393 | |
paul@21 | 394 | """ |
paul@21 | 395 | self.dispatch(node.expr) |
paul@21 | 396 | for op_name, next_node in compare.ops: |
paul@21 | 397 | methods = self.comparison_methods[op_name] |
paul@21 | 398 | if methods is not None: |
paul@21 | 399 | # Generate method call using evaluated argument and next node. |
paul@21 | 400 | else: |
paul@21 | 401 | # Deal with the special operators. |
paul@21 | 402 | # Provide short-circuiting. |
paul@21 | 403 | """ |
paul@17 | 404 | |
paul@22 | 405 | def visitConst(self, node): |
paul@22 | 406 | const = self.module.constant_values[node.value] |
paul@22 | 407 | self.new_op(LoadConst(const)) |
paul@17 | 408 | |
paul@41 | 409 | def visitContinue(self, node): |
paul@41 | 410 | next_label, exit_label = self.loop_labels[-1] |
paul@41 | 411 | self.new_op(Jump(next_label)) |
paul@17 | 412 | |
paul@19 | 413 | def visitDecorators(self, node): pass |
paul@17 | 414 | |
paul@19 | 415 | def visitDict(self, node): pass |
paul@17 | 416 | |
paul@18 | 417 | def visitDiscard(self, node): |
paul@19 | 418 | self.dispatch(node.expr) |
paul@17 | 419 | |
paul@19 | 420 | def visitDiv(self, node): pass |
paul@17 | 421 | |
paul@19 | 422 | def visitEllipsis(self, node): pass |
paul@17 | 423 | |
paul@19 | 424 | def visitExec(self, node): pass |
paul@17 | 425 | |
paul@19 | 426 | def visitExpression(self, node): pass |
paul@17 | 427 | |
paul@19 | 428 | def visitFloorDiv(self, node): pass |
paul@17 | 429 | |
paul@44 | 430 | def visitFor(self, node): |
paul@44 | 431 | exit_label = self.new_label() |
paul@44 | 432 | next_label = self.new_label() |
paul@44 | 433 | else_label = self.new_label() |
paul@44 | 434 | |
paul@44 | 435 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 436 | |
paul@45 | 437 | self._startCallFunc() |
paul@44 | 438 | self.dispatch(node.list) |
paul@44 | 439 | self._generateAttr("__iter__", (LoadAttr, LoadAttrIndex)) |
paul@44 | 440 | self._generateCallFunc([], node) |
paul@44 | 441 | # Iterator on stack. |
paul@44 | 442 | |
paul@44 | 443 | # In the loop... |
paul@44 | 444 | |
paul@44 | 445 | self.set_label(next_label) |
paul@44 | 446 | |
paul@44 | 447 | # Use the iterator to get the next value. |
paul@44 | 448 | |
paul@45 | 449 | self._startCallFunc() |
paul@44 | 450 | self.new_op(Duplicate()) |
paul@44 | 451 | self._generateAttr("next", (LoadAttr, LoadAttrIndex)) |
paul@44 | 452 | self._generateCallFunc([], node) |
paul@44 | 453 | |
paul@44 | 454 | # Test for StopIteration. |
paul@44 | 455 | |
paul@44 | 456 | self.new_op(CheckException("StopIteration")) # NOTE: To be done properly. |
paul@44 | 457 | if node.else_ is not None: |
paul@44 | 458 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 459 | else: |
paul@44 | 460 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 461 | |
paul@44 | 462 | # Assign to the target. |
paul@44 | 463 | |
paul@44 | 464 | self.dispatch(node.assign) |
paul@44 | 465 | |
paul@44 | 466 | # Process the body with the current next and exit points. |
paul@44 | 467 | |
paul@44 | 468 | self.loop_labels.append((next_label, exit_label)) |
paul@44 | 469 | self.dispatch(node.body) |
paul@44 | 470 | self.loop_labels.pop() |
paul@44 | 471 | |
paul@44 | 472 | # Repeat the loop. |
paul@44 | 473 | |
paul@44 | 474 | self.new_op(Jump(next_label)) |
paul@44 | 475 | |
paul@44 | 476 | # Produce the "else" section. |
paul@44 | 477 | |
paul@44 | 478 | if node.else_ is not None: |
paul@44 | 479 | self.set_label(exit_label) |
paul@44 | 480 | self.dispatch(node.else_) |
paul@44 | 481 | |
paul@44 | 482 | # Pop the iterator. |
paul@44 | 483 | |
paul@44 | 484 | self.set_label(exit_label) |
paul@44 | 485 | self.new_op(Pop()) |
paul@17 | 486 | |
paul@19 | 487 | def visitFrom(self, node): pass |
paul@17 | 488 | |
paul@17 | 489 | def visitFunction(self, node): |
paul@17 | 490 | |
paul@17 | 491 | # Only store the name when visiting this node from outside. |
paul@17 | 492 | |
paul@39 | 493 | if self.unit is not node.unit: |
paul@39 | 494 | self.new_op(LoadConst(node.unit)) |
paul@19 | 495 | self._visitName(node, (StoreName, StoreAttr)) |
paul@22 | 496 | |
paul@22 | 497 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 498 | |
paul@17 | 499 | else: |
paul@19 | 500 | self.dispatch(node.code) |
paul@21 | 501 | self.new_op(Return()) |
paul@17 | 502 | |
paul@19 | 503 | def visitGenExpr(self, node): pass |
paul@17 | 504 | |
paul@19 | 505 | def visitGenExprFor(self, node): pass |
paul@17 | 506 | |
paul@19 | 507 | def visitGenExprIf(self, node): pass |
paul@17 | 508 | |
paul@19 | 509 | def visitGenExprInner(self, node): pass |
paul@17 | 510 | |
paul@43 | 511 | def visitGetattr(self, node): |
paul@43 | 512 | self._visitAttr(node, (LoadAttr, LoadAttrIndex)) |
paul@17 | 513 | |
paul@19 | 514 | def visitGlobal(self, node): pass |
paul@17 | 515 | |
paul@17 | 516 | def visitIf(self, node): |
paul@19 | 517 | first = 1 |
paul@19 | 518 | exit_label = self.new_label() |
paul@17 | 519 | |
paul@19 | 520 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 521 | if body is None: |
paul@19 | 522 | break |
paul@19 | 523 | if not first: |
paul@19 | 524 | self.set_label(next_label) |
paul@19 | 525 | if test is not None: |
paul@19 | 526 | self.dispatch(test) |
paul@19 | 527 | next_label = self.new_label() |
paul@41 | 528 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 529 | self.dispatch(body) |
paul@19 | 530 | self.new_op(Jump(exit_label)) |
paul@19 | 531 | first = 0 |
paul@17 | 532 | |
paul@19 | 533 | self.set_label(exit_label) |
paul@17 | 534 | |
paul@19 | 535 | def visitImport(self, node): pass |
paul@19 | 536 | |
paul@19 | 537 | def visitInvert(self, node): pass |
paul@17 | 538 | |
paul@19 | 539 | def visitKeyword(self, node): pass |
paul@17 | 540 | |
paul@19 | 541 | def visitLambda(self, node): pass |
paul@19 | 542 | |
paul@19 | 543 | def visitLeftShift(self, node): pass |
paul@17 | 544 | |
paul@19 | 545 | def visitList(self, node): pass |
paul@17 | 546 | |
paul@19 | 547 | def visitListComp(self, node): pass |
paul@17 | 548 | |
paul@19 | 549 | def visitListCompFor(self, node): pass |
paul@17 | 550 | |
paul@19 | 551 | def visitListCompIf(self, node): pass |
paul@19 | 552 | |
paul@19 | 553 | def visitMod(self, node): pass |
paul@17 | 554 | |
paul@17 | 555 | def visitModule(self, node): |
paul@19 | 556 | self.dispatch(node.node) |
paul@17 | 557 | |
paul@19 | 558 | def visitMul(self, node): pass |
paul@17 | 559 | |
paul@17 | 560 | def visitName(self, node): |
paul@19 | 561 | self._visitName(node, (LoadName, LoadAttr)) |
paul@17 | 562 | |
paul@19 | 563 | def visitNot(self, node): pass |
paul@17 | 564 | |
paul@19 | 565 | def visitOr(self, node): pass |
paul@17 | 566 | |
paul@19 | 567 | def visitPass(self, node): pass |
paul@17 | 568 | |
paul@19 | 569 | def visitPower(self, node): pass |
paul@17 | 570 | |
paul@19 | 571 | def visitPrint(self, node): pass |
paul@17 | 572 | |
paul@19 | 573 | def visitPrintnl(self, node): pass |
paul@17 | 574 | |
paul@19 | 575 | def visitRaise(self, node): pass |
paul@17 | 576 | |
paul@21 | 577 | def visitReturn(self, node): |
paul@21 | 578 | if node.value is not None: |
paul@21 | 579 | self.dispatch(node.value) |
paul@21 | 580 | self.new_op(Return()) |
paul@17 | 581 | |
paul@19 | 582 | def visitRightShift(self, node): pass |
paul@17 | 583 | |
paul@19 | 584 | def visitSlice(self, node): pass |
paul@17 | 585 | |
paul@17 | 586 | def visitStmt(self, node): |
paul@17 | 587 | for n in node.nodes: |
paul@19 | 588 | self.dispatch(n) |
paul@19 | 589 | |
paul@19 | 590 | def visitSub(self, node): pass |
paul@17 | 591 | |
paul@19 | 592 | def visitSubscript(self, node): pass |
paul@17 | 593 | |
paul@19 | 594 | def visitTryExcept(self, node): pass |
paul@17 | 595 | |
paul@19 | 596 | def visitTryFinally(self, node): pass |
paul@17 | 597 | |
paul@19 | 598 | def visitTuple(self, node): pass |
paul@17 | 599 | |
paul@19 | 600 | def visitUnaryAdd(self, node): pass |
paul@17 | 601 | |
paul@19 | 602 | def visitUnarySub(self, node): pass |
paul@17 | 603 | |
paul@41 | 604 | def visitWhile(self, node): |
paul@41 | 605 | exit_label = self.new_label() |
paul@44 | 606 | next_label = self.new_label() |
paul@44 | 607 | else_label = self.new_label() |
paul@41 | 608 | |
paul@41 | 609 | self.set_label(next_label) |
paul@44 | 610 | self.dispatch(node.test) |
paul@44 | 611 | if node.else_ is not None: |
paul@44 | 612 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 613 | else: |
paul@44 | 614 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 615 | |
paul@41 | 616 | self.loop_labels.append((next_label, exit_label)) |
paul@41 | 617 | |
paul@41 | 618 | self.dispatch(node.body) |
paul@41 | 619 | self.new_op(Jump(next_label)) |
paul@41 | 620 | |
paul@41 | 621 | if node.else_ is not None: |
paul@44 | 622 | self.set_label(else_label) |
paul@41 | 623 | self.dispatch(node.else_) |
paul@41 | 624 | |
paul@41 | 625 | self.set_label(exit_label) |
paul@41 | 626 | self.loop_labels.pop() |
paul@17 | 627 | |
paul@19 | 628 | def visitWith(self, node): pass |
paul@17 | 629 | |
paul@19 | 630 | def visitYield(self, node): pass |
paul@17 | 631 | |
paul@21 | 632 | # Useful data. |
paul@21 | 633 | |
paul@21 | 634 | comparison_methods = { |
paul@21 | 635 | "==" : ("__eq__", "__ne__"), |
paul@21 | 636 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 637 | "<" : ("__lt__", "__gt__"), |
paul@21 | 638 | "<=" : ("__le__", "__ge__"), |
paul@21 | 639 | ">=" : ("__ge__", "__le__"), |
paul@21 | 640 | ">" : ("__gt__", "__lt__"), |
paul@21 | 641 | "is" : None, |
paul@21 | 642 | "is not" : None, |
paul@21 | 643 | "in" : None, |
paul@21 | 644 | "not in" : None |
paul@21 | 645 | } |
paul@21 | 646 | |
paul@17 | 647 | # vim: tabstop=4 expandtab shiftwidth=4 |