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