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@5 | 33 | Covered: And, 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@5 | 36 | List, Module, Name, Not, Or, Pass, Raise, Return, Stmt, TryExcept, |
paul@5 | 37 | TryFinally, Tuple, While, UnaryAdd, UnarySub. |
paul@0 | 38 | |
paul@5 | 39 | Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Compare, Decorators, |
paul@5 | 40 | Div, Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor, |
paul@5 | 41 | ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, |
paul@5 | 42 | Slice, Sliceobj, Sub, Subscript, Yield. |
paul@0 | 43 | """ |
paul@0 | 44 | |
paul@0 | 45 | def __init__(self): |
paul@0 | 46 | ASTVisitor.__init__(self) |
paul@1 | 47 | self.result = None # The resulting tree. |
paul@1 | 48 | self.subprograms = [] # Subprograms outside the tree. |
paul@1 | 49 | self.current_subprograms = [] # Current subprograms being processed. |
paul@0 | 50 | |
paul@0 | 51 | # Generic visitor methods. |
paul@0 | 52 | |
paul@0 | 53 | def default(self, node, *args): |
paul@0 | 54 | raise ValueError, node.__class__ |
paul@0 | 55 | |
paul@0 | 56 | def dispatch(self, node, *args): |
paul@0 | 57 | return ASTVisitor.dispatch(self, node, *args) |
paul@0 | 58 | |
paul@0 | 59 | def dispatches(self, nodes, *args): |
paul@0 | 60 | results = [] |
paul@0 | 61 | for node in nodes: |
paul@0 | 62 | results.append(self.dispatch(node, *args)) |
paul@0 | 63 | return results |
paul@0 | 64 | |
paulb@6 | 65 | def dispatch_or_none(self, node, *args): |
paulb@6 | 66 | if node is not None: |
paulb@6 | 67 | return self.dispatch(node, *args) |
paulb@6 | 68 | else: |
paulb@6 | 69 | return LoadName(name="None") |
paulb@6 | 70 | |
paul@0 | 71 | # Placeholder or deletion transformations. |
paul@0 | 72 | |
paul@0 | 73 | def visitStmt(self, stmt): |
paul@0 | 74 | return self.dispatches(stmt.nodes) |
paul@0 | 75 | |
paul@0 | 76 | def visitPass(self, pass_): |
paul@0 | 77 | return Pass(pass_) |
paul@0 | 78 | |
paul@0 | 79 | def visitDiscard(self, discard): |
paul@0 | 80 | return self.dispatch(discard.expr) |
paul@0 | 81 | |
paul@0 | 82 | # Relatively trivial transformations. |
paul@0 | 83 | |
paul@0 | 84 | def visitModule(self, module): |
paul@0 | 85 | self.result = Module(module) |
paul@0 | 86 | self.result.code = self.dispatch(module.node) |
paul@0 | 87 | return self.result |
paul@0 | 88 | |
paul@0 | 89 | def visitClass(self, class_): |
paul@0 | 90 | result = Class(class_, name=class_.name, bases=class_.bases) |
paul@0 | 91 | result.code = self.dispatch(class_.code) |
paul@0 | 92 | return result |
paul@0 | 93 | |
paul@0 | 94 | def visitGetattr(self, getattr): |
paul@0 | 95 | result = LoadAttr(getattr, name=getattr.attrname) |
paul@0 | 96 | result.expr = self.dispatch(getattr.expr) |
paul@0 | 97 | return result |
paul@0 | 98 | |
paul@0 | 99 | def visitKeyword(self, keyword): |
paul@0 | 100 | result = Keyword(keyword, name=keyword.name) |
paul@0 | 101 | result.expr = self.dispatch(keyword.expr) |
paul@0 | 102 | return result |
paul@0 | 103 | |
paul@1 | 104 | def visitGlobal(self, global_): |
paul@1 | 105 | result = Global(global_, names=global_.names) |
paul@1 | 106 | return result |
paul@1 | 107 | |
paul@3 | 108 | def visitImport(self, import_): |
paul@3 | 109 | result = Assign(import_) |
paul@3 | 110 | code = [] |
paul@3 | 111 | for path, alias in import_.names: |
paul@3 | 112 | importer = Import(name=path) |
paul@3 | 113 | top = alias or path.split(".")[0] |
paul@3 | 114 | code.append(StoreName(expr=importer, name=top)) |
paul@3 | 115 | result.code = code |
paul@3 | 116 | return result |
paul@3 | 117 | |
paul@3 | 118 | def visitFrom(self, from_): |
paul@3 | 119 | result = Assign(from_) |
paul@3 | 120 | code = [] |
paul@3 | 121 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paul@3 | 122 | for name, alias in from_.names: |
paul@3 | 123 | code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name))) |
paul@3 | 124 | result.code = code |
paul@3 | 125 | return result |
paul@3 | 126 | |
paul@0 | 127 | def visitName(self, name): |
paul@0 | 128 | result = LoadName(name, name=name.name) |
paul@0 | 129 | return result |
paul@0 | 130 | |
paul@0 | 131 | def visitConst(self, const): |
paul@0 | 132 | result = LoadConst(const, value=const.value) |
paul@0 | 133 | return result |
paul@0 | 134 | |
paul@0 | 135 | def visitReturn(self, return_): |
paul@0 | 136 | result = Return(return_) |
paul@0 | 137 | result.expr = self.dispatch(return_.value) |
paul@0 | 138 | return result |
paul@0 | 139 | |
paul@0 | 140 | def visitBreak(self, break_): |
paul@0 | 141 | result = Return(break_) |
paul@0 | 142 | return result |
paul@0 | 143 | |
paul@0 | 144 | def visitContinue(self, continue_): |
paul@1 | 145 | result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 146 | result.expr = LoadRef(ref=self.current_subprograms[-1]) |
paul@0 | 147 | return result |
paul@0 | 148 | |
paul@5 | 149 | def visitRaise(self, raise_): |
paul@5 | 150 | result = Raise(raise_, expr=self.dispatch(raise_.expr1), traceback=None) |
paul@5 | 151 | if raise_.expr2 is not None: |
paul@5 | 152 | result.args = [self.dispatch(raise_.expr2)] |
paul@5 | 153 | if raise_.expr3 is not None: |
paul@5 | 154 | result.traceback = self.dispatch(raise_.expr3) |
paul@5 | 155 | return result |
paul@5 | 156 | |
paul@0 | 157 | def visitIf(self, if_): |
paul@0 | 158 | result = If(if_, else_=[]) |
paul@0 | 159 | tests = [] |
paul@0 | 160 | for compare, stmt in if_.tests: |
paul@1 | 161 | # Produce something like... |
paul@1 | 162 | # expr.__true__() ? body |
paul@0 | 163 | test = Conditional(else_=[], test=Invoke( |
paul@0 | 164 | expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), |
paul@0 | 165 | params=[], star=None, dstar=None)) |
paul@0 | 166 | test.body = self.dispatch(stmt) |
paul@0 | 167 | tests.append(test) |
paul@0 | 168 | result.tests = tests |
paul@0 | 169 | if if_.else_ is not None: |
paul@0 | 170 | result.else_ = self.dispatch(if_.else_) |
paul@0 | 171 | return result |
paul@0 | 172 | |
paul@0 | 173 | def _visitBuiltin(self, builtin, name): |
paul@1 | 174 | result = Invoke(builtin, expr=LoadName(name=name)) |
paul@1 | 175 | result.args = self.dispatches(builtin.nodes) |
paul@0 | 176 | return result |
paul@0 | 177 | |
paul@0 | 178 | def visitTuple(self, tuple): |
paul@0 | 179 | return self._visitBuiltin(tuple, "Tuple") |
paul@0 | 180 | |
paul@0 | 181 | def visitList(self, list): |
paul@0 | 182 | return self._visitBuiltin(list, "List") |
paul@0 | 183 | |
paul@0 | 184 | def visitDict(self, dict): |
paul@0 | 185 | result = Invoke(expr=LoadName(name="Dict")) |
paul@0 | 186 | args = [] |
paul@0 | 187 | for key, value in dict.items: |
paul@0 | 188 | tuple = Invoke(expr=LoadName(name="Tuple"), star=None, dstar=None) |
paul@0 | 189 | tuple.args = [self.dispatch(key), self.dispatch(value)] |
paul@0 | 190 | args.append(tuple) |
paul@0 | 191 | result.args = args |
paul@0 | 192 | return result |
paul@0 | 193 | |
paul@1 | 194 | # Logical operators. |
paul@1 | 195 | |
paul@1 | 196 | def visitAnd(self, and_): |
paul@1 | 197 | result = And(and_) |
paul@1 | 198 | nodes = [] |
paul@1 | 199 | for node in and_.nodes: |
paul@1 | 200 | nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), |
paul@1 | 201 | params=[], star=None, dstar=None)) |
paul@1 | 202 | result.nodes = nodes |
paul@1 | 203 | return result |
paul@1 | 204 | |
paul@1 | 205 | def visitOr(self, or_): |
paul@1 | 206 | result = Or(or_) |
paul@1 | 207 | nodes = [] |
paul@1 | 208 | for node in or_.nodes: |
paul@1 | 209 | nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), |
paul@1 | 210 | params=[], star=None, dstar=None)) |
paul@1 | 211 | result.nodes = nodes |
paul@1 | 212 | return result |
paul@1 | 213 | |
paul@1 | 214 | def visitNot(self, not_): |
paul@1 | 215 | result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), |
paul@1 | 216 | params=[], star=None, dstar=None)) |
paul@1 | 217 | return result |
paul@1 | 218 | |
paul@4 | 219 | # Operators. |
paul@4 | 220 | |
paul@4 | 221 | def visitUnaryAdd(self, unaryadd): |
paul@4 | 222 | return Invoke(expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[]) |
paul@4 | 223 | |
paul@4 | 224 | def visitUnarySub(self, unarysub): |
paul@4 | 225 | return Invoke(expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[]) |
paul@4 | 226 | |
paul@4 | 227 | def visitInvert(self, invert): |
paul@4 | 228 | return Invoke(expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[]) |
paul@4 | 229 | |
paul@0 | 230 | # Assignments. |
paul@0 | 231 | |
paul@2 | 232 | augassign_methods = { |
paul@2 | 233 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__" |
paul@2 | 234 | } |
paul@2 | 235 | |
paul@2 | 236 | def visitAugAssign(self, augassign): |
paul@2 | 237 | result = Assign(augassign) |
paul@2 | 238 | expr = self.dispatch(augassign.expr) |
paul@2 | 239 | |
paul@2 | 240 | # Simple augmented assignment: name += expr |
paul@2 | 241 | |
paul@2 | 242 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@6 | 243 | name = augassign.node |
paulb@6 | 244 | node = self.dispatch(name) |
paul@2 | 245 | get_incremented = StoreTemp( |
paul@2 | 246 | expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 247 | ) |
paulb@6 | 248 | store = StoreName(expr=LoadTemp(), name=name.name) |
paul@2 | 249 | result.code = [get_incremented, store, ReleaseTemp()] |
paul@2 | 250 | |
paul@2 | 251 | # Complicated augmented assignment: expr.attr += expr |
paul@2 | 252 | |
paul@2 | 253 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 254 | |
paulb@6 | 255 | # <expr> -> <expr>.attr.__xxx__( |
paulb@6 | 256 | |
paulb@6 | 257 | getattr = augassign.node |
paulb@6 | 258 | store_expr = StoreTemp(index="expr", expr=self.dispatch(getattr.expr)) |
paulb@6 | 259 | node_attr = LoadAttr(expr=LoadTemp(index="expr"), name=getattr.attrname) |
paul@2 | 260 | get_incremented = StoreTemp( |
paul@2 | 261 | expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 262 | ) |
paulb@6 | 263 | store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index="expr"), name=getattr.attrname) |
paulb@6 | 264 | result.code = [store_expr, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp()] |
paulb@6 | 265 | |
paulb@6 | 266 | # Complicated augassign using slices and subscripts. |
paulb@6 | 267 | |
paulb@6 | 268 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@6 | 269 | slice = augassign.node |
paulb@6 | 270 | store_expr = StoreTemp(index="expr", expr=self.dispatch(slice.expr)) |
paulb@6 | 271 | store_lower = StoreTemp(index="lower", expr=self.dispatch_or_none(slice.lower)) |
paulb@6 | 272 | store_upper = StoreTemp(index="upper", expr=self.dispatch_or_none(slice.upper)) |
paulb@6 | 273 | node_slice = self._visitSlice(LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY") |
paulb@6 | 274 | get_incremented = StoreTemp( |
paulb@6 | 275 | expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr]) |
paulb@6 | 276 | ) |
paulb@6 | 277 | store = self._visitSlice(LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", expr) |
paulb@6 | 278 | result.code = [store_expr, store_lower, store_upper, get_incremented, store, |
paulb@6 | 279 | ReleaseTemp(index="expr"), ReleaseTemp(index="lower"), ReleaseTemp(index="upper"), ReleaseTemp()] |
paul@2 | 280 | |
paul@2 | 281 | else: |
paul@2 | 282 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 283 | |
paul@2 | 284 | return result |
paul@2 | 285 | |
paul@0 | 286 | def visitAssign(self, assign): |
paul@0 | 287 | result = Assign(assign) |
paul@0 | 288 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paul@0 | 289 | release = ReleaseTemp() |
paul@0 | 290 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paul@0 | 291 | return result |
paul@0 | 292 | |
paul@0 | 293 | def visitAssList(self, asslist, in_sequence=0): |
paul@0 | 294 | if not in_sequence: |
paul@0 | 295 | expr = LoadTemp(asslist) |
paul@0 | 296 | else: |
paul@0 | 297 | expr = Invoke(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 298 | result = Assign(asslist) |
paul@0 | 299 | store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr))) |
paul@0 | 300 | release = ReleaseTemp() |
paul@0 | 301 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paul@0 | 302 | return result |
paul@0 | 303 | |
paul@0 | 304 | visitAssTuple = visitAssList |
paul@0 | 305 | |
paul@0 | 306 | def _visitAssNameOrAttr(self, node, in_sequence): |
paul@0 | 307 | if not in_sequence: |
paul@0 | 308 | return LoadTemp(node) |
paul@0 | 309 | else: |
paul@0 | 310 | return Invoke(node, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 311 | |
paul@0 | 312 | def visitAssName(self, assname, in_sequence=0): |
paul@0 | 313 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paul@0 | 314 | result = StoreName(assname, name=assname.name, expr=expr) |
paul@0 | 315 | return result |
paul@0 | 316 | |
paul@0 | 317 | def visitAssAttr(self, assattr, in_sequence=0): |
paul@0 | 318 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paul@0 | 319 | lvalue = self.dispatch(assattr.expr) |
paul@0 | 320 | result = StoreAttr(assattr, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@0 | 321 | return result |
paul@0 | 322 | |
paulb@6 | 323 | def _visitSlice(self, expr, lower, upper, flags, value=None): |
paulb@6 | 324 | if flags == "OP_ASSIGN": |
paulb@6 | 325 | args = [value] |
paulb@6 | 326 | result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__")) |
paulb@6 | 327 | elif flags == "OP_APPLY": |
paulb@6 | 328 | args = [] |
paulb@6 | 329 | result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__")) |
paulb@6 | 330 | else: |
paulb@6 | 331 | raise NotImplementedError, flags |
paulb@6 | 332 | |
paulb@6 | 333 | # Add the dimensions. |
paulb@6 | 334 | |
paulb@6 | 335 | args.insert(0, lower) |
paulb@6 | 336 | args.insert(1, upper) |
paulb@6 | 337 | |
paulb@6 | 338 | result.args = args |
paulb@6 | 339 | return result |
paulb@6 | 340 | |
paulb@6 | 341 | def visitSlice(self, slice, in_sequence=0): |
paulb@6 | 342 | value = self._visitAssNameOrAttr(slice, in_sequence) |
paulb@6 | 343 | return self._visitSlice(self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), self.dispatch_or_none(slice.upper), slice.flags, value) |
paulb@6 | 344 | |
paul@0 | 345 | # Invocation and subprogram transformations. |
paul@0 | 346 | |
paul@0 | 347 | def _visitFunction(self, function, subprogram): |
paul@0 | 348 | if function.flags & 4 != 0: has_star = 1 |
paul@0 | 349 | else: has_star = 0 |
paul@0 | 350 | if function.flags & 8 != 0: has_dstar = 1 |
paul@0 | 351 | else: has_dstar = 0 |
paul@0 | 352 | ndefaults = len(function.defaults) |
paul@0 | 353 | npositional = len(function.argnames) - has_star - has_dstar |
paul@0 | 354 | if has_star: star = function.argnames[npositional] |
paul@0 | 355 | else: star = None |
paul@0 | 356 | if has_dstar: dstar = function.argnames[npositional + has_star] |
paul@0 | 357 | else: dstar = None |
paul@0 | 358 | |
paul@0 | 359 | params = [] |
paul@0 | 360 | for i in range(0, npositional - ndefaults): |
paul@0 | 361 | params.append((function.argnames[i], None)) |
paul@0 | 362 | |
paul@0 | 363 | # NOTE: Fix/process defaults. |
paul@0 | 364 | |
paul@0 | 365 | for i in range(0, ndefaults): |
paul@0 | 366 | default = function.defaults[i] |
paul@0 | 367 | if default is not None: |
paul@0 | 368 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paul@0 | 369 | else: |
paul@0 | 370 | params.append((function.argnames[npositional - ndefaults + i], default)) |
paul@0 | 371 | |
paul@0 | 372 | subprogram.params = params |
paul@0 | 373 | subprogram.star = star |
paul@0 | 374 | subprogram.dstar = dstar |
paul@0 | 375 | self.subprograms.append(subprogram) |
paul@0 | 376 | |
paul@0 | 377 | def visitFunction(self, function): |
paul@0 | 378 | |
paul@0 | 379 | # Make a subprogram for the function and record it outside the main |
paul@0 | 380 | # tree. |
paul@0 | 381 | |
paul@0 | 382 | subprogram = Subprogram(function, name=function.name, star=None, dstar=None) |
paul@0 | 383 | self.current_subprograms.append(subprogram) |
paul@0 | 384 | subprogram.code = self.dispatch(function.code) |
paul@0 | 385 | self.current_subprograms.pop() |
paul@0 | 386 | self._visitFunction(function, subprogram) |
paul@0 | 387 | |
paul@0 | 388 | # Make a definition of the function associating it with a name. |
paul@0 | 389 | |
paul@0 | 390 | result = Assign(function) |
paul@0 | 391 | load = LoadRef(ref=subprogram) |
paul@0 | 392 | store = StoreName(name=function.name) |
paul@0 | 393 | result.code = [load, store] |
paul@0 | 394 | return result |
paul@0 | 395 | |
paul@0 | 396 | def visitLambda(self, lambda_): |
paul@0 | 397 | |
paul@0 | 398 | # Make a subprogram for the function and record it outside the main |
paul@0 | 399 | # tree. |
paul@0 | 400 | |
paul@0 | 401 | subprogram = Subprogram(lambda_, name=hex(id(lambda_)), star=None, dstar=None) |
paul@0 | 402 | self.current_subprograms.append(subprogram) |
paul@0 | 403 | subprogram.code = [Return(expr=self.dispatch(lambda_.code))] |
paul@0 | 404 | self.current_subprograms.pop() |
paul@0 | 405 | self._visitFunction(lambda_, subprogram) |
paul@0 | 406 | |
paul@0 | 407 | # Get the subprogram reference to the lambda. |
paul@0 | 408 | |
paul@0 | 409 | return LoadRef(ref=subprogram) |
paul@0 | 410 | |
paul@0 | 411 | def visitCallFunc(self, callfunc): |
paul@0 | 412 | result = Invoke(callfunc, same_frame=0, star=None, dstar=None) |
paul@0 | 413 | result.args = self.dispatches(callfunc.args) |
paul@0 | 414 | if callfunc.star_args is not None: |
paul@0 | 415 | result.star = self.dispatch(callfunc.star_args) |
paul@0 | 416 | if callfunc.dstar_args is not None: |
paul@0 | 417 | result.dstar = self.dispatch(callfunc.dstar_args) |
paul@0 | 418 | result.expr = self.dispatch(callfunc.node) |
paul@0 | 419 | return result |
paul@0 | 420 | |
paul@0 | 421 | def visitWhile(self, while_): |
paul@0 | 422 | |
paul@0 | 423 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 424 | |
paul@0 | 425 | subprogram = Subprogram(while_, name=hex(id(while_)), acquire_locals=1, params=[], star=None, dstar=None) |
paul@0 | 426 | self.current_subprograms.append(subprogram) |
paul@0 | 427 | |
paul@0 | 428 | # Include a conditional statement in the subprogram. |
paul@0 | 429 | |
paul@0 | 430 | test = Conditional(else_=[]) |
paul@0 | 431 | test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), |
paul@0 | 432 | params=[], star=None, dstar=None) |
paul@0 | 433 | |
paul@0 | 434 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 435 | # if the test condition was satisfied. |
paul@0 | 436 | |
paul@0 | 437 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 438 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 439 | test.body = self.dispatch(while_.body) + [continuation] |
paul@0 | 440 | if while_.else_ is not None: |
paul@0 | 441 | test.else_ = self.dispatch(while_.else_) |
paul@0 | 442 | subprogram.code = [test] |
paul@0 | 443 | |
paul@0 | 444 | self.current_subprograms.pop() |
paul@0 | 445 | self.subprograms.append(subprogram) |
paul@0 | 446 | |
paul@0 | 447 | # Make an invocation of the subprogram. |
paul@0 | 448 | |
paul@0 | 449 | result = Invoke(while_, same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 450 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 451 | return result |
paul@0 | 452 | |
paul@0 | 453 | def visitFor(self, for_): |
paul@0 | 454 | |
paul@0 | 455 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 456 | |
paul@0 | 457 | subprogram = Subprogram(for_, name=hex(id(for_)), acquire_locals=1, params=[], star=None, dstar=None) |
paul@0 | 458 | self.current_subprograms.append(subprogram) |
paul@0 | 459 | |
paul@0 | 460 | # Wrap the assignment in a try...except statement. |
paul@0 | 461 | |
paul@0 | 462 | try_except = Try(body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 463 | except_spec = Invoke(expr=LoadName(name="Tuple"), params=[LoadName(name="StopIteration")]) |
paul@0 | 464 | stopiteration = Except(spec=except_spec) |
paul@0 | 465 | stopiteration.code = self.dispatch(for_.else_) |
paul@0 | 466 | try_except.handlers = [stopiteration] |
paul@0 | 467 | |
paul@0 | 468 | assign = Assign() |
paul@0 | 469 | assign.code = [ |
paul@0 | 470 | StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paul@0 | 471 | self.dispatch(for_.assign), |
paul@0 | 472 | ReleaseTemp() |
paul@0 | 473 | ] |
paul@0 | 474 | |
paul@0 | 475 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 476 | # if the test condition was satisfied. |
paul@0 | 477 | |
paul@0 | 478 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 479 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 480 | try_except.body = [assign] + self.dispatch(for_.body) + [continuation] |
paul@0 | 481 | subprogram.code = [try_except] |
paul@0 | 482 | |
paul@0 | 483 | self.subprograms.append(subprogram) |
paul@0 | 484 | self.current_subprograms.pop() |
paul@0 | 485 | |
paul@0 | 486 | # Obtain an iterator for the sequence involved. |
paul@0 | 487 | # Then, make an invocation of the subprogram. |
paul@0 | 488 | |
paul@0 | 489 | result = Assign(for_) |
paul@0 | 490 | result.code = [ |
paul@0 | 491 | StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))), |
paul@0 | 492 | Invoke(expr=LoadRef(ref=subprogram), same_frame=1, star=None, dstar=None, args=[]), |
paul@0 | 493 | ReleaseTemp() |
paul@0 | 494 | ] |
paul@0 | 495 | return result |
paul@0 | 496 | |
paul@0 | 497 | # Exception node transformations. |
paul@0 | 498 | |
paul@0 | 499 | def visitTryExcept(self, tryexcept): |
paul@0 | 500 | result = Try(tryexcept, body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 501 | if tryexcept.body is not None: |
paul@0 | 502 | result.body = self.dispatch(tryexcept.body) |
paul@0 | 503 | if tryexcept.else_ is not None: |
paul@0 | 504 | result.else_ = self.dispatch(tryexcept.else_) |
paul@0 | 505 | handlers = [] |
paul@0 | 506 | for spec, assign, stmt in tryexcept.handlers: |
paul@0 | 507 | get_exc = Assign() |
paul@5 | 508 | get_exc.code = [StoreTemp(expr=LoadExc())] |
paul@5 | 509 | if assign is not None: |
paul@5 | 510 | get_exc.code.append(self.dispatch(assign)) |
paul@5 | 511 | get_exc.code.append(ReleaseTemp()) |
paul@5 | 512 | handler = Except() |
paul@5 | 513 | if spec is not None: |
paul@5 | 514 | handler.spec = self.dispatch(spec) |
paul@0 | 515 | handler.code = [get_exc] + self.dispatch(stmt) |
paul@0 | 516 | handlers.append(handler) |
paul@0 | 517 | result.handlers = handlers |
paul@0 | 518 | return result |
paul@0 | 519 | |
paul@0 | 520 | def visitTryFinally(self, tryfinally): |
paul@0 | 521 | result = Try(tryfinally, body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 522 | if tryfinally.body is not None: |
paul@0 | 523 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 524 | if tryfinally.final is not None: |
paul@0 | 525 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 526 | return result |
paul@0 | 527 | |
paul@0 | 528 | # vim: tabstop=4 expandtab shiftwidth=4 |