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