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