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