paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@24 | 4 | Simplify AST structures for easier type propagation and analysis. The code in |
paulb@24 | 5 | this module processes AST trees originating from the compiler module and |
paulb@24 | 6 | produces a result tree consisting of instruction-oriented program nodes. |
paul@0 | 7 | |
paul@0 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This software is free software; you can redistribute it and/or |
paul@0 | 11 | modify it under the terms of the GNU General Public License as |
paul@0 | 12 | published by the Free Software Foundation; either version 2 of |
paul@0 | 13 | the License, or (at your option) any later version. |
paul@0 | 14 | |
paul@0 | 15 | This software is distributed in the hope that it will be useful, |
paul@0 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 18 | GNU General Public License for more details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public |
paul@0 | 21 | License along with this library; see the file LICENCE.txt |
paul@0 | 22 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paul@0 | 24 | """ |
paul@0 | 25 | |
paulb@19 | 26 | from simplified import * |
paul@0 | 27 | import compiler.ast |
paul@0 | 28 | |
paulb@19 | 29 | class Simplifier(Visitor): |
paul@0 | 30 | |
paul@0 | 31 | """ |
paul@0 | 32 | A simplifying visitor for AST nodes. |
paul@0 | 33 | |
paul@5 | 34 | Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, |
paulb@13 | 35 | CallFunc, Class, Compare, Const, Continue, Dict, Discard, For, |
paulb@13 | 36 | From, Function, Getattr, Global, If, Import, Invert, Keyword, |
paulb@13 | 37 | Lambda, List, Module, Name, Not, Or, Pass, Raise, Return, Slice, |
paulb@13 | 38 | Stmt, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@13 | 39 | UnarySub. |
paul@0 | 40 | |
paulb@13 | 41 | Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div, |
paulb@13 | 42 | Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor, |
paulb@13 | 43 | ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, Sliceobj, |
paulb@13 | 44 | Sub, Yield. |
paul@0 | 45 | """ |
paul@0 | 46 | |
paulb@34 | 47 | def __init__(self, builtins=0): |
paulb@19 | 48 | Visitor.__init__(self) |
paul@1 | 49 | self.result = None # The resulting tree. |
paul@1 | 50 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 51 | self.structures = [] # Structures/classes. |
paulb@24 | 52 | self.constants = {} # Constants. |
paul@1 | 53 | self.current_subprograms = [] # Current subprograms being processed. |
paulb@34 | 54 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 55 | |
paulb@20 | 56 | def process(self, node): |
paulb@20 | 57 | self.visitor = self |
paulb@20 | 58 | return self.dispatch(node) |
paulb@20 | 59 | |
paulb@6 | 60 | def dispatch_or_none(self, node, *args): |
paulb@6 | 61 | if node is not None: |
paulb@6 | 62 | return self.dispatch(node, *args) |
paulb@6 | 63 | else: |
paulb@6 | 64 | return LoadName(name="None") |
paulb@6 | 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) |
paulb@34 | 81 | module_code = self.dispatch(module.node) |
paulb@34 | 82 | |
paulb@34 | 83 | # NOTE: Constant initialisation necessary for annotation but perhaps |
paulb@34 | 84 | # NOTE: redundant in the program. |
paulb@34 | 85 | |
paulb@34 | 86 | init_code = [] |
paulb@34 | 87 | for value, constant in self.constants.items(): |
paulb@34 | 88 | init_code.append(StoreAttr(lvalue=LoadRef(ref=constant), name="__class__", expr=LoadName(name=constant.typename))) |
paulb@34 | 89 | |
paulb@34 | 90 | # NOTE: Hack to ensure correct initialisation of constants. |
paulb@34 | 91 | |
paulb@34 | 92 | if self.builtins: |
paulb@34 | 93 | self.result.code = module_code + init_code |
paulb@34 | 94 | else: |
paulb@34 | 95 | self.result.code = init_code + module_code |
paul@0 | 96 | return self.result |
paul@0 | 97 | |
paul@0 | 98 | def visitGetattr(self, getattr): |
paul@0 | 99 | result = LoadAttr(getattr, name=getattr.attrname) |
paul@0 | 100 | result.expr = self.dispatch(getattr.expr) |
paul@0 | 101 | return result |
paul@0 | 102 | |
paul@0 | 103 | def visitKeyword(self, keyword): |
paul@0 | 104 | result = Keyword(keyword, name=keyword.name) |
paul@0 | 105 | result.expr = self.dispatch(keyword.expr) |
paul@0 | 106 | return result |
paul@0 | 107 | |
paul@1 | 108 | def visitGlobal(self, global_): |
paul@1 | 109 | result = Global(global_, names=global_.names) |
paul@1 | 110 | return result |
paul@1 | 111 | |
paul@3 | 112 | def visitImport(self, import_): |
paul@3 | 113 | result = Assign(import_) |
paul@3 | 114 | code = [] |
paul@3 | 115 | for path, alias in import_.names: |
paul@3 | 116 | importer = Import(name=path) |
paul@3 | 117 | top = alias or path.split(".")[0] |
paul@3 | 118 | code.append(StoreName(expr=importer, name=top)) |
paul@3 | 119 | result.code = code |
paul@3 | 120 | return result |
paul@3 | 121 | |
paul@3 | 122 | def visitFrom(self, from_): |
paul@3 | 123 | result = Assign(from_) |
paul@3 | 124 | code = [] |
paul@3 | 125 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paul@3 | 126 | for name, alias in from_.names: |
paul@3 | 127 | code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name))) |
paul@3 | 128 | result.code = code |
paul@3 | 129 | return result |
paul@3 | 130 | |
paul@0 | 131 | def visitName(self, name): |
paul@0 | 132 | result = LoadName(name, name=name.name) |
paul@0 | 133 | return result |
paul@0 | 134 | |
paul@0 | 135 | def visitConst(self, const): |
paulb@24 | 136 | if not self.constants.has_key(const.value): |
paulb@24 | 137 | self.constants[const.value] = Constant(name=repr(const.value), value=const.value) |
paulb@24 | 138 | result = LoadRef(ref=self.constants[const.value]) |
paul@0 | 139 | return result |
paul@0 | 140 | |
paul@0 | 141 | def visitReturn(self, return_): |
paul@0 | 142 | result = Return(return_) |
paul@0 | 143 | result.expr = self.dispatch(return_.value) |
paul@0 | 144 | return result |
paul@0 | 145 | |
paul@0 | 146 | def visitBreak(self, break_): |
paul@0 | 147 | result = Return(break_) |
paul@0 | 148 | return result |
paul@0 | 149 | |
paul@0 | 150 | def visitContinue(self, continue_): |
paulb@7 | 151 | result = Invoke(continue_, same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 152 | result.expr = LoadRef(ref=self.current_subprograms[-1]) |
paul@0 | 153 | return result |
paul@0 | 154 | |
paul@5 | 155 | def visitRaise(self, raise_): |
paul@5 | 156 | result = Raise(raise_, expr=self.dispatch(raise_.expr1), traceback=None) |
paul@5 | 157 | if raise_.expr2 is not None: |
paul@5 | 158 | result.args = [self.dispatch(raise_.expr2)] |
paul@5 | 159 | if raise_.expr3 is not None: |
paul@5 | 160 | result.traceback = self.dispatch(raise_.expr3) |
paul@5 | 161 | return result |
paul@5 | 162 | |
paul@0 | 163 | def _visitBuiltin(self, builtin, name): |
paul@1 | 164 | result = Invoke(builtin, expr=LoadName(name=name)) |
paul@1 | 165 | result.args = self.dispatches(builtin.nodes) |
paul@0 | 166 | return result |
paul@0 | 167 | |
paul@0 | 168 | def visitTuple(self, tuple): |
paulb@24 | 169 | return self._visitBuiltin(tuple, "tuple") |
paul@0 | 170 | |
paul@0 | 171 | def visitList(self, list): |
paulb@24 | 172 | return self._visitBuiltin(list, "list") |
paul@0 | 173 | |
paul@0 | 174 | def visitDict(self, dict): |
paulb@24 | 175 | result = Invoke(dict, expr=LoadName(name="dict")) |
paul@0 | 176 | args = [] |
paul@0 | 177 | for key, value in dict.items: |
paulb@24 | 178 | tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None) |
paul@0 | 179 | tuple.args = [self.dispatch(key), self.dispatch(value)] |
paul@0 | 180 | args.append(tuple) |
paul@0 | 181 | result.args = args |
paul@0 | 182 | return result |
paul@0 | 183 | |
paulb@17 | 184 | # Logical and comparison operations plus chained statements. |
paulb@17 | 185 | |
paulb@17 | 186 | def visitIf(self, if_): |
paulb@17 | 187 | |
paulb@26 | 188 | """ |
paulb@35 | 189 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@35 | 190 | and putting each subsequent test as part of the conditional's else |
paulb@35 | 191 | section. |
paulb@35 | 192 | |
paulb@35 | 193 | Convert... |
paulb@26 | 194 | |
paulb@35 | 195 | If (test/body) |
paulb@35 | 196 | (test/body) |
paulb@35 | 197 | ... |
paulb@35 | 198 | (else/body) |
paulb@35 | 199 | |
paulb@35 | 200 | ...to: |
paulb@35 | 201 | |
paulb@35 | 202 | Conditional (test) -> (body) |
paulb@35 | 203 | (else) -> Conditional (test) -> (body) |
paulb@35 | 204 | (else) -> ... |
paulb@26 | 205 | """ |
paulb@26 | 206 | |
paulb@17 | 207 | |
paulb@35 | 208 | results = nodes = [] |
paulb@17 | 209 | |
paulb@17 | 210 | for compare, stmt in if_.tests: |
paulb@17 | 211 | # Produce something like... |
paulb@17 | 212 | # expr.__true__() ? body |
paulb@35 | 213 | test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), |
paulb@17 | 214 | args=[], star=None, dstar=None)) |
paulb@35 | 215 | test.body = self.dispatch(stmt) |
paulb@17 | 216 | nodes.append(test) |
paulb@35 | 217 | nodes = test.else_ = [] |
paulb@17 | 218 | |
paulb@17 | 219 | # Add the compound statement from any else clause to the end. |
paulb@17 | 220 | |
paulb@17 | 221 | if if_.else_ is not None: |
paulb@35 | 222 | nodes += self.dispatch(if_.else_) |
paulb@34 | 223 | |
paulb@35 | 224 | result = results[0] |
paulb@17 | 225 | return result |
paulb@17 | 226 | |
paulb@35 | 227 | def visitTryExcept(self, tryexcept): |
paulb@35 | 228 | |
paulb@35 | 229 | """ |
paulb@35 | 230 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@35 | 231 | |
paulb@35 | 232 | Convert... |
paulb@17 | 233 | |
paulb@35 | 234 | TryExcept (body) |
paulb@35 | 235 | (else) |
paulb@35 | 236 | (spec/assign/stmt) |
paulb@35 | 237 | ... |
paulb@35 | 238 | |
paulb@35 | 239 | ...to: |
paulb@17 | 240 | |
paulb@35 | 241 | Try (body) |
paulb@35 | 242 | (else) |
paulb@35 | 243 | (handler) -> Conditional (test) -> (stmt) |
paulb@35 | 244 | (else) -> Conditional (test) -> (stmt) |
paulb@35 | 245 | (else) -> ... |
paulb@35 | 246 | """ |
paulb@35 | 247 | |
paulb@35 | 248 | result = Try(tryexcept, body=[], else_=[], finally_=[]) |
paulb@17 | 249 | |
paulb@35 | 250 | if tryexcept.body is not None: |
paulb@35 | 251 | result.body = self.dispatch(tryexcept.body) |
paulb@35 | 252 | if tryexcept.else_ is not None: |
paulb@35 | 253 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@17 | 254 | |
paulb@35 | 255 | results = nodes = [] |
paulb@17 | 256 | for spec, assign, stmt in tryexcept.handlers: |
paulb@17 | 257 | |
paulb@17 | 258 | # If no specification exists, produce an unconditional block. |
paulb@17 | 259 | |
paulb@17 | 260 | if spec is None: |
paulb@17 | 261 | nodes += self.dispatch(stmt) |
paulb@17 | 262 | |
paulb@17 | 263 | # Produce something like... |
paulb@17 | 264 | # isinstance(<exc>, <spec>) |
paulb@17 | 265 | |
paulb@17 | 266 | else: |
paulb@17 | 267 | new_spec = self.dispatch(spec) |
paulb@35 | 268 | test = Conditional(test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), new_spec], star=None, dstar=None)) |
paulb@35 | 269 | test.body = [] |
paulb@35 | 270 | |
paulb@17 | 271 | if assign is not None: |
paulb@17 | 272 | test.body.append(Assign(code=[StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()])) |
paulb@34 | 273 | |
paulb@34 | 274 | # Always return from conditional sections. |
paulb@34 | 275 | |
paulb@17 | 276 | test.body += self.dispatch(stmt) + [Return()] |
paulb@17 | 277 | nodes.append(test) |
paulb@35 | 278 | nodes = test.else_ = [] |
paulb@17 | 279 | |
paulb@17 | 280 | # Add a raise operation to deal with unhandled exceptions. |
paulb@17 | 281 | |
paulb@17 | 282 | nodes.append(Raise(expr=LoadExc())) |
paulb@17 | 283 | |
paulb@35 | 284 | result.handler = results |
paulb@17 | 285 | return result |
paulb@13 | 286 | |
paulb@13 | 287 | comparison_methods = { |
paulb@13 | 288 | "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", |
paulb@13 | 289 | ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None |
paulb@13 | 290 | } |
paulb@13 | 291 | |
paulb@13 | 292 | def visitCompare(self, compare): |
paulb@13 | 293 | |
paulb@35 | 294 | """ |
paulb@35 | 295 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@35 | 296 | the subprogram. Convert... |
paulb@35 | 297 | |
paulb@35 | 298 | Compare (expr) |
paulb@35 | 299 | (name/node) |
paulb@35 | 300 | ... |
paulb@35 | 301 | |
paulb@35 | 302 | ...to: |
paulb@35 | 303 | |
paulb@35 | 304 | Subprogram -> Conditional (test) -> (body) |
paulb@35 | 305 | (else) -> Conditional (test) -> (body) |
paulb@35 | 306 | (else) -> ... |
paulb@35 | 307 | """ |
paulb@13 | 308 | |
paulb@28 | 309 | subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@13 | 310 | self.current_subprograms.append(subprogram) |
paulb@13 | 311 | |
paulb@13 | 312 | # In the subprogram, make instructions which invoke a method on the |
paulb@13 | 313 | # first operand of each operand pair and, if appropriate, return with |
paulb@13 | 314 | # the value from that method. |
paulb@13 | 315 | |
paulb@13 | 316 | last = compare.ops[-1] |
paulb@13 | 317 | previous = self.dispatch(compare.expr) |
paulb@35 | 318 | results = nodes = [] |
paulb@35 | 319 | |
paulb@13 | 320 | for op in compare.ops: |
paulb@13 | 321 | op_name, node = op |
paulb@13 | 322 | expr = self.dispatch(node) |
paulb@35 | 323 | |
paulb@35 | 324 | # Identify the operation and produce the appropriate method call. |
paulb@35 | 325 | |
paulb@13 | 326 | method_name = self.comparison_methods[op_name] |
paulb@13 | 327 | if method_name: |
paulb@15 | 328 | invocation = Invoke(expr=LoadAttr(expr=previous, name=method_name), args=[expr], star=None, dstar=None) |
paulb@13 | 329 | elif op_name == "is": |
paulb@15 | 330 | invocation = Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None) |
paulb@13 | 331 | elif op_name == "is not": |
paulb@15 | 332 | invocation = Not(expr=Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None)) |
paulb@13 | 333 | else: |
paulb@13 | 334 | raise NotImplementedError, op_name |
paulb@13 | 335 | nodes.append(StoreTemp(expr=invocation)) |
paulb@34 | 336 | |
paulb@35 | 337 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 338 | |
paulb@13 | 339 | if op is not last: |
paulb@35 | 340 | test = Conditional(test=Not(expr=LoadTemp()), body=[Return(expr=LoadTemp())]) |
paulb@35 | 341 | nodes.append(test) |
paulb@35 | 342 | |
paulb@35 | 343 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 344 | |
paulb@35 | 345 | nodes = test.else_ = [ReleaseTemp()] |
paulb@35 | 346 | |
paulb@35 | 347 | # For the last operation, return the result. |
paulb@35 | 348 | |
paulb@13 | 349 | else: |
paulb@13 | 350 | nodes.append(Return(expr=LoadTemp())) |
paulb@35 | 351 | |
paulb@15 | 352 | previous = expr |
paulb@35 | 353 | |
paulb@35 | 354 | # Finish the subprogram definition. |
paulb@35 | 355 | |
paulb@35 | 356 | subprogram.code = results |
paulb@13 | 357 | |
paulb@13 | 358 | self.current_subprograms.pop() |
paulb@13 | 359 | self.subprograms.append(subprogram) |
paulb@13 | 360 | |
paulb@13 | 361 | # Make an invocation of the subprogram. |
paulb@13 | 362 | |
paulb@13 | 363 | result = Invoke(compare, same_frame=1, star=None, dstar=None, args=[]) |
paulb@13 | 364 | result.expr = LoadRef(ref=subprogram) |
paulb@13 | 365 | return result |
paul@1 | 366 | |
paul@1 | 367 | def visitAnd(self, and_): |
paulb@7 | 368 | |
paulb@35 | 369 | """ |
paulb@35 | 370 | Make a subprogram for the 'and_' node and record its contents inside the |
paulb@35 | 371 | subprogram. Convert... |
paulb@35 | 372 | |
paulb@35 | 373 | And (test) |
paulb@35 | 374 | (test) |
paulb@35 | 375 | ... |
paulb@35 | 376 | |
paulb@35 | 377 | ...to: |
paulb@35 | 378 | |
paulb@35 | 379 | Subprogram -> Conditional (test) -> Return ... |
paulb@35 | 380 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 381 | (else) -> ... |
paulb@35 | 382 | """ |
paulb@7 | 383 | |
paulb@28 | 384 | subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 385 | self.current_subprograms.append(subprogram) |
paulb@7 | 386 | |
paulb@12 | 387 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 388 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 389 | # subprogram with the value of the operand. |
paulb@12 | 390 | |
paulb@7 | 391 | last = and_.nodes[-1] |
paulb@35 | 392 | results = nodes = [] |
paulb@35 | 393 | |
paul@1 | 394 | for node in and_.nodes: |
paulb@7 | 395 | expr = self.dispatch(node) |
paulb@34 | 396 | |
paulb@35 | 397 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 398 | |
paulb@7 | 399 | if node is not last: |
paulb@12 | 400 | nodes.append(StoreTemp(expr=expr)) |
paulb@15 | 401 | invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) |
paulb@35 | 402 | test = Conditional(test=Not(expr=invocation), body=[Return(expr=LoadTemp())]) |
paulb@35 | 403 | nodes.append(test) |
paulb@35 | 404 | |
paulb@35 | 405 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 406 | |
paulb@35 | 407 | nodes = test.else_ = [ReleaseTemp()] |
paulb@35 | 408 | |
paulb@35 | 409 | # For the last operation, return the result. |
paulb@35 | 410 | |
paulb@7 | 411 | else: |
paulb@7 | 412 | nodes.append(Return(expr=expr)) |
paulb@35 | 413 | |
paulb@35 | 414 | # Finish the subprogram definition. |
paulb@35 | 415 | |
paulb@35 | 416 | subprogram.code = results |
paulb@7 | 417 | |
paulb@7 | 418 | self.current_subprograms.pop() |
paulb@7 | 419 | self.subprograms.append(subprogram) |
paulb@7 | 420 | |
paulb@7 | 421 | # Make an invocation of the subprogram. |
paulb@7 | 422 | |
paulb@7 | 423 | result = Invoke(and_, same_frame=1, star=None, dstar=None, args=[]) |
paulb@7 | 424 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 425 | return result |
paul@1 | 426 | |
paul@1 | 427 | def visitOr(self, or_): |
paulb@7 | 428 | |
paulb@35 | 429 | """ |
paulb@35 | 430 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@35 | 431 | subprogram. Convert... |
paulb@35 | 432 | |
paulb@35 | 433 | Or (test) |
paulb@35 | 434 | (test) |
paulb@35 | 435 | ... |
paulb@35 | 436 | |
paulb@35 | 437 | ...to: |
paulb@35 | 438 | |
paulb@35 | 439 | Subprogram -> Conditional (test) -> Return ... |
paulb@35 | 440 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 441 | (else) -> ... |
paulb@35 | 442 | """ |
paulb@7 | 443 | |
paulb@28 | 444 | subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 445 | self.current_subprograms.append(subprogram) |
paulb@7 | 446 | |
paulb@12 | 447 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 448 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 449 | # subprogram with the value of the operand. |
paulb@12 | 450 | |
paulb@7 | 451 | last = or_.nodes[-1] |
paulb@35 | 452 | results = nodes = [] |
paulb@35 | 453 | |
paul@1 | 454 | for node in or_.nodes: |
paulb@7 | 455 | expr = self.dispatch(node) |
paulb@34 | 456 | |
paulb@35 | 457 | # Return from the subprogram where the test is satisfied. |
paulb@34 | 458 | |
paulb@7 | 459 | if node is not last: |
paulb@12 | 460 | nodes.append(StoreTemp(expr=expr)) |
paulb@15 | 461 | invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) |
paulb@35 | 462 | test = Conditional(test=invocation, body=[Return(expr=LoadTemp())]) |
paulb@35 | 463 | nodes.append(test) |
paulb@35 | 464 | |
paulb@35 | 465 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 466 | |
paulb@35 | 467 | nodes = test.else_ = [ReleaseTemp()] |
paulb@35 | 468 | |
paulb@35 | 469 | # For the last operation, return the result. |
paulb@35 | 470 | |
paulb@7 | 471 | else: |
paulb@7 | 472 | nodes.append(Return(expr=expr)) |
paulb@35 | 473 | |
paulb@35 | 474 | # Finish the subprogram definition. |
paulb@35 | 475 | |
paulb@35 | 476 | subprogram.code = results |
paulb@7 | 477 | |
paulb@7 | 478 | self.current_subprograms.pop() |
paulb@7 | 479 | self.subprograms.append(subprogram) |
paulb@7 | 480 | |
paulb@7 | 481 | # Make an invocation of the subprogram. |
paulb@7 | 482 | |
paulb@7 | 483 | result = Invoke(or_, same_frame=1, star=None, dstar=None, args=[]) |
paulb@7 | 484 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 485 | return result |
paul@1 | 486 | |
paul@1 | 487 | def visitNot(self, not_): |
paul@1 | 488 | result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), |
paulb@15 | 489 | args=[], star=None, dstar=None)) |
paul@1 | 490 | return result |
paul@1 | 491 | |
paul@4 | 492 | # Operators. |
paul@4 | 493 | |
paul@4 | 494 | def visitUnaryAdd(self, unaryadd): |
paulb@11 | 495 | return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[]) |
paul@4 | 496 | |
paul@4 | 497 | def visitUnarySub(self, unarysub): |
paulb@11 | 498 | return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[]) |
paul@4 | 499 | |
paul@4 | 500 | def visitInvert(self, invert): |
paulb@11 | 501 | return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[]) |
paul@4 | 502 | |
paulb@32 | 503 | def visitAdd(self, add): |
paulb@32 | 504 | |
paulb@32 | 505 | # NOTE: Strictly one of the choices must be evaluated, never more than one. |
paulb@32 | 506 | |
paulb@32 | 507 | result = Choice(add) |
paulb@32 | 508 | result.choices = [ |
paulb@32 | 509 | Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)]), |
paulb@32 | 510 | Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)]) |
paulb@32 | 511 | ] |
paulb@32 | 512 | return result |
paulb@32 | 513 | |
paul@0 | 514 | # Assignments. |
paul@0 | 515 | |
paul@2 | 516 | augassign_methods = { |
paulb@9 | 517 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 518 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 519 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 520 | } |
paul@2 | 521 | |
paul@2 | 522 | def visitAugAssign(self, augassign): |
paul@2 | 523 | result = Assign(augassign) |
paul@2 | 524 | expr = self.dispatch(augassign.expr) |
paul@2 | 525 | |
paul@2 | 526 | # Simple augmented assignment: name += expr |
paul@2 | 527 | |
paul@2 | 528 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@6 | 529 | name = augassign.node |
paulb@6 | 530 | node = self.dispatch(name) |
paul@2 | 531 | get_incremented = StoreTemp( |
paul@2 | 532 | expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 533 | ) |
paulb@6 | 534 | store = StoreName(expr=LoadTemp(), name=name.name) |
paul@2 | 535 | result.code = [get_incremented, store, ReleaseTemp()] |
paul@2 | 536 | |
paulb@9 | 537 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 538 | |
paul@2 | 539 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 540 | |
paulb@9 | 541 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 542 | |
paulb@6 | 543 | getattr = augassign.node |
paulb@6 | 544 | store_expr = StoreTemp(index="expr", expr=self.dispatch(getattr.expr)) |
paulb@6 | 545 | node_attr = LoadAttr(expr=LoadTemp(index="expr"), name=getattr.attrname) |
paul@2 | 546 | get_incremented = StoreTemp( |
paul@2 | 547 | expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 548 | ) |
paulb@6 | 549 | store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index="expr"), name=getattr.attrname) |
paulb@6 | 550 | result.code = [store_expr, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp()] |
paulb@6 | 551 | |
paulb@9 | 552 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 553 | |
paulb@6 | 554 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 555 | |
paulb@9 | 556 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 557 | |
paulb@6 | 558 | slice = augassign.node |
paulb@6 | 559 | store_expr = StoreTemp(index="expr", expr=self.dispatch(slice.expr)) |
paulb@6 | 560 | store_lower = StoreTemp(index="lower", expr=self.dispatch_or_none(slice.lower)) |
paulb@6 | 561 | store_upper = StoreTemp(index="upper", expr=self.dispatch_or_none(slice.upper)) |
paulb@11 | 562 | node_slice = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY") |
paulb@6 | 563 | get_incremented = StoreTemp( |
paulb@6 | 564 | expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr]) |
paulb@6 | 565 | ) |
paulb@11 | 566 | store = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", LoadTemp()) |
paulb@6 | 567 | result.code = [store_expr, store_lower, store_upper, get_incremented, store, |
paulb@6 | 568 | ReleaseTemp(index="expr"), ReleaseTemp(index="lower"), ReleaseTemp(index="upper"), ReleaseTemp()] |
paul@2 | 569 | |
paulb@10 | 570 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 571 | |
paulb@10 | 572 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 573 | |
paulb@10 | 574 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 575 | |
paulb@10 | 576 | subscript = augassign.node |
paulb@10 | 577 | store_expr = StoreTemp(index="expr", expr=self.dispatch(subscript.expr)) |
paulb@10 | 578 | subs = self._visitSubscriptSubs(subscript.subs) |
paulb@10 | 579 | store_subs = StoreTemp(index="subs", expr=subs) |
paulb@11 | 580 | node_subscript = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY") |
paulb@10 | 581 | get_incremented = StoreTemp( |
paulb@10 | 582 | expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr]) |
paulb@10 | 583 | ) |
paulb@11 | 584 | store = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp()) |
paulb@10 | 585 | result.code = [store_expr, store_subs, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp(index="subs"), ReleaseTemp()] |
paulb@10 | 586 | |
paul@2 | 587 | else: |
paul@2 | 588 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 589 | |
paul@2 | 590 | return result |
paul@2 | 591 | |
paul@0 | 592 | def visitAssign(self, assign): |
paul@0 | 593 | result = Assign(assign) |
paul@0 | 594 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paul@0 | 595 | release = ReleaseTemp() |
paul@0 | 596 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paul@0 | 597 | return result |
paul@0 | 598 | |
paul@0 | 599 | def visitAssList(self, asslist, in_sequence=0): |
paul@0 | 600 | if not in_sequence: |
paulb@11 | 601 | expr = LoadTemp() |
paul@0 | 602 | else: |
paulb@11 | 603 | expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 604 | result = Assign(asslist) |
paul@0 | 605 | store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr))) |
paul@0 | 606 | release = ReleaseTemp() |
paul@0 | 607 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paul@0 | 608 | return result |
paul@0 | 609 | |
paul@0 | 610 | visitAssTuple = visitAssList |
paul@0 | 611 | |
paul@0 | 612 | def _visitAssNameOrAttr(self, node, in_sequence): |
paul@0 | 613 | if not in_sequence: |
paulb@11 | 614 | return LoadTemp() |
paul@0 | 615 | else: |
paulb@11 | 616 | return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 617 | |
paul@0 | 618 | def visitAssName(self, assname, in_sequence=0): |
paul@0 | 619 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paul@0 | 620 | result = StoreName(assname, name=assname.name, expr=expr) |
paul@0 | 621 | return result |
paul@0 | 622 | |
paul@0 | 623 | def visitAssAttr(self, assattr, in_sequence=0): |
paul@0 | 624 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paul@0 | 625 | lvalue = self.dispatch(assattr.expr) |
paul@0 | 626 | result = StoreAttr(assattr, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@0 | 627 | return result |
paul@0 | 628 | |
paulb@11 | 629 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@6 | 630 | if flags == "OP_ASSIGN": |
paulb@6 | 631 | args = [value] |
paulb@6 | 632 | result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__")) |
paulb@6 | 633 | elif flags == "OP_APPLY": |
paulb@6 | 634 | args = [] |
paulb@6 | 635 | result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__")) |
paulb@10 | 636 | elif flags == "OP_DELETE": |
paulb@10 | 637 | args = [] |
paulb@10 | 638 | result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__")) |
paulb@6 | 639 | else: |
paulb@6 | 640 | raise NotImplementedError, flags |
paulb@6 | 641 | |
paulb@6 | 642 | # Add the dimensions. |
paulb@6 | 643 | |
paulb@6 | 644 | args.insert(0, lower) |
paulb@6 | 645 | args.insert(1, upper) |
paulb@6 | 646 | |
paulb@11 | 647 | result.original = slice |
paulb@6 | 648 | result.args = args |
paulb@6 | 649 | return result |
paulb@6 | 650 | |
paulb@6 | 651 | def visitSlice(self, slice, in_sequence=0): |
paulb@6 | 652 | value = self._visitAssNameOrAttr(slice, in_sequence) |
paulb@11 | 653 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@11 | 654 | self.dispatch_or_none(slice.upper), slice.flags, value) |
paulb@6 | 655 | |
paulb@11 | 656 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@10 | 657 | if flags == "OP_ASSIGN": |
paulb@10 | 658 | args = [value] |
paulb@10 | 659 | result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__")) |
paulb@10 | 660 | elif flags == "OP_APPLY": |
paulb@10 | 661 | args = [] |
paulb@10 | 662 | result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__")) |
paulb@10 | 663 | elif flags == "OP_DELETE": |
paulb@10 | 664 | args = [] |
paulb@10 | 665 | result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__")) |
paulb@10 | 666 | else: |
paulb@10 | 667 | raise NotImplementedError, flags |
paulb@10 | 668 | |
paulb@10 | 669 | # Add the dimensions. |
paulb@10 | 670 | |
paulb@10 | 671 | args.insert(0, subs) |
paulb@10 | 672 | |
paulb@11 | 673 | result.original = subscript |
paulb@10 | 674 | result.args = args |
paulb@10 | 675 | return result |
paulb@10 | 676 | |
paulb@10 | 677 | def _visitSubscriptSubs(self, subs): |
paulb@10 | 678 | if len(subs) == 1: |
paulb@10 | 679 | return self.dispatch(subs[0]) |
paulb@10 | 680 | else: |
paulb@24 | 681 | return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs)) |
paulb@10 | 682 | |
paulb@10 | 683 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@10 | 684 | value = self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@10 | 685 | subs = self._visitSubscriptSubs(subscript.subs) |
paulb@11 | 686 | return self._visitSubscript(subscript, self.dispatch(subscript.expr), subs, subscript.flags, value) |
paulb@10 | 687 | |
paul@0 | 688 | # Invocation and subprogram transformations. |
paul@0 | 689 | |
paulb@19 | 690 | def visitClass(self, class_): |
paulb@28 | 691 | structure = Class(name=class_.name, bases=self.dispatches(class_.bases)) |
paulb@19 | 692 | self.structures.append(structure) |
paulb@19 | 693 | |
paulb@24 | 694 | # Make a subprogram which initialises the class structure. |
paulb@24 | 695 | |
paulb@34 | 696 | subprogram = Subprogram(name=None, acquire_locals=1, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 697 | self.current_subprograms.append(subprogram) |
paulb@19 | 698 | |
paulb@28 | 699 | # The class is initialised using the code found inside. |
paulb@28 | 700 | |
paulb@34 | 701 | subprogram.code = self.dispatch(class_.code) + [Return()] |
paulb@19 | 702 | |
paulb@19 | 703 | self.current_subprograms.pop() |
paulb@19 | 704 | self.subprograms.append(subprogram) |
paulb@19 | 705 | |
paulb@19 | 706 | # Make a definition of the class associating it with a name. |
paulb@19 | 707 | |
paulb@19 | 708 | result = Assign(class_) |
paulb@31 | 709 | store = StoreName(name=class_.name, expr=LoadRef(ref=structure)) |
paulb@19 | 710 | init = Invoke(expr=LoadRef(ref=subprogram), args=[], star=None, dstar=None) |
paulb@31 | 711 | result.code = [store, init] |
paulb@19 | 712 | return result |
paulb@19 | 713 | |
paul@0 | 714 | def _visitFunction(self, function, subprogram): |
paul@0 | 715 | if function.flags & 4 != 0: has_star = 1 |
paul@0 | 716 | else: has_star = 0 |
paul@0 | 717 | if function.flags & 8 != 0: has_dstar = 1 |
paul@0 | 718 | else: has_dstar = 0 |
paul@0 | 719 | ndefaults = len(function.defaults) |
paul@0 | 720 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@24 | 721 | |
paulb@24 | 722 | # Produce star and dstar parameters with appropriate defaults. |
paulb@24 | 723 | |
paulb@24 | 724 | if has_star: |
paulb@24 | 725 | star = (function.argnames[npositional], Invoke(expr=LoadName(name="list"), args=[], star=None, dstar=None)) |
paulb@24 | 726 | else: |
paulb@24 | 727 | star = None |
paulb@24 | 728 | if has_dstar: |
paulb@24 | 729 | dstar = (function.argnames[npositional + has_star], Invoke(expr=LoadName(name="dict"), args=[], star=None, dstar=None)) |
paulb@24 | 730 | else: |
paulb@24 | 731 | dstar = None |
paul@0 | 732 | |
paul@0 | 733 | params = [] |
paul@0 | 734 | for i in range(0, npositional - ndefaults): |
paul@0 | 735 | params.append((function.argnames[i], None)) |
paul@0 | 736 | |
paul@0 | 737 | # NOTE: Fix/process defaults. |
paul@0 | 738 | |
paul@0 | 739 | for i in range(0, ndefaults): |
paul@0 | 740 | default = function.defaults[i] |
paul@0 | 741 | if default is not None: |
paul@0 | 742 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paul@0 | 743 | else: |
paul@0 | 744 | params.append((function.argnames[npositional - ndefaults + i], default)) |
paul@0 | 745 | |
paul@0 | 746 | subprogram.params = params |
paul@0 | 747 | subprogram.star = star |
paul@0 | 748 | subprogram.dstar = dstar |
paul@0 | 749 | self.subprograms.append(subprogram) |
paul@0 | 750 | |
paul@0 | 751 | def visitFunction(self, function): |
paul@0 | 752 | |
paul@0 | 753 | # Make a subprogram for the function and record it outside the main |
paul@0 | 754 | # tree. |
paul@0 | 755 | |
paulb@34 | 756 | subprogram = Subprogram(name=function.name, acquire_locals=0, returns_value=1, star=None, dstar=None) |
paul@0 | 757 | self.current_subprograms.append(subprogram) |
paulb@34 | 758 | subprogram.code = self.dispatch(function.code) + [Return()] |
paul@0 | 759 | self.current_subprograms.pop() |
paul@0 | 760 | self._visitFunction(function, subprogram) |
paul@0 | 761 | |
paul@0 | 762 | # Make a definition of the function associating it with a name. |
paul@0 | 763 | |
paulb@19 | 764 | result = StoreName(name=function.name, expr=LoadRef(ref=subprogram)) |
paul@0 | 765 | return result |
paul@0 | 766 | |
paul@0 | 767 | def visitLambda(self, lambda_): |
paul@0 | 768 | |
paul@0 | 769 | # Make a subprogram for the function and record it outside the main |
paul@0 | 770 | # tree. |
paul@0 | 771 | |
paulb@34 | 772 | subprogram = Subprogram(name=None, acquire_locals=0, returns_value=1, star=None, dstar=None) |
paul@0 | 773 | self.current_subprograms.append(subprogram) |
paul@0 | 774 | subprogram.code = [Return(expr=self.dispatch(lambda_.code))] |
paul@0 | 775 | self.current_subprograms.pop() |
paul@0 | 776 | self._visitFunction(lambda_, subprogram) |
paul@0 | 777 | |
paul@0 | 778 | # Get the subprogram reference to the lambda. |
paul@0 | 779 | |
paulb@11 | 780 | return LoadRef(lambda_, ref=subprogram) |
paul@0 | 781 | |
paul@0 | 782 | def visitCallFunc(self, callfunc): |
paul@0 | 783 | result = Invoke(callfunc, same_frame=0, star=None, dstar=None) |
paul@0 | 784 | result.args = self.dispatches(callfunc.args) |
paul@0 | 785 | if callfunc.star_args is not None: |
paul@0 | 786 | result.star = self.dispatch(callfunc.star_args) |
paul@0 | 787 | if callfunc.dstar_args is not None: |
paul@0 | 788 | result.dstar = self.dispatch(callfunc.dstar_args) |
paul@0 | 789 | result.expr = self.dispatch(callfunc.node) |
paul@0 | 790 | return result |
paul@0 | 791 | |
paul@0 | 792 | def visitWhile(self, while_): |
paul@0 | 793 | |
paulb@35 | 794 | """ |
paulb@35 | 795 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@35 | 796 | subprogram. Convert... |
paulb@35 | 797 | |
paulb@35 | 798 | While (test) -> (body) |
paulb@35 | 799 | (else) |
paulb@35 | 800 | |
paulb@35 | 801 | ...to: |
paulb@35 | 802 | |
paulb@35 | 803 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@35 | 804 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 805 | (else) -> ... |
paulb@35 | 806 | """ |
paul@0 | 807 | |
paulb@28 | 808 | subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 809 | self.current_subprograms.append(subprogram) |
paul@0 | 810 | |
paul@0 | 811 | # Include a conditional statement in the subprogram. |
paul@0 | 812 | |
paul@0 | 813 | test = Conditional(else_=[]) |
paul@0 | 814 | test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), |
paulb@17 | 815 | args=[], star=None, dstar=None) |
paul@0 | 816 | |
paul@0 | 817 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 818 | # if the test condition was satisfied. |
paul@0 | 819 | |
paul@0 | 820 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 821 | continuation.expr = LoadRef(ref=subprogram) |
paulb@34 | 822 | |
paulb@35 | 823 | # Return within the main section of the loop. |
paulb@34 | 824 | |
paulb@34 | 825 | test.body = self.dispatch(while_.body) + [continuation, Return()] |
paulb@35 | 826 | |
paulb@35 | 827 | # Provide the else section, if present, along with an explicit return. |
paulb@35 | 828 | |
paul@0 | 829 | if while_.else_ is not None: |
paulb@35 | 830 | test.else_ = self.dispatch(while_.else_) + [Return()] |
paulb@35 | 831 | |
paulb@35 | 832 | # Finish the subprogram definition. |
paulb@35 | 833 | |
paulb@36 | 834 | subprogram.code = [test] |
paul@0 | 835 | |
paul@0 | 836 | self.current_subprograms.pop() |
paul@0 | 837 | self.subprograms.append(subprogram) |
paul@0 | 838 | |
paul@0 | 839 | # Make an invocation of the subprogram. |
paul@0 | 840 | |
paulb@7 | 841 | result = Invoke(while_, same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 842 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 843 | return result |
paul@0 | 844 | |
paul@0 | 845 | def visitFor(self, for_): |
paul@0 | 846 | |
paulb@35 | 847 | """ |
paulb@35 | 848 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 849 | subprogram. Convert... |
paulb@35 | 850 | |
paulb@35 | 851 | For (assign) |
paulb@35 | 852 | (body) |
paulb@35 | 853 | (else) |
paulb@35 | 854 | |
paulb@35 | 855 | ...to: |
paulb@35 | 856 | |
paulb@35 | 857 | Assign (assign #1) |
paulb@35 | 858 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 859 | (body) |
paulb@35 | 860 | Invoke subprogram |
paulb@35 | 861 | (handler) -> ... |
paulb@35 | 862 | (else) -> ... |
paulb@35 | 863 | """ |
paul@0 | 864 | |
paulb@28 | 865 | subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 866 | self.current_subprograms.append(subprogram) |
paul@0 | 867 | |
paulb@34 | 868 | # Always return from conditional sections/subprograms. |
paulb@34 | 869 | |
paulb@17 | 870 | if for_.else_ is not None: |
paulb@34 | 871 | else_stmt = self.dispatch(for_.else_) + [Return()] |
paulb@17 | 872 | else: |
paulb@34 | 873 | else_stmt = [Return()] |
paulb@17 | 874 | |
paul@0 | 875 | # Wrap the assignment in a try...except statement. |
paul@0 | 876 | |
paulb@17 | 877 | try_except = Try(body=[], else_=[], finally_=[]) |
paulb@36 | 878 | test = Conditional( |
paulb@35 | 879 | test=Invoke( |
paulb@35 | 880 | expr=LoadName(name="isinstance"), |
paulb@35 | 881 | args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None |
paulb@35 | 882 | ), |
paulb@35 | 883 | body=else_stmt, |
paulb@35 | 884 | else_=[Raise(expr=LoadExc())]) |
paulb@36 | 885 | try_except.handler = [test] |
paul@0 | 886 | |
paul@0 | 887 | assign = Assign() |
paul@0 | 888 | assign.code = [ |
paul@0 | 889 | StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paul@0 | 890 | self.dispatch(for_.assign), |
paul@0 | 891 | ReleaseTemp() |
paul@0 | 892 | ] |
paul@0 | 893 | |
paul@0 | 894 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 895 | # if the test condition was satisfied. |
paul@0 | 896 | |
paulb@7 | 897 | continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 898 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 899 | try_except.body = [assign] + self.dispatch(for_.body) + [continuation] |
paulb@34 | 900 | subprogram.code = [try_except, Return()] |
paul@0 | 901 | |
paulb@35 | 902 | # Finish the subprogram definition. |
paulb@35 | 903 | |
paul@0 | 904 | self.subprograms.append(subprogram) |
paul@0 | 905 | self.current_subprograms.pop() |
paul@0 | 906 | |
paul@0 | 907 | # Obtain an iterator for the sequence involved. |
paul@0 | 908 | # Then, make an invocation of the subprogram. |
paul@0 | 909 | |
paul@0 | 910 | result = Assign(for_) |
paul@0 | 911 | result.code = [ |
paul@0 | 912 | StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))), |
paulb@7 | 913 | Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]), |
paul@0 | 914 | ReleaseTemp() |
paul@0 | 915 | ] |
paul@0 | 916 | return result |
paul@0 | 917 | |
paul@0 | 918 | # Exception node transformations. |
paul@0 | 919 | |
paul@0 | 920 | def visitTryFinally(self, tryfinally): |
paulb@32 | 921 | result = Try(tryfinally, body=[], else_=[], finally_=[]) |
paul@0 | 922 | if tryfinally.body is not None: |
paul@0 | 923 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 924 | if tryfinally.final is not None: |
paul@0 | 925 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 926 | return result |
paul@0 | 927 | |
paul@0 | 928 | # vim: tabstop=4 expandtab shiftwidth=4 |