paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@24 | 4 | Simplify AST structures for easier type propagation and analysis. The code in |
paulb@24 | 5 | this module processes AST trees originating from the compiler module and |
paulb@24 | 6 | produces a result tree consisting of instruction-oriented program nodes. |
paul@0 | 7 | |
paulb@257 | 8 | Some of the simplified nodes will be altered by the fixnames module, principally |
paulb@257 | 9 | LoadName and StoreName nodes which, in the case of globals and builtins, will be |
paulb@257 | 10 | changed to LoadAttr and StoreAttr respectively. |
paulb@257 | 11 | |
paulb@257 | 12 | -------- |
paulb@257 | 13 | |
paulb@178 | 14 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 15 | |
paul@0 | 16 | This software is free software; you can redistribute it and/or |
paul@0 | 17 | modify it under the terms of the GNU General Public License as |
paul@0 | 18 | published by the Free Software Foundation; either version 2 of |
paul@0 | 19 | the License, or (at your option) any later version. |
paul@0 | 20 | |
paul@0 | 21 | This software is distributed in the hope that it will be useful, |
paul@0 | 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 24 | GNU General Public License for more details. |
paul@0 | 25 | |
paul@0 | 26 | You should have received a copy of the GNU General Public |
paul@0 | 27 | License along with this library; see the file LICENCE.txt |
paul@0 | 28 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 30 | |
paulb@46 | 31 | -------- |
paulb@46 | 32 | |
paulb@57 | 33 | To use this module, the easiest approach is to use the simplify function: |
paulb@57 | 34 | |
paulb@57 | 35 | simplify(filename) |
paulb@57 | 36 | |
paulb@57 | 37 | The more complicated approach involves first instantiating a Simplifier object: |
paulb@46 | 38 | |
paulb@46 | 39 | simplifier = Simplifier() |
paulb@46 | 40 | |
paulb@57 | 41 | Then, applying the simplifier to an AST tree: |
paulb@46 | 42 | |
paulb@57 | 43 | module = compiler.parseFile(filename) |
paulb@46 | 44 | simplifier.process(module) |
paul@0 | 45 | """ |
paul@0 | 46 | |
paulb@220 | 47 | from simplify.simplified import * |
paul@0 | 48 | import compiler.ast |
paulb@88 | 49 | import os |
paul@0 | 50 | |
paulb@19 | 51 | class Simplifier(Visitor): |
paul@0 | 52 | |
paul@0 | 53 | """ |
paul@0 | 54 | A simplifying visitor for AST nodes. |
paul@0 | 55 | |
paulb@202 | 56 | Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign, |
paulb@203 | 57 | AugAssign, Bitand, Break, CallFunc, Class, Compare, Const, |
paulb@203 | 58 | Continue, Dict, Discard, Div, FloorDiv, For, From, Function, |
paulb@203 | 59 | Getattr, Global, If, Import, Invert, Keyword, Lambda, List, |
paulb@203 | 60 | ListComp, ListCompFor, ListCompIf, Mod, Module, Mul, Name, Not, Or, |
paulb@203 | 61 | Pass, Power, Print, Printnl, Raise, Return, Slice, Sliceobj, Stmt, |
paulb@203 | 62 | Sub, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@203 | 63 | UnarySub. |
paul@0 | 64 | |
paulb@203 | 65 | Missing: Backquote, Bitor, Bitxor, Decorators, Ellipsis, Exec, LeftShift, |
paulb@203 | 66 | RightShift, Yield. |
paul@0 | 67 | """ |
paul@0 | 68 | |
paulb@34 | 69 | def __init__(self, builtins=0): |
paulb@46 | 70 | |
paulb@46 | 71 | """ |
paulb@46 | 72 | Initialise the simplifier with the optional 'builtins' parameter |
paulb@46 | 73 | indicating whether the module contains the built-in classes and |
paulb@46 | 74 | functions. |
paulb@46 | 75 | """ |
paulb@46 | 76 | |
paulb@19 | 77 | Visitor.__init__(self) |
paul@1 | 78 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 79 | self.structures = [] # Structures/classes. |
paulb@24 | 80 | self.constants = {} # Constants. |
paul@1 | 81 | self.current_subprograms = [] # Current subprograms being processed. |
paulb@120 | 82 | self.current_structures = [] # Current structures being processed. |
paulb@172 | 83 | self.within_class = 0 # Whether a class is being defined. |
paulb@34 | 84 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 85 | |
paulb@65 | 86 | # Convenience attributes. |
paulb@65 | 87 | |
paulb@65 | 88 | self.subnames = {} |
paulb@65 | 89 | |
paulb@51 | 90 | # For compiler package mechanisms. |
paulb@51 | 91 | |
paulb@51 | 92 | self.visitor = self |
paulb@51 | 93 | |
paulb@255 | 94 | def process(self, module, name): |
paulb@255 | 95 | |
paulb@255 | 96 | """ |
paulb@255 | 97 | Process the 'module' having the given 'name'. Return the simplified node |
paulb@255 | 98 | representing the 'module'. |
paulb@255 | 99 | """ |
paulb@255 | 100 | |
paulb@255 | 101 | result = self.dispatch(module, name) |
paulb@57 | 102 | result.simplifier = self |
paulb@57 | 103 | return result |
paulb@20 | 104 | |
paulb@6 | 105 | def dispatch_or_none(self, node, *args): |
paulb@255 | 106 | |
paulb@255 | 107 | """ |
paulb@255 | 108 | Dispatch to a handler for 'node', returning the result, or if 'node' is |
paulb@255 | 109 | None then return a node which loads None in the simplified program. |
paulb@255 | 110 | """ |
paulb@255 | 111 | |
paulb@6 | 112 | if node is not None: |
paulb@6 | 113 | return self.dispatch(node, *args) |
paulb@6 | 114 | else: |
paulb@79 | 115 | return LoadName(node, name="None") |
paulb@6 | 116 | |
paulb@135 | 117 | # Top-level transformation. |
paul@0 | 118 | |
paulb@88 | 119 | def visitModule(self, module, name=None): |
paulb@46 | 120 | |
paulb@46 | 121 | """ |
paulb@46 | 122 | Process the given 'module', producing a Module object which contains the |
paulb@88 | 123 | resulting program nodes. If the optional 'name' is provided, the 'name' |
paulb@88 | 124 | attribute is set on the Module object using a value other than None. |
paulb@46 | 125 | """ |
paulb@46 | 126 | |
paulb@118 | 127 | result = self.module = Module(module, 1, name=name) |
paulb@258 | 128 | result.code = [] |
paulb@258 | 129 | |
paulb@258 | 130 | # Add an initialiser for the module name first for non-builtins. |
paulb@258 | 131 | |
paulb@258 | 132 | if self.builtins: |
paulb@258 | 133 | result.code += self.dispatch(module.node) |
paulb@258 | 134 | |
paulb@258 | 135 | result.code += [ |
paulb@258 | 136 | StoreName(name="__name__", expr=self._visitConst(None, name)) |
paulb@258 | 137 | ] |
paulb@258 | 138 | |
paulb@258 | 139 | # Add an initialiser for the module name last for builtins. |
paulb@258 | 140 | |
paulb@258 | 141 | if not self.builtins: |
paulb@258 | 142 | result.code += self.dispatch(module.node) |
paulb@57 | 143 | return result |
paul@0 | 144 | |
paulb@137 | 145 | # Node transformations. |
paulb@17 | 146 | |
paulb@137 | 147 | def visitAdd(self, add): |
paulb@137 | 148 | return self._visitBinary(add, "__add__", "__radd__") |
paul@1 | 149 | |
paul@1 | 150 | def visitAnd(self, and_): |
paulb@7 | 151 | |
paulb@35 | 152 | """ |
paulb@35 | 153 | Make a subprogram for the 'and_' node and record its contents inside the |
paulb@35 | 154 | subprogram. Convert... |
paulb@35 | 155 | |
paulb@35 | 156 | And (test) |
paulb@35 | 157 | (test) |
paulb@35 | 158 | ... |
paulb@35 | 159 | |
paulb@35 | 160 | ...to: |
paulb@35 | 161 | |
paulb@135 | 162 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@135 | 163 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@35 | 164 | (else) -> ... |
paulb@35 | 165 | """ |
paulb@7 | 166 | |
paulb@118 | 167 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 168 | self.current_subprograms.append(subprogram) |
paulb@7 | 169 | |
paulb@12 | 170 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 171 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 172 | # subprogram with the value of the operand. |
paulb@12 | 173 | |
paulb@7 | 174 | last = and_.nodes[-1] |
paulb@35 | 175 | results = nodes = [] |
paulb@35 | 176 | |
paul@1 | 177 | for node in and_.nodes: |
paulb@7 | 178 | expr = self.dispatch(node) |
paulb@34 | 179 | |
paulb@35 | 180 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 181 | |
paulb@7 | 182 | if node is not last: |
paulb@135 | 183 | nodes += [ |
paulb@135 | 184 | StoreTemp(expr=expr), |
paulb@135 | 185 | Conditional( |
paulb@135 | 186 | test=self._visitNot(LoadTemp()), |
paulb@135 | 187 | body=[ |
paulb@135 | 188 | ReturnFromBlock( |
paulb@135 | 189 | expr=LoadTemp() |
paulb@135 | 190 | ) |
paulb@135 | 191 | ], |
paulb@135 | 192 | else_=[ |
paulb@135 | 193 | ReleaseTemp() |
paulb@135 | 194 | # Subsequent operations go here! |
paulb@135 | 195 | ] |
paulb@135 | 196 | ) |
paulb@135 | 197 | ] |
paulb@35 | 198 | |
paulb@35 | 199 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 200 | |
paulb@135 | 201 | nodes = nodes[-1].else_ |
paulb@35 | 202 | |
paulb@35 | 203 | # For the last operation, return the result. |
paulb@35 | 204 | |
paulb@7 | 205 | else: |
paulb@135 | 206 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@35 | 207 | |
paulb@35 | 208 | # Finish the subprogram definition. |
paulb@35 | 209 | |
paulb@35 | 210 | subprogram.code = results |
paulb@7 | 211 | |
paulb@7 | 212 | self.current_subprograms.pop() |
paulb@79 | 213 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@7 | 214 | |
paulb@7 | 215 | # Make an invocation of the subprogram. |
paulb@7 | 216 | |
paulb@204 | 217 | result = InvokeRef(and_, 1, produces_result=1, ref=subprogram) |
paul@1 | 218 | return result |
paul@1 | 219 | |
paulb@202 | 220 | def visitAssert(self, assert_): |
paulb@202 | 221 | if assert_.fail: |
paulb@202 | 222 | fail_args = [self.dispatch(assert_.fail)] |
paulb@202 | 223 | else: |
paulb@202 | 224 | fail_args = [] |
paulb@202 | 225 | |
paulb@202 | 226 | result = Conditional(assert_, 1, |
paulb@202 | 227 | test=self.dispatch(assert_.test), |
paulb@202 | 228 | body=[], |
paulb@202 | 229 | else_=[ |
paulb@202 | 230 | Raise(assert_, |
paulb@202 | 231 | expr=InvokeFunction(assert_, |
paulb@202 | 232 | expr=LoadName(name="AssertionError"), |
paulb@202 | 233 | args=fail_args, |
paulb@202 | 234 | star=None, |
paulb@202 | 235 | dstar=None |
paulb@202 | 236 | ) |
paulb@202 | 237 | ) |
paulb@202 | 238 | ] |
paulb@202 | 239 | ) |
paulb@202 | 240 | |
paulb@202 | 241 | # Make nice annotations for the viewer. |
paulb@202 | 242 | |
paulb@202 | 243 | assert_._raises = result.else_[0].expr |
paulb@202 | 244 | return result |
paulb@202 | 245 | |
paulb@137 | 246 | # Assignments. |
paulb@34 | 247 | |
paulb@257 | 248 | def visitAssAttr(self, assattr, element=None): |
paulb@257 | 249 | expr = self._visitAssNameOrAttr(assattr, element) |
paulb@137 | 250 | lvalue = self.dispatch(assattr.expr) |
paulb@137 | 251 | result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@1 | 252 | return result |
paul@1 | 253 | |
paulb@137 | 254 | def visitAssign(self, assign): |
paulb@137 | 255 | result = Assign(assign, 1) |
paulb@137 | 256 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paulb@137 | 257 | release = ReleaseTemp() |
paulb@257 | 258 | result.code = [store] + self.dispatches(assign.nodes) + [release] |
paulb@129 | 259 | return result |
paulb@129 | 260 | |
paulb@257 | 261 | def visitAssList(self, asslist, element=None): |
paulb@257 | 262 | if element is None: |
paulb@137 | 263 | expr = LoadTemp() |
paulb@137 | 264 | else: |
paulb@257 | 265 | expr = LoadAttr(expr=LoadTemp(), name=("__value__%d" % element)) |
paulb@137 | 266 | result = Assign(asslist, 1) |
paulb@257 | 267 | result.code = [StoreTemp(expr=expr)] |
paulb@257 | 268 | for i, node in enumerate(asslist.nodes): |
paulb@257 | 269 | result.code.append(self.dispatch(node, i)) |
paulb@257 | 270 | result.code.append(ReleaseTemp()) |
paulb@32 | 271 | return result |
paulb@32 | 272 | |
paulb@137 | 273 | visitAssTuple = visitAssList |
paulb@135 | 274 | |
paulb@257 | 275 | def _visitAssNameOrAttr(self, node, element=None): |
paulb@257 | 276 | |
paulb@257 | 277 | "Produce a suitable value for assignment from the given 'node'." |
paulb@257 | 278 | |
paulb@257 | 279 | if element is None: |
paulb@137 | 280 | return LoadTemp() |
paulb@137 | 281 | else: |
paulb@257 | 282 | return LoadAttr(expr=LoadTemp(), name=("__value__%d" % element)) |
paulb@257 | 283 | |
paulb@257 | 284 | def visitAssName(self, assname, element=None): |
paulb@135 | 285 | |
paulb@257 | 286 | """ |
paulb@257 | 287 | Visit the 'assname' node, producing an appropriate StoreName simplified |
paulb@257 | 288 | node which if 'element' is None will store a temporary value; otherwise, |
paulb@257 | 289 | a special attribute will be obtained from the current temporary value in |
paulb@257 | 290 | order to attempt to support optimised sequence assignment. |
paulb@257 | 291 | """ |
paulb@257 | 292 | |
paulb@257 | 293 | expr = self._visitAssNameOrAttr(assname, element) |
paulb@137 | 294 | result = StoreName(assname, 1, name=assname.name, expr=expr) |
paulb@137 | 295 | return result |
paul@0 | 296 | |
paul@2 | 297 | augassign_methods = { |
paulb@9 | 298 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 299 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 300 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 301 | } |
paul@2 | 302 | |
paul@2 | 303 | def visitAugAssign(self, augassign): |
paulb@92 | 304 | |
paulb@92 | 305 | """ |
paulb@92 | 306 | Convert the augmented assignment... |
paulb@92 | 307 | |
paulb@92 | 308 | AugAssign (node) -> Name | Getattr | Slice | Subscript |
paulb@92 | 309 | (op) |
paulb@92 | 310 | (expr) |
paulb@92 | 311 | |
paulb@92 | 312 | ...to: |
paulb@92 | 313 | |
paulb@92 | 314 | Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> |
paulb@92 | 315 | (name) -> <op> |
paulb@92 | 316 | StoreName (name) -> <name> |
paulb@92 | 317 | (expr) -> LoadTemp |
paulb@92 | 318 | ReleaseTemp |
paulb@92 | 319 | """ |
paulb@92 | 320 | |
paulb@79 | 321 | result = Assign(augassign, 1) |
paul@2 | 322 | expr = self.dispatch(augassign.expr) |
paul@2 | 323 | |
paul@2 | 324 | # Simple augmented assignment: name += expr |
paul@2 | 325 | |
paul@2 | 326 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@56 | 327 | result.code = [ |
paulb@101 | 328 | StoreTemp( |
paulb@173 | 329 | expr=InvokeFunction( # referenced below |
paulb@185 | 330 | augassign, |
paulb@59 | 331 | args=[expr], |
paulb@59 | 332 | star=None, |
paulb@59 | 333 | dstar=None, |
paulb@101 | 334 | expr=LoadAttr( |
paulb@56 | 335 | expr=self.dispatch(augassign.node), |
paulb@56 | 336 | name=self.augassign_methods[augassign.op] |
paulb@56 | 337 | ) |
paulb@56 | 338 | ) |
paulb@56 | 339 | ), |
paulb@101 | 340 | StoreName( |
paulb@101 | 341 | expr=LoadTemp(), |
paulb@56 | 342 | name=augassign.node.name), |
paulb@101 | 343 | ReleaseTemp() |
paulb@56 | 344 | ] |
paul@2 | 345 | |
paulb@173 | 346 | # Make nice annotations for the viewer. |
paulb@173 | 347 | |
paulb@173 | 348 | augassign._op_call = result.code[0].expr |
paulb@173 | 349 | |
paulb@9 | 350 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 351 | |
paul@2 | 352 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 353 | |
paulb@9 | 354 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 355 | |
paulb@56 | 356 | result.code = [ |
paulb@101 | 357 | StoreTemp( |
paulb@56 | 358 | index="expr", |
paulb@56 | 359 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 360 | ), |
paulb@101 | 361 | StoreTemp( |
paulb@173 | 362 | expr=InvokeFunction( # referenced below |
paulb@185 | 363 | augassign, |
paulb@56 | 364 | args=[expr], star=None, dstar=None, |
paulb@101 | 365 | expr=LoadAttr( |
paulb@92 | 366 | expr=LoadAttr(augassign.node, 1, |
paulb@101 | 367 | expr=LoadTemp(index="expr"), |
paulb@56 | 368 | name=augassign.node.attrname |
paulb@56 | 369 | ), |
paulb@56 | 370 | name=self.augassign_methods[augassign.op] |
paulb@56 | 371 | ) |
paulb@56 | 372 | ) |
paulb@56 | 373 | ), |
paulb@101 | 374 | StoreAttr( |
paulb@101 | 375 | expr=LoadTemp(), |
paulb@101 | 376 | lvalue=LoadTemp(index="expr"), |
paulb@56 | 377 | name=augassign.node.attrname |
paulb@56 | 378 | ), |
paulb@101 | 379 | ReleaseTemp(index="expr"), |
paulb@101 | 380 | ReleaseTemp() |
paulb@56 | 381 | ] |
paulb@6 | 382 | |
paulb@173 | 383 | # Make nice annotations for the viewer. |
paulb@173 | 384 | |
paulb@173 | 385 | augassign._op_call = result.code[1].expr |
paulb@173 | 386 | |
paulb@9 | 387 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 388 | |
paulb@6 | 389 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 390 | |
paulb@9 | 391 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 392 | |
paulb@56 | 393 | result.code = [ |
paulb@101 | 394 | StoreTemp( |
paulb@56 | 395 | index="expr", |
paulb@56 | 396 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 397 | ), |
paulb@101 | 398 | StoreTemp( |
paulb@56 | 399 | index="lower", |
paulb@56 | 400 | expr=self.dispatch_or_none(augassign.node.lower) |
paulb@56 | 401 | ), |
paulb@101 | 402 | StoreTemp( |
paulb@56 | 403 | index="upper", |
paulb@56 | 404 | expr=self.dispatch_or_none(augassign.node.upper) |
paulb@56 | 405 | ), |
paulb@101 | 406 | StoreTemp( |
paulb@173 | 407 | expr=InvokeFunction( # referenced below |
paulb@185 | 408 | augassign, |
paulb@56 | 409 | args=[expr], star=None, dstar=None, |
paulb@101 | 410 | expr=LoadAttr( |
paulb@59 | 411 | expr=self._visitSlice( |
paulb@59 | 412 | augassign.node, |
paulb@101 | 413 | LoadTemp(index="expr"), |
paulb@101 | 414 | LoadTemp(index="lower"), |
paulb@101 | 415 | LoadTemp(index="upper"), |
paulb@59 | 416 | "OP_APPLY"), |
paulb@56 | 417 | name=self.augassign_methods[augassign.op] |
paulb@56 | 418 | ) |
paulb@56 | 419 | ) |
paulb@56 | 420 | ), |
paulb@59 | 421 | self._visitSlice( |
paulb@59 | 422 | augassign.node, |
paulb@101 | 423 | LoadTemp(index="expr"), |
paulb@101 | 424 | LoadTemp(index="lower"), |
paulb@101 | 425 | LoadTemp(index="upper"), |
paulb@59 | 426 | "OP_ASSIGN", |
paulb@101 | 427 | LoadTemp() |
paulb@59 | 428 | ), |
paulb@101 | 429 | ReleaseTemp(index="expr"), |
paulb@101 | 430 | ReleaseTemp(index="lower"), |
paulb@101 | 431 | ReleaseTemp(index="upper"), |
paulb@101 | 432 | ReleaseTemp() |
paulb@56 | 433 | ] |
paul@2 | 434 | |
paulb@173 | 435 | # Make nice annotations for the viewer. |
paulb@173 | 436 | |
paulb@173 | 437 | augassign._op_call = result.code[3].expr |
paulb@173 | 438 | |
paulb@10 | 439 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 440 | |
paulb@10 | 441 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 442 | |
paulb@10 | 443 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 444 | |
paulb@56 | 445 | result.code = [ |
paulb@101 | 446 | StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), |
paulb@101 | 447 | StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), |
paulb@101 | 448 | StoreTemp( |
paulb@173 | 449 | expr=InvokeFunction( # referenced below |
paulb@185 | 450 | augassign, |
paulb@56 | 451 | args=[expr], star=None, dstar=None, |
paulb@101 | 452 | expr=LoadAttr( |
paulb@59 | 453 | expr=self._visitSubscript( |
paulb@59 | 454 | augassign.node, |
paulb@101 | 455 | LoadTemp(index="expr"), |
paulb@101 | 456 | LoadTemp(index="subs"), |
paulb@59 | 457 | "OP_APPLY" |
paulb@59 | 458 | ), |
paulb@56 | 459 | name=self.augassign_methods[augassign.op] |
paulb@56 | 460 | ) |
paulb@56 | 461 | ) |
paulb@56 | 462 | ), |
paulb@59 | 463 | self._visitSubscript( |
paulb@59 | 464 | augassign.node, |
paulb@101 | 465 | LoadTemp(index="expr"), |
paulb@101 | 466 | LoadTemp(index="subs"), |
paulb@59 | 467 | "OP_ASSIGN", |
paulb@101 | 468 | LoadTemp() |
paulb@59 | 469 | ), |
paulb@101 | 470 | ReleaseTemp(index="expr"), |
paulb@101 | 471 | ReleaseTemp(index="subs"), |
paulb@101 | 472 | ReleaseTemp() |
paulb@56 | 473 | ] |
paulb@10 | 474 | |
paulb@173 | 475 | # Make nice annotations for the viewer. |
paulb@173 | 476 | |
paulb@173 | 477 | augassign._op_call = result.code[2].expr |
paulb@173 | 478 | |
paul@2 | 479 | else: |
paul@2 | 480 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 481 | |
paul@2 | 482 | return result |
paul@2 | 483 | |
paulb@203 | 484 | def visitBitand(self, bitand): |
paulb@203 | 485 | |
paulb@203 | 486 | """ |
paulb@203 | 487 | Make a subprogram for the 'bitand' node and record its contents inside the |
paulb@203 | 488 | subprogram. Convert... |
paulb@203 | 489 | |
paulb@203 | 490 | Bitand (node) |
paulb@203 | 491 | (node) |
paulb@203 | 492 | ... |
paulb@203 | 493 | |
paulb@203 | 494 | ...to: |
paulb@203 | 495 | |
paulb@203 | 496 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 497 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 498 | (else) -> ... |
paulb@203 | 499 | """ |
paulb@203 | 500 | |
paulb@203 | 501 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@203 | 502 | self.current_subprograms.append(subprogram) |
paulb@203 | 503 | |
paulb@203 | 504 | # In the subprogram, make instructions which store each operand, test |
paulb@203 | 505 | # for each operand's truth status, and if appropriate return from the |
paulb@203 | 506 | # subprogram with the value of the operand. |
paulb@203 | 507 | |
paulb@203 | 508 | last = bitand.nodes[-1] |
paulb@203 | 509 | results = nodes = [] |
paulb@203 | 510 | |
paulb@203 | 511 | # Start by storing the first operand. |
paulb@203 | 512 | |
paulb@203 | 513 | nodes += [ |
paulb@203 | 514 | StoreTemp(expr=self.dispatch(bitand.nodes[0])) |
paulb@203 | 515 | ] |
paulb@203 | 516 | |
paulb@203 | 517 | # For viewing purposes, record invocations on the AST node. |
paulb@203 | 518 | |
paulb@203 | 519 | bitand._ops = [] |
paulb@203 | 520 | |
paulb@203 | 521 | for node in bitand.nodes[1:]: |
paulb@203 | 522 | |
paulb@203 | 523 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@203 | 524 | |
paulb@203 | 525 | new_op = Op("&", node) |
paulb@203 | 526 | bitand._ops.append(new_op) |
paulb@203 | 527 | |
paulb@203 | 528 | # Generate the operation involving the previous result and the |
paulb@203 | 529 | # current operand. |
paulb@203 | 530 | |
paulb@203 | 531 | expr = self._visitBinaryOp(new_op, LoadTemp(), self.dispatch(node), "__and__", "__rand__") |
paulb@203 | 532 | |
paulb@203 | 533 | # Return from the subprogram where the test is not satisfied. |
paulb@203 | 534 | |
paulb@203 | 535 | if node is not last: |
paulb@203 | 536 | nodes += [ |
paulb@203 | 537 | StoreTemp(expr=expr), |
paulb@203 | 538 | Conditional( |
paulb@203 | 539 | test=self._visitNot(LoadTemp()), |
paulb@203 | 540 | body=[ |
paulb@203 | 541 | ReturnFromBlock( |
paulb@203 | 542 | expr=LoadTemp() |
paulb@203 | 543 | ) |
paulb@203 | 544 | ], |
paulb@203 | 545 | else_=[ |
paulb@203 | 546 | # Subsequent operations go here! |
paulb@203 | 547 | ] |
paulb@203 | 548 | ) |
paulb@203 | 549 | ] |
paulb@203 | 550 | |
paulb@203 | 551 | # Put subsequent operations in the else section of this conditional. |
paulb@203 | 552 | |
paulb@203 | 553 | nodes = nodes[-1].else_ |
paulb@203 | 554 | |
paulb@203 | 555 | # For the last operation, return the result. |
paulb@203 | 556 | |
paulb@203 | 557 | else: |
paulb@203 | 558 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@203 | 559 | |
paulb@203 | 560 | # Finish the subprogram definition. |
paulb@203 | 561 | |
paulb@203 | 562 | subprogram.code = results |
paulb@203 | 563 | |
paulb@203 | 564 | self.current_subprograms.pop() |
paulb@203 | 565 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@203 | 566 | |
paulb@203 | 567 | # Make an invocation of the subprogram. |
paulb@203 | 568 | |
paulb@204 | 569 | result = InvokeRef(bitand, 1, produces_result=1, ref=subprogram) |
paulb@203 | 570 | return result |
paulb@203 | 571 | |
paulb@137 | 572 | def visitBreak(self, break_): |
paulb@137 | 573 | result = ReturnFromBlock(break_, 1) |
paul@0 | 574 | return result |
paul@0 | 575 | |
paulb@137 | 576 | def visitCallFunc(self, callfunc): |
paulb@137 | 577 | result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) |
paulb@137 | 578 | if callfunc.star_args is not None: |
paulb@137 | 579 | result.star = self.dispatch(callfunc.star_args) |
paulb@137 | 580 | if callfunc.dstar_args is not None: |
paulb@137 | 581 | result.dstar = self.dispatch(callfunc.dstar_args) |
paulb@137 | 582 | result.expr = self.dispatch(callfunc.node) |
paulb@6 | 583 | return result |
paulb@6 | 584 | |
paulb@19 | 585 | def visitClass(self, class_): |
paulb@129 | 586 | |
paulb@129 | 587 | # Add "object" if the class is not "object" and has an empty bases list. |
paulb@129 | 588 | |
paulb@129 | 589 | if class_.name != "object" and not class_.bases: |
paulb@129 | 590 | bases = [compiler.ast.Name("object")] |
paulb@129 | 591 | else: |
paulb@129 | 592 | bases = class_.bases |
paulb@129 | 593 | |
paulb@249 | 594 | structure = get_class()(name=class_.name, module=self.module, bases=self.dispatches(bases)) |
paulb@19 | 595 | self.structures.append(structure) |
paulb@172 | 596 | within_class = self.within_class |
paulb@172 | 597 | self.within_class = 1 |
paulb@19 | 598 | |
paulb@24 | 599 | # Make a subprogram which initialises the class structure. |
paulb@24 | 600 | |
paulb@118 | 601 | subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 602 | self.current_subprograms.append(subprogram) |
paulb@120 | 603 | self.current_structures.append(structure) # mostly for name construction |
paulb@19 | 604 | |
paulb@28 | 605 | # The class is initialised using the code found inside. |
paulb@28 | 606 | |
paulb@135 | 607 | subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] |
paulb@19 | 608 | |
paulb@172 | 609 | self.within_class = within_class |
paulb@120 | 610 | self.current_structures.pop() |
paulb@19 | 611 | self.current_subprograms.pop() |
paulb@79 | 612 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@19 | 613 | |
paulb@19 | 614 | # Make a definition of the class associating it with a name. |
paulb@19 | 615 | |
paulb@101 | 616 | result = Assign( |
paulb@56 | 617 | code=[ |
paulb@79 | 618 | StoreName(class_, 1, # defines the class |
paulb@56 | 619 | name=class_.name, |
paulb@101 | 620 | expr=LoadRef(ref=structure) |
paulb@56 | 621 | ), |
paulb@204 | 622 | InvokeRef( |
paulb@185 | 623 | class_, |
paulb@204 | 624 | share_locals=0, # override the local sharing usually in InvokeRef |
paulb@204 | 625 | ref=subprogram |
paulb@56 | 626 | ) |
paulb@56 | 627 | ] |
paulb@56 | 628 | ) |
paulb@19 | 629 | return result |
paulb@19 | 630 | |
paulb@137 | 631 | comparison_methods = { |
paulb@192 | 632 | "==" : ("__eq__", "__ne__"), |
paulb@192 | 633 | "!=" : ("__ne__", "__eq__"), |
paulb@192 | 634 | "<" : ("__lt__", "__gt__"), |
paulb@192 | 635 | "<=" : ("__le__", "__ge__"), |
paulb@192 | 636 | ">=" : ("__ge__", "__le__"), |
paulb@192 | 637 | ">" : ("__gt__", "__lt__"), |
paulb@192 | 638 | "is" : None, |
paulb@201 | 639 | "is not" : None, |
paulb@201 | 640 | "in" : None, |
paulb@201 | 641 | "not in" : None |
paulb@137 | 642 | } |
paulb@137 | 643 | |
paulb@137 | 644 | def visitCompare(self, compare): |
paulb@46 | 645 | |
paulb@46 | 646 | """ |
paulb@137 | 647 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@137 | 648 | the subprogram. Convert... |
paul@0 | 649 | |
paulb@137 | 650 | Compare (expr) |
paulb@137 | 651 | (name/node) |
paulb@137 | 652 | ... |
paulb@35 | 653 | |
paulb@35 | 654 | ...to: |
paulb@35 | 655 | |
paulb@204 | 656 | InvokeRef -> Subprogram -> Conditional (test) -> (body) |
paulb@137 | 657 | (else) -> Conditional (test) -> (body) |
paulb@137 | 658 | (else) -> ... |
paulb@35 | 659 | """ |
paul@0 | 660 | |
paulb@137 | 661 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paul@0 | 662 | self.current_subprograms.append(subprogram) |
paul@0 | 663 | |
paulb@137 | 664 | # In the subprogram, make instructions which invoke a method on the |
paulb@137 | 665 | # first operand of each operand pair and, if appropriate, return with |
paulb@137 | 666 | # the value from that method. |
paulb@137 | 667 | |
paulb@137 | 668 | last = compare.ops[-1] |
paulb@137 | 669 | previous = self.dispatch(compare.expr) |
paulb@137 | 670 | results = nodes = [] |
paulb@137 | 671 | |
paulb@137 | 672 | # For viewing purposes, record invocations on the AST node. |
paulb@137 | 673 | |
paulb@137 | 674 | compare._ops = [] |
paulb@137 | 675 | |
paulb@137 | 676 | for op in compare.ops: |
paulb@137 | 677 | op_name, node = op |
paulb@192 | 678 | |
paulb@192 | 679 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@192 | 680 | |
paulb@192 | 681 | new_op = Op(op_name, node) |
paulb@203 | 682 | compare._ops.append(new_op) |
paulb@203 | 683 | |
paulb@137 | 684 | expr = self.dispatch(node) |
paul@0 | 685 | |
paulb@137 | 686 | # Identify the operation and produce the appropriate method call. |
paul@0 | 687 | |
paulb@192 | 688 | method_names = self.comparison_methods[op_name] |
paulb@192 | 689 | if method_names: |
paulb@192 | 690 | first_name, alternative_name = method_names |
paulb@193 | 691 | invocation = self._visitBinaryCompareOp(new_op, previous, expr, first_name, alternative_name) |
paulb@137 | 692 | |
paulb@137 | 693 | elif op_name == "is": |
paulb@137 | 694 | invocation = InvokeFunction( |
paulb@192 | 695 | new_op, 1, |
paulb@137 | 696 | expr=LoadName(name="__is__"), |
paulb@137 | 697 | args=[previous, expr], |
paulb@137 | 698 | star=None, |
paulb@137 | 699 | dstar=None) |
paul@0 | 700 | |
paulb@137 | 701 | elif op_name == "is not": |
paulb@137 | 702 | invocation = Not( |
paulb@192 | 703 | new_op, 1, |
paulb@137 | 704 | expr=InvokeFunction( |
paulb@192 | 705 | new_op, |
paulb@137 | 706 | expr=LoadName(name="__is__"), |
paulb@137 | 707 | args=[previous, expr], |
paulb@137 | 708 | star=None, |
paulb@137 | 709 | dstar=None) |
paulb@137 | 710 | ) |
paulb@201 | 711 | |
paulb@201 | 712 | elif op_name == "in": |
paulb@201 | 713 | invocation = InvokeFunction( |
paulb@201 | 714 | new_op, 1, |
paulb@201 | 715 | expr=LoadAttr( |
paulb@201 | 716 | expr=previous, |
paulb@201 | 717 | name="__contains__" |
paulb@201 | 718 | ), |
paulb@201 | 719 | args=[expr], |
paulb@201 | 720 | star=None, |
paulb@201 | 721 | dstar=None) |
paulb@201 | 722 | |
paulb@201 | 723 | elif op_name == "not in": |
paulb@201 | 724 | invocation = Not( |
paulb@201 | 725 | new_op, 1, |
paulb@201 | 726 | expr=InvokeFunction( |
paulb@201 | 727 | new_op, |
paulb@201 | 728 | expr=LoadAttr( |
paulb@201 | 729 | expr=previous, |
paulb@201 | 730 | name="__contains__" |
paulb@201 | 731 | ), |
paulb@201 | 732 | args=[expr], |
paulb@201 | 733 | star=None, |
paulb@201 | 734 | dstar=None) |
paulb@201 | 735 | ) |
paulb@201 | 736 | |
paulb@137 | 737 | else: |
paulb@137 | 738 | raise NotImplementedError, op_name |
paulb@34 | 739 | |
paulb@137 | 740 | nodes.append(StoreTemp(expr=invocation)) |
paulb@137 | 741 | |
paulb@137 | 742 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 743 | |
paulb@137 | 744 | if op is not last: |
paulb@137 | 745 | nodes.append( |
paulb@137 | 746 | Conditional( |
paulb@137 | 747 | test=self._visitNot(LoadTemp()), |
paulb@137 | 748 | body=[ |
paulb@137 | 749 | ReturnFromBlock(expr=LoadTemp()) |
paulb@137 | 750 | ], |
paulb@137 | 751 | else_=[ |
paulb@137 | 752 | ReleaseTemp() |
paulb@137 | 753 | # Subsequent operations go here! |
paulb@137 | 754 | ] |
paulb@137 | 755 | ) |
paulb@137 | 756 | ) |
paulb@35 | 757 | |
paulb@137 | 758 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 759 | |
paulb@137 | 760 | nodes = nodes[-1].else_ |
paulb@137 | 761 | |
paulb@137 | 762 | # For the last operation, return the result. |
paulb@35 | 763 | |
paulb@137 | 764 | else: |
paulb@137 | 765 | nodes.append( |
paulb@137 | 766 | ReturnFromBlock(expr=LoadTemp(release=1)) |
paulb@137 | 767 | ) |
paulb@137 | 768 | |
paulb@137 | 769 | previous = expr |
paulb@35 | 770 | |
paulb@35 | 771 | # Finish the subprogram definition. |
paulb@35 | 772 | |
paulb@137 | 773 | subprogram.code = results |
paul@0 | 774 | |
paul@0 | 775 | self.current_subprograms.pop() |
paulb@79 | 776 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 777 | |
paul@0 | 778 | # Make an invocation of the subprogram. |
paul@0 | 779 | |
paulb@204 | 780 | result = InvokeRef(compare, 1, produces_result=1, ref=subprogram) |
paul@0 | 781 | return result |
paul@0 | 782 | |
paulb@137 | 783 | def visitConst(self, const): |
paulb@258 | 784 | return self._visitConst(const, const.value) |
paulb@258 | 785 | |
paulb@258 | 786 | def _visitConst(self, node, value): |
paulb@258 | 787 | key = "%s-%s" % (value.__class__.__name__, value) |
paulb@154 | 788 | if not self.constants.has_key(key): |
paulb@258 | 789 | self.constants[key] = Constant(name=repr(value), value=value) |
paulb@258 | 790 | if node is not None: |
paulb@258 | 791 | result = InvokeFunction(node, 1, expr=LoadName(name=self.constants[key].typename)) |
paulb@258 | 792 | else: |
paulb@258 | 793 | result = InvokeFunction(expr=LoadName(name=self.constants[key].typename)) |
paulb@137 | 794 | return result |
paulb@137 | 795 | |
paulb@137 | 796 | def visitContinue(self, continue_): |
paulb@204 | 797 | result = InvokeRef(continue_, 1, ref=self.current_subprograms[-1]) |
paulb@137 | 798 | return result |
paulb@137 | 799 | |
paulb@137 | 800 | def visitDict(self, dict): |
paulb@212 | 801 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict")) |
paulb@137 | 802 | args = [] |
paulb@137 | 803 | for key, value in dict.items: |
paulb@212 | 804 | tuple = InvokeFunction(dict, expr=LoadName(name="tuple")) |
paulb@137 | 805 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paulb@137 | 806 | args.append(tuple) |
paulb@137 | 807 | result.set_args(args) |
paulb@137 | 808 | return result |
paulb@137 | 809 | |
paulb@137 | 810 | def visitDiscard(self, discard): |
paulb@137 | 811 | return self.dispatch(discard.expr) |
paulb@137 | 812 | |
paulb@137 | 813 | def visitDiv(self, div): |
paulb@137 | 814 | return self._visitBinary(div, "__div__", "__rdiv__") |
paulb@137 | 815 | |
paulb@137 | 816 | def visitFloorDiv(self, floordiv): |
paulb@137 | 817 | return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") |
paulb@137 | 818 | |
paul@0 | 819 | def visitFor(self, for_): |
paul@0 | 820 | |
paulb@35 | 821 | """ |
paulb@35 | 822 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 823 | subprogram. Convert... |
paulb@35 | 824 | |
paulb@35 | 825 | For (assign) |
paulb@35 | 826 | (body) |
paulb@35 | 827 | (else) |
paulb@35 | 828 | |
paulb@35 | 829 | ...to: |
paulb@35 | 830 | |
paulb@35 | 831 | Assign (assign #1) |
paulb@35 | 832 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 833 | (body) |
paulb@35 | 834 | Invoke subprogram |
paulb@35 | 835 | (handler) -> ... |
paulb@35 | 836 | (else) -> ... |
paulb@35 | 837 | """ |
paul@0 | 838 | |
paulb@201 | 839 | return self._visitFor(for_, self.dispatches(for_.body), for_.else_) |
paulb@201 | 840 | |
paulb@201 | 841 | def _visitFor(self, node, body_stmt, else_=None): |
paulb@201 | 842 | |
paulb@212 | 843 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[]) |
paul@0 | 844 | self.current_subprograms.append(subprogram) |
paul@0 | 845 | |
paulb@34 | 846 | # Always return from conditional sections/subprograms. |
paulb@34 | 847 | |
paulb@201 | 848 | if else_ is not None: |
paulb@201 | 849 | else_stmt = self.dispatch(else_) + [ReturnFromBlock()] |
paulb@17 | 850 | else: |
paulb@135 | 851 | else_stmt = [ReturnFromBlock()] |
paulb@17 | 852 | |
paul@0 | 853 | # Wrap the assignment in a try...except statement. |
paulb@135 | 854 | # Inside the body, add a recursive invocation to the subprogram. |
paul@0 | 855 | |
paulb@135 | 856 | subprogram.code = [ |
paulb@135 | 857 | Try( |
paulb@135 | 858 | body=[ |
paulb@135 | 859 | Assign( |
paulb@135 | 860 | code=[ |
paulb@205 | 861 | StoreTemp( |
paulb@205 | 862 | expr=InvokeFunction(node, |
paulb@205 | 863 | expr=LoadAttr( |
paulb@205 | 864 | expr=LoadTemp(), |
paulb@205 | 865 | name="next" |
paulb@205 | 866 | ) |
paulb@205 | 867 | ) |
paulb@205 | 868 | ), |
paulb@201 | 869 | self.dispatch(node.assign), |
paulb@135 | 870 | ReleaseTemp() |
paulb@135 | 871 | ]) |
paulb@201 | 872 | ] + body_stmt + [ |
paulb@204 | 873 | InvokeRef( |
paulb@201 | 874 | node, |
paulb@204 | 875 | ref=subprogram |
paulb@135 | 876 | ) |
paulb@135 | 877 | ], |
paulb@135 | 878 | handler=[ |
paulb@135 | 879 | Conditional( |
paulb@135 | 880 | test=InvokeFunction( |
paulb@201 | 881 | node, |
paulb@135 | 882 | expr=LoadName(name="isinstance"), |
paulb@135 | 883 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@135 | 884 | star=None, |
paulb@135 | 885 | dstar=None), |
paulb@135 | 886 | body=else_stmt, |
paulb@205 | 887 | else_=[ |
paulb@205 | 888 | Raise( |
paulb@205 | 889 | expr=LoadExc() |
paulb@205 | 890 | ) |
paulb@205 | 891 | ] |
paulb@135 | 892 | ) |
paulb@135 | 893 | ], |
paulb@135 | 894 | else_=[], |
paulb@135 | 895 | finally_=[] |
paulb@135 | 896 | ), |
paulb@135 | 897 | ReturnFromBlock() |
paulb@135 | 898 | ] |
paul@0 | 899 | |
paulb@35 | 900 | # Finish the subprogram definition. |
paulb@35 | 901 | |
paul@0 | 902 | self.current_subprograms.pop() |
paulb@79 | 903 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 904 | |
paul@0 | 905 | # Obtain an iterator for the sequence involved. |
paul@0 | 906 | # Then, make an invocation of the subprogram. |
paul@0 | 907 | |
paulb@201 | 908 | result = Assign(node, 1, |
paulb@148 | 909 | code=[ |
paulb@148 | 910 | StoreTemp( |
paulb@148 | 911 | expr=InvokeFunction( |
paulb@201 | 912 | node, |
paulb@148 | 913 | expr=LoadAttr( |
paulb@148 | 914 | name="__iter__", |
paulb@201 | 915 | expr=self.dispatch(node.list) |
paulb@148 | 916 | ) |
paulb@148 | 917 | ) |
paulb@148 | 918 | ), |
paulb@204 | 919 | InvokeRef(node, ref=subprogram), |
paulb@148 | 920 | ReleaseTemp() |
paulb@148 | 921 | ] |
paulb@148 | 922 | ) |
paulb@148 | 923 | |
paulb@148 | 924 | # Make nice annotations for the viewer. |
paulb@148 | 925 | |
paulb@201 | 926 | node._iter_call = result.code[0].expr |
paulb@201 | 927 | node._next_call = subprogram.code[0].body[0].code[0].expr |
paulb@148 | 928 | |
paul@0 | 929 | return result |
paul@0 | 930 | |
paulb@137 | 931 | def visitFrom(self, from_): |
paulb@137 | 932 | result = Assign(from_, 1) |
paulb@137 | 933 | code = [] |
paulb@157 | 934 | _names = [] |
paulb@157 | 935 | code.append( |
paulb@157 | 936 | StoreTemp( |
paulb@158 | 937 | expr=Import(name=from_.modname, alias=1) |
paulb@157 | 938 | ) |
paulb@157 | 939 | ) |
paulb@157 | 940 | from_._modname = code[-1].expr |
paulb@137 | 941 | for name, alias in from_.names: |
paulb@137 | 942 | code.append( |
paulb@137 | 943 | StoreName( |
paulb@137 | 944 | expr=LoadAttr( |
paulb@137 | 945 | expr=LoadTemp(), |
paulb@137 | 946 | name=name), |
paulb@137 | 947 | name=(alias or name) |
paulb@137 | 948 | ) |
paulb@137 | 949 | ) |
paulb@157 | 950 | _names.append(code[-1].expr) |
paulb@137 | 951 | code.append(ReleaseTemp()) |
paulb@137 | 952 | result.code = code |
paulb@157 | 953 | from_._names = _names |
paulb@137 | 954 | return result |
paulb@137 | 955 | |
paulb@137 | 956 | def _visitFunction(self, function, subprogram): |
paulb@137 | 957 | |
paulb@137 | 958 | """ |
paulb@137 | 959 | A common function generator which transforms the given 'function' node |
paulb@137 | 960 | and initialises the given 'subprogram' appropriately. |
paulb@137 | 961 | """ |
paulb@137 | 962 | |
paulb@137 | 963 | # Discover star and dstar parameters. |
paulb@137 | 964 | |
paulb@137 | 965 | if function.flags & 4 != 0: |
paulb@137 | 966 | has_star = 1 |
paulb@137 | 967 | else: |
paulb@137 | 968 | has_star = 0 |
paulb@137 | 969 | if function.flags & 8 != 0: |
paulb@137 | 970 | has_dstar = 1 |
paulb@137 | 971 | else: |
paulb@137 | 972 | has_dstar = 0 |
paulb@137 | 973 | |
paulb@137 | 974 | # Discover the number of defaults and positional parameters. |
paulb@137 | 975 | |
paulb@137 | 976 | ndefaults = len(function.defaults) |
paulb@137 | 977 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@137 | 978 | |
paulb@137 | 979 | # Produce star and dstar parameters with appropriate defaults. |
paulb@137 | 980 | |
paulb@137 | 981 | if has_star: |
paulb@137 | 982 | star = ( |
paulb@137 | 983 | function.argnames[npositional], |
paulb@185 | 984 | self.dispatch(compiler.ast.List([])) |
paulb@137 | 985 | ) |
paulb@137 | 986 | else: |
paulb@137 | 987 | star = None |
paulb@137 | 988 | if has_dstar: |
paulb@137 | 989 | dstar = ( |
paulb@137 | 990 | function.argnames[npositional + has_star], |
paulb@185 | 991 | self.dispatch(compiler.ast.Dict([])) |
paulb@137 | 992 | ) |
paulb@137 | 993 | else: |
paulb@137 | 994 | dstar = None |
paulb@137 | 995 | |
paulb@137 | 996 | params = [] |
paulb@137 | 997 | for i in range(0, npositional - ndefaults): |
paulb@137 | 998 | params.append((function.argnames[i], None)) |
paulb@137 | 999 | |
paulb@137 | 1000 | # Process defaults. |
paulb@137 | 1001 | |
paulb@137 | 1002 | for i in range(0, ndefaults): |
paulb@137 | 1003 | default = function.defaults[i] |
paulb@137 | 1004 | if default is not None: |
paulb@137 | 1005 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paulb@137 | 1006 | else: |
paulb@137 | 1007 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paulb@137 | 1008 | |
paulb@137 | 1009 | subprogram.params = params |
paulb@137 | 1010 | subprogram.star = star |
paulb@137 | 1011 | subprogram.dstar = dstar |
paulb@137 | 1012 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1013 | |
paulb@137 | 1014 | def visitFunction(self, function): |
paulb@137 | 1015 | |
paulb@137 | 1016 | """ |
paulb@137 | 1017 | Make a subprogram for the 'function' and record it outside the main |
paulb@137 | 1018 | tree. Produce something like the following: |
paulb@137 | 1019 | |
paulb@137 | 1020 | StoreName (name) |
paulb@147 | 1021 | (expr) -> LoadRef (ref) -> Subprogram (params) |
paulb@147 | 1022 | (star) |
paulb@147 | 1023 | (dstar) |
paulb@137 | 1024 | """ |
paulb@137 | 1025 | |
paulb@196 | 1026 | subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@196 | 1027 | internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) |
paulb@187 | 1028 | |
paulb@196 | 1029 | # Make nice annotations for the viewer. |
paulb@196 | 1030 | |
paulb@196 | 1031 | function._subprogram = subprogram |
paulb@137 | 1032 | |
paulb@137 | 1033 | self.current_subprograms.append(subprogram) |
paulb@172 | 1034 | within_class = self.within_class |
paulb@172 | 1035 | self.within_class = 0 |
paulb@172 | 1036 | |
paulb@137 | 1037 | subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] |
paulb@172 | 1038 | |
paulb@172 | 1039 | self.within_class = within_class |
paulb@137 | 1040 | self.current_subprograms.pop() |
paulb@137 | 1041 | self._visitFunction(function, subprogram) |
paulb@137 | 1042 | |
paulb@137 | 1043 | # Make a definition of the function associating it with a name. |
paulb@137 | 1044 | |
paulb@137 | 1045 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paulb@137 | 1046 | return result |
paulb@137 | 1047 | |
paulb@137 | 1048 | def visitGetattr(self, getattr): |
paulb@137 | 1049 | result = LoadAttr(getattr, 1, |
paulb@137 | 1050 | name=getattr.attrname, |
paulb@137 | 1051 | expr=self.dispatch(getattr.expr) |
paulb@137 | 1052 | ) |
paulb@137 | 1053 | return result |
paulb@137 | 1054 | |
paulb@137 | 1055 | def visitGlobal(self, global_): |
paulb@137 | 1056 | result = Global(global_, 1, |
paulb@137 | 1057 | names=global_.names |
paulb@137 | 1058 | ) |
paulb@137 | 1059 | return result |
paulb@137 | 1060 | |
paulb@137 | 1061 | def visitIf(self, if_): |
paulb@137 | 1062 | |
paulb@137 | 1063 | """ |
paulb@137 | 1064 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@137 | 1065 | and putting each subsequent test as part of the conditional's else |
paulb@137 | 1066 | section. |
paulb@137 | 1067 | |
paulb@137 | 1068 | Convert... |
paulb@137 | 1069 | |
paulb@137 | 1070 | If (test/body) |
paulb@137 | 1071 | (test/body) |
paulb@137 | 1072 | ... |
paulb@137 | 1073 | (else/body) |
paulb@137 | 1074 | |
paulb@137 | 1075 | ...to: |
paulb@137 | 1076 | |
paulb@137 | 1077 | Conditional (test) -> (body) |
paulb@137 | 1078 | (else) -> Conditional (test) -> (body) |
paulb@137 | 1079 | (else) -> ... |
paulb@137 | 1080 | """ |
paulb@137 | 1081 | |
paulb@137 | 1082 | |
paulb@137 | 1083 | results = nodes = [] |
paulb@137 | 1084 | |
paulb@137 | 1085 | # Produce something like... |
paulb@137 | 1086 | # expr.__bool__() ? body |
paulb@137 | 1087 | |
paulb@137 | 1088 | first = 1 |
paulb@137 | 1089 | for compare, stmt in if_.tests: |
paulb@137 | 1090 | |
paulb@137 | 1091 | # Set the first as the defining node. |
paulb@137 | 1092 | |
paulb@137 | 1093 | test = Conditional(if_, first, |
paulb@137 | 1094 | test=InvokeFunction( |
paulb@185 | 1095 | if_, |
paulb@137 | 1096 | expr=LoadAttr( |
paulb@137 | 1097 | expr=self.dispatch(compare), |
paulb@137 | 1098 | name="__bool__" |
paulb@137 | 1099 | ), |
paulb@148 | 1100 | ) |
paulb@137 | 1101 | ) |
paulb@137 | 1102 | test.body = self.dispatch(stmt) |
paulb@137 | 1103 | nodes.append(test) |
paulb@137 | 1104 | nodes = test.else_ = [] |
paulb@137 | 1105 | first = 0 |
paulb@137 | 1106 | |
paulb@137 | 1107 | # Add the compound statement from any else clause to the end. |
paulb@137 | 1108 | |
paulb@137 | 1109 | if if_.else_ is not None: |
paulb@137 | 1110 | nodes += self.dispatch(if_.else_) |
paulb@137 | 1111 | |
paulb@137 | 1112 | result = results[0] |
paulb@137 | 1113 | return result |
paulb@137 | 1114 | |
paulb@137 | 1115 | def visitImport(self, import_): |
paulb@137 | 1116 | result = Assign(import_, 1) |
paulb@137 | 1117 | code = [] |
paulb@157 | 1118 | _names = [] |
paulb@137 | 1119 | for path, alias in import_.names: |
paulb@158 | 1120 | importer = Import(name=path, alias=alias) |
paulb@137 | 1121 | top = alias or path.split(".")[0] |
paulb@137 | 1122 | code.append(StoreName(expr=importer, name=top)) |
paulb@157 | 1123 | _names.append(code[-1].expr) |
paulb@137 | 1124 | result.code = code |
paulb@157 | 1125 | import_._names = _names |
paulb@137 | 1126 | return result |
paulb@137 | 1127 | |
paulb@137 | 1128 | def visitInvert(self, invert): |
paulb@137 | 1129 | return self._visitUnary(invert, "__invert__") |
paulb@137 | 1130 | |
paulb@137 | 1131 | def visitKeyword(self, keyword): |
paulb@137 | 1132 | result = Keyword(keyword, 1, |
paulb@137 | 1133 | name=keyword.name, |
paulb@137 | 1134 | expr=self.dispatch(keyword.expr) |
paulb@137 | 1135 | ) |
paulb@137 | 1136 | return result |
paulb@137 | 1137 | |
paulb@137 | 1138 | def visitLambda(self, lambda_): |
paulb@137 | 1139 | |
paulb@137 | 1140 | # Make a subprogram for the function and record it outside the main |
paulb@137 | 1141 | # tree. |
paulb@196 | 1142 | |
paulb@196 | 1143 | subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) |
paulb@196 | 1144 | |
paulb@196 | 1145 | # Make nice annotations for the viewer. |
paulb@137 | 1146 | |
paulb@202 | 1147 | lambda_._subprogram = subprogram |
paulb@196 | 1148 | |
paulb@196 | 1149 | # Process the lambda contents. |
paulb@196 | 1150 | |
paulb@137 | 1151 | self.current_subprograms.append(subprogram) |
paulb@137 | 1152 | subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] |
paulb@137 | 1153 | self.current_subprograms.pop() |
paulb@137 | 1154 | self._visitFunction(lambda_, subprogram) |
paulb@137 | 1155 | |
paulb@137 | 1156 | # Get the subprogram reference to the lambda. |
paulb@137 | 1157 | |
paulb@137 | 1158 | return LoadRef(lambda_, 1, ref=subprogram) |
paulb@137 | 1159 | |
paulb@137 | 1160 | def visitList(self, list): |
paulb@204 | 1161 | |
paulb@204 | 1162 | # Make a subprogram for the list construction and record it outside the |
paulb@204 | 1163 | # main tree. |
paulb@204 | 1164 | |
paulb@204 | 1165 | subprogram = Subprogram(list, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=list) |
paulb@204 | 1166 | self.current_subprograms.append(subprogram) |
paulb@204 | 1167 | |
paulb@204 | 1168 | # Make nice annotations for the viewer. |
paulb@204 | 1169 | |
paulb@204 | 1170 | list._subprogram = subprogram |
paulb@204 | 1171 | |
paulb@204 | 1172 | subprogram.code=[ |
paulb@204 | 1173 | StoreTemp( |
paulb@204 | 1174 | expr=InvokeFunction( |
paulb@204 | 1175 | list, |
paulb@204 | 1176 | expr=LoadName( |
paulb@204 | 1177 | name="list" |
paulb@204 | 1178 | ), |
paulb@204 | 1179 | args=[], |
paulb@204 | 1180 | star=None, |
paulb@204 | 1181 | dstar=None |
paulb@204 | 1182 | ) |
paulb@204 | 1183 | ) |
paulb@204 | 1184 | ] |
paulb@204 | 1185 | |
paulb@204 | 1186 | for node in list.nodes: |
paulb@204 | 1187 | subprogram.code.append( |
paulb@204 | 1188 | InvokeFunction( |
paulb@204 | 1189 | list, |
paulb@204 | 1190 | expr=LoadAttr( |
paulb@204 | 1191 | expr=LoadTemp(), |
paulb@204 | 1192 | name="append" |
paulb@204 | 1193 | ), |
paulb@204 | 1194 | args=[self.dispatch(node)], |
paulb@204 | 1195 | star=None, |
paulb@204 | 1196 | dstar=None |
paulb@204 | 1197 | ) |
paulb@204 | 1198 | ) |
paulb@204 | 1199 | |
paulb@204 | 1200 | subprogram.code.append( |
paulb@204 | 1201 | ReturnFromBlock( |
paulb@204 | 1202 | expr=LoadTemp(release=1) |
paulb@204 | 1203 | ) |
paulb@204 | 1204 | ) |
paulb@204 | 1205 | |
paulb@204 | 1206 | self.current_subprograms.pop() |
paulb@204 | 1207 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@204 | 1208 | |
paulb@204 | 1209 | # Make an invocation of the subprogram. |
paulb@204 | 1210 | |
paulb@204 | 1211 | result = InvokeRef(list, 1, |
paulb@204 | 1212 | produces_result=1, |
paulb@204 | 1213 | ref=subprogram |
paulb@204 | 1214 | ) |
paulb@204 | 1215 | return result |
paulb@137 | 1216 | |
paulb@201 | 1217 | def visitListComp(self, listcomp): |
paulb@201 | 1218 | |
paulb@201 | 1219 | # Make a subprogram for the list comprehension and record it outside the |
paulb@201 | 1220 | # main tree. |
paulb@201 | 1221 | |
paulb@240 | 1222 | subprogram = Subprogram(listcomp, name=None, module=self.module, internal=1, returns_value=1, star=None, dstar=None, original_def=listcomp) |
paulb@201 | 1223 | self.current_subprograms.append(subprogram) |
paulb@201 | 1224 | |
paulb@201 | 1225 | # Make nice annotations for the viewer. |
paulb@201 | 1226 | |
paulb@201 | 1227 | listcomp._subprogram = subprogram |
paulb@201 | 1228 | |
paulb@201 | 1229 | # Add a temporary variable. |
paulb@201 | 1230 | # Produce for loops within the subprogram. |
paulb@201 | 1231 | # Return the result. |
paulb@201 | 1232 | |
paulb@201 | 1233 | subprogram.code = [ |
paulb@201 | 1234 | StoreTemp( |
paulb@201 | 1235 | index="listcomp", |
paulb@201 | 1236 | expr=InvokeFunction( |
paulb@201 | 1237 | expr=LoadName(name="list"), |
paulb@201 | 1238 | args=[], |
paulb@201 | 1239 | star=None, |
paulb@201 | 1240 | dstar=None |
paulb@201 | 1241 | ) |
paulb@201 | 1242 | ) |
paulb@201 | 1243 | ] + self._visitListCompFor(listcomp, listcomp.quals) + [ |
paulb@201 | 1244 | ReturnFromBlock( |
paulb@201 | 1245 | expr=LoadTemp( |
paulb@201 | 1246 | index="listcomp", |
paulb@201 | 1247 | release=1 |
paulb@201 | 1248 | ) |
paulb@201 | 1249 | ) |
paulb@201 | 1250 | ] |
paulb@201 | 1251 | |
paulb@201 | 1252 | self.current_subprograms.pop() |
paulb@201 | 1253 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@201 | 1254 | |
paulb@201 | 1255 | # Make an invocation of the subprogram. |
paulb@201 | 1256 | |
paulb@204 | 1257 | result = InvokeRef(listcomp, 1, |
paulb@201 | 1258 | produces_result=1, |
paulb@204 | 1259 | ref=subprogram |
paulb@201 | 1260 | ) |
paulb@201 | 1261 | return result |
paulb@201 | 1262 | |
paulb@201 | 1263 | def _visitListCompFor(self, node, quals): |
paulb@201 | 1264 | qual = quals[0] |
paulb@201 | 1265 | if len(quals) > 1: |
paulb@201 | 1266 | body = self._visitListCompFor(node, quals[1:]) |
paulb@201 | 1267 | if qual.ifs: |
paulb@201 | 1268 | body = self._visitListCompIf(node, qual.ifs, body) |
paulb@202 | 1269 | elif qual.ifs: |
paulb@202 | 1270 | body = self._visitListCompIf(node, qual.ifs) |
paulb@201 | 1271 | else: |
paulb@202 | 1272 | body = self._visitListCompBody(node) |
paulb@201 | 1273 | return [self._visitFor(qual, body)] |
paulb@201 | 1274 | |
paulb@201 | 1275 | def _visitListCompIf(self, node, ifs, expr=None): |
paulb@201 | 1276 | if_ = ifs[0] |
paulb@201 | 1277 | if len(ifs) > 1: |
paulb@201 | 1278 | body = self._visitListCompIf(node, ifs[1:], expr) |
paulb@201 | 1279 | elif expr is None: |
paulb@202 | 1280 | body = self._visitListCompBody(node) |
paulb@201 | 1281 | else: |
paulb@201 | 1282 | body = expr |
paulb@201 | 1283 | return [ |
paulb@201 | 1284 | Conditional(if_, 1, |
paulb@201 | 1285 | test=InvokeFunction( |
paulb@201 | 1286 | if_, |
paulb@201 | 1287 | expr=LoadAttr( |
paulb@201 | 1288 | expr=self.dispatch(if_.test), |
paulb@201 | 1289 | name="__bool__" |
paulb@201 | 1290 | ), |
paulb@201 | 1291 | ), |
paulb@201 | 1292 | body=body, |
paulb@201 | 1293 | else_=[] |
paulb@201 | 1294 | ) |
paulb@201 | 1295 | ] |
paulb@201 | 1296 | |
paulb@202 | 1297 | def _visitListCompBody(self, node): |
paulb@202 | 1298 | return [ |
paulb@202 | 1299 | InvokeFunction( |
paulb@202 | 1300 | expr=LoadAttr( |
paulb@202 | 1301 | expr=LoadTemp(index="listcomp"), |
paulb@202 | 1302 | name="append" |
paulb@202 | 1303 | ), |
paulb@202 | 1304 | args=[self.dispatch(node.expr)], |
paulb@202 | 1305 | star=None, |
paulb@202 | 1306 | dstar=None |
paulb@202 | 1307 | ) |
paulb@202 | 1308 | ] |
paulb@202 | 1309 | |
paulb@157 | 1310 | def visitMod(self, mod): |
paulb@157 | 1311 | return self._visitBinary(mod, "__mod__", "__rmod__") |
paulb@157 | 1312 | |
paulb@137 | 1313 | def visitMul(self, mul): |
paulb@137 | 1314 | return self._visitBinary(mul, "__mul__", "__rmul__") |
paulb@137 | 1315 | |
paulb@137 | 1316 | def visitName(self, name): |
paulb@137 | 1317 | result = LoadName(name, 1, name=name.name) |
paulb@137 | 1318 | return result |
paulb@137 | 1319 | |
paulb@137 | 1320 | def _visitNot(self, expr, not_=None): |
paulb@137 | 1321 | invocation = InvokeFunction( |
paulb@185 | 1322 | not_, # NOTE: May need a real original node. |
paulb@137 | 1323 | expr=LoadAttr( |
paulb@137 | 1324 | expr=expr, |
paulb@137 | 1325 | name="__bool__" |
paulb@137 | 1326 | ), |
paulb@137 | 1327 | ) |
paulb@137 | 1328 | if not_ is not None: |
paulb@137 | 1329 | result = Not(not_, 1, expr=invocation) |
paulb@137 | 1330 | else: |
paulb@137 | 1331 | result = Not(expr=invocation) |
paulb@137 | 1332 | return result |
paulb@137 | 1333 | |
paulb@137 | 1334 | def visitNot(self, not_): |
paulb@137 | 1335 | return self._visitNot(self.dispatch(not_.expr), not_) |
paulb@137 | 1336 | |
paulb@137 | 1337 | def visitOr(self, or_): |
paulb@137 | 1338 | |
paulb@137 | 1339 | """ |
paulb@137 | 1340 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@137 | 1341 | subprogram. Convert... |
paulb@137 | 1342 | |
paulb@137 | 1343 | Or (test) |
paulb@137 | 1344 | (test) |
paulb@137 | 1345 | ... |
paulb@137 | 1346 | |
paulb@137 | 1347 | ...to: |
paulb@137 | 1348 | |
paulb@137 | 1349 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1350 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1351 | (else) -> ... |
paulb@137 | 1352 | """ |
paulb@137 | 1353 | |
paulb@137 | 1354 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1355 | self.current_subprograms.append(subprogram) |
paulb@137 | 1356 | |
paulb@137 | 1357 | # In the subprogram, make instructions which store each operand, test |
paulb@137 | 1358 | # for each operand's truth status, and if appropriate return from the |
paulb@137 | 1359 | # subprogram with the value of the operand. |
paulb@137 | 1360 | |
paulb@137 | 1361 | last = or_.nodes[-1] |
paulb@137 | 1362 | results = nodes = [] |
paulb@137 | 1363 | |
paulb@137 | 1364 | for node in or_.nodes: |
paulb@137 | 1365 | expr = self.dispatch(node) |
paulb@137 | 1366 | |
paulb@137 | 1367 | # Return from the subprogram where the test is satisfied. |
paulb@137 | 1368 | |
paulb@137 | 1369 | if node is not last: |
paulb@137 | 1370 | nodes.append(StoreTemp(expr=expr)) |
paulb@185 | 1371 | invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) |
paulb@137 | 1372 | test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) |
paulb@137 | 1373 | nodes.append(test) |
paulb@137 | 1374 | |
paulb@137 | 1375 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 1376 | |
paulb@137 | 1377 | nodes = test.else_ = [ReleaseTemp()] |
paulb@137 | 1378 | |
paulb@137 | 1379 | # For the last operation, return the result. |
paulb@137 | 1380 | |
paulb@137 | 1381 | else: |
paulb@137 | 1382 | nodes.append( |
paulb@137 | 1383 | ReturnFromBlock(expr=expr) |
paulb@137 | 1384 | ) |
paulb@137 | 1385 | |
paulb@137 | 1386 | # Finish the subprogram definition. |
paulb@137 | 1387 | |
paulb@137 | 1388 | subprogram.code = results |
paulb@137 | 1389 | |
paulb@137 | 1390 | self.current_subprograms.pop() |
paulb@137 | 1391 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1392 | |
paulb@137 | 1393 | # Make an invocation of the subprogram. |
paulb@137 | 1394 | |
paulb@204 | 1395 | result = InvokeRef(or_, 1, |
paulb@201 | 1396 | produces_result=1, |
paulb@204 | 1397 | ref=subprogram |
paulb@201 | 1398 | ) |
paulb@137 | 1399 | return result |
paulb@137 | 1400 | |
paulb@137 | 1401 | def visitPass(self, pass_): |
paulb@137 | 1402 | return Pass(pass_, 1) |
paulb@137 | 1403 | |
paulb@141 | 1404 | def visitPower(self, power): |
paulb@141 | 1405 | return self._visitBinary(power, "__pow__", "__rpow__") |
paulb@141 | 1406 | |
paulb@147 | 1407 | def visitPrint(self, print_): |
paulb@147 | 1408 | |
paulb@147 | 1409 | """ |
paulb@147 | 1410 | Convert... |
paulb@147 | 1411 | |
paulb@147 | 1412 | Print (dest) -> |
paulb@147 | 1413 | (nodes) |
paulb@147 | 1414 | |
paulb@147 | 1415 | ...to: |
paulb@147 | 1416 | |
paulb@147 | 1417 | StoreTemp (index) -> "print" |
paulb@147 | 1418 | (expr) -> LoadAttr (expr) -> (dest) |
paulb@147 | 1419 | (name) -> "write" |
paulb@147 | 1420 | InvokeFunction (expr) -> LoadTemp (index) -> "print" |
paulb@147 | 1421 | (args) -> [(node)] |
paulb@147 | 1422 | ReleaseTemp (index) -> "print" |
paulb@147 | 1423 | """ |
paulb@147 | 1424 | |
paulb@147 | 1425 | if print_.dest is not None: |
paulb@147 | 1426 | dest = self.dispatch(print_.dest) |
paulb@147 | 1427 | else: |
paulb@147 | 1428 | dest = self.dispatch(compiler.ast.Name("stdout")) |
paulb@147 | 1429 | |
paulb@147 | 1430 | result = Assign(print_, 1, |
paulb@147 | 1431 | code=[ |
paulb@147 | 1432 | StoreTemp( |
paulb@147 | 1433 | index="print", |
paulb@147 | 1434 | expr=LoadAttr( |
paulb@147 | 1435 | expr=dest, |
paulb@147 | 1436 | name="write" |
paulb@147 | 1437 | ) |
paulb@147 | 1438 | ) |
paulb@147 | 1439 | ] |
paulb@147 | 1440 | ) |
paulb@147 | 1441 | |
paulb@147 | 1442 | for node in print_.nodes: |
paulb@147 | 1443 | result.code.append( |
paulb@147 | 1444 | InvokeFunction( |
paulb@185 | 1445 | print_, |
paulb@147 | 1446 | expr=LoadTemp(index="print"), |
paulb@147 | 1447 | args=[self.dispatch(node)], |
paulb@147 | 1448 | star=None, |
paulb@147 | 1449 | dstar=None |
paulb@147 | 1450 | ) |
paulb@147 | 1451 | ) |
paulb@147 | 1452 | |
paulb@147 | 1453 | result.code.append( |
paulb@147 | 1454 | ReleaseTemp(index="print") |
paulb@147 | 1455 | ) |
paulb@147 | 1456 | |
paulb@147 | 1457 | return result |
paulb@147 | 1458 | |
paulb@147 | 1459 | def visitPrintnl(self, printnl): |
paulb@147 | 1460 | result = self.visitPrint(printnl) |
paulb@147 | 1461 | result.code.insert( |
paulb@147 | 1462 | len(result.code) - 1, |
paulb@147 | 1463 | InvokeFunction( |
paulb@185 | 1464 | printnl, |
paulb@147 | 1465 | expr=LoadTemp(index="print"), |
paulb@147 | 1466 | args=[self.dispatch(compiler.ast.Const("\n"))], |
paulb@147 | 1467 | star=None, |
paulb@147 | 1468 | dstar=None |
paulb@147 | 1469 | ) |
paulb@147 | 1470 | ) |
paulb@147 | 1471 | return result |
paulb@147 | 1472 | |
paulb@137 | 1473 | def visitRaise(self, raise_): |
paulb@137 | 1474 | result = Raise(raise_, 1) |
paulb@137 | 1475 | if raise_.expr2 is None: |
paulb@137 | 1476 | result.expr = self.dispatch(raise_.expr1) |
paulb@137 | 1477 | else: |
paulb@137 | 1478 | result.expr = InvokeFunction( |
paulb@185 | 1479 | raise_, |
paulb@137 | 1480 | expr=self.dispatch(raise_.expr1), |
paulb@137 | 1481 | args=[self.dispatch(raise_.expr2)], |
paulb@137 | 1482 | star=None, |
paulb@137 | 1483 | dstar=None |
paulb@137 | 1484 | ) |
paulb@137 | 1485 | if raise_.expr3 is not None: |
paulb@137 | 1486 | result.traceback = self.dispatch(raise_.expr3) |
paulb@137 | 1487 | else: |
paulb@137 | 1488 | result.traceback = None |
paulb@137 | 1489 | return result |
paulb@137 | 1490 | |
paulb@137 | 1491 | def visitReturn(self, return_): |
paulb@137 | 1492 | result = ReturnFromFunction(return_, 1, |
paulb@137 | 1493 | expr=self.dispatch(return_.value) |
paulb@137 | 1494 | ) |
paulb@137 | 1495 | return result |
paulb@137 | 1496 | |
paulb@137 | 1497 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@137 | 1498 | if flags == "OP_ASSIGN": |
paulb@137 | 1499 | result = InvokeFunction(slice, 1, |
paulb@137 | 1500 | expr=LoadAttr( |
paulb@137 | 1501 | expr=expr, |
paulb@137 | 1502 | name="__setslice__" |
paulb@137 | 1503 | ), |
paulb@137 | 1504 | star=None, |
paulb@137 | 1505 | dstar=None, |
paulb@137 | 1506 | args=[lower, upper, value] |
paulb@137 | 1507 | ) |
paulb@137 | 1508 | elif flags == "OP_APPLY": |
paulb@137 | 1509 | args = [] |
paulb@137 | 1510 | result = InvokeFunction(slice, 1, |
paulb@137 | 1511 | expr=LoadAttr( |
paulb@137 | 1512 | expr=expr, |
paulb@137 | 1513 | name="__getslice__" |
paulb@137 | 1514 | ), |
paulb@137 | 1515 | star=None, |
paulb@137 | 1516 | dstar=None, |
paulb@137 | 1517 | args=[lower, upper] |
paulb@137 | 1518 | ) |
paulb@137 | 1519 | elif flags == "OP_DELETE": |
paulb@137 | 1520 | args = [] |
paulb@137 | 1521 | result = InvokeFunction(slice, 1, |
paulb@137 | 1522 | expr=LoadAttr( |
paulb@137 | 1523 | expr=expr, |
paulb@137 | 1524 | name="__delslice__" |
paulb@137 | 1525 | ), |
paulb@137 | 1526 | star=None, |
paulb@137 | 1527 | dstar=None, |
paulb@137 | 1528 | args=[lower, upper] |
paulb@137 | 1529 | ) |
paulb@137 | 1530 | else: |
paulb@137 | 1531 | raise NotImplementedError, flags |
paulb@137 | 1532 | |
paulb@137 | 1533 | return result |
paulb@137 | 1534 | |
paulb@257 | 1535 | def visitSlice(self, slice, element=None): |
paulb@137 | 1536 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@257 | 1537 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, element)) |
paulb@137 | 1538 | |
paulb@201 | 1539 | def visitSliceobj(self, sliceobj): |
paulb@201 | 1540 | return InvokeFunction(sliceobj, 1, |
paulb@201 | 1541 | expr=LoadName(name="slice"), |
paulb@201 | 1542 | args=self.dispatches(sliceobj.nodes), |
paulb@201 | 1543 | star=None, |
paulb@201 | 1544 | dstar=None |
paulb@201 | 1545 | ) |
paulb@201 | 1546 | |
paulb@137 | 1547 | def visitStmt(self, stmt): |
paulb@137 | 1548 | return self.dispatches(stmt.nodes) |
paulb@137 | 1549 | |
paulb@137 | 1550 | def visitSub(self, sub): |
paulb@137 | 1551 | return self._visitBinary(sub, "__sub__", "__rsub__") |
paulb@137 | 1552 | |
paulb@137 | 1553 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@137 | 1554 | if flags == "OP_ASSIGN": |
paulb@137 | 1555 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1556 | expr=LoadAttr( |
paulb@137 | 1557 | expr=expr, |
paulb@137 | 1558 | name="__setitem__" |
paulb@137 | 1559 | ), |
paulb@137 | 1560 | star=None, |
paulb@137 | 1561 | dstar=None, |
paulb@137 | 1562 | args=[subs, value] |
paulb@137 | 1563 | ) |
paulb@137 | 1564 | elif flags == "OP_APPLY": |
paulb@137 | 1565 | args = [] |
paulb@137 | 1566 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1567 | expr=LoadAttr( |
paulb@137 | 1568 | expr=expr, |
paulb@137 | 1569 | name="__getitem__" |
paulb@137 | 1570 | ), |
paulb@137 | 1571 | star=None, |
paulb@137 | 1572 | dstar=None, |
paulb@137 | 1573 | args=[subs] |
paulb@137 | 1574 | ) |
paulb@137 | 1575 | elif flags == "OP_DELETE": |
paulb@137 | 1576 | args = [] |
paulb@137 | 1577 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1578 | expr=LoadAttr( |
paulb@137 | 1579 | expr=expr, |
paulb@137 | 1580 | name="__delitem__" |
paulb@137 | 1581 | ), |
paulb@137 | 1582 | star=None, |
paulb@137 | 1583 | dstar=None, |
paulb@137 | 1584 | args=[subs] |
paulb@137 | 1585 | ) |
paulb@137 | 1586 | else: |
paulb@137 | 1587 | raise NotImplementedError, flags |
paulb@137 | 1588 | |
paulb@137 | 1589 | return result |
paulb@137 | 1590 | |
paulb@137 | 1591 | def _visitSubscriptSubs(self, node, subs): |
paulb@137 | 1592 | if len(subs) == 1: |
paulb@137 | 1593 | return self.dispatch(subs[0]) |
paulb@137 | 1594 | else: |
paulb@137 | 1595 | return InvokeFunction(node, 1, |
paulb@137 | 1596 | expr=LoadName(name="tuple"), |
paulb@137 | 1597 | args=self.dispatches(subs), |
paulb@137 | 1598 | star=None, |
paulb@137 | 1599 | dstar=None |
paulb@137 | 1600 | ) |
paulb@137 | 1601 | |
paulb@257 | 1602 | def visitSubscript(self, subscript, element=None): |
paulb@137 | 1603 | return self._visitSubscript( |
paulb@137 | 1604 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@257 | 1605 | self._visitAssNameOrAttr(subscript, element) |
paulb@137 | 1606 | ) |
paulb@137 | 1607 | |
paulb@137 | 1608 | def visitTryExcept(self, tryexcept): |
paulb@137 | 1609 | |
paulb@137 | 1610 | """ |
paulb@137 | 1611 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@137 | 1612 | |
paulb@137 | 1613 | Convert... |
paulb@137 | 1614 | |
paulb@137 | 1615 | TryExcept (body) |
paulb@137 | 1616 | (else) |
paulb@137 | 1617 | (spec/assign/stmt) |
paulb@137 | 1618 | ... |
paulb@137 | 1619 | |
paulb@137 | 1620 | ...to: |
paulb@137 | 1621 | |
paulb@137 | 1622 | Try (body) |
paulb@137 | 1623 | (else) |
paulb@137 | 1624 | (handler) -> Conditional (test) -> (stmt) |
paulb@190 | 1625 | (body) -> ResetExc ... |
paulb@137 | 1626 | (else) -> Conditional (test) -> (stmt) |
paulb@190 | 1627 | (body) -> ResetExc ... |
paulb@137 | 1628 | (else) -> ... |
paulb@137 | 1629 | """ |
paulb@137 | 1630 | |
paulb@137 | 1631 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@137 | 1632 | |
paulb@137 | 1633 | if tryexcept.body is not None: |
paulb@137 | 1634 | result.body = self.dispatch(tryexcept.body) |
paulb@137 | 1635 | if tryexcept.else_ is not None: |
paulb@137 | 1636 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@137 | 1637 | |
paulb@137 | 1638 | results = nodes = [] |
paulb@137 | 1639 | catch_all = 0 |
paulb@137 | 1640 | |
paulb@137 | 1641 | for spec, assign, stmt in tryexcept.handlers: |
paulb@137 | 1642 | |
paulb@137 | 1643 | # If no specification exists, produce an unconditional block. |
paulb@137 | 1644 | |
paulb@137 | 1645 | if spec is None: |
paulb@137 | 1646 | nodes += self.dispatch(stmt) |
paulb@137 | 1647 | catch_all = 1 |
paulb@137 | 1648 | |
paulb@137 | 1649 | # Produce an exception value check. |
paulb@137 | 1650 | |
paulb@137 | 1651 | else: |
paulb@137 | 1652 | test = Conditional( |
paulb@137 | 1653 | isolate_test=1, |
paulb@193 | 1654 | test=CheckType(expr=LoadExc(), choices=self._visitTryExcept(spec)) |
paulb@137 | 1655 | ) |
paulb@137 | 1656 | test.body = [] |
paulb@137 | 1657 | |
paulb@137 | 1658 | if assign is not None: |
paulb@137 | 1659 | test.body.append( |
paulb@137 | 1660 | Assign( |
paulb@137 | 1661 | code=[ |
paulb@137 | 1662 | StoreTemp(expr=LoadExc()), |
paulb@137 | 1663 | self.dispatch(assign), |
paulb@137 | 1664 | ReleaseTemp() |
paulb@137 | 1665 | ] |
paulb@137 | 1666 | ) |
paulb@137 | 1667 | ) |
paulb@137 | 1668 | |
paulb@190 | 1669 | test.body += [ResetExc()] + self.dispatch(stmt) |
paulb@137 | 1670 | nodes.append(test) |
paulb@137 | 1671 | nodes = test.else_ = [] |
paulb@137 | 1672 | |
paulb@137 | 1673 | # Add a raise operation to deal with unhandled exceptions. |
paulb@137 | 1674 | |
paulb@137 | 1675 | if not catch_all: |
paulb@137 | 1676 | nodes.append( |
paulb@137 | 1677 | Raise( |
paulb@137 | 1678 | expr=LoadExc()) |
paulb@137 | 1679 | ) |
paulb@137 | 1680 | |
paulb@137 | 1681 | result.handler = results |
paulb@137 | 1682 | return result |
paulb@137 | 1683 | |
paulb@137 | 1684 | def _visitTryExcept(self, spec): |
paulb@137 | 1685 | |
paulb@137 | 1686 | "Return a list of nodes for the given exception type 'spec'." |
paulb@137 | 1687 | |
paulb@137 | 1688 | if isinstance(spec, compiler.ast.Tuple): |
paulb@137 | 1689 | nodes = [] |
paulb@137 | 1690 | for node in spec.nodes: |
paulb@137 | 1691 | nodes += self._visitTryExcept(node) |
paulb@137 | 1692 | else: |
paulb@137 | 1693 | nodes = [self.dispatch(spec)] |
paulb@137 | 1694 | return nodes |
paul@0 | 1695 | |
paul@0 | 1696 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1697 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1698 | if tryfinally.body is not None: |
paul@0 | 1699 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1700 | if tryfinally.final is not None: |
paul@0 | 1701 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1702 | return result |
paul@0 | 1703 | |
paulb@137 | 1704 | def visitTuple(self, tuple): |
paulb@219 | 1705 | |
paulb@219 | 1706 | "Make a MakeTuple node containing the original 'tuple' contents." |
paulb@219 | 1707 | |
paulb@219 | 1708 | result = MakeTuple(tuple, 1, |
paulb@219 | 1709 | nodes=self.dispatches(tuple.nodes) |
paulb@219 | 1710 | ) |
paulb@219 | 1711 | return result |
paulb@137 | 1712 | |
paulb@137 | 1713 | def visitUnaryAdd(self, unaryadd): |
paulb@137 | 1714 | return self._visitUnary(unaryadd, "__pos__") |
paulb@137 | 1715 | |
paulb@137 | 1716 | def visitUnarySub(self, unarysub): |
paulb@137 | 1717 | return self._visitUnary(unarysub, "__neg__") |
paulb@137 | 1718 | |
paulb@137 | 1719 | def visitWhile(self, while_): |
paulb@137 | 1720 | |
paulb@137 | 1721 | """ |
paulb@137 | 1722 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@137 | 1723 | subprogram. Convert... |
paulb@137 | 1724 | |
paulb@137 | 1725 | While (test) -> (body) |
paulb@137 | 1726 | (else) |
paulb@137 | 1727 | |
paulb@137 | 1728 | ...to: |
paulb@137 | 1729 | |
paulb@137 | 1730 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@137 | 1731 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1732 | (else) -> ... |
paulb@137 | 1733 | """ |
paulb@137 | 1734 | |
paulb@137 | 1735 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@137 | 1736 | self.current_subprograms.append(subprogram) |
paulb@137 | 1737 | |
paulb@137 | 1738 | # Include a conditional statement in the subprogram. |
paulb@137 | 1739 | # Inside the conditional, add a recursive invocation to the subprogram |
paulb@137 | 1740 | # if the test condition was satisfied. |
paulb@137 | 1741 | # Return within the main section of the loop. |
paulb@137 | 1742 | |
paulb@147 | 1743 | test = Conditional( |
paulb@147 | 1744 | test=InvokeFunction( |
paulb@185 | 1745 | while_, |
paulb@147 | 1746 | expr=LoadAttr( |
paulb@147 | 1747 | expr=self.dispatch(while_.test), |
paulb@147 | 1748 | name="__bool__"), |
paulb@147 | 1749 | ), |
paulb@147 | 1750 | body=self.dispatch(while_.body) + [ |
paulb@204 | 1751 | InvokeRef( |
paulb@185 | 1752 | while_, |
paulb@204 | 1753 | ref=subprogram |
paulb@147 | 1754 | ), |
paulb@147 | 1755 | ReturnFromBlock() |
paulb@147 | 1756 | ], |
paulb@147 | 1757 | else_=[] |
paulb@147 | 1758 | ) |
paulb@137 | 1759 | |
paulb@137 | 1760 | # Provide the else section, if present, along with an explicit return. |
paulb@137 | 1761 | |
paulb@137 | 1762 | if while_.else_ is not None: |
paulb@137 | 1763 | test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] |
paulb@137 | 1764 | |
paulb@137 | 1765 | # Finish the subprogram definition. |
paulb@137 | 1766 | |
paulb@137 | 1767 | subprogram.code = [test] |
paulb@137 | 1768 | |
paulb@137 | 1769 | self.current_subprograms.pop() |
paulb@137 | 1770 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1771 | |
paulb@137 | 1772 | # Make an invocation of the subprogram. |
paulb@137 | 1773 | |
paulb@204 | 1774 | result = InvokeRef(while_, 1, ref=subprogram) |
paulb@148 | 1775 | |
paulb@148 | 1776 | # Make nice annotations for the viewer. |
paulb@148 | 1777 | |
paulb@148 | 1778 | while_._test_call = subprogram.code[0].test |
paulb@148 | 1779 | |
paulb@137 | 1780 | return result |
paulb@137 | 1781 | |
paulb@203 | 1782 | # NOTE: Not actually supported. |
paulb@203 | 1783 | # NOTE: Virtually the same as visitReturn... |
paulb@203 | 1784 | |
paulb@203 | 1785 | def visitYield(self, yield_): |
paulb@203 | 1786 | result = Yield(yield_, 1, |
paulb@203 | 1787 | expr=self.dispatch(yield_.value) |
paulb@203 | 1788 | ) |
paulb@203 | 1789 | return result |
paulb@203 | 1790 | |
paulb@137 | 1791 | # Convenience methods. |
paulb@137 | 1792 | |
paulb@137 | 1793 | def _visitBinary(self, binary, left_name, right_name): |
paulb@192 | 1794 | return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name) |
paulb@192 | 1795 | |
paulb@193 | 1796 | def _visitBinaryCompareOp(self, binary, left, right, left_name, right_name): |
paulb@193 | 1797 | |
paulb@193 | 1798 | """ |
paulb@193 | 1799 | Emulate the current mechanisms by producing nodes as follows: |
paulb@193 | 1800 | |
paulb@204 | 1801 | InvokeRef -> Subprogram -> StoreTemp (expr) -> x.__lt__(y) |
paulb@193 | 1802 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1803 | (body) -> ReleaseTemp |
paulb@193 | 1804 | StoreTemp (expr) -> y.__gt__(x) |
paulb@193 | 1805 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1806 | (body) -> ReturnFromBlock (expr) -> False |
paulb@193 | 1807 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1808 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1809 | """ |
paulb@193 | 1810 | |
paulb@193 | 1811 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@193 | 1812 | self.current_subprograms.append(subprogram) |
paulb@193 | 1813 | |
paulb@193 | 1814 | subprogram.code = [ |
paulb@193 | 1815 | StoreTemp( |
paulb@227 | 1816 | expr=left, |
paulb@227 | 1817 | index="left" |
paulb@227 | 1818 | ), |
paulb@227 | 1819 | StoreTemp( |
paulb@227 | 1820 | expr=right, |
paulb@227 | 1821 | index="right" |
paulb@227 | 1822 | ), |
paulb@227 | 1823 | StoreTemp( |
paulb@193 | 1824 | expr=InvokeFunction( |
paulb@193 | 1825 | binary, |
paulb@227 | 1826 | expr=LoadAttr( |
paulb@227 | 1827 | expr=LoadTemp(index="left"), |
paulb@227 | 1828 | name=left_name), |
paulb@227 | 1829 | args=[ |
paulb@227 | 1830 | LoadTemp(index="right") |
paulb@227 | 1831 | ], |
paulb@193 | 1832 | star=None, |
paulb@193 | 1833 | dstar=None) |
paulb@193 | 1834 | ), |
paulb@193 | 1835 | Conditional( |
paulb@193 | 1836 | isolate_test=1, |
paulb@193 | 1837 | test=CheckType( |
paulb@193 | 1838 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1839 | ), |
paulb@193 | 1840 | body=[ |
paulb@193 | 1841 | ReleaseTemp(), |
paulb@193 | 1842 | StoreTemp( |
paulb@193 | 1843 | expr=InvokeFunction( |
paulb@193 | 1844 | binary, |
paulb@227 | 1845 | expr=LoadAttr( |
paulb@227 | 1846 | expr=LoadTemp(index="right"), |
paulb@227 | 1847 | name=right_name), |
paulb@227 | 1848 | args=[ |
paulb@227 | 1849 | LoadTemp(index="left") |
paulb@227 | 1850 | ], |
paulb@193 | 1851 | star=None, |
paulb@193 | 1852 | dstar=None) |
paulb@193 | 1853 | ), |
paulb@193 | 1854 | Conditional( |
paulb@193 | 1855 | isolate_test=1, |
paulb@193 | 1856 | test=CheckType( |
paulb@193 | 1857 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1858 | ), |
paulb@193 | 1859 | body=[ |
paulb@193 | 1860 | ReturnFromBlock( |
paulb@193 | 1861 | expr=LoadName(name="False") |
paulb@193 | 1862 | ) |
paulb@193 | 1863 | ], |
paulb@193 | 1864 | else_=[ |
paulb@193 | 1865 | CheckType( |
paulb@193 | 1866 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1867 | ), |
paulb@193 | 1868 | ReturnFromBlock( |
paulb@193 | 1869 | expr=LoadTemp() |
paulb@193 | 1870 | ) |
paulb@193 | 1871 | ] |
paulb@193 | 1872 | ) |
paulb@193 | 1873 | ], |
paulb@193 | 1874 | else_=[ |
paulb@193 | 1875 | CheckType( |
paulb@193 | 1876 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1877 | ), |
paulb@193 | 1878 | ReturnFromBlock( |
paulb@193 | 1879 | expr=LoadTemp() |
paulb@193 | 1880 | ) |
paulb@193 | 1881 | ] |
paulb@193 | 1882 | ) |
paulb@193 | 1883 | ] |
paulb@193 | 1884 | |
paulb@193 | 1885 | self.current_subprograms.pop() |
paulb@193 | 1886 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@193 | 1887 | |
paulb@204 | 1888 | result = InvokeRef( |
paulb@193 | 1889 | binary, |
paulb@193 | 1890 | produces_result=1, |
paulb@204 | 1891 | ref=subprogram |
paulb@193 | 1892 | ) |
paulb@193 | 1893 | |
paulb@193 | 1894 | # Make nice annotations for the viewer. |
paulb@193 | 1895 | |
paulb@227 | 1896 | binary._left_call = subprogram.code[2].expr |
paulb@227 | 1897 | binary._right_call = subprogram.code[3].body[1].expr |
paulb@193 | 1898 | |
paulb@193 | 1899 | return result |
paulb@193 | 1900 | |
paulb@192 | 1901 | def _visitBinaryOp(self, binary, left, right, left_name, right_name): |
paulb@137 | 1902 | |
paulb@137 | 1903 | """ |
paulb@137 | 1904 | Emulate the current mechanisms by producing nodes as follows: |
paulb@137 | 1905 | |
paulb@204 | 1906 | InvokeRef -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) |
paulb@137 | 1907 | (else) |
paulb@193 | 1908 | (handler) -> Conditional (test) -> CheckType (expr) -> LoadExc |
paulb@137 | 1909 | (choices) -> LoadName TypeError |
paulb@137 | 1910 | (body) -> ReturnFromBlock (expr) -> y.__radd__(x) |
paulb@137 | 1911 | (else) |
paulb@137 | 1912 | """ |
paulb@137 | 1913 | |
paulb@137 | 1914 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1915 | self.current_subprograms.append(subprogram) |
paulb@137 | 1916 | |
paulb@137 | 1917 | subprogram.code = [ |
paulb@227 | 1918 | StoreTemp( |
paulb@227 | 1919 | expr=left, |
paulb@227 | 1920 | index="left" |
paulb@227 | 1921 | ), |
paulb@227 | 1922 | StoreTemp( |
paulb@227 | 1923 | expr=right, |
paulb@227 | 1924 | index="right" |
paulb@227 | 1925 | ), |
paulb@137 | 1926 | Try(binary, 1, |
paulb@137 | 1927 | body=[ |
paulb@137 | 1928 | ReturnFromBlock( |
paulb@137 | 1929 | expr=InvokeFunction( |
paulb@185 | 1930 | binary, |
paulb@227 | 1931 | expr=LoadAttr(expr=LoadTemp(index="left"), name=left_name), |
paulb@227 | 1932 | args=[LoadTemp(index="right")], |
paulb@137 | 1933 | star=None, |
paulb@137 | 1934 | dstar=None) |
paulb@137 | 1935 | ) |
paulb@137 | 1936 | ], |
paulb@137 | 1937 | else_=[], |
paulb@137 | 1938 | finally_=[], |
paulb@137 | 1939 | handler=[ |
paulb@137 | 1940 | Conditional( |
paulb@193 | 1941 | test=CheckType(expr=LoadExc(), choices=[LoadName(name="TypeError")]), |
paulb@137 | 1942 | body=[ |
paulb@137 | 1943 | ReturnFromBlock( |
paulb@137 | 1944 | expr=InvokeFunction( |
paulb@185 | 1945 | binary, |
paulb@227 | 1946 | expr=LoadAttr(expr=LoadTemp(index="right"), name=right_name), |
paulb@227 | 1947 | args=[LoadTemp(index="left")], |
paulb@137 | 1948 | star=None, |
paulb@137 | 1949 | dstar=None) |
paulb@137 | 1950 | ) |
paulb@137 | 1951 | ], |
paulb@137 | 1952 | else_=[] |
paulb@137 | 1953 | ) |
paulb@137 | 1954 | ] |
paulb@137 | 1955 | ) |
paulb@137 | 1956 | ] |
paulb@137 | 1957 | |
paulb@137 | 1958 | self.current_subprograms.pop() |
paulb@137 | 1959 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1960 | |
paulb@204 | 1961 | result = InvokeRef( |
paulb@185 | 1962 | binary, |
paulb@148 | 1963 | produces_result=1, |
paulb@204 | 1964 | ref=subprogram |
paulb@148 | 1965 | ) |
paulb@148 | 1966 | |
paulb@148 | 1967 | # Make nice annotations for the viewer. |
paulb@148 | 1968 | |
paulb@227 | 1969 | binary._left_call = subprogram.code[2].body[0].expr |
paulb@227 | 1970 | binary._right_call = subprogram.code[2].handler[0].body[0].expr |
paulb@148 | 1971 | |
paulb@137 | 1972 | return result |
paulb@137 | 1973 | |
paulb@137 | 1974 | def _visitBuiltin(self, builtin, name): |
paulb@212 | 1975 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes)) |
paulb@137 | 1976 | return result |
paulb@137 | 1977 | |
paulb@137 | 1978 | def _visitUnary(self, unary, name): |
paulb@148 | 1979 | result = InvokeFunction(unary, 1, |
paulb@137 | 1980 | expr=LoadAttr( |
paulb@137 | 1981 | expr=self.dispatch(unary.expr), |
paulb@137 | 1982 | name=name |
paulb@148 | 1983 | ) |
paulb@137 | 1984 | ) |
paulb@137 | 1985 | |
paulb@148 | 1986 | # Make nice annotations for the viewer. |
paulb@148 | 1987 | |
paulb@148 | 1988 | unary._unary_call = result |
paulb@148 | 1989 | |
paulb@148 | 1990 | return result |
paulb@148 | 1991 | |
paulb@57 | 1992 | # Convenience functions. |
paulb@57 | 1993 | |
paulb@158 | 1994 | def simplify(filename, builtins=0, module_name=None): |
paulb@57 | 1995 | |
paulb@57 | 1996 | """ |
paulb@88 | 1997 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1998 | |
paulb@88 | 1999 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 2000 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 2001 | """ |
paulb@57 | 2002 | |
paulb@57 | 2003 | simplifier = Simplifier(builtins) |
paulb@57 | 2004 | module = compiler.parseFile(filename) |
paulb@57 | 2005 | compiler.misc.set_filename(filename, module) |
paulb@88 | 2006 | if builtins: |
paulb@158 | 2007 | name = module_name or "__builtins__" |
paulb@88 | 2008 | else: |
paulb@88 | 2009 | path, ext = os.path.splitext(filename) |
paulb@88 | 2010 | path, name = os.path.split(path) |
paulb@158 | 2011 | name = module_name or name |
paulb@88 | 2012 | simplified = simplifier.process(module, name) |
paulb@57 | 2013 | return simplified |
paulb@57 | 2014 | |
paul@0 | 2015 | # vim: tabstop=4 expandtab shiftwidth=4 |