paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Simplify AST structures for easier type propagation and analysis. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This software is free software; you can redistribute it and/or |
paul@0 | 9 | modify it under the terms of the GNU General Public License as |
paul@0 | 10 | published by the Free Software Foundation; either version 2 of |
paul@0 | 11 | the License, or (at your option) any later version. |
paul@0 | 12 | |
paul@0 | 13 | This software is distributed in the hope that it will be useful, |
paul@0 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 16 | GNU General Public License for more details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public |
paul@0 | 19 | License along with this library; see the file LICENCE.txt |
paul@0 | 20 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@0 | 22 | """ |
paul@0 | 23 | |
paul@0 | 24 | from compiler.visitor import ASTVisitor |
paul@0 | 25 | import compiler.ast |
paul@0 | 26 | from simplified import * |
paul@0 | 27 | |
paul@0 | 28 | class Simplifier(ASTVisitor): |
paul@0 | 29 | |
paul@0 | 30 | """ |
paul@0 | 31 | A simplifying visitor for AST nodes. |
paul@0 | 32 | |
paul@2 | 33 | Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, |
paul@3 | 34 | CallFunc, Class, Const, Continue, Dict, Discard, For, From, |
paul@4 | 35 | Function, Getattr, Global, If, Import, Invert, Keyword, Lambda, |
paul@4 | 36 | List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, |
paul@4 | 37 | Tuple, While, UnaryAdd, UnarySub. |
paul@0 | 38 | |
paul@2 | 39 | Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare, |
paul@4 | 40 | Decorators, Div, Ellipsis, Exec, FloorDiv, LeftShift, |
paul@3 | 41 | ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print, |
paul@3 | 42 | Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript, |
paul@4 | 43 | Yield. |
paul@0 | 44 | """ |
paul@0 | 45 | |
paul@0 | 46 | def __init__(self): |
paul@0 | 47 | ASTVisitor.__init__(self) |
paul@1 | 48 | self.result = None # The resulting tree. |
paul@1 | 49 | self.subprograms = [] # Subprograms outside the tree. |
paul@1 | 50 | self.current_subprograms = [] # Current subprograms being processed. |
paul@0 | 51 | |
paul@0 | 52 | # Generic visitor methods. |
paul@0 | 53 | |
paul@0 | 54 | def default(self, node, *args): |
paul@0 | 55 | raise ValueError, node.__class__ |
paul@0 | 56 | |
paul@0 | 57 | def dispatch(self, node, *args): |
paul@0 | 58 | return ASTVisitor.dispatch(self, node, *args) |
paul@0 | 59 | |
paul@0 | 60 | def dispatches(self, nodes, *args): |
paul@0 | 61 | results = [] |
paul@0 | 62 | for node in nodes: |
paul@0 | 63 | results.append(self.dispatch(node, *args)) |
paul@0 | 64 | return results |
paul@0 | 65 | |
paul@0 | 66 | # Placeholder or deletion transformations. |
paul@0 | 67 | |
paul@0 | 68 | def visitStmt(self, stmt): |
paul@0 | 69 | return self.dispatches(stmt.nodes) |
paul@0 | 70 | |
paul@0 | 71 | def visitPass(self, pass_): |
paul@0 | 72 | return Pass(pass_) |
paul@0 | 73 | |
paul@0 | 74 | def visitDiscard(self, discard): |
paul@0 | 75 | return self.dispatch(discard.expr) |
paul@0 | 76 | |
paul@0 | 77 | # Relatively trivial transformations. |
paul@0 | 78 | |
paul@0 | 79 | def visitModule(self, module): |
paul@0 | 80 | self.result = Module(module) |
paul@0 | 81 | self.result.code = self.dispatch(module.node) |
paul@0 | 82 | return self.result |
paul@0 | 83 | |
paul@0 | 84 | def visitClass(self, class_): |
paul@0 | 85 | result = Class(class_, name=class_.name, bases=class_.bases) |
paul@0 | 86 | result.code = self.dispatch(class_.code) |
paul@0 | 87 | return result |
paul@0 | 88 | |
paul@0 | 89 | def visitGetattr(self, getattr): |
paul@0 | 90 | result = LoadAttr(getattr, name=getattr.attrname) |
paul@0 | 91 | result.expr = self.dispatch(getattr.expr) |
paul@0 | 92 | return result |
paul@0 | 93 | |
paul@0 | 94 | def visitKeyword(self, keyword): |
paul@0 | 95 | result = Keyword(keyword, name=keyword.name) |
paul@0 | 96 | result.expr = self.dispatch(keyword.expr) |
paul@0 | 97 | return result |
paul@0 | 98 | |
paul@1 | 99 | def visitGlobal(self, global_): |
paul@1 | 100 | result = Global(global_, names=global_.names) |
paul@1 | 101 | return result |
paul@1 | 102 | |
paul@3 | 103 | def visitImport(self, import_): |
paul@3 | 104 | result = Assign(import_) |
paul@3 | 105 | code = [] |
paul@3 | 106 | for path, alias in import_.names: |
paul@3 | 107 | importer = Import(name=path) |
paul@3 | 108 | top = alias or path.split(".")[0] |
paul@3 | 109 | code.append(StoreName(expr=importer, name=top)) |
paul@3 | 110 | result.code = code |
paul@3 | 111 | return result |
paul@3 | 112 | |
paul@3 | 113 | def visitFrom(self, from_): |
paul@3 | 114 | result = Assign(from_) |
paul@3 | 115 | code = [] |
paul@3 | 116 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paul@3 | 117 | for name, alias in from_.names: |
paul@3 | 118 | code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name))) |
paul@3 | 119 | result.code = code |
paul@3 | 120 | return result |
paul@3 | 121 | |
paul@0 | 122 | def visitName(self, name): |
paul@0 | 123 | result = LoadName(name, name=name.name) |
paul@0 | 124 | return result |
paul@0 | 125 | |
paul@0 | 126 | def visitConst(self, const): |
paul@0 | 127 | result = LoadConst(const, value=const.value) |
paul@0 | 128 | return result |
paul@0 | 129 | |
paul@0 | 130 | def visitReturn(self, return_): |
paul@0 | 131 | result = Return(return_) |
paul@0 | 132 | result.expr = self.dispatch(return_.value) |
paul@0 | 133 | return result |
paul@0 | 134 | |
paul@0 | 135 | def visitBreak(self, break_): |
paul@0 | 136 | result = Return(break_) |
paul@0 | 137 | return result |
paul@0 | 138 | |
paul@0 | 139 | def visitContinue(self, continue_): |
paul@1 | 140 | result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 141 | result.expr = LoadRef(ref=self.current_subprograms[-1]) |
paul@0 | 142 | return result |
paul@0 | 143 | |
paul@0 | 144 | def visitIf(self, if_): |
paul@0 | 145 | result = If(if_, else_=[]) |
paul@0 | 146 | tests = [] |
paul@0 | 147 | for compare, stmt in if_.tests: |
paul@1 | 148 | # Produce something like... |
paul@1 | 149 | # expr.__true__() ? body |
paul@0 | 150 | test = Conditional(else_=[], test=Invoke( |
paul@0 | 151 | expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), |
paul@0 | 152 | params=[], star=None, dstar=None)) |
paul@0 | 153 | test.body = self.dispatch(stmt) |
paul@0 | 154 | tests.append(test) |
paul@0 | 155 | result.tests = tests |
paul@0 | 156 | if if_.else_ is not None: |
paul@0 | 157 | result.else_ = self.dispatch(if_.else_) |
paul@0 | 158 | return result |
paul@0 | 159 | |
paul@0 | 160 | def _visitBuiltin(self, builtin, name): |
paul@1 | 161 | result = Invoke(builtin, expr=LoadName(name=name)) |
paul@1 | 162 | result.args = self.dispatches(builtin.nodes) |
paul@0 | 163 | return result |
paul@0 | 164 | |
paul@0 | 165 | def visitTuple(self, tuple): |
paul@0 | 166 | return self._visitBuiltin(tuple, "Tuple") |
paul@0 | 167 | |
paul@0 | 168 | def visitList(self, list): |
paul@0 | 169 | return self._visitBuiltin(list, "List") |
paul@0 | 170 | |
paul@0 | 171 | def visitDict(self, dict): |
paul@0 | 172 | result = Invoke(expr=LoadName(name="Dict")) |
paul@0 | 173 | args = [] |
paul@0 | 174 | for key, value in dict.items: |
paul@0 | 175 | tuple = Invoke(expr=LoadName(name="Tuple"), star=None, dstar=None) |
paul@0 | 176 | tuple.args = [self.dispatch(key), self.dispatch(value)] |
paul@0 | 177 | args.append(tuple) |
paul@0 | 178 | result.args = args |
paul@0 | 179 | return result |
paul@0 | 180 | |
paul@1 | 181 | # Logical operators. |
paul@1 | 182 | |
paul@1 | 183 | def visitAnd(self, and_): |
paul@1 | 184 | result = And(and_) |
paul@1 | 185 | nodes = [] |
paul@1 | 186 | for node in and_.nodes: |
paul@1 | 187 | nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), |
paul@1 | 188 | params=[], star=None, dstar=None)) |
paul@1 | 189 | result.nodes = nodes |
paul@1 | 190 | return result |
paul@1 | 191 | |
paul@1 | 192 | def visitOr(self, or_): |
paul@1 | 193 | result = Or(or_) |
paul@1 | 194 | nodes = [] |
paul@1 | 195 | for node in or_.nodes: |
paul@1 | 196 | nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), |
paul@1 | 197 | params=[], star=None, dstar=None)) |
paul@1 | 198 | result.nodes = nodes |
paul@1 | 199 | return result |
paul@1 | 200 | |
paul@1 | 201 | def visitNot(self, not_): |
paul@1 | 202 | result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), |
paul@1 | 203 | params=[], star=None, dstar=None)) |
paul@1 | 204 | return result |
paul@1 | 205 | |
paul@4 | 206 | # Operators. |
paul@4 | 207 | |
paul@4 | 208 | def visitUnaryAdd(self, unaryadd): |
paul@4 | 209 | return Invoke(expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[]) |
paul@4 | 210 | |
paul@4 | 211 | def visitUnarySub(self, unarysub): |
paul@4 | 212 | return Invoke(expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[]) |
paul@4 | 213 | |
paul@4 | 214 | def visitInvert(self, invert): |
paul@4 | 215 | return Invoke(expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[]) |
paul@4 | 216 | |
paul@0 | 217 | # Assignments. |
paul@0 | 218 | |
paul@2 | 219 | augassign_methods = { |
paul@2 | 220 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__" |
paul@2 | 221 | } |
paul@2 | 222 | |
paul@2 | 223 | def visitAugAssign(self, augassign): |
paul@2 | 224 | result = Assign(augassign) |
paul@2 | 225 | expr = self.dispatch(augassign.expr) |
paul@2 | 226 | |
paul@2 | 227 | # Simple augmented assignment: name += expr |
paul@2 | 228 | |
paul@2 | 229 | if isinstance(augassign.node, compiler.ast.Name): |
paul@2 | 230 | node = self.dispatch(augassign.node) |
paul@2 | 231 | get_incremented = StoreTemp( |
paul@2 | 232 | expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 233 | ) |
paul@2 | 234 | store = StoreName(expr=LoadTemp(), name=augassign.node.name) |
paul@2 | 235 | result.code = [get_incremented, store, ReleaseTemp()] |
paul@2 | 236 | |
paul@2 | 237 | # Complicated augmented assignment: expr.attr += expr |
paul@2 | 238 | |
paul@2 | 239 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paul@2 | 240 | store_expr = StoreTemp(index=1, expr=self.dispatch(augassign.node.expr)) |
paul@2 | 241 | node_attr = LoadAttr(expr=LoadTemp(index=1), name=augassign.node.attrname) |
paul@2 | 242 | get_incremented = StoreTemp( |
paul@2 | 243 | expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 244 | ) |
paul@2 | 245 | store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index=1), name=augassign.node.attrname) |
paul@2 | 246 | result.code = [store_expr, get_incremented, store, ReleaseTemp(index=1), ReleaseTemp()] |
paul@2 | 247 | |
paul@2 | 248 | else: |
paul@2 | 249 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 250 | |
paul@2 | 251 | return result |
paul@2 | 252 | |
paul@0 | 253 | def visitAssign(self, assign): |
paul@0 | 254 | result = Assign(assign) |
paul@0 | 255 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paul@0 | 256 | release = ReleaseTemp() |
paul@0 | 257 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paul@0 | 258 | return result |
paul@0 | 259 | |
paul@0 | 260 | def visitAssList(self, asslist, in_sequence=0): |
paul@0 | 261 | if not in_sequence: |
paul@0 | 262 | expr = LoadTemp(asslist) |
paul@0 | 263 | else: |
paul@0 | 264 | expr = Invoke(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 265 | result = Assign(asslist) |
paul@0 | 266 | store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr))) |
paul@0 | 267 | release = ReleaseTemp() |
paul@0 | 268 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paul@0 | 269 | return result |
paul@0 | 270 | |
paul@0 | 271 | visitAssTuple = visitAssList |
paul@0 | 272 | |
paul@0 | 273 | def _visitAssNameOrAttr(self, node, in_sequence): |
paul@0 | 274 | if not in_sequence: |
paul@0 | 275 | return LoadTemp(node) |
paul@0 | 276 | else: |
paul@0 | 277 | return Invoke(node, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 278 | |
paul@0 | 279 | def visitAssName(self, assname, in_sequence=0): |
paul@0 | 280 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paul@0 | 281 | result = StoreName(assname, name=assname.name, expr=expr) |
paul@0 | 282 | return result |
paul@0 | 283 | |
paul@0 | 284 | def visitAssAttr(self, assattr, in_sequence=0): |
paul@0 | 285 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paul@0 | 286 | lvalue = self.dispatch(assattr.expr) |
paul@0 | 287 | result = StoreAttr(assattr, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@0 | 288 | return result |
paul@0 | 289 | |
paul@0 | 290 | # Invocation and subprogram transformations. |
paul@0 | 291 | |
paul@0 | 292 | def _visitFunction(self, function, subprogram): |
paul@0 | 293 | if function.flags & 4 != 0: has_star = 1 |
paul@0 | 294 | else: has_star = 0 |
paul@0 | 295 | if function.flags & 8 != 0: has_dstar = 1 |
paul@0 | 296 | else: has_dstar = 0 |
paul@0 | 297 | ndefaults = len(function.defaults) |
paul@0 | 298 | npositional = len(function.argnames) - has_star - has_dstar |
paul@0 | 299 | if has_star: star = function.argnames[npositional] |
paul@0 | 300 | else: star = None |
paul@0 | 301 | if has_dstar: dstar = function.argnames[npositional + has_star] |
paul@0 | 302 | else: dstar = None |
paul@0 | 303 | |
paul@0 | 304 | params = [] |
paul@0 | 305 | for i in range(0, npositional - ndefaults): |
paul@0 | 306 | params.append((function.argnames[i], None)) |
paul@0 | 307 | |
paul@0 | 308 | # NOTE: Fix/process defaults. |
paul@0 | 309 | |
paul@0 | 310 | for i in range(0, ndefaults): |
paul@0 | 311 | default = function.defaults[i] |
paul@0 | 312 | if default is not None: |
paul@0 | 313 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paul@0 | 314 | else: |
paul@0 | 315 | params.append((function.argnames[npositional - ndefaults + i], default)) |
paul@0 | 316 | |
paul@0 | 317 | subprogram.params = params |
paul@0 | 318 | subprogram.star = star |
paul@0 | 319 | subprogram.dstar = dstar |
paul@0 | 320 | self.subprograms.append(subprogram) |
paul@0 | 321 | |
paul@0 | 322 | def visitFunction(self, function): |
paul@0 | 323 | |
paul@0 | 324 | # Make a subprogram for the function and record it outside the main |
paul@0 | 325 | # tree. |
paul@0 | 326 | |
paul@0 | 327 | subprogram = Subprogram(function, name=function.name, star=None, dstar=None) |
paul@0 | 328 | self.current_subprograms.append(subprogram) |
paul@0 | 329 | subprogram.code = self.dispatch(function.code) |
paul@0 | 330 | self.current_subprograms.pop() |
paul@0 | 331 | self._visitFunction(function, subprogram) |
paul@0 | 332 | |
paul@0 | 333 | # Make a definition of the function associating it with a name. |
paul@0 | 334 | |
paul@0 | 335 | result = Assign(function) |
paul@0 | 336 | load = LoadRef(ref=subprogram) |
paul@0 | 337 | store = StoreName(name=function.name) |
paul@0 | 338 | result.code = [load, store] |
paul@0 | 339 | return result |
paul@0 | 340 | |
paul@0 | 341 | def visitLambda(self, lambda_): |
paul@0 | 342 | |
paul@0 | 343 | # Make a subprogram for the function and record it outside the main |
paul@0 | 344 | # tree. |
paul@0 | 345 | |
paul@0 | 346 | subprogram = Subprogram(lambda_, name=hex(id(lambda_)), star=None, dstar=None) |
paul@0 | 347 | self.current_subprograms.append(subprogram) |
paul@0 | 348 | subprogram.code = [Return(expr=self.dispatch(lambda_.code))] |
paul@0 | 349 | self.current_subprograms.pop() |
paul@0 | 350 | self._visitFunction(lambda_, subprogram) |
paul@0 | 351 | |
paul@0 | 352 | # Get the subprogram reference to the lambda. |
paul@0 | 353 | |
paul@0 | 354 | return LoadRef(ref=subprogram) |
paul@0 | 355 | |
paul@0 | 356 | def visitCallFunc(self, callfunc): |
paul@0 | 357 | result = Invoke(callfunc, same_frame=0, star=None, dstar=None) |
paul@0 | 358 | result.args = self.dispatches(callfunc.args) |
paul@0 | 359 | if callfunc.star_args is not None: |
paul@0 | 360 | result.star = self.dispatch(callfunc.star_args) |
paul@0 | 361 | if callfunc.dstar_args is not None: |
paul@0 | 362 | result.dstar = self.dispatch(callfunc.dstar_args) |
paul@0 | 363 | result.expr = self.dispatch(callfunc.node) |
paul@0 | 364 | return result |
paul@0 | 365 | |
paul@0 | 366 | def visitWhile(self, while_): |
paul@0 | 367 | |
paul@0 | 368 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 369 | |
paul@0 | 370 | subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, params=[], star=None, dstar=None) |
paul@0 | 371 | self.current_subprograms.append(subprogram) |
paul@0 | 372 | |
paul@0 | 373 | # Include a conditional statement in the subprogram. |
paul@0 | 374 | |
paul@0 | 375 | test = Conditional(else_=[]) |
paul@0 | 376 | test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), |
paul@0 | 377 | params=[], star=None, dstar=None) |
paul@0 | 378 | |
paul@0 | 379 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 380 | # if the test condition was satisfied. |
paul@0 | 381 | |
paul@0 | 382 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 383 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 384 | test.body = self.dispatch(while_.body) + [continuation] |
paul@0 | 385 | if while_.else_ is not None: |
paul@0 | 386 | test.else_ = self.dispatch(while_.else_) |
paul@0 | 387 | subprogram.code = [test] |
paul@0 | 388 | |
paul@0 | 389 | self.current_subprograms.pop() |
paul@0 | 390 | self.subprograms.append(subprogram) |
paul@0 | 391 | |
paul@0 | 392 | # Make an invocation of the subprogram. |
paul@0 | 393 | |
paul@0 | 394 | result = Invoke(while_, same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 395 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 396 | return result |
paul@0 | 397 | |
paul@0 | 398 | def visitFor(self, for_): |
paul@0 | 399 | |
paul@0 | 400 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 401 | |
paul@0 | 402 | subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, params=[], star=None, dstar=None) |
paul@0 | 403 | self.current_subprograms.append(subprogram) |
paul@0 | 404 | |
paul@0 | 405 | # Wrap the assignment in a try...except statement. |
paul@0 | 406 | |
paul@0 | 407 | try_except = Try(body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 408 | except_spec = Invoke(expr=LoadName(name="Tuple"), params=[LoadName(name="StopIteration")]) |
paul@0 | 409 | stopiteration = Except(spec=except_spec) |
paul@0 | 410 | stopiteration.code = self.dispatch(for_.else_) |
paul@0 | 411 | try_except.handlers = [stopiteration] |
paul@0 | 412 | |
paul@0 | 413 | assign = Assign() |
paul@0 | 414 | assign.code = [ |
paul@0 | 415 | StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paul@0 | 416 | self.dispatch(for_.assign), |
paul@0 | 417 | ReleaseTemp() |
paul@0 | 418 | ] |
paul@0 | 419 | |
paul@0 | 420 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 421 | # if the test condition was satisfied. |
paul@0 | 422 | |
paul@0 | 423 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 424 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 425 | try_except.body = [assign] + self.dispatch(for_.body) + [continuation] |
paul@0 | 426 | subprogram.code = [try_except] |
paul@0 | 427 | |
paul@0 | 428 | self.subprograms.append(subprogram) |
paul@0 | 429 | self.current_subprograms.pop() |
paul@0 | 430 | |
paul@0 | 431 | # Obtain an iterator for the sequence involved. |
paul@0 | 432 | # Then, make an invocation of the subprogram. |
paul@0 | 433 | |
paul@0 | 434 | result = Assign(for_) |
paul@0 | 435 | result.code = [ |
paul@0 | 436 | StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))), |
paul@0 | 437 | Invoke(expr=LoadRef(ref=subprogram), same_frame=1, star=None, dstar=None, args=[]), |
paul@0 | 438 | ReleaseTemp() |
paul@0 | 439 | ] |
paul@0 | 440 | return result |
paul@0 | 441 | |
paul@0 | 442 | # Exception node transformations. |
paul@0 | 443 | |
paul@0 | 444 | def visitTryExcept(self, tryexcept): |
paul@0 | 445 | result = Try(tryexcept, body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 446 | if tryexcept.body is not None: |
paul@0 | 447 | result.body = self.dispatch(tryexcept.body) |
paul@0 | 448 | if tryexcept.else_ is not None: |
paul@0 | 449 | result.else_ = self.dispatch(tryexcept.else_) |
paul@0 | 450 | handlers = [] |
paul@0 | 451 | for spec, assign, stmt in tryexcept.handlers: |
paul@0 | 452 | get_exc = Assign() |
paul@0 | 453 | get_exc.code = [StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()] |
paul@0 | 454 | handler = Except(spec=self.dispatch(spec)) |
paul@0 | 455 | handler.code = [get_exc] + self.dispatch(stmt) |
paul@0 | 456 | handlers.append(handler) |
paul@0 | 457 | result.handlers = handlers |
paul@0 | 458 | return result |
paul@0 | 459 | |
paul@0 | 460 | def visitTryFinally(self, tryfinally): |
paul@0 | 461 | result = Try(tryfinally, body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 462 | if tryfinally.body is not None: |
paul@0 | 463 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 464 | if tryfinally.final is not None: |
paul@0 | 465 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 466 | return result |
paul@0 | 467 | |
paul@0 | 468 | # vim: tabstop=4 expandtab shiftwidth=4 |