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