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