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 | |
paul@0 | 8 | Copyright (C) 2006 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@157 | 53 | Invert, Keyword, Lambda, List, Mod, Module, Mul, Name, Not, Or, |
paulb@157 | 54 | Pass, Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, |
paulb@157 | 55 | Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub. |
paul@0 | 56 | |
paulb@136 | 57 | Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, |
paulb@147 | 58 | Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift, |
paulb@147 | 59 | Sliceobj, 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@148 | 227 | expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next")) |
paulb@137 | 228 | result = Assign(asslist, 1) |
paulb@148 | 229 | store = StoreTemp(expr=InvokeFunction(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@148 | 240 | return InvokeFunction(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@59 | 280 | args=[expr], |
paulb@59 | 281 | star=None, |
paulb@59 | 282 | dstar=None, |
paulb@101 | 283 | expr=LoadAttr( |
paulb@56 | 284 | expr=self.dispatch(augassign.node), |
paulb@56 | 285 | name=self.augassign_methods[augassign.op] |
paulb@56 | 286 | ) |
paulb@56 | 287 | ) |
paulb@56 | 288 | ), |
paulb@101 | 289 | StoreName( |
paulb@101 | 290 | expr=LoadTemp(), |
paulb@56 | 291 | name=augassign.node.name), |
paulb@101 | 292 | ReleaseTemp() |
paulb@56 | 293 | ] |
paul@2 | 294 | |
paulb@173 | 295 | # Make nice annotations for the viewer. |
paulb@173 | 296 | |
paulb@173 | 297 | augassign._op_call = result.code[0].expr |
paulb@173 | 298 | |
paulb@9 | 299 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 300 | |
paul@2 | 301 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 302 | |
paulb@9 | 303 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 304 | |
paulb@56 | 305 | result.code = [ |
paulb@101 | 306 | StoreTemp( |
paulb@56 | 307 | index="expr", |
paulb@56 | 308 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 309 | ), |
paulb@101 | 310 | StoreTemp( |
paulb@173 | 311 | expr=InvokeFunction( # referenced below |
paulb@56 | 312 | args=[expr], star=None, dstar=None, |
paulb@101 | 313 | expr=LoadAttr( |
paulb@92 | 314 | expr=LoadAttr(augassign.node, 1, |
paulb@101 | 315 | expr=LoadTemp(index="expr"), |
paulb@56 | 316 | name=augassign.node.attrname |
paulb@56 | 317 | ), |
paulb@56 | 318 | name=self.augassign_methods[augassign.op] |
paulb@56 | 319 | ) |
paulb@56 | 320 | ) |
paulb@56 | 321 | ), |
paulb@101 | 322 | StoreAttr( |
paulb@101 | 323 | expr=LoadTemp(), |
paulb@101 | 324 | lvalue=LoadTemp(index="expr"), |
paulb@56 | 325 | name=augassign.node.attrname |
paulb@56 | 326 | ), |
paulb@101 | 327 | ReleaseTemp(index="expr"), |
paulb@101 | 328 | ReleaseTemp() |
paulb@56 | 329 | ] |
paulb@6 | 330 | |
paulb@173 | 331 | # Make nice annotations for the viewer. |
paulb@173 | 332 | |
paulb@173 | 333 | augassign._op_call = result.code[1].expr |
paulb@173 | 334 | |
paulb@9 | 335 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 336 | |
paulb@6 | 337 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 338 | |
paulb@9 | 339 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 340 | |
paulb@56 | 341 | result.code = [ |
paulb@101 | 342 | StoreTemp( |
paulb@56 | 343 | index="expr", |
paulb@56 | 344 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 345 | ), |
paulb@101 | 346 | StoreTemp( |
paulb@56 | 347 | index="lower", |
paulb@56 | 348 | expr=self.dispatch_or_none(augassign.node.lower) |
paulb@56 | 349 | ), |
paulb@101 | 350 | StoreTemp( |
paulb@56 | 351 | index="upper", |
paulb@56 | 352 | expr=self.dispatch_or_none(augassign.node.upper) |
paulb@56 | 353 | ), |
paulb@101 | 354 | StoreTemp( |
paulb@173 | 355 | expr=InvokeFunction( # referenced below |
paulb@56 | 356 | args=[expr], star=None, dstar=None, |
paulb@101 | 357 | expr=LoadAttr( |
paulb@59 | 358 | expr=self._visitSlice( |
paulb@59 | 359 | augassign.node, |
paulb@101 | 360 | LoadTemp(index="expr"), |
paulb@101 | 361 | LoadTemp(index="lower"), |
paulb@101 | 362 | LoadTemp(index="upper"), |
paulb@59 | 363 | "OP_APPLY"), |
paulb@56 | 364 | name=self.augassign_methods[augassign.op] |
paulb@56 | 365 | ) |
paulb@56 | 366 | ) |
paulb@56 | 367 | ), |
paulb@59 | 368 | self._visitSlice( |
paulb@59 | 369 | augassign.node, |
paulb@101 | 370 | LoadTemp(index="expr"), |
paulb@101 | 371 | LoadTemp(index="lower"), |
paulb@101 | 372 | LoadTemp(index="upper"), |
paulb@59 | 373 | "OP_ASSIGN", |
paulb@101 | 374 | LoadTemp() |
paulb@59 | 375 | ), |
paulb@101 | 376 | ReleaseTemp(index="expr"), |
paulb@101 | 377 | ReleaseTemp(index="lower"), |
paulb@101 | 378 | ReleaseTemp(index="upper"), |
paulb@101 | 379 | ReleaseTemp() |
paulb@56 | 380 | ] |
paul@2 | 381 | |
paulb@173 | 382 | # Make nice annotations for the viewer. |
paulb@173 | 383 | |
paulb@173 | 384 | augassign._op_call = result.code[3].expr |
paulb@173 | 385 | |
paulb@10 | 386 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 387 | |
paulb@10 | 388 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 389 | |
paulb@10 | 390 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 391 | |
paulb@56 | 392 | result.code = [ |
paulb@101 | 393 | StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), |
paulb@101 | 394 | StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), |
paulb@101 | 395 | StoreTemp( |
paulb@173 | 396 | expr=InvokeFunction( # referenced below |
paulb@56 | 397 | args=[expr], star=None, dstar=None, |
paulb@101 | 398 | expr=LoadAttr( |
paulb@59 | 399 | expr=self._visitSubscript( |
paulb@59 | 400 | augassign.node, |
paulb@101 | 401 | LoadTemp(index="expr"), |
paulb@101 | 402 | LoadTemp(index="subs"), |
paulb@59 | 403 | "OP_APPLY" |
paulb@59 | 404 | ), |
paulb@56 | 405 | name=self.augassign_methods[augassign.op] |
paulb@56 | 406 | ) |
paulb@56 | 407 | ) |
paulb@56 | 408 | ), |
paulb@59 | 409 | self._visitSubscript( |
paulb@59 | 410 | augassign.node, |
paulb@101 | 411 | LoadTemp(index="expr"), |
paulb@101 | 412 | LoadTemp(index="subs"), |
paulb@59 | 413 | "OP_ASSIGN", |
paulb@101 | 414 | LoadTemp() |
paulb@59 | 415 | ), |
paulb@101 | 416 | ReleaseTemp(index="expr"), |
paulb@101 | 417 | ReleaseTemp(index="subs"), |
paulb@101 | 418 | ReleaseTemp() |
paulb@56 | 419 | ] |
paulb@10 | 420 | |
paulb@173 | 421 | # Make nice annotations for the viewer. |
paulb@173 | 422 | |
paulb@173 | 423 | augassign._op_call = result.code[2].expr |
paulb@173 | 424 | |
paul@2 | 425 | else: |
paul@2 | 426 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 427 | |
paul@2 | 428 | return result |
paul@2 | 429 | |
paulb@137 | 430 | def visitBreak(self, break_): |
paulb@137 | 431 | result = ReturnFromBlock(break_, 1) |
paul@0 | 432 | return result |
paul@0 | 433 | |
paulb@137 | 434 | def visitCallFunc(self, callfunc): |
paulb@137 | 435 | result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) |
paulb@137 | 436 | if callfunc.star_args is not None: |
paulb@137 | 437 | result.star = self.dispatch(callfunc.star_args) |
paulb@137 | 438 | if callfunc.dstar_args is not None: |
paulb@137 | 439 | result.dstar = self.dispatch(callfunc.dstar_args) |
paulb@137 | 440 | result.expr = self.dispatch(callfunc.node) |
paulb@6 | 441 | return result |
paulb@6 | 442 | |
paulb@19 | 443 | def visitClass(self, class_): |
paulb@129 | 444 | |
paulb@129 | 445 | # Add "object" if the class is not "object" and has an empty bases list. |
paulb@129 | 446 | |
paulb@129 | 447 | if class_.name != "object" and not class_.bases: |
paulb@129 | 448 | bases = [compiler.ast.Name("object")] |
paulb@129 | 449 | else: |
paulb@129 | 450 | bases = class_.bases |
paulb@129 | 451 | |
paulb@129 | 452 | structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) |
paulb@19 | 453 | self.structures.append(structure) |
paulb@172 | 454 | within_class = self.within_class |
paulb@172 | 455 | self.within_class = 1 |
paulb@19 | 456 | |
paulb@24 | 457 | # Make a subprogram which initialises the class structure. |
paulb@24 | 458 | |
paulb@118 | 459 | subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 460 | self.current_subprograms.append(subprogram) |
paulb@120 | 461 | self.current_structures.append(structure) # mostly for name construction |
paulb@19 | 462 | |
paulb@28 | 463 | # The class is initialised using the code found inside. |
paulb@28 | 464 | |
paulb@135 | 465 | subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] |
paulb@19 | 466 | |
paulb@172 | 467 | self.within_class = within_class |
paulb@120 | 468 | self.current_structures.pop() |
paulb@19 | 469 | self.current_subprograms.pop() |
paulb@79 | 470 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@19 | 471 | |
paulb@19 | 472 | # Make a definition of the class associating it with a name. |
paulb@19 | 473 | |
paulb@101 | 474 | result = Assign( |
paulb@56 | 475 | code=[ |
paulb@79 | 476 | StoreName(class_, 1, # defines the class |
paulb@56 | 477 | name=class_.name, |
paulb@101 | 478 | expr=LoadRef(ref=structure) |
paulb@56 | 479 | ), |
paulb@101 | 480 | InvokeBlock( |
paulb@132 | 481 | share_locals=0, # override the local sharing usually in InvokeBlock |
paulb@101 | 482 | expr=LoadRef(ref=subprogram) |
paulb@56 | 483 | ) |
paulb@56 | 484 | ] |
paulb@56 | 485 | ) |
paulb@19 | 486 | return result |
paulb@19 | 487 | |
paulb@137 | 488 | comparison_methods = { |
paulb@137 | 489 | "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", |
paulb@137 | 490 | ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None |
paulb@137 | 491 | } |
paulb@137 | 492 | |
paulb@137 | 493 | def visitCompare(self, compare): |
paulb@46 | 494 | |
paulb@46 | 495 | """ |
paulb@137 | 496 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@137 | 497 | the subprogram. Convert... |
paul@0 | 498 | |
paulb@137 | 499 | Compare (expr) |
paulb@137 | 500 | (name/node) |
paulb@137 | 501 | ... |
paulb@35 | 502 | |
paulb@35 | 503 | ...to: |
paulb@35 | 504 | |
paulb@137 | 505 | InvokeBlock -> Subprogram -> Conditional (test) -> (body) |
paulb@137 | 506 | (else) -> Conditional (test) -> (body) |
paulb@137 | 507 | (else) -> ... |
paulb@35 | 508 | """ |
paul@0 | 509 | |
paulb@137 | 510 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paul@0 | 511 | self.current_subprograms.append(subprogram) |
paul@0 | 512 | |
paulb@137 | 513 | # In the subprogram, make instructions which invoke a method on the |
paulb@137 | 514 | # first operand of each operand pair and, if appropriate, return with |
paulb@137 | 515 | # the value from that method. |
paulb@137 | 516 | |
paulb@137 | 517 | last = compare.ops[-1] |
paulb@137 | 518 | previous = self.dispatch(compare.expr) |
paulb@137 | 519 | results = nodes = [] |
paulb@137 | 520 | |
paulb@137 | 521 | # For viewing purposes, record invocations on the AST node. |
paulb@137 | 522 | |
paulb@137 | 523 | compare._ops = [] |
paulb@137 | 524 | |
paulb@137 | 525 | for op in compare.ops: |
paulb@137 | 526 | op_name, node = op |
paulb@137 | 527 | expr = self.dispatch(node) |
paul@0 | 528 | |
paulb@137 | 529 | # Identify the operation and produce the appropriate method call. |
paul@0 | 530 | |
paulb@137 | 531 | method_name = self.comparison_methods[op_name] |
paulb@137 | 532 | if method_name: |
paulb@137 | 533 | invocation = InvokeFunction( |
paulb@137 | 534 | expr=LoadAttr( |
paulb@137 | 535 | expr=previous, |
paulb@137 | 536 | name=method_name), |
paulb@137 | 537 | args=[expr], |
paulb@137 | 538 | star=None, |
paulb@137 | 539 | dstar=None) |
paulb@137 | 540 | |
paulb@137 | 541 | elif op_name == "is": |
paulb@137 | 542 | invocation = InvokeFunction( |
paulb@137 | 543 | expr=LoadName(name="__is__"), |
paulb@137 | 544 | args=[previous, expr], |
paulb@137 | 545 | star=None, |
paulb@137 | 546 | dstar=None) |
paul@0 | 547 | |
paulb@137 | 548 | elif op_name == "is not": |
paulb@137 | 549 | invocation = Not( |
paulb@137 | 550 | expr=InvokeFunction( |
paulb@137 | 551 | expr=LoadName(name="__is__"), |
paulb@137 | 552 | args=[previous, expr], |
paulb@137 | 553 | star=None, |
paulb@137 | 554 | dstar=None) |
paulb@137 | 555 | ) |
paulb@137 | 556 | else: |
paulb@137 | 557 | raise NotImplementedError, op_name |
paulb@34 | 558 | |
paulb@137 | 559 | nodes.append(StoreTemp(expr=invocation)) |
paulb@137 | 560 | compare._ops.append(invocation) |
paulb@137 | 561 | |
paulb@137 | 562 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 563 | |
paulb@137 | 564 | if op is not last: |
paulb@137 | 565 | nodes.append( |
paulb@137 | 566 | Conditional( |
paulb@137 | 567 | test=self._visitNot(LoadTemp()), |
paulb@137 | 568 | body=[ |
paulb@137 | 569 | ReturnFromBlock(expr=LoadTemp()) |
paulb@137 | 570 | ], |
paulb@137 | 571 | else_=[ |
paulb@137 | 572 | ReleaseTemp() |
paulb@137 | 573 | # Subsequent operations go here! |
paulb@137 | 574 | ] |
paulb@137 | 575 | ) |
paulb@137 | 576 | ) |
paulb@35 | 577 | |
paulb@137 | 578 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 579 | |
paulb@137 | 580 | nodes = nodes[-1].else_ |
paulb@137 | 581 | |
paulb@137 | 582 | # For the last operation, return the result. |
paulb@35 | 583 | |
paulb@137 | 584 | else: |
paulb@137 | 585 | nodes.append( |
paulb@137 | 586 | ReturnFromBlock(expr=LoadTemp(release=1)) |
paulb@137 | 587 | ) |
paulb@137 | 588 | |
paulb@137 | 589 | previous = expr |
paulb@35 | 590 | |
paulb@35 | 591 | # Finish the subprogram definition. |
paulb@35 | 592 | |
paulb@137 | 593 | subprogram.code = results |
paul@0 | 594 | |
paul@0 | 595 | self.current_subprograms.pop() |
paulb@79 | 596 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 597 | |
paul@0 | 598 | # Make an invocation of the subprogram. |
paul@0 | 599 | |
paulb@137 | 600 | result = InvokeBlock(compare, 1, produces_result=1) |
paulb@101 | 601 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 602 | return result |
paul@0 | 603 | |
paulb@137 | 604 | def visitConst(self, const): |
paulb@154 | 605 | key = "%s-%s" % (const.value.__class__.__name__, const.value) |
paulb@154 | 606 | if not self.constants.has_key(key): |
paulb@154 | 607 | self.constants[key] = Constant(name=repr(const.value), value=const.value) |
paulb@154 | 608 | result = LoadRef(const, 1, ref=self.constants[key]) |
paulb@137 | 609 | return result |
paulb@137 | 610 | |
paulb@137 | 611 | def visitContinue(self, continue_): |
paulb@137 | 612 | result = InvokeBlock(continue_, 1, |
paulb@137 | 613 | expr=LoadRef(ref=self.current_subprograms[-1]) |
paulb@137 | 614 | ) |
paulb@137 | 615 | return result |
paulb@137 | 616 | |
paulb@137 | 617 | def visitDict(self, dict): |
paulb@137 | 618 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) |
paulb@137 | 619 | args = [] |
paulb@137 | 620 | for key, value in dict.items: |
paulb@137 | 621 | tuple = InvokeFunction(expr=LoadName(name="tuple"), star=None, dstar=None) |
paulb@137 | 622 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paulb@137 | 623 | args.append(tuple) |
paulb@137 | 624 | result.set_args(args) |
paulb@137 | 625 | return result |
paulb@137 | 626 | |
paulb@137 | 627 | def visitDiscard(self, discard): |
paulb@137 | 628 | return self.dispatch(discard.expr) |
paulb@137 | 629 | |
paulb@137 | 630 | def visitDiv(self, div): |
paulb@137 | 631 | return self._visitBinary(div, "__div__", "__rdiv__") |
paulb@137 | 632 | |
paulb@137 | 633 | def visitFloorDiv(self, floordiv): |
paulb@137 | 634 | return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") |
paulb@137 | 635 | |
paul@0 | 636 | def visitFor(self, for_): |
paul@0 | 637 | |
paulb@35 | 638 | """ |
paulb@35 | 639 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 640 | subprogram. Convert... |
paulb@35 | 641 | |
paulb@35 | 642 | For (assign) |
paulb@35 | 643 | (body) |
paulb@35 | 644 | (else) |
paulb@35 | 645 | |
paulb@35 | 646 | ...to: |
paulb@35 | 647 | |
paulb@35 | 648 | Assign (assign #1) |
paulb@35 | 649 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 650 | (body) |
paulb@35 | 651 | Invoke subprogram |
paulb@35 | 652 | (handler) -> ... |
paulb@35 | 653 | (else) -> ... |
paulb@35 | 654 | """ |
paul@0 | 655 | |
paulb@118 | 656 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 657 | self.current_subprograms.append(subprogram) |
paul@0 | 658 | |
paulb@34 | 659 | # Always return from conditional sections/subprograms. |
paulb@34 | 660 | |
paulb@17 | 661 | if for_.else_ is not None: |
paulb@135 | 662 | else_stmt = self.dispatch(for_.else_) + [ReturnFromBlock()] |
paulb@17 | 663 | else: |
paulb@135 | 664 | else_stmt = [ReturnFromBlock()] |
paulb@17 | 665 | |
paul@0 | 666 | # Wrap the assignment in a try...except statement. |
paulb@135 | 667 | # Inside the body, add a recursive invocation to the subprogram. |
paul@0 | 668 | |
paulb@135 | 669 | subprogram.code = [ |
paulb@135 | 670 | Try( |
paulb@135 | 671 | body=[ |
paulb@135 | 672 | Assign( |
paulb@135 | 673 | code=[ |
paulb@148 | 674 | StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paulb@135 | 675 | self.dispatch(for_.assign), |
paulb@135 | 676 | ReleaseTemp() |
paulb@135 | 677 | ]) |
paulb@135 | 678 | ] + self.dispatch(for_.body) + [ |
paulb@135 | 679 | InvokeBlock( |
paulb@135 | 680 | expr=LoadRef(ref=subprogram) |
paulb@135 | 681 | ) |
paulb@135 | 682 | ], |
paulb@135 | 683 | handler=[ |
paulb@135 | 684 | Conditional( |
paulb@135 | 685 | test=InvokeFunction( |
paulb@135 | 686 | expr=LoadName(name="isinstance"), |
paulb@135 | 687 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@135 | 688 | star=None, |
paulb@135 | 689 | dstar=None), |
paulb@135 | 690 | body=else_stmt, |
paulb@135 | 691 | else_=[Raise(expr=LoadExc())] |
paulb@135 | 692 | ) |
paulb@135 | 693 | ], |
paulb@135 | 694 | else_=[], |
paulb@135 | 695 | finally_=[] |
paulb@135 | 696 | ), |
paulb@135 | 697 | ReturnFromBlock() |
paulb@135 | 698 | ] |
paul@0 | 699 | |
paulb@35 | 700 | # Finish the subprogram definition. |
paulb@35 | 701 | |
paul@0 | 702 | self.current_subprograms.pop() |
paulb@79 | 703 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 704 | |
paul@0 | 705 | # Obtain an iterator for the sequence involved. |
paul@0 | 706 | # Then, make an invocation of the subprogram. |
paul@0 | 707 | |
paulb@148 | 708 | result = Assign(for_, 1, |
paulb@148 | 709 | code=[ |
paulb@148 | 710 | StoreTemp( |
paulb@148 | 711 | expr=InvokeFunction( |
paulb@148 | 712 | expr=LoadAttr( |
paulb@148 | 713 | name="__iter__", |
paulb@148 | 714 | expr=self.dispatch(for_.list) |
paulb@148 | 715 | ) |
paulb@148 | 716 | ) |
paulb@148 | 717 | ), |
paulb@148 | 718 | InvokeBlock(expr=LoadRef(ref=subprogram)), |
paulb@148 | 719 | ReleaseTemp() |
paulb@148 | 720 | ] |
paulb@148 | 721 | ) |
paulb@148 | 722 | |
paulb@148 | 723 | # Make nice annotations for the viewer. |
paulb@148 | 724 | |
paulb@148 | 725 | for_._iter_call = result.code[0].expr |
paulb@148 | 726 | for_._next_call = subprogram.code[0].body[0].code[0].expr |
paulb@148 | 727 | |
paul@0 | 728 | return result |
paul@0 | 729 | |
paulb@137 | 730 | def visitFrom(self, from_): |
paulb@137 | 731 | result = Assign(from_, 1) |
paulb@137 | 732 | code = [] |
paulb@157 | 733 | _names = [] |
paulb@157 | 734 | code.append( |
paulb@157 | 735 | StoreTemp( |
paulb@158 | 736 | expr=Import(name=from_.modname, alias=1) |
paulb@157 | 737 | ) |
paulb@157 | 738 | ) |
paulb@157 | 739 | from_._modname = code[-1].expr |
paulb@137 | 740 | for name, alias in from_.names: |
paulb@137 | 741 | code.append( |
paulb@137 | 742 | StoreName( |
paulb@137 | 743 | expr=LoadAttr( |
paulb@137 | 744 | expr=LoadTemp(), |
paulb@137 | 745 | name=name), |
paulb@137 | 746 | name=(alias or name) |
paulb@137 | 747 | ) |
paulb@137 | 748 | ) |
paulb@157 | 749 | _names.append(code[-1].expr) |
paulb@137 | 750 | code.append(ReleaseTemp()) |
paulb@137 | 751 | result.code = code |
paulb@157 | 752 | from_._names = _names |
paulb@137 | 753 | return result |
paulb@137 | 754 | |
paulb@137 | 755 | def _visitFunction(self, function, subprogram): |
paulb@137 | 756 | |
paulb@137 | 757 | """ |
paulb@137 | 758 | A common function generator which transforms the given 'function' node |
paulb@137 | 759 | and initialises the given 'subprogram' appropriately. |
paulb@137 | 760 | """ |
paulb@137 | 761 | |
paulb@137 | 762 | # Discover star and dstar parameters. |
paulb@137 | 763 | |
paulb@137 | 764 | if function.flags & 4 != 0: |
paulb@137 | 765 | has_star = 1 |
paulb@137 | 766 | else: |
paulb@137 | 767 | has_star = 0 |
paulb@137 | 768 | if function.flags & 8 != 0: |
paulb@137 | 769 | has_dstar = 1 |
paulb@137 | 770 | else: |
paulb@137 | 771 | has_dstar = 0 |
paulb@137 | 772 | |
paulb@137 | 773 | # Discover the number of defaults and positional parameters. |
paulb@137 | 774 | |
paulb@137 | 775 | ndefaults = len(function.defaults) |
paulb@137 | 776 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@137 | 777 | |
paulb@137 | 778 | # Produce star and dstar parameters with appropriate defaults. |
paulb@137 | 779 | |
paulb@137 | 780 | if has_star: |
paulb@137 | 781 | star = ( |
paulb@137 | 782 | function.argnames[npositional], |
paulb@148 | 783 | InvokeFunction(expr=LoadName(name="list")) |
paulb@137 | 784 | ) |
paulb@137 | 785 | else: |
paulb@137 | 786 | star = None |
paulb@137 | 787 | if has_dstar: |
paulb@137 | 788 | dstar = ( |
paulb@137 | 789 | function.argnames[npositional + has_star], |
paulb@148 | 790 | InvokeFunction(expr=LoadName(name="dict")) |
paulb@137 | 791 | ) |
paulb@137 | 792 | else: |
paulb@137 | 793 | dstar = None |
paulb@137 | 794 | |
paulb@137 | 795 | params = [] |
paulb@137 | 796 | for i in range(0, npositional - ndefaults): |
paulb@137 | 797 | params.append((function.argnames[i], None)) |
paulb@137 | 798 | |
paulb@137 | 799 | # Process defaults. |
paulb@137 | 800 | |
paulb@137 | 801 | for i in range(0, ndefaults): |
paulb@137 | 802 | default = function.defaults[i] |
paulb@137 | 803 | if default is not None: |
paulb@137 | 804 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paulb@137 | 805 | else: |
paulb@137 | 806 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paulb@137 | 807 | |
paulb@137 | 808 | subprogram.params = params |
paulb@137 | 809 | subprogram.star = star |
paulb@137 | 810 | subprogram.dstar = dstar |
paulb@137 | 811 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 812 | |
paulb@137 | 813 | def visitFunction(self, function): |
paulb@137 | 814 | |
paulb@137 | 815 | """ |
paulb@137 | 816 | Make a subprogram for the 'function' and record it outside the main |
paulb@137 | 817 | tree. Produce something like the following: |
paulb@137 | 818 | |
paulb@137 | 819 | StoreName (name) |
paulb@147 | 820 | (expr) -> LoadRef (ref) -> Subprogram (params) |
paulb@147 | 821 | (star) |
paulb@147 | 822 | (dstar) |
paulb@137 | 823 | """ |
paulb@137 | 824 | |
paulb@137 | 825 | subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@172 | 826 | internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class) |
paulb@137 | 827 | |
paulb@137 | 828 | self.current_subprograms.append(subprogram) |
paulb@172 | 829 | within_class = self.within_class |
paulb@172 | 830 | self.within_class = 0 |
paulb@172 | 831 | |
paulb@137 | 832 | subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] |
paulb@172 | 833 | |
paulb@172 | 834 | self.within_class = within_class |
paulb@137 | 835 | self.current_subprograms.pop() |
paulb@137 | 836 | self._visitFunction(function, subprogram) |
paulb@137 | 837 | |
paulb@137 | 838 | # Make a definition of the function associating it with a name. |
paulb@137 | 839 | |
paulb@137 | 840 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paulb@137 | 841 | return result |
paulb@137 | 842 | |
paulb@137 | 843 | def visitGetattr(self, getattr): |
paulb@137 | 844 | result = LoadAttr(getattr, 1, |
paulb@137 | 845 | name=getattr.attrname, |
paulb@137 | 846 | expr=self.dispatch(getattr.expr) |
paulb@137 | 847 | ) |
paulb@137 | 848 | return result |
paulb@137 | 849 | |
paulb@137 | 850 | def visitGlobal(self, global_): |
paulb@137 | 851 | result = Global(global_, 1, |
paulb@137 | 852 | names=global_.names |
paulb@137 | 853 | ) |
paulb@137 | 854 | return result |
paulb@137 | 855 | |
paulb@137 | 856 | def visitIf(self, if_): |
paulb@137 | 857 | |
paulb@137 | 858 | """ |
paulb@137 | 859 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@137 | 860 | and putting each subsequent test as part of the conditional's else |
paulb@137 | 861 | section. |
paulb@137 | 862 | |
paulb@137 | 863 | Convert... |
paulb@137 | 864 | |
paulb@137 | 865 | If (test/body) |
paulb@137 | 866 | (test/body) |
paulb@137 | 867 | ... |
paulb@137 | 868 | (else/body) |
paulb@137 | 869 | |
paulb@137 | 870 | ...to: |
paulb@137 | 871 | |
paulb@137 | 872 | Conditional (test) -> (body) |
paulb@137 | 873 | (else) -> Conditional (test) -> (body) |
paulb@137 | 874 | (else) -> ... |
paulb@137 | 875 | """ |
paulb@137 | 876 | |
paulb@137 | 877 | |
paulb@137 | 878 | results = nodes = [] |
paulb@137 | 879 | |
paulb@137 | 880 | # Produce something like... |
paulb@137 | 881 | # expr.__bool__() ? body |
paulb@137 | 882 | |
paulb@137 | 883 | first = 1 |
paulb@137 | 884 | for compare, stmt in if_.tests: |
paulb@137 | 885 | |
paulb@137 | 886 | # Set the first as the defining node. |
paulb@137 | 887 | |
paulb@137 | 888 | test = Conditional(if_, first, |
paulb@137 | 889 | test=InvokeFunction( |
paulb@137 | 890 | expr=LoadAttr( |
paulb@137 | 891 | expr=self.dispatch(compare), |
paulb@137 | 892 | name="__bool__" |
paulb@137 | 893 | ), |
paulb@148 | 894 | ) |
paulb@137 | 895 | ) |
paulb@137 | 896 | test.body = self.dispatch(stmt) |
paulb@137 | 897 | nodes.append(test) |
paulb@137 | 898 | nodes = test.else_ = [] |
paulb@137 | 899 | first = 0 |
paulb@137 | 900 | |
paulb@137 | 901 | # Add the compound statement from any else clause to the end. |
paulb@137 | 902 | |
paulb@137 | 903 | if if_.else_ is not None: |
paulb@137 | 904 | nodes += self.dispatch(if_.else_) |
paulb@137 | 905 | |
paulb@137 | 906 | result = results[0] |
paulb@137 | 907 | return result |
paulb@137 | 908 | |
paulb@137 | 909 | def visitImport(self, import_): |
paulb@137 | 910 | result = Assign(import_, 1) |
paulb@137 | 911 | code = [] |
paulb@157 | 912 | _names = [] |
paulb@137 | 913 | for path, alias in import_.names: |
paulb@158 | 914 | importer = Import(name=path, alias=alias) |
paulb@137 | 915 | top = alias or path.split(".")[0] |
paulb@137 | 916 | code.append(StoreName(expr=importer, name=top)) |
paulb@157 | 917 | _names.append(code[-1].expr) |
paulb@137 | 918 | result.code = code |
paulb@157 | 919 | import_._names = _names |
paulb@137 | 920 | return result |
paulb@137 | 921 | |
paulb@137 | 922 | def visitInvert(self, invert): |
paulb@137 | 923 | return self._visitUnary(invert, "__invert__") |
paulb@137 | 924 | |
paulb@137 | 925 | def visitKeyword(self, keyword): |
paulb@137 | 926 | result = Keyword(keyword, 1, |
paulb@137 | 927 | name=keyword.name, |
paulb@137 | 928 | expr=self.dispatch(keyword.expr) |
paulb@137 | 929 | ) |
paulb@137 | 930 | return result |
paulb@137 | 931 | |
paulb@137 | 932 | def visitLambda(self, lambda_): |
paulb@137 | 933 | |
paulb@137 | 934 | # Make a subprogram for the function and record it outside the main |
paulb@137 | 935 | # tree. |
paulb@137 | 936 | |
paulb@137 | 937 | subprogram = Subprogram(name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None) |
paulb@137 | 938 | self.current_subprograms.append(subprogram) |
paulb@137 | 939 | subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] |
paulb@137 | 940 | self.current_subprograms.pop() |
paulb@137 | 941 | self._visitFunction(lambda_, subprogram) |
paulb@137 | 942 | |
paulb@137 | 943 | # Get the subprogram reference to the lambda. |
paulb@137 | 944 | |
paulb@137 | 945 | return LoadRef(lambda_, 1, ref=subprogram) |
paulb@137 | 946 | |
paulb@137 | 947 | def visitList(self, list): |
paulb@137 | 948 | return self._visitBuiltin(list, "list") |
paulb@137 | 949 | |
paulb@157 | 950 | def visitMod(self, mod): |
paulb@157 | 951 | return self._visitBinary(mod, "__mod__", "__rmod__") |
paulb@157 | 952 | |
paulb@137 | 953 | def visitMul(self, mul): |
paulb@137 | 954 | return self._visitBinary(mul, "__mul__", "__rmul__") |
paulb@137 | 955 | |
paulb@137 | 956 | def visitName(self, name): |
paulb@137 | 957 | result = LoadName(name, 1, name=name.name) |
paulb@137 | 958 | return result |
paulb@137 | 959 | |
paulb@137 | 960 | def _visitNot(self, expr, not_=None): |
paulb@137 | 961 | invocation = InvokeFunction( |
paulb@137 | 962 | expr=LoadAttr( |
paulb@137 | 963 | expr=expr, |
paulb@137 | 964 | name="__bool__" |
paulb@137 | 965 | ), |
paulb@137 | 966 | ) |
paulb@137 | 967 | if not_ is not None: |
paulb@137 | 968 | result = Not(not_, 1, expr=invocation) |
paulb@137 | 969 | else: |
paulb@137 | 970 | result = Not(expr=invocation) |
paulb@137 | 971 | return result |
paulb@137 | 972 | |
paulb@137 | 973 | def visitNot(self, not_): |
paulb@137 | 974 | return self._visitNot(self.dispatch(not_.expr), not_) |
paulb@137 | 975 | |
paulb@137 | 976 | def visitOr(self, or_): |
paulb@137 | 977 | |
paulb@137 | 978 | """ |
paulb@137 | 979 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@137 | 980 | subprogram. Convert... |
paulb@137 | 981 | |
paulb@137 | 982 | Or (test) |
paulb@137 | 983 | (test) |
paulb@137 | 984 | ... |
paulb@137 | 985 | |
paulb@137 | 986 | ...to: |
paulb@137 | 987 | |
paulb@137 | 988 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 989 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 990 | (else) -> ... |
paulb@137 | 991 | """ |
paulb@137 | 992 | |
paulb@137 | 993 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 994 | self.current_subprograms.append(subprogram) |
paulb@137 | 995 | |
paulb@137 | 996 | # In the subprogram, make instructions which store each operand, test |
paulb@137 | 997 | # for each operand's truth status, and if appropriate return from the |
paulb@137 | 998 | # subprogram with the value of the operand. |
paulb@137 | 999 | |
paulb@137 | 1000 | last = or_.nodes[-1] |
paulb@137 | 1001 | results = nodes = [] |
paulb@137 | 1002 | |
paulb@137 | 1003 | for node in or_.nodes: |
paulb@137 | 1004 | expr = self.dispatch(node) |
paulb@137 | 1005 | |
paulb@137 | 1006 | # Return from the subprogram where the test is satisfied. |
paulb@137 | 1007 | |
paulb@137 | 1008 | if node is not last: |
paulb@137 | 1009 | nodes.append(StoreTemp(expr=expr)) |
paulb@148 | 1010 | invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__")) |
paulb@137 | 1011 | test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) |
paulb@137 | 1012 | nodes.append(test) |
paulb@137 | 1013 | |
paulb@137 | 1014 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 1015 | |
paulb@137 | 1016 | nodes = test.else_ = [ReleaseTemp()] |
paulb@137 | 1017 | |
paulb@137 | 1018 | # For the last operation, return the result. |
paulb@137 | 1019 | |
paulb@137 | 1020 | else: |
paulb@137 | 1021 | nodes.append( |
paulb@137 | 1022 | ReturnFromBlock(expr=expr) |
paulb@137 | 1023 | ) |
paulb@137 | 1024 | |
paulb@137 | 1025 | # Finish the subprogram definition. |
paulb@137 | 1026 | |
paulb@137 | 1027 | subprogram.code = results |
paulb@137 | 1028 | |
paulb@137 | 1029 | self.current_subprograms.pop() |
paulb@137 | 1030 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1031 | |
paulb@137 | 1032 | # Make an invocation of the subprogram. |
paulb@137 | 1033 | |
paulb@137 | 1034 | result = InvokeBlock(or_, 1, produces_result=1) |
paulb@137 | 1035 | result.expr = LoadRef(ref=subprogram) |
paulb@137 | 1036 | return result |
paulb@137 | 1037 | |
paulb@137 | 1038 | def visitPass(self, pass_): |
paulb@137 | 1039 | return Pass(pass_, 1) |
paulb@137 | 1040 | |
paulb@141 | 1041 | def visitPower(self, power): |
paulb@141 | 1042 | return self._visitBinary(power, "__pow__", "__rpow__") |
paulb@141 | 1043 | |
paulb@147 | 1044 | def visitPrint(self, print_): |
paulb@147 | 1045 | |
paulb@147 | 1046 | """ |
paulb@147 | 1047 | Convert... |
paulb@147 | 1048 | |
paulb@147 | 1049 | Print (dest) -> |
paulb@147 | 1050 | (nodes) |
paulb@147 | 1051 | |
paulb@147 | 1052 | ...to: |
paulb@147 | 1053 | |
paulb@147 | 1054 | StoreTemp (index) -> "print" |
paulb@147 | 1055 | (expr) -> LoadAttr (expr) -> (dest) |
paulb@147 | 1056 | (name) -> "write" |
paulb@147 | 1057 | InvokeFunction (expr) -> LoadTemp (index) -> "print" |
paulb@147 | 1058 | (args) -> [(node)] |
paulb@147 | 1059 | ReleaseTemp (index) -> "print" |
paulb@147 | 1060 | """ |
paulb@147 | 1061 | |
paulb@147 | 1062 | if print_.dest is not None: |
paulb@147 | 1063 | dest = self.dispatch(print_.dest) |
paulb@147 | 1064 | else: |
paulb@147 | 1065 | dest = self.dispatch(compiler.ast.Name("stdout")) |
paulb@147 | 1066 | |
paulb@147 | 1067 | result = Assign(print_, 1, |
paulb@147 | 1068 | code=[ |
paulb@147 | 1069 | StoreTemp( |
paulb@147 | 1070 | index="print", |
paulb@147 | 1071 | expr=LoadAttr( |
paulb@147 | 1072 | expr=dest, |
paulb@147 | 1073 | name="write" |
paulb@147 | 1074 | ) |
paulb@147 | 1075 | ) |
paulb@147 | 1076 | ] |
paulb@147 | 1077 | ) |
paulb@147 | 1078 | |
paulb@147 | 1079 | for node in print_.nodes: |
paulb@147 | 1080 | result.code.append( |
paulb@147 | 1081 | InvokeFunction( |
paulb@147 | 1082 | expr=LoadTemp(index="print"), |
paulb@147 | 1083 | args=[self.dispatch(node)], |
paulb@147 | 1084 | star=None, |
paulb@147 | 1085 | dstar=None |
paulb@147 | 1086 | ) |
paulb@147 | 1087 | ) |
paulb@147 | 1088 | |
paulb@147 | 1089 | result.code.append( |
paulb@147 | 1090 | ReleaseTemp(index="print") |
paulb@147 | 1091 | ) |
paulb@147 | 1092 | |
paulb@147 | 1093 | return result |
paulb@147 | 1094 | |
paulb@147 | 1095 | def visitPrintnl(self, printnl): |
paulb@147 | 1096 | result = self.visitPrint(printnl) |
paulb@147 | 1097 | result.code.insert( |
paulb@147 | 1098 | len(result.code) - 1, |
paulb@147 | 1099 | InvokeFunction( |
paulb@147 | 1100 | expr=LoadTemp(index="print"), |
paulb@147 | 1101 | args=[self.dispatch(compiler.ast.Const("\n"))], |
paulb@147 | 1102 | star=None, |
paulb@147 | 1103 | dstar=None |
paulb@147 | 1104 | ) |
paulb@147 | 1105 | ) |
paulb@147 | 1106 | return result |
paulb@147 | 1107 | |
paulb@137 | 1108 | def visitRaise(self, raise_): |
paulb@137 | 1109 | result = Raise(raise_, 1) |
paulb@137 | 1110 | if raise_.expr2 is None: |
paulb@137 | 1111 | result.expr = self.dispatch(raise_.expr1) |
paulb@137 | 1112 | else: |
paulb@137 | 1113 | result.expr = InvokeFunction( |
paulb@137 | 1114 | expr=self.dispatch(raise_.expr1), |
paulb@137 | 1115 | args=[self.dispatch(raise_.expr2)], |
paulb@137 | 1116 | star=None, |
paulb@137 | 1117 | dstar=None |
paulb@137 | 1118 | ) |
paulb@137 | 1119 | if raise_.expr3 is not None: |
paulb@137 | 1120 | result.traceback = self.dispatch(raise_.expr3) |
paulb@137 | 1121 | else: |
paulb@137 | 1122 | result.traceback = None |
paulb@137 | 1123 | return result |
paulb@137 | 1124 | |
paulb@137 | 1125 | def visitReturn(self, return_): |
paulb@137 | 1126 | result = ReturnFromFunction(return_, 1, |
paulb@137 | 1127 | expr=self.dispatch(return_.value) |
paulb@137 | 1128 | ) |
paulb@137 | 1129 | return result |
paulb@137 | 1130 | |
paulb@137 | 1131 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@137 | 1132 | if flags == "OP_ASSIGN": |
paulb@137 | 1133 | result = InvokeFunction(slice, 1, |
paulb@137 | 1134 | expr=LoadAttr( |
paulb@137 | 1135 | expr=expr, |
paulb@137 | 1136 | name="__setslice__" |
paulb@137 | 1137 | ), |
paulb@137 | 1138 | star=None, |
paulb@137 | 1139 | dstar=None, |
paulb@137 | 1140 | args=[lower, upper, value] |
paulb@137 | 1141 | ) |
paulb@137 | 1142 | elif flags == "OP_APPLY": |
paulb@137 | 1143 | args = [] |
paulb@137 | 1144 | result = InvokeFunction(slice, 1, |
paulb@137 | 1145 | expr=LoadAttr( |
paulb@137 | 1146 | expr=expr, |
paulb@137 | 1147 | name="__getslice__" |
paulb@137 | 1148 | ), |
paulb@137 | 1149 | star=None, |
paulb@137 | 1150 | dstar=None, |
paulb@137 | 1151 | args=[lower, upper] |
paulb@137 | 1152 | ) |
paulb@137 | 1153 | elif flags == "OP_DELETE": |
paulb@137 | 1154 | args = [] |
paulb@137 | 1155 | result = InvokeFunction(slice, 1, |
paulb@137 | 1156 | expr=LoadAttr( |
paulb@137 | 1157 | expr=expr, |
paulb@137 | 1158 | name="__delslice__" |
paulb@137 | 1159 | ), |
paulb@137 | 1160 | star=None, |
paulb@137 | 1161 | dstar=None, |
paulb@137 | 1162 | args=[lower, upper] |
paulb@137 | 1163 | ) |
paulb@137 | 1164 | else: |
paulb@137 | 1165 | raise NotImplementedError, flags |
paulb@137 | 1166 | |
paulb@137 | 1167 | return result |
paulb@137 | 1168 | |
paulb@137 | 1169 | def visitSlice(self, slice, in_sequence=0): |
paulb@137 | 1170 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@137 | 1171 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) |
paulb@137 | 1172 | |
paulb@137 | 1173 | def visitStmt(self, stmt): |
paulb@137 | 1174 | return self.dispatches(stmt.nodes) |
paulb@137 | 1175 | |
paulb@137 | 1176 | def visitSub(self, sub): |
paulb@137 | 1177 | return self._visitBinary(sub, "__sub__", "__rsub__") |
paulb@137 | 1178 | |
paulb@137 | 1179 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@137 | 1180 | if flags == "OP_ASSIGN": |
paulb@137 | 1181 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1182 | expr=LoadAttr( |
paulb@137 | 1183 | expr=expr, |
paulb@137 | 1184 | name="__setitem__" |
paulb@137 | 1185 | ), |
paulb@137 | 1186 | star=None, |
paulb@137 | 1187 | dstar=None, |
paulb@137 | 1188 | args=[subs, value] |
paulb@137 | 1189 | ) |
paulb@137 | 1190 | elif flags == "OP_APPLY": |
paulb@137 | 1191 | args = [] |
paulb@137 | 1192 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1193 | expr=LoadAttr( |
paulb@137 | 1194 | expr=expr, |
paulb@137 | 1195 | name="__getitem__" |
paulb@137 | 1196 | ), |
paulb@137 | 1197 | star=None, |
paulb@137 | 1198 | dstar=None, |
paulb@137 | 1199 | args=[subs] |
paulb@137 | 1200 | ) |
paulb@137 | 1201 | elif flags == "OP_DELETE": |
paulb@137 | 1202 | args = [] |
paulb@137 | 1203 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1204 | expr=LoadAttr( |
paulb@137 | 1205 | expr=expr, |
paulb@137 | 1206 | name="__delitem__" |
paulb@137 | 1207 | ), |
paulb@137 | 1208 | star=None, |
paulb@137 | 1209 | dstar=None, |
paulb@137 | 1210 | args=[subs] |
paulb@137 | 1211 | ) |
paulb@137 | 1212 | else: |
paulb@137 | 1213 | raise NotImplementedError, flags |
paulb@137 | 1214 | |
paulb@137 | 1215 | return result |
paulb@137 | 1216 | |
paulb@137 | 1217 | def _visitSubscriptSubs(self, node, subs): |
paulb@137 | 1218 | if len(subs) == 1: |
paulb@137 | 1219 | return self.dispatch(subs[0]) |
paulb@137 | 1220 | else: |
paulb@137 | 1221 | return InvokeFunction(node, 1, |
paulb@137 | 1222 | expr=LoadName(name="tuple"), |
paulb@137 | 1223 | args=self.dispatches(subs), |
paulb@137 | 1224 | star=None, |
paulb@137 | 1225 | dstar=None |
paulb@137 | 1226 | ) |
paulb@137 | 1227 | |
paulb@137 | 1228 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@137 | 1229 | return self._visitSubscript( |
paulb@137 | 1230 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@137 | 1231 | self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@137 | 1232 | ) |
paulb@137 | 1233 | |
paulb@137 | 1234 | def visitTryExcept(self, tryexcept): |
paulb@137 | 1235 | |
paulb@137 | 1236 | """ |
paulb@137 | 1237 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@137 | 1238 | |
paulb@137 | 1239 | Convert... |
paulb@137 | 1240 | |
paulb@137 | 1241 | TryExcept (body) |
paulb@137 | 1242 | (else) |
paulb@137 | 1243 | (spec/assign/stmt) |
paulb@137 | 1244 | ... |
paulb@137 | 1245 | |
paulb@137 | 1246 | ...to: |
paulb@137 | 1247 | |
paulb@137 | 1248 | Try (body) |
paulb@137 | 1249 | (else) |
paulb@137 | 1250 | (handler) -> Conditional (test) -> (stmt) |
paulb@137 | 1251 | (body) -> ... |
paulb@137 | 1252 | (else) -> Conditional (test) -> (stmt) |
paulb@137 | 1253 | (body) -> ... |
paulb@137 | 1254 | (else) -> ... |
paulb@137 | 1255 | """ |
paulb@137 | 1256 | |
paulb@137 | 1257 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@137 | 1258 | |
paulb@137 | 1259 | if tryexcept.body is not None: |
paulb@137 | 1260 | result.body = self.dispatch(tryexcept.body) |
paulb@137 | 1261 | if tryexcept.else_ is not None: |
paulb@137 | 1262 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@137 | 1263 | |
paulb@137 | 1264 | results = nodes = [] |
paulb@137 | 1265 | catch_all = 0 |
paulb@137 | 1266 | |
paulb@137 | 1267 | for spec, assign, stmt in tryexcept.handlers: |
paulb@137 | 1268 | |
paulb@137 | 1269 | # If no specification exists, produce an unconditional block. |
paulb@137 | 1270 | |
paulb@137 | 1271 | if spec is None: |
paulb@137 | 1272 | nodes += self.dispatch(stmt) |
paulb@137 | 1273 | catch_all = 1 |
paulb@137 | 1274 | |
paulb@137 | 1275 | # Produce an exception value check. |
paulb@137 | 1276 | |
paulb@137 | 1277 | else: |
paulb@137 | 1278 | test = Conditional( |
paulb@137 | 1279 | isolate_test=1, |
paulb@137 | 1280 | test=CheckExc(expr=LoadExc(), choices=self._visitTryExcept(spec)) |
paulb@137 | 1281 | ) |
paulb@137 | 1282 | test.body = [] |
paulb@137 | 1283 | |
paulb@137 | 1284 | if assign is not None: |
paulb@137 | 1285 | test.body.append( |
paulb@137 | 1286 | Assign( |
paulb@137 | 1287 | code=[ |
paulb@137 | 1288 | StoreTemp(expr=LoadExc()), |
paulb@137 | 1289 | self.dispatch(assign), |
paulb@137 | 1290 | ReleaseTemp() |
paulb@137 | 1291 | ] |
paulb@137 | 1292 | ) |
paulb@137 | 1293 | ) |
paulb@137 | 1294 | |
paulb@166 | 1295 | test.body += self.dispatch(stmt) |
paulb@137 | 1296 | nodes.append(test) |
paulb@137 | 1297 | nodes = test.else_ = [] |
paulb@137 | 1298 | |
paulb@137 | 1299 | # Add a raise operation to deal with unhandled exceptions. |
paulb@137 | 1300 | |
paulb@137 | 1301 | if not catch_all: |
paulb@137 | 1302 | nodes.append( |
paulb@137 | 1303 | Raise( |
paulb@137 | 1304 | expr=LoadExc()) |
paulb@137 | 1305 | ) |
paulb@137 | 1306 | |
paulb@137 | 1307 | result.handler = results |
paulb@137 | 1308 | return result |
paulb@137 | 1309 | |
paulb@137 | 1310 | def _visitTryExcept(self, spec): |
paulb@137 | 1311 | |
paulb@137 | 1312 | "Return a list of nodes for the given exception type 'spec'." |
paulb@137 | 1313 | |
paulb@137 | 1314 | if isinstance(spec, compiler.ast.Tuple): |
paulb@137 | 1315 | nodes = [] |
paulb@137 | 1316 | for node in spec.nodes: |
paulb@137 | 1317 | nodes += self._visitTryExcept(node) |
paulb@137 | 1318 | else: |
paulb@137 | 1319 | nodes = [self.dispatch(spec)] |
paulb@137 | 1320 | return nodes |
paul@0 | 1321 | |
paul@0 | 1322 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1323 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1324 | if tryfinally.body is not None: |
paul@0 | 1325 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1326 | if tryfinally.final is not None: |
paul@0 | 1327 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1328 | return result |
paul@0 | 1329 | |
paulb@137 | 1330 | def visitTuple(self, tuple): |
paulb@137 | 1331 | return self._visitBuiltin(tuple, "tuple") |
paulb@137 | 1332 | |
paulb@137 | 1333 | def visitUnaryAdd(self, unaryadd): |
paulb@137 | 1334 | return self._visitUnary(unaryadd, "__pos__") |
paulb@137 | 1335 | |
paulb@137 | 1336 | def visitUnarySub(self, unarysub): |
paulb@137 | 1337 | return self._visitUnary(unarysub, "__neg__") |
paulb@137 | 1338 | |
paulb@137 | 1339 | def visitWhile(self, while_): |
paulb@137 | 1340 | |
paulb@137 | 1341 | """ |
paulb@137 | 1342 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@137 | 1343 | subprogram. Convert... |
paulb@137 | 1344 | |
paulb@137 | 1345 | While (test) -> (body) |
paulb@137 | 1346 | (else) |
paulb@137 | 1347 | |
paulb@137 | 1348 | ...to: |
paulb@137 | 1349 | |
paulb@137 | 1350 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@137 | 1351 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1352 | (else) -> ... |
paulb@137 | 1353 | """ |
paulb@137 | 1354 | |
paulb@137 | 1355 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@137 | 1356 | self.current_subprograms.append(subprogram) |
paulb@137 | 1357 | |
paulb@137 | 1358 | # Include a conditional statement in the subprogram. |
paulb@137 | 1359 | # Inside the conditional, add a recursive invocation to the subprogram |
paulb@137 | 1360 | # if the test condition was satisfied. |
paulb@137 | 1361 | # Return within the main section of the loop. |
paulb@137 | 1362 | |
paulb@147 | 1363 | test = Conditional( |
paulb@147 | 1364 | test=InvokeFunction( |
paulb@147 | 1365 | expr=LoadAttr( |
paulb@147 | 1366 | expr=self.dispatch(while_.test), |
paulb@147 | 1367 | name="__bool__"), |
paulb@147 | 1368 | ), |
paulb@147 | 1369 | body=self.dispatch(while_.body) + [ |
paulb@147 | 1370 | InvokeBlock( |
paulb@147 | 1371 | expr=LoadRef(ref=subprogram) |
paulb@147 | 1372 | ), |
paulb@147 | 1373 | ReturnFromBlock() |
paulb@147 | 1374 | ], |
paulb@147 | 1375 | else_=[] |
paulb@147 | 1376 | ) |
paulb@137 | 1377 | |
paulb@137 | 1378 | # Provide the else section, if present, along with an explicit return. |
paulb@137 | 1379 | |
paulb@137 | 1380 | if while_.else_ is not None: |
paulb@137 | 1381 | test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] |
paulb@137 | 1382 | |
paulb@137 | 1383 | # Finish the subprogram definition. |
paulb@137 | 1384 | |
paulb@137 | 1385 | subprogram.code = [test] |
paulb@137 | 1386 | |
paulb@137 | 1387 | self.current_subprograms.pop() |
paulb@137 | 1388 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1389 | |
paulb@137 | 1390 | # Make an invocation of the subprogram. |
paulb@137 | 1391 | |
paulb@147 | 1392 | result = InvokeBlock(while_, 1, |
paulb@147 | 1393 | expr=LoadRef(ref=subprogram) |
paulb@147 | 1394 | ) |
paulb@148 | 1395 | |
paulb@148 | 1396 | # Make nice annotations for the viewer. |
paulb@148 | 1397 | |
paulb@148 | 1398 | while_._test_call = subprogram.code[0].test |
paulb@148 | 1399 | |
paulb@137 | 1400 | return result |
paulb@137 | 1401 | |
paulb@137 | 1402 | # Convenience methods. |
paulb@137 | 1403 | |
paulb@137 | 1404 | def _visitBinary(self, binary, left_name, right_name): |
paulb@137 | 1405 | |
paulb@137 | 1406 | """ |
paulb@137 | 1407 | Emulate the current mechanisms by producing nodes as follows: |
paulb@137 | 1408 | |
paulb@137 | 1409 | InvokeBlock -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) |
paulb@137 | 1410 | (else) |
paulb@137 | 1411 | (handler) -> Conditional (test) -> CheckExc (expr) -> LoadExc |
paulb@137 | 1412 | (choices) -> LoadName TypeError |
paulb@137 | 1413 | (body) -> ReturnFromBlock (expr) -> y.__radd__(x) |
paulb@137 | 1414 | (else) |
paulb@137 | 1415 | """ |
paulb@137 | 1416 | |
paulb@137 | 1417 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1418 | self.current_subprograms.append(subprogram) |
paulb@137 | 1419 | |
paulb@137 | 1420 | subprogram.code = [ |
paulb@137 | 1421 | Try(binary, 1, |
paulb@137 | 1422 | body=[ |
paulb@137 | 1423 | ReturnFromBlock( |
paulb@137 | 1424 | expr=InvokeFunction( |
paulb@137 | 1425 | expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name), |
paulb@137 | 1426 | args=[self.dispatch(binary.right)], |
paulb@137 | 1427 | star=None, |
paulb@137 | 1428 | dstar=None) |
paulb@137 | 1429 | ) |
paulb@137 | 1430 | ], |
paulb@137 | 1431 | else_=[], |
paulb@137 | 1432 | finally_=[], |
paulb@137 | 1433 | handler=[ |
paulb@137 | 1434 | Conditional( |
paulb@137 | 1435 | test=CheckExc(expr=LoadExc(), choices=[LoadName(name="TypeError")]), |
paulb@137 | 1436 | body=[ |
paulb@137 | 1437 | ReturnFromBlock( |
paulb@137 | 1438 | expr=InvokeFunction( |
paulb@137 | 1439 | expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name), |
paulb@137 | 1440 | args=[self.dispatch(binary.left)], |
paulb@137 | 1441 | star=None, |
paulb@137 | 1442 | dstar=None) |
paulb@137 | 1443 | ) |
paulb@137 | 1444 | ], |
paulb@137 | 1445 | else_=[] |
paulb@137 | 1446 | ) |
paulb@137 | 1447 | ] |
paulb@137 | 1448 | ) |
paulb@137 | 1449 | ] |
paulb@137 | 1450 | |
paulb@137 | 1451 | self.current_subprograms.pop() |
paulb@137 | 1452 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1453 | |
paulb@148 | 1454 | result = InvokeBlock( |
paulb@148 | 1455 | produces_result=1, |
paulb@148 | 1456 | expr=LoadRef(ref=subprogram) |
paulb@148 | 1457 | ) |
paulb@148 | 1458 | |
paulb@148 | 1459 | # Make nice annotations for the viewer. |
paulb@148 | 1460 | |
paulb@148 | 1461 | binary._left_call = subprogram.code[0].body[0].expr |
paulb@148 | 1462 | binary._right_call = subprogram.code[0].handler[0].body[0].expr |
paulb@148 | 1463 | |
paulb@137 | 1464 | return result |
paulb@137 | 1465 | |
paulb@137 | 1466 | def _visitBuiltin(self, builtin, name): |
paulb@137 | 1467 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) |
paulb@137 | 1468 | return result |
paulb@137 | 1469 | |
paulb@137 | 1470 | def _visitUnary(self, unary, name): |
paulb@148 | 1471 | result = InvokeFunction(unary, 1, |
paulb@137 | 1472 | expr=LoadAttr( |
paulb@137 | 1473 | expr=self.dispatch(unary.expr), |
paulb@137 | 1474 | name=name |
paulb@148 | 1475 | ) |
paulb@137 | 1476 | ) |
paulb@137 | 1477 | |
paulb@148 | 1478 | # Make nice annotations for the viewer. |
paulb@148 | 1479 | |
paulb@148 | 1480 | unary._unary_call = result |
paulb@148 | 1481 | |
paulb@148 | 1482 | return result |
paulb@148 | 1483 | |
paulb@57 | 1484 | # Convenience functions. |
paulb@57 | 1485 | |
paulb@158 | 1486 | def simplify(filename, builtins=0, module_name=None): |
paulb@57 | 1487 | |
paulb@57 | 1488 | """ |
paulb@88 | 1489 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1490 | |
paulb@88 | 1491 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 1492 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 1493 | """ |
paulb@57 | 1494 | |
paulb@57 | 1495 | simplifier = Simplifier(builtins) |
paulb@57 | 1496 | module = compiler.parseFile(filename) |
paulb@57 | 1497 | compiler.misc.set_filename(filename, module) |
paulb@88 | 1498 | if builtins: |
paulb@158 | 1499 | name = module_name or "__builtins__" |
paulb@88 | 1500 | else: |
paulb@88 | 1501 | path, ext = os.path.splitext(filename) |
paulb@88 | 1502 | path, name = os.path.split(path) |
paulb@158 | 1503 | name = module_name or name |
paulb@88 | 1504 | simplified = simplifier.process(module, name) |
paulb@57 | 1505 | return simplified |
paulb@57 | 1506 | |
paul@0 | 1507 | # vim: tabstop=4 expandtab shiftwidth=4 |