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