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