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