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