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