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