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