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 | |
paulb@178 | 8 | Copyright (C) 2006, 2007 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@202 | 50 | Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign, |
paulb@203 | 51 | AugAssign, Bitand, Break, CallFunc, Class, Compare, Const, |
paulb@203 | 52 | Continue, Dict, Discard, Div, FloorDiv, For, From, Function, |
paulb@203 | 53 | Getattr, Global, If, Import, Invert, Keyword, Lambda, List, |
paulb@203 | 54 | ListComp, ListCompFor, ListCompIf, Mod, Module, Mul, Name, Not, Or, |
paulb@203 | 55 | Pass, Power, Print, Printnl, Raise, Return, Slice, Sliceobj, Stmt, |
paulb@203 | 56 | Sub, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@203 | 57 | UnarySub. |
paul@0 | 58 | |
paulb@203 | 59 | Missing: Backquote, Bitor, Bitxor, Decorators, Ellipsis, Exec, LeftShift, |
paulb@203 | 60 | RightShift, 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@120 | 76 | self.current_structures = [] # Current structures being processed. |
paulb@172 | 77 | self.within_class = 0 # Whether a class is being defined. |
paulb@34 | 78 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 79 | |
paulb@65 | 80 | # Convenience attributes. |
paulb@65 | 81 | |
paulb@65 | 82 | self.subnames = {} |
paulb@65 | 83 | |
paulb@51 | 84 | # For compiler package mechanisms. |
paulb@51 | 85 | |
paulb@51 | 86 | self.visitor = self |
paulb@51 | 87 | |
paulb@88 | 88 | def process(self, node, name): |
paulb@88 | 89 | result = self.dispatch(node, name) |
paulb@57 | 90 | result.simplifier = self |
paulb@57 | 91 | return result |
paulb@20 | 92 | |
paulb@6 | 93 | def dispatch_or_none(self, node, *args): |
paulb@6 | 94 | if node is not None: |
paulb@6 | 95 | return self.dispatch(node, *args) |
paulb@6 | 96 | else: |
paulb@79 | 97 | return LoadName(node, name="None") |
paulb@6 | 98 | |
paulb@135 | 99 | # Top-level transformation. |
paul@0 | 100 | |
paulb@88 | 101 | def visitModule(self, module, name=None): |
paulb@46 | 102 | |
paulb@46 | 103 | """ |
paulb@46 | 104 | Process the given 'module', producing a Module object which contains the |
paulb@88 | 105 | resulting program nodes. If the optional 'name' is provided, the 'name' |
paulb@88 | 106 | attribute is set on the Module object using a value other than None. |
paulb@46 | 107 | """ |
paulb@46 | 108 | |
paulb@118 | 109 | result = self.module = Module(module, 1, name=name) |
paulb@34 | 110 | module_code = self.dispatch(module.node) |
paulb@34 | 111 | |
paulb@34 | 112 | # NOTE: Constant initialisation necessary for annotation but perhaps |
paulb@34 | 113 | # NOTE: redundant in the program. |
paulb@34 | 114 | |
paulb@34 | 115 | init_code = [] |
paulb@34 | 116 | for value, constant in self.constants.items(): |
paulb@59 | 117 | init_code.append( |
paulb@101 | 118 | StoreAttr( |
paulb@101 | 119 | lvalue=LoadRef(ref=constant), |
paulb@59 | 120 | name="__class__", |
paulb@101 | 121 | expr=LoadName(name=constant.typename) |
paulb@59 | 122 | ) |
paulb@59 | 123 | ) |
paulb@34 | 124 | |
paulb@34 | 125 | # NOTE: Hack to ensure correct initialisation of constants. |
paulb@34 | 126 | |
paulb@34 | 127 | if self.builtins: |
paulb@57 | 128 | result.code = module_code + init_code |
paulb@34 | 129 | else: |
paulb@57 | 130 | result.code = init_code + module_code |
paulb@57 | 131 | return result |
paul@0 | 132 | |
paulb@137 | 133 | # Node transformations. |
paulb@17 | 134 | |
paulb@137 | 135 | def visitAdd(self, add): |
paulb@137 | 136 | return self._visitBinary(add, "__add__", "__radd__") |
paul@1 | 137 | |
paul@1 | 138 | def visitAnd(self, and_): |
paulb@7 | 139 | |
paulb@35 | 140 | """ |
paulb@35 | 141 | Make a subprogram for the 'and_' node and record its contents inside the |
paulb@35 | 142 | subprogram. Convert... |
paulb@35 | 143 | |
paulb@35 | 144 | And (test) |
paulb@35 | 145 | (test) |
paulb@35 | 146 | ... |
paulb@35 | 147 | |
paulb@35 | 148 | ...to: |
paulb@35 | 149 | |
paulb@135 | 150 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@135 | 151 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@35 | 152 | (else) -> ... |
paulb@35 | 153 | """ |
paulb@7 | 154 | |
paulb@118 | 155 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 156 | self.current_subprograms.append(subprogram) |
paulb@7 | 157 | |
paulb@12 | 158 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 159 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 160 | # subprogram with the value of the operand. |
paulb@12 | 161 | |
paulb@7 | 162 | last = and_.nodes[-1] |
paulb@35 | 163 | results = nodes = [] |
paulb@35 | 164 | |
paul@1 | 165 | for node in and_.nodes: |
paulb@7 | 166 | expr = self.dispatch(node) |
paulb@34 | 167 | |
paulb@35 | 168 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 169 | |
paulb@7 | 170 | if node is not last: |
paulb@135 | 171 | nodes += [ |
paulb@135 | 172 | StoreTemp(expr=expr), |
paulb@135 | 173 | Conditional( |
paulb@135 | 174 | test=self._visitNot(LoadTemp()), |
paulb@135 | 175 | body=[ |
paulb@135 | 176 | ReturnFromBlock( |
paulb@135 | 177 | expr=LoadTemp() |
paulb@135 | 178 | ) |
paulb@135 | 179 | ], |
paulb@135 | 180 | else_=[ |
paulb@135 | 181 | ReleaseTemp() |
paulb@135 | 182 | # Subsequent operations go here! |
paulb@135 | 183 | ] |
paulb@135 | 184 | ) |
paulb@135 | 185 | ] |
paulb@35 | 186 | |
paulb@35 | 187 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 188 | |
paulb@135 | 189 | nodes = nodes[-1].else_ |
paulb@35 | 190 | |
paulb@35 | 191 | # For the last operation, return the result. |
paulb@35 | 192 | |
paulb@7 | 193 | else: |
paulb@135 | 194 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@35 | 195 | |
paulb@35 | 196 | # Finish the subprogram definition. |
paulb@35 | 197 | |
paulb@35 | 198 | subprogram.code = results |
paulb@7 | 199 | |
paulb@7 | 200 | self.current_subprograms.pop() |
paulb@79 | 201 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@7 | 202 | |
paulb@7 | 203 | # Make an invocation of the subprogram. |
paulb@7 | 204 | |
paulb@204 | 205 | result = InvokeRef(and_, 1, produces_result=1, ref=subprogram) |
paul@1 | 206 | return result |
paul@1 | 207 | |
paulb@202 | 208 | def visitAssert(self, assert_): |
paulb@202 | 209 | if assert_.fail: |
paulb@202 | 210 | fail_args = [self.dispatch(assert_.fail)] |
paulb@202 | 211 | else: |
paulb@202 | 212 | fail_args = [] |
paulb@202 | 213 | |
paulb@202 | 214 | result = Conditional(assert_, 1, |
paulb@202 | 215 | test=self.dispatch(assert_.test), |
paulb@202 | 216 | body=[], |
paulb@202 | 217 | else_=[ |
paulb@202 | 218 | Raise(assert_, |
paulb@202 | 219 | expr=InvokeFunction(assert_, |
paulb@202 | 220 | expr=LoadName(name="AssertionError"), |
paulb@202 | 221 | args=fail_args, |
paulb@202 | 222 | star=None, |
paulb@202 | 223 | dstar=None |
paulb@202 | 224 | ) |
paulb@202 | 225 | ) |
paulb@202 | 226 | ] |
paulb@202 | 227 | ) |
paulb@202 | 228 | |
paulb@202 | 229 | # Make nice annotations for the viewer. |
paulb@202 | 230 | |
paulb@202 | 231 | assert_._raises = result.else_[0].expr |
paulb@202 | 232 | return result |
paulb@202 | 233 | |
paulb@137 | 234 | # Assignments. |
paulb@34 | 235 | |
paulb@137 | 236 | def visitAssAttr(self, assattr, in_sequence=0): |
paulb@137 | 237 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paulb@137 | 238 | lvalue = self.dispatch(assattr.expr) |
paulb@137 | 239 | result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@1 | 240 | return result |
paul@1 | 241 | |
paulb@137 | 242 | def visitAssign(self, assign): |
paulb@137 | 243 | result = Assign(assign, 1) |
paulb@137 | 244 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paulb@137 | 245 | release = ReleaseTemp() |
paulb@137 | 246 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paulb@129 | 247 | return result |
paulb@129 | 248 | |
paulb@137 | 249 | def visitAssList(self, asslist, in_sequence=0): |
paulb@137 | 250 | if not in_sequence: |
paulb@137 | 251 | expr = LoadTemp() |
paulb@137 | 252 | else: |
paulb@185 | 253 | expr = InvokeFunction(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paulb@137 | 254 | result = Assign(asslist, 1) |
paulb@185 | 255 | store = StoreTemp(expr=InvokeFunction(asslist, expr=LoadAttr(name="__iter__", expr=expr))) |
paulb@137 | 256 | release = ReleaseTemp() |
paulb@137 | 257 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paulb@32 | 258 | return result |
paulb@32 | 259 | |
paulb@137 | 260 | visitAssTuple = visitAssList |
paulb@135 | 261 | |
paulb@137 | 262 | def _visitAssNameOrAttr(self, node, in_sequence): |
paulb@137 | 263 | if not in_sequence: |
paulb@137 | 264 | return LoadTemp() |
paulb@137 | 265 | else: |
paulb@185 | 266 | return InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paulb@135 | 267 | |
paulb@137 | 268 | def visitAssName(self, assname, in_sequence=0): |
paulb@137 | 269 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paulb@137 | 270 | result = StoreName(assname, 1, name=assname.name, expr=expr) |
paulb@137 | 271 | return result |
paul@0 | 272 | |
paul@2 | 273 | augassign_methods = { |
paulb@9 | 274 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 275 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 276 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 277 | } |
paul@2 | 278 | |
paul@2 | 279 | def visitAugAssign(self, augassign): |
paulb@92 | 280 | |
paulb@92 | 281 | """ |
paulb@92 | 282 | Convert the augmented assignment... |
paulb@92 | 283 | |
paulb@92 | 284 | AugAssign (node) -> Name | Getattr | Slice | Subscript |
paulb@92 | 285 | (op) |
paulb@92 | 286 | (expr) |
paulb@92 | 287 | |
paulb@92 | 288 | ...to: |
paulb@92 | 289 | |
paulb@92 | 290 | Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> |
paulb@92 | 291 | (name) -> <op> |
paulb@92 | 292 | StoreName (name) -> <name> |
paulb@92 | 293 | (expr) -> LoadTemp |
paulb@92 | 294 | ReleaseTemp |
paulb@92 | 295 | """ |
paulb@92 | 296 | |
paulb@79 | 297 | result = Assign(augassign, 1) |
paul@2 | 298 | expr = self.dispatch(augassign.expr) |
paul@2 | 299 | |
paul@2 | 300 | # Simple augmented assignment: name += expr |
paul@2 | 301 | |
paul@2 | 302 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@56 | 303 | result.code = [ |
paulb@101 | 304 | StoreTemp( |
paulb@173 | 305 | expr=InvokeFunction( # referenced below |
paulb@185 | 306 | augassign, |
paulb@59 | 307 | args=[expr], |
paulb@59 | 308 | star=None, |
paulb@59 | 309 | dstar=None, |
paulb@101 | 310 | expr=LoadAttr( |
paulb@56 | 311 | expr=self.dispatch(augassign.node), |
paulb@56 | 312 | name=self.augassign_methods[augassign.op] |
paulb@56 | 313 | ) |
paulb@56 | 314 | ) |
paulb@56 | 315 | ), |
paulb@101 | 316 | StoreName( |
paulb@101 | 317 | expr=LoadTemp(), |
paulb@56 | 318 | name=augassign.node.name), |
paulb@101 | 319 | ReleaseTemp() |
paulb@56 | 320 | ] |
paul@2 | 321 | |
paulb@173 | 322 | # Make nice annotations for the viewer. |
paulb@173 | 323 | |
paulb@173 | 324 | augassign._op_call = result.code[0].expr |
paulb@173 | 325 | |
paulb@9 | 326 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 327 | |
paul@2 | 328 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 329 | |
paulb@9 | 330 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 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@173 | 338 | expr=InvokeFunction( # referenced below |
paulb@185 | 339 | augassign, |
paulb@56 | 340 | args=[expr], star=None, dstar=None, |
paulb@101 | 341 | expr=LoadAttr( |
paulb@92 | 342 | expr=LoadAttr(augassign.node, 1, |
paulb@101 | 343 | expr=LoadTemp(index="expr"), |
paulb@56 | 344 | name=augassign.node.attrname |
paulb@56 | 345 | ), |
paulb@56 | 346 | name=self.augassign_methods[augassign.op] |
paulb@56 | 347 | ) |
paulb@56 | 348 | ) |
paulb@56 | 349 | ), |
paulb@101 | 350 | StoreAttr( |
paulb@101 | 351 | expr=LoadTemp(), |
paulb@101 | 352 | lvalue=LoadTemp(index="expr"), |
paulb@56 | 353 | name=augassign.node.attrname |
paulb@56 | 354 | ), |
paulb@101 | 355 | ReleaseTemp(index="expr"), |
paulb@101 | 356 | ReleaseTemp() |
paulb@56 | 357 | ] |
paulb@6 | 358 | |
paulb@173 | 359 | # Make nice annotations for the viewer. |
paulb@173 | 360 | |
paulb@173 | 361 | augassign._op_call = result.code[1].expr |
paulb@173 | 362 | |
paulb@9 | 363 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 364 | |
paulb@6 | 365 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 366 | |
paulb@9 | 367 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 368 | |
paulb@56 | 369 | result.code = [ |
paulb@101 | 370 | StoreTemp( |
paulb@56 | 371 | index="expr", |
paulb@56 | 372 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 373 | ), |
paulb@101 | 374 | StoreTemp( |
paulb@56 | 375 | index="lower", |
paulb@56 | 376 | expr=self.dispatch_or_none(augassign.node.lower) |
paulb@56 | 377 | ), |
paulb@101 | 378 | StoreTemp( |
paulb@56 | 379 | index="upper", |
paulb@56 | 380 | expr=self.dispatch_or_none(augassign.node.upper) |
paulb@56 | 381 | ), |
paulb@101 | 382 | StoreTemp( |
paulb@173 | 383 | expr=InvokeFunction( # referenced below |
paulb@185 | 384 | augassign, |
paulb@56 | 385 | args=[expr], star=None, dstar=None, |
paulb@101 | 386 | expr=LoadAttr( |
paulb@59 | 387 | expr=self._visitSlice( |
paulb@59 | 388 | augassign.node, |
paulb@101 | 389 | LoadTemp(index="expr"), |
paulb@101 | 390 | LoadTemp(index="lower"), |
paulb@101 | 391 | LoadTemp(index="upper"), |
paulb@59 | 392 | "OP_APPLY"), |
paulb@56 | 393 | name=self.augassign_methods[augassign.op] |
paulb@56 | 394 | ) |
paulb@56 | 395 | ) |
paulb@56 | 396 | ), |
paulb@59 | 397 | self._visitSlice( |
paulb@59 | 398 | augassign.node, |
paulb@101 | 399 | LoadTemp(index="expr"), |
paulb@101 | 400 | LoadTemp(index="lower"), |
paulb@101 | 401 | LoadTemp(index="upper"), |
paulb@59 | 402 | "OP_ASSIGN", |
paulb@101 | 403 | LoadTemp() |
paulb@59 | 404 | ), |
paulb@101 | 405 | ReleaseTemp(index="expr"), |
paulb@101 | 406 | ReleaseTemp(index="lower"), |
paulb@101 | 407 | ReleaseTemp(index="upper"), |
paulb@101 | 408 | ReleaseTemp() |
paulb@56 | 409 | ] |
paul@2 | 410 | |
paulb@173 | 411 | # Make nice annotations for the viewer. |
paulb@173 | 412 | |
paulb@173 | 413 | augassign._op_call = result.code[3].expr |
paulb@173 | 414 | |
paulb@10 | 415 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 416 | |
paulb@10 | 417 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 418 | |
paulb@10 | 419 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 420 | |
paulb@56 | 421 | result.code = [ |
paulb@101 | 422 | StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), |
paulb@101 | 423 | StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), |
paulb@101 | 424 | StoreTemp( |
paulb@173 | 425 | expr=InvokeFunction( # referenced below |
paulb@185 | 426 | augassign, |
paulb@56 | 427 | args=[expr], star=None, dstar=None, |
paulb@101 | 428 | expr=LoadAttr( |
paulb@59 | 429 | expr=self._visitSubscript( |
paulb@59 | 430 | augassign.node, |
paulb@101 | 431 | LoadTemp(index="expr"), |
paulb@101 | 432 | LoadTemp(index="subs"), |
paulb@59 | 433 | "OP_APPLY" |
paulb@59 | 434 | ), |
paulb@56 | 435 | name=self.augassign_methods[augassign.op] |
paulb@56 | 436 | ) |
paulb@56 | 437 | ) |
paulb@56 | 438 | ), |
paulb@59 | 439 | self._visitSubscript( |
paulb@59 | 440 | augassign.node, |
paulb@101 | 441 | LoadTemp(index="expr"), |
paulb@101 | 442 | LoadTemp(index="subs"), |
paulb@59 | 443 | "OP_ASSIGN", |
paulb@101 | 444 | LoadTemp() |
paulb@59 | 445 | ), |
paulb@101 | 446 | ReleaseTemp(index="expr"), |
paulb@101 | 447 | ReleaseTemp(index="subs"), |
paulb@101 | 448 | ReleaseTemp() |
paulb@56 | 449 | ] |
paulb@10 | 450 | |
paulb@173 | 451 | # Make nice annotations for the viewer. |
paulb@173 | 452 | |
paulb@173 | 453 | augassign._op_call = result.code[2].expr |
paulb@173 | 454 | |
paul@2 | 455 | else: |
paul@2 | 456 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 457 | |
paul@2 | 458 | return result |
paul@2 | 459 | |
paulb@203 | 460 | def visitBitand(self, bitand): |
paulb@203 | 461 | |
paulb@203 | 462 | """ |
paulb@203 | 463 | Make a subprogram for the 'bitand' node and record its contents inside the |
paulb@203 | 464 | subprogram. Convert... |
paulb@203 | 465 | |
paulb@203 | 466 | Bitand (node) |
paulb@203 | 467 | (node) |
paulb@203 | 468 | ... |
paulb@203 | 469 | |
paulb@203 | 470 | ...to: |
paulb@203 | 471 | |
paulb@203 | 472 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 473 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 474 | (else) -> ... |
paulb@203 | 475 | """ |
paulb@203 | 476 | |
paulb@203 | 477 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@203 | 478 | self.current_subprograms.append(subprogram) |
paulb@203 | 479 | |
paulb@203 | 480 | # In the subprogram, make instructions which store each operand, test |
paulb@203 | 481 | # for each operand's truth status, and if appropriate return from the |
paulb@203 | 482 | # subprogram with the value of the operand. |
paulb@203 | 483 | |
paulb@203 | 484 | last = bitand.nodes[-1] |
paulb@203 | 485 | results = nodes = [] |
paulb@203 | 486 | |
paulb@203 | 487 | # Start by storing the first operand. |
paulb@203 | 488 | |
paulb@203 | 489 | nodes += [ |
paulb@203 | 490 | StoreTemp(expr=self.dispatch(bitand.nodes[0])) |
paulb@203 | 491 | ] |
paulb@203 | 492 | |
paulb@203 | 493 | # For viewing purposes, record invocations on the AST node. |
paulb@203 | 494 | |
paulb@203 | 495 | bitand._ops = [] |
paulb@203 | 496 | |
paulb@203 | 497 | for node in bitand.nodes[1:]: |
paulb@203 | 498 | |
paulb@203 | 499 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@203 | 500 | |
paulb@203 | 501 | new_op = Op("&", node) |
paulb@203 | 502 | bitand._ops.append(new_op) |
paulb@203 | 503 | |
paulb@203 | 504 | # Generate the operation involving the previous result and the |
paulb@203 | 505 | # current operand. |
paulb@203 | 506 | |
paulb@203 | 507 | expr = self._visitBinaryOp(new_op, LoadTemp(), self.dispatch(node), "__and__", "__rand__") |
paulb@203 | 508 | |
paulb@203 | 509 | # Return from the subprogram where the test is not satisfied. |
paulb@203 | 510 | |
paulb@203 | 511 | if node is not last: |
paulb@203 | 512 | nodes += [ |
paulb@203 | 513 | StoreTemp(expr=expr), |
paulb@203 | 514 | Conditional( |
paulb@203 | 515 | test=self._visitNot(LoadTemp()), |
paulb@203 | 516 | body=[ |
paulb@203 | 517 | ReturnFromBlock( |
paulb@203 | 518 | expr=LoadTemp() |
paulb@203 | 519 | ) |
paulb@203 | 520 | ], |
paulb@203 | 521 | else_=[ |
paulb@203 | 522 | # Subsequent operations go here! |
paulb@203 | 523 | ] |
paulb@203 | 524 | ) |
paulb@203 | 525 | ] |
paulb@203 | 526 | |
paulb@203 | 527 | # Put subsequent operations in the else section of this conditional. |
paulb@203 | 528 | |
paulb@203 | 529 | nodes = nodes[-1].else_ |
paulb@203 | 530 | |
paulb@203 | 531 | # For the last operation, return the result. |
paulb@203 | 532 | |
paulb@203 | 533 | else: |
paulb@203 | 534 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@203 | 535 | |
paulb@203 | 536 | # Finish the subprogram definition. |
paulb@203 | 537 | |
paulb@203 | 538 | subprogram.code = results |
paulb@203 | 539 | |
paulb@203 | 540 | self.current_subprograms.pop() |
paulb@203 | 541 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@203 | 542 | |
paulb@203 | 543 | # Make an invocation of the subprogram. |
paulb@203 | 544 | |
paulb@204 | 545 | result = InvokeRef(bitand, 1, produces_result=1, ref=subprogram) |
paulb@203 | 546 | return result |
paulb@203 | 547 | |
paulb@137 | 548 | def visitBreak(self, break_): |
paulb@137 | 549 | result = ReturnFromBlock(break_, 1) |
paul@0 | 550 | return result |
paul@0 | 551 | |
paulb@137 | 552 | def visitCallFunc(self, callfunc): |
paulb@137 | 553 | result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) |
paulb@137 | 554 | if callfunc.star_args is not None: |
paulb@137 | 555 | result.star = self.dispatch(callfunc.star_args) |
paulb@137 | 556 | if callfunc.dstar_args is not None: |
paulb@137 | 557 | result.dstar = self.dispatch(callfunc.dstar_args) |
paulb@137 | 558 | result.expr = self.dispatch(callfunc.node) |
paulb@6 | 559 | return result |
paulb@6 | 560 | |
paulb@19 | 561 | def visitClass(self, class_): |
paulb@129 | 562 | |
paulb@129 | 563 | # Add "object" if the class is not "object" and has an empty bases list. |
paulb@129 | 564 | |
paulb@129 | 565 | if class_.name != "object" and not class_.bases: |
paulb@129 | 566 | bases = [compiler.ast.Name("object")] |
paulb@129 | 567 | else: |
paulb@129 | 568 | bases = class_.bases |
paulb@129 | 569 | |
paulb@129 | 570 | structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) |
paulb@19 | 571 | self.structures.append(structure) |
paulb@172 | 572 | within_class = self.within_class |
paulb@172 | 573 | self.within_class = 1 |
paulb@19 | 574 | |
paulb@24 | 575 | # Make a subprogram which initialises the class structure. |
paulb@24 | 576 | |
paulb@118 | 577 | subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 578 | self.current_subprograms.append(subprogram) |
paulb@120 | 579 | self.current_structures.append(structure) # mostly for name construction |
paulb@19 | 580 | |
paulb@28 | 581 | # The class is initialised using the code found inside. |
paulb@28 | 582 | |
paulb@135 | 583 | subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] |
paulb@19 | 584 | |
paulb@172 | 585 | self.within_class = within_class |
paulb@120 | 586 | self.current_structures.pop() |
paulb@19 | 587 | self.current_subprograms.pop() |
paulb@79 | 588 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@19 | 589 | |
paulb@19 | 590 | # Make a definition of the class associating it with a name. |
paulb@19 | 591 | |
paulb@101 | 592 | result = Assign( |
paulb@56 | 593 | code=[ |
paulb@79 | 594 | StoreName(class_, 1, # defines the class |
paulb@56 | 595 | name=class_.name, |
paulb@101 | 596 | expr=LoadRef(ref=structure) |
paulb@56 | 597 | ), |
paulb@204 | 598 | InvokeRef( |
paulb@185 | 599 | class_, |
paulb@204 | 600 | share_locals=0, # override the local sharing usually in InvokeRef |
paulb@204 | 601 | ref=subprogram |
paulb@56 | 602 | ) |
paulb@56 | 603 | ] |
paulb@56 | 604 | ) |
paulb@19 | 605 | return result |
paulb@19 | 606 | |
paulb@137 | 607 | comparison_methods = { |
paulb@192 | 608 | "==" : ("__eq__", "__ne__"), |
paulb@192 | 609 | "!=" : ("__ne__", "__eq__"), |
paulb@192 | 610 | "<" : ("__lt__", "__gt__"), |
paulb@192 | 611 | "<=" : ("__le__", "__ge__"), |
paulb@192 | 612 | ">=" : ("__ge__", "__le__"), |
paulb@192 | 613 | ">" : ("__gt__", "__lt__"), |
paulb@192 | 614 | "is" : None, |
paulb@201 | 615 | "is not" : None, |
paulb@201 | 616 | "in" : None, |
paulb@201 | 617 | "not in" : None |
paulb@137 | 618 | } |
paulb@137 | 619 | |
paulb@137 | 620 | def visitCompare(self, compare): |
paulb@46 | 621 | |
paulb@46 | 622 | """ |
paulb@137 | 623 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@137 | 624 | the subprogram. Convert... |
paul@0 | 625 | |
paulb@137 | 626 | Compare (expr) |
paulb@137 | 627 | (name/node) |
paulb@137 | 628 | ... |
paulb@35 | 629 | |
paulb@35 | 630 | ...to: |
paulb@35 | 631 | |
paulb@204 | 632 | InvokeRef -> Subprogram -> Conditional (test) -> (body) |
paulb@137 | 633 | (else) -> Conditional (test) -> (body) |
paulb@137 | 634 | (else) -> ... |
paulb@35 | 635 | """ |
paul@0 | 636 | |
paulb@137 | 637 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paul@0 | 638 | self.current_subprograms.append(subprogram) |
paul@0 | 639 | |
paulb@137 | 640 | # In the subprogram, make instructions which invoke a method on the |
paulb@137 | 641 | # first operand of each operand pair and, if appropriate, return with |
paulb@137 | 642 | # the value from that method. |
paulb@137 | 643 | |
paulb@137 | 644 | last = compare.ops[-1] |
paulb@137 | 645 | previous = self.dispatch(compare.expr) |
paulb@137 | 646 | results = nodes = [] |
paulb@137 | 647 | |
paulb@137 | 648 | # For viewing purposes, record invocations on the AST node. |
paulb@137 | 649 | |
paulb@137 | 650 | compare._ops = [] |
paulb@137 | 651 | |
paulb@137 | 652 | for op in compare.ops: |
paulb@137 | 653 | op_name, node = op |
paulb@192 | 654 | |
paulb@192 | 655 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@192 | 656 | |
paulb@192 | 657 | new_op = Op(op_name, node) |
paulb@203 | 658 | compare._ops.append(new_op) |
paulb@203 | 659 | |
paulb@137 | 660 | expr = self.dispatch(node) |
paul@0 | 661 | |
paulb@137 | 662 | # Identify the operation and produce the appropriate method call. |
paul@0 | 663 | |
paulb@192 | 664 | method_names = self.comparison_methods[op_name] |
paulb@192 | 665 | if method_names: |
paulb@192 | 666 | first_name, alternative_name = method_names |
paulb@193 | 667 | invocation = self._visitBinaryCompareOp(new_op, previous, expr, first_name, alternative_name) |
paulb@137 | 668 | |
paulb@137 | 669 | elif op_name == "is": |
paulb@137 | 670 | invocation = InvokeFunction( |
paulb@192 | 671 | new_op, 1, |
paulb@137 | 672 | expr=LoadName(name="__is__"), |
paulb@137 | 673 | args=[previous, expr], |
paulb@137 | 674 | star=None, |
paulb@137 | 675 | dstar=None) |
paul@0 | 676 | |
paulb@137 | 677 | elif op_name == "is not": |
paulb@137 | 678 | invocation = Not( |
paulb@192 | 679 | new_op, 1, |
paulb@137 | 680 | expr=InvokeFunction( |
paulb@192 | 681 | new_op, |
paulb@137 | 682 | expr=LoadName(name="__is__"), |
paulb@137 | 683 | args=[previous, expr], |
paulb@137 | 684 | star=None, |
paulb@137 | 685 | dstar=None) |
paulb@137 | 686 | ) |
paulb@201 | 687 | |
paulb@201 | 688 | elif op_name == "in": |
paulb@201 | 689 | invocation = InvokeFunction( |
paulb@201 | 690 | new_op, 1, |
paulb@201 | 691 | expr=LoadAttr( |
paulb@201 | 692 | expr=previous, |
paulb@201 | 693 | name="__contains__" |
paulb@201 | 694 | ), |
paulb@201 | 695 | args=[expr], |
paulb@201 | 696 | star=None, |
paulb@201 | 697 | dstar=None) |
paulb@201 | 698 | |
paulb@201 | 699 | elif op_name == "not in": |
paulb@201 | 700 | invocation = Not( |
paulb@201 | 701 | new_op, 1, |
paulb@201 | 702 | expr=InvokeFunction( |
paulb@201 | 703 | new_op, |
paulb@201 | 704 | expr=LoadAttr( |
paulb@201 | 705 | expr=previous, |
paulb@201 | 706 | name="__contains__" |
paulb@201 | 707 | ), |
paulb@201 | 708 | args=[expr], |
paulb@201 | 709 | star=None, |
paulb@201 | 710 | dstar=None) |
paulb@201 | 711 | ) |
paulb@201 | 712 | |
paulb@137 | 713 | else: |
paulb@137 | 714 | raise NotImplementedError, op_name |
paulb@34 | 715 | |
paulb@137 | 716 | nodes.append(StoreTemp(expr=invocation)) |
paulb@137 | 717 | |
paulb@137 | 718 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 719 | |
paulb@137 | 720 | if op is not last: |
paulb@137 | 721 | nodes.append( |
paulb@137 | 722 | Conditional( |
paulb@137 | 723 | test=self._visitNot(LoadTemp()), |
paulb@137 | 724 | body=[ |
paulb@137 | 725 | ReturnFromBlock(expr=LoadTemp()) |
paulb@137 | 726 | ], |
paulb@137 | 727 | else_=[ |
paulb@137 | 728 | ReleaseTemp() |
paulb@137 | 729 | # Subsequent operations go here! |
paulb@137 | 730 | ] |
paulb@137 | 731 | ) |
paulb@137 | 732 | ) |
paulb@35 | 733 | |
paulb@137 | 734 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 735 | |
paulb@137 | 736 | nodes = nodes[-1].else_ |
paulb@137 | 737 | |
paulb@137 | 738 | # For the last operation, return the result. |
paulb@35 | 739 | |
paulb@137 | 740 | else: |
paulb@137 | 741 | nodes.append( |
paulb@137 | 742 | ReturnFromBlock(expr=LoadTemp(release=1)) |
paulb@137 | 743 | ) |
paulb@137 | 744 | |
paulb@137 | 745 | previous = expr |
paulb@35 | 746 | |
paulb@35 | 747 | # Finish the subprogram definition. |
paulb@35 | 748 | |
paulb@137 | 749 | subprogram.code = results |
paul@0 | 750 | |
paul@0 | 751 | self.current_subprograms.pop() |
paulb@79 | 752 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 753 | |
paul@0 | 754 | # Make an invocation of the subprogram. |
paul@0 | 755 | |
paulb@204 | 756 | result = InvokeRef(compare, 1, produces_result=1, ref=subprogram) |
paul@0 | 757 | return result |
paul@0 | 758 | |
paulb@137 | 759 | def visitConst(self, const): |
paulb@154 | 760 | key = "%s-%s" % (const.value.__class__.__name__, const.value) |
paulb@154 | 761 | if not self.constants.has_key(key): |
paulb@154 | 762 | self.constants[key] = Constant(name=repr(const.value), value=const.value) |
paulb@154 | 763 | result = LoadRef(const, 1, ref=self.constants[key]) |
paulb@137 | 764 | return result |
paulb@137 | 765 | |
paulb@137 | 766 | def visitContinue(self, continue_): |
paulb@204 | 767 | result = InvokeRef(continue_, 1, ref=self.current_subprograms[-1]) |
paulb@137 | 768 | return result |
paulb@137 | 769 | |
paulb@137 | 770 | def visitDict(self, dict): |
paulb@137 | 771 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) |
paulb@137 | 772 | args = [] |
paulb@137 | 773 | for key, value in dict.items: |
paulb@185 | 774 | tuple = InvokeFunction(dict, expr=LoadName(name="tuple"), star=None, dstar=None) |
paulb@137 | 775 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paulb@137 | 776 | args.append(tuple) |
paulb@137 | 777 | result.set_args(args) |
paulb@137 | 778 | return result |
paulb@137 | 779 | |
paulb@137 | 780 | def visitDiscard(self, discard): |
paulb@137 | 781 | return self.dispatch(discard.expr) |
paulb@137 | 782 | |
paulb@137 | 783 | def visitDiv(self, div): |
paulb@137 | 784 | return self._visitBinary(div, "__div__", "__rdiv__") |
paulb@137 | 785 | |
paulb@137 | 786 | def visitFloorDiv(self, floordiv): |
paulb@137 | 787 | return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") |
paulb@137 | 788 | |
paul@0 | 789 | def visitFor(self, for_): |
paul@0 | 790 | |
paulb@35 | 791 | """ |
paulb@35 | 792 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 793 | subprogram. Convert... |
paulb@35 | 794 | |
paulb@35 | 795 | For (assign) |
paulb@35 | 796 | (body) |
paulb@35 | 797 | (else) |
paulb@35 | 798 | |
paulb@35 | 799 | ...to: |
paulb@35 | 800 | |
paulb@35 | 801 | Assign (assign #1) |
paulb@35 | 802 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 803 | (body) |
paulb@35 | 804 | Invoke subprogram |
paulb@35 | 805 | (handler) -> ... |
paulb@35 | 806 | (else) -> ... |
paulb@35 | 807 | """ |
paul@0 | 808 | |
paulb@201 | 809 | return self._visitFor(for_, self.dispatches(for_.body), for_.else_) |
paulb@201 | 810 | |
paulb@201 | 811 | def _visitFor(self, node, body_stmt, else_=None): |
paulb@201 | 812 | |
paulb@118 | 813 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 814 | self.current_subprograms.append(subprogram) |
paul@0 | 815 | |
paulb@34 | 816 | # Always return from conditional sections/subprograms. |
paulb@34 | 817 | |
paulb@201 | 818 | if else_ is not None: |
paulb@201 | 819 | else_stmt = self.dispatch(else_) + [ReturnFromBlock()] |
paulb@17 | 820 | else: |
paulb@135 | 821 | else_stmt = [ReturnFromBlock()] |
paulb@17 | 822 | |
paul@0 | 823 | # Wrap the assignment in a try...except statement. |
paulb@135 | 824 | # Inside the body, add a recursive invocation to the subprogram. |
paul@0 | 825 | |
paulb@135 | 826 | subprogram.code = [ |
paulb@135 | 827 | Try( |
paulb@135 | 828 | body=[ |
paulb@135 | 829 | Assign( |
paulb@135 | 830 | code=[ |
paulb@201 | 831 | StoreTemp(expr=InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paulb@201 | 832 | self.dispatch(node.assign), |
paulb@135 | 833 | ReleaseTemp() |
paulb@135 | 834 | ]) |
paulb@201 | 835 | ] + body_stmt + [ |
paulb@204 | 836 | InvokeRef( |
paulb@201 | 837 | node, |
paulb@204 | 838 | ref=subprogram |
paulb@135 | 839 | ) |
paulb@135 | 840 | ], |
paulb@135 | 841 | handler=[ |
paulb@135 | 842 | Conditional( |
paulb@135 | 843 | test=InvokeFunction( |
paulb@201 | 844 | node, |
paulb@135 | 845 | expr=LoadName(name="isinstance"), |
paulb@135 | 846 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@135 | 847 | star=None, |
paulb@135 | 848 | dstar=None), |
paulb@135 | 849 | body=else_stmt, |
paulb@135 | 850 | else_=[Raise(expr=LoadExc())] |
paulb@135 | 851 | ) |
paulb@135 | 852 | ], |
paulb@135 | 853 | else_=[], |
paulb@135 | 854 | finally_=[] |
paulb@135 | 855 | ), |
paulb@135 | 856 | ReturnFromBlock() |
paulb@135 | 857 | ] |
paul@0 | 858 | |
paulb@35 | 859 | # Finish the subprogram definition. |
paulb@35 | 860 | |
paul@0 | 861 | self.current_subprograms.pop() |
paulb@79 | 862 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 863 | |
paul@0 | 864 | # Obtain an iterator for the sequence involved. |
paul@0 | 865 | # Then, make an invocation of the subprogram. |
paul@0 | 866 | |
paulb@201 | 867 | result = Assign(node, 1, |
paulb@148 | 868 | code=[ |
paulb@148 | 869 | StoreTemp( |
paulb@148 | 870 | expr=InvokeFunction( |
paulb@201 | 871 | node, |
paulb@148 | 872 | expr=LoadAttr( |
paulb@148 | 873 | name="__iter__", |
paulb@201 | 874 | expr=self.dispatch(node.list) |
paulb@148 | 875 | ) |
paulb@148 | 876 | ) |
paulb@148 | 877 | ), |
paulb@204 | 878 | InvokeRef(node, ref=subprogram), |
paulb@148 | 879 | ReleaseTemp() |
paulb@148 | 880 | ] |
paulb@148 | 881 | ) |
paulb@148 | 882 | |
paulb@148 | 883 | # Make nice annotations for the viewer. |
paulb@148 | 884 | |
paulb@201 | 885 | node._iter_call = result.code[0].expr |
paulb@201 | 886 | node._next_call = subprogram.code[0].body[0].code[0].expr |
paulb@148 | 887 | |
paul@0 | 888 | return result |
paul@0 | 889 | |
paulb@137 | 890 | def visitFrom(self, from_): |
paulb@137 | 891 | result = Assign(from_, 1) |
paulb@137 | 892 | code = [] |
paulb@157 | 893 | _names = [] |
paulb@157 | 894 | code.append( |
paulb@157 | 895 | StoreTemp( |
paulb@158 | 896 | expr=Import(name=from_.modname, alias=1) |
paulb@157 | 897 | ) |
paulb@157 | 898 | ) |
paulb@157 | 899 | from_._modname = code[-1].expr |
paulb@137 | 900 | for name, alias in from_.names: |
paulb@137 | 901 | code.append( |
paulb@137 | 902 | StoreName( |
paulb@137 | 903 | expr=LoadAttr( |
paulb@137 | 904 | expr=LoadTemp(), |
paulb@137 | 905 | name=name), |
paulb@137 | 906 | name=(alias or name) |
paulb@137 | 907 | ) |
paulb@137 | 908 | ) |
paulb@157 | 909 | _names.append(code[-1].expr) |
paulb@137 | 910 | code.append(ReleaseTemp()) |
paulb@137 | 911 | result.code = code |
paulb@157 | 912 | from_._names = _names |
paulb@137 | 913 | return result |
paulb@137 | 914 | |
paulb@137 | 915 | def _visitFunction(self, function, subprogram): |
paulb@137 | 916 | |
paulb@137 | 917 | """ |
paulb@137 | 918 | A common function generator which transforms the given 'function' node |
paulb@137 | 919 | and initialises the given 'subprogram' appropriately. |
paulb@137 | 920 | """ |
paulb@137 | 921 | |
paulb@137 | 922 | # Discover star and dstar parameters. |
paulb@137 | 923 | |
paulb@137 | 924 | if function.flags & 4 != 0: |
paulb@137 | 925 | has_star = 1 |
paulb@137 | 926 | else: |
paulb@137 | 927 | has_star = 0 |
paulb@137 | 928 | if function.flags & 8 != 0: |
paulb@137 | 929 | has_dstar = 1 |
paulb@137 | 930 | else: |
paulb@137 | 931 | has_dstar = 0 |
paulb@137 | 932 | |
paulb@137 | 933 | # Discover the number of defaults and positional parameters. |
paulb@137 | 934 | |
paulb@137 | 935 | ndefaults = len(function.defaults) |
paulb@137 | 936 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@137 | 937 | |
paulb@137 | 938 | # Produce star and dstar parameters with appropriate defaults. |
paulb@137 | 939 | |
paulb@137 | 940 | if has_star: |
paulb@137 | 941 | star = ( |
paulb@137 | 942 | function.argnames[npositional], |
paulb@185 | 943 | self.dispatch(compiler.ast.List([])) |
paulb@137 | 944 | ) |
paulb@137 | 945 | else: |
paulb@137 | 946 | star = None |
paulb@137 | 947 | if has_dstar: |
paulb@137 | 948 | dstar = ( |
paulb@137 | 949 | function.argnames[npositional + has_star], |
paulb@185 | 950 | self.dispatch(compiler.ast.Dict([])) |
paulb@137 | 951 | ) |
paulb@137 | 952 | else: |
paulb@137 | 953 | dstar = None |
paulb@137 | 954 | |
paulb@137 | 955 | params = [] |
paulb@137 | 956 | for i in range(0, npositional - ndefaults): |
paulb@137 | 957 | params.append((function.argnames[i], None)) |
paulb@137 | 958 | |
paulb@137 | 959 | # Process defaults. |
paulb@137 | 960 | |
paulb@137 | 961 | for i in range(0, ndefaults): |
paulb@137 | 962 | default = function.defaults[i] |
paulb@137 | 963 | if default is not None: |
paulb@137 | 964 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paulb@137 | 965 | else: |
paulb@137 | 966 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paulb@137 | 967 | |
paulb@137 | 968 | subprogram.params = params |
paulb@137 | 969 | subprogram.star = star |
paulb@137 | 970 | subprogram.dstar = dstar |
paulb@137 | 971 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 972 | |
paulb@137 | 973 | def visitFunction(self, function): |
paulb@137 | 974 | |
paulb@137 | 975 | """ |
paulb@137 | 976 | Make a subprogram for the 'function' and record it outside the main |
paulb@137 | 977 | tree. Produce something like the following: |
paulb@137 | 978 | |
paulb@137 | 979 | StoreName (name) |
paulb@147 | 980 | (expr) -> LoadRef (ref) -> Subprogram (params) |
paulb@147 | 981 | (star) |
paulb@147 | 982 | (dstar) |
paulb@137 | 983 | """ |
paulb@137 | 984 | |
paulb@196 | 985 | subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@196 | 986 | internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) |
paulb@187 | 987 | |
paulb@196 | 988 | # Make nice annotations for the viewer. |
paulb@196 | 989 | |
paulb@196 | 990 | function._subprogram = subprogram |
paulb@137 | 991 | |
paulb@137 | 992 | self.current_subprograms.append(subprogram) |
paulb@172 | 993 | within_class = self.within_class |
paulb@172 | 994 | self.within_class = 0 |
paulb@172 | 995 | |
paulb@137 | 996 | subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] |
paulb@172 | 997 | |
paulb@172 | 998 | self.within_class = within_class |
paulb@137 | 999 | self.current_subprograms.pop() |
paulb@137 | 1000 | self._visitFunction(function, subprogram) |
paulb@137 | 1001 | |
paulb@137 | 1002 | # Make a definition of the function associating it with a name. |
paulb@137 | 1003 | |
paulb@137 | 1004 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paulb@137 | 1005 | return result |
paulb@137 | 1006 | |
paulb@137 | 1007 | def visitGetattr(self, getattr): |
paulb@137 | 1008 | result = LoadAttr(getattr, 1, |
paulb@137 | 1009 | name=getattr.attrname, |
paulb@137 | 1010 | expr=self.dispatch(getattr.expr) |
paulb@137 | 1011 | ) |
paulb@137 | 1012 | return result |
paulb@137 | 1013 | |
paulb@137 | 1014 | def visitGlobal(self, global_): |
paulb@137 | 1015 | result = Global(global_, 1, |
paulb@137 | 1016 | names=global_.names |
paulb@137 | 1017 | ) |
paulb@137 | 1018 | return result |
paulb@137 | 1019 | |
paulb@137 | 1020 | def visitIf(self, if_): |
paulb@137 | 1021 | |
paulb@137 | 1022 | """ |
paulb@137 | 1023 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@137 | 1024 | and putting each subsequent test as part of the conditional's else |
paulb@137 | 1025 | section. |
paulb@137 | 1026 | |
paulb@137 | 1027 | Convert... |
paulb@137 | 1028 | |
paulb@137 | 1029 | If (test/body) |
paulb@137 | 1030 | (test/body) |
paulb@137 | 1031 | ... |
paulb@137 | 1032 | (else/body) |
paulb@137 | 1033 | |
paulb@137 | 1034 | ...to: |
paulb@137 | 1035 | |
paulb@137 | 1036 | Conditional (test) -> (body) |
paulb@137 | 1037 | (else) -> Conditional (test) -> (body) |
paulb@137 | 1038 | (else) -> ... |
paulb@137 | 1039 | """ |
paulb@137 | 1040 | |
paulb@137 | 1041 | |
paulb@137 | 1042 | results = nodes = [] |
paulb@137 | 1043 | |
paulb@137 | 1044 | # Produce something like... |
paulb@137 | 1045 | # expr.__bool__() ? body |
paulb@137 | 1046 | |
paulb@137 | 1047 | first = 1 |
paulb@137 | 1048 | for compare, stmt in if_.tests: |
paulb@137 | 1049 | |
paulb@137 | 1050 | # Set the first as the defining node. |
paulb@137 | 1051 | |
paulb@137 | 1052 | test = Conditional(if_, first, |
paulb@137 | 1053 | test=InvokeFunction( |
paulb@185 | 1054 | if_, |
paulb@137 | 1055 | expr=LoadAttr( |
paulb@137 | 1056 | expr=self.dispatch(compare), |
paulb@137 | 1057 | name="__bool__" |
paulb@137 | 1058 | ), |
paulb@148 | 1059 | ) |
paulb@137 | 1060 | ) |
paulb@137 | 1061 | test.body = self.dispatch(stmt) |
paulb@137 | 1062 | nodes.append(test) |
paulb@137 | 1063 | nodes = test.else_ = [] |
paulb@137 | 1064 | first = 0 |
paulb@137 | 1065 | |
paulb@137 | 1066 | # Add the compound statement from any else clause to the end. |
paulb@137 | 1067 | |
paulb@137 | 1068 | if if_.else_ is not None: |
paulb@137 | 1069 | nodes += self.dispatch(if_.else_) |
paulb@137 | 1070 | |
paulb@137 | 1071 | result = results[0] |
paulb@137 | 1072 | return result |
paulb@137 | 1073 | |
paulb@137 | 1074 | def visitImport(self, import_): |
paulb@137 | 1075 | result = Assign(import_, 1) |
paulb@137 | 1076 | code = [] |
paulb@157 | 1077 | _names = [] |
paulb@137 | 1078 | for path, alias in import_.names: |
paulb@158 | 1079 | importer = Import(name=path, alias=alias) |
paulb@137 | 1080 | top = alias or path.split(".")[0] |
paulb@137 | 1081 | code.append(StoreName(expr=importer, name=top)) |
paulb@157 | 1082 | _names.append(code[-1].expr) |
paulb@137 | 1083 | result.code = code |
paulb@157 | 1084 | import_._names = _names |
paulb@137 | 1085 | return result |
paulb@137 | 1086 | |
paulb@137 | 1087 | def visitInvert(self, invert): |
paulb@137 | 1088 | return self._visitUnary(invert, "__invert__") |
paulb@137 | 1089 | |
paulb@137 | 1090 | def visitKeyword(self, keyword): |
paulb@137 | 1091 | result = Keyword(keyword, 1, |
paulb@137 | 1092 | name=keyword.name, |
paulb@137 | 1093 | expr=self.dispatch(keyword.expr) |
paulb@137 | 1094 | ) |
paulb@137 | 1095 | return result |
paulb@137 | 1096 | |
paulb@137 | 1097 | def visitLambda(self, lambda_): |
paulb@137 | 1098 | |
paulb@137 | 1099 | # Make a subprogram for the function and record it outside the main |
paulb@137 | 1100 | # tree. |
paulb@196 | 1101 | |
paulb@196 | 1102 | subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) |
paulb@196 | 1103 | |
paulb@196 | 1104 | # Make nice annotations for the viewer. |
paulb@137 | 1105 | |
paulb@202 | 1106 | lambda_._subprogram = subprogram |
paulb@196 | 1107 | |
paulb@196 | 1108 | # Process the lambda contents. |
paulb@196 | 1109 | |
paulb@137 | 1110 | self.current_subprograms.append(subprogram) |
paulb@137 | 1111 | subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] |
paulb@137 | 1112 | self.current_subprograms.pop() |
paulb@137 | 1113 | self._visitFunction(lambda_, subprogram) |
paulb@137 | 1114 | |
paulb@137 | 1115 | # Get the subprogram reference to the lambda. |
paulb@137 | 1116 | |
paulb@137 | 1117 | return LoadRef(lambda_, 1, ref=subprogram) |
paulb@137 | 1118 | |
paulb@137 | 1119 | def visitList(self, list): |
paulb@204 | 1120 | |
paulb@204 | 1121 | # Make a subprogram for the list construction and record it outside the |
paulb@204 | 1122 | # main tree. |
paulb@204 | 1123 | |
paulb@204 | 1124 | subprogram = Subprogram(list, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=list) |
paulb@204 | 1125 | self.current_subprograms.append(subprogram) |
paulb@204 | 1126 | |
paulb@204 | 1127 | # Make nice annotations for the viewer. |
paulb@204 | 1128 | |
paulb@204 | 1129 | list._subprogram = subprogram |
paulb@204 | 1130 | |
paulb@204 | 1131 | subprogram.code=[ |
paulb@204 | 1132 | StoreTemp( |
paulb@204 | 1133 | expr=InvokeFunction( |
paulb@204 | 1134 | list, |
paulb@204 | 1135 | expr=LoadName( |
paulb@204 | 1136 | name="list" |
paulb@204 | 1137 | ), |
paulb@204 | 1138 | args=[], |
paulb@204 | 1139 | star=None, |
paulb@204 | 1140 | dstar=None |
paulb@204 | 1141 | ) |
paulb@204 | 1142 | ) |
paulb@204 | 1143 | ] |
paulb@204 | 1144 | |
paulb@204 | 1145 | for node in list.nodes: |
paulb@204 | 1146 | subprogram.code.append( |
paulb@204 | 1147 | InvokeFunction( |
paulb@204 | 1148 | list, |
paulb@204 | 1149 | expr=LoadAttr( |
paulb@204 | 1150 | expr=LoadTemp(), |
paulb@204 | 1151 | name="append" |
paulb@204 | 1152 | ), |
paulb@204 | 1153 | args=[self.dispatch(node)], |
paulb@204 | 1154 | star=None, |
paulb@204 | 1155 | dstar=None |
paulb@204 | 1156 | ) |
paulb@204 | 1157 | ) |
paulb@204 | 1158 | |
paulb@204 | 1159 | subprogram.code.append( |
paulb@204 | 1160 | ReturnFromBlock( |
paulb@204 | 1161 | expr=LoadTemp(release=1) |
paulb@204 | 1162 | ) |
paulb@204 | 1163 | ) |
paulb@204 | 1164 | |
paulb@204 | 1165 | self.current_subprograms.pop() |
paulb@204 | 1166 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@204 | 1167 | |
paulb@204 | 1168 | # Make an invocation of the subprogram. |
paulb@204 | 1169 | |
paulb@204 | 1170 | result = InvokeRef(list, 1, |
paulb@204 | 1171 | produces_result=1, |
paulb@204 | 1172 | ref=subprogram |
paulb@204 | 1173 | ) |
paulb@204 | 1174 | return result |
paulb@137 | 1175 | |
paulb@201 | 1176 | def visitListComp(self, listcomp): |
paulb@201 | 1177 | |
paulb@201 | 1178 | # Make a subprogram for the list comprehension and record it outside the |
paulb@201 | 1179 | # main tree. |
paulb@201 | 1180 | |
paulb@201 | 1181 | subprogram = Subprogram(listcomp, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=listcomp) |
paulb@201 | 1182 | self.current_subprograms.append(subprogram) |
paulb@201 | 1183 | |
paulb@201 | 1184 | # Make nice annotations for the viewer. |
paulb@201 | 1185 | |
paulb@201 | 1186 | listcomp._subprogram = subprogram |
paulb@201 | 1187 | |
paulb@201 | 1188 | # Add a temporary variable. |
paulb@201 | 1189 | # Produce for loops within the subprogram. |
paulb@201 | 1190 | # Return the result. |
paulb@201 | 1191 | |
paulb@201 | 1192 | subprogram.code = [ |
paulb@201 | 1193 | StoreTemp( |
paulb@201 | 1194 | index="listcomp", |
paulb@201 | 1195 | expr=InvokeFunction( |
paulb@201 | 1196 | expr=LoadName(name="list"), |
paulb@201 | 1197 | args=[], |
paulb@201 | 1198 | star=None, |
paulb@201 | 1199 | dstar=None |
paulb@201 | 1200 | ) |
paulb@201 | 1201 | ) |
paulb@201 | 1202 | ] + self._visitListCompFor(listcomp, listcomp.quals) + [ |
paulb@201 | 1203 | ReturnFromBlock( |
paulb@201 | 1204 | expr=LoadTemp( |
paulb@201 | 1205 | index="listcomp", |
paulb@201 | 1206 | release=1 |
paulb@201 | 1207 | ) |
paulb@201 | 1208 | ) |
paulb@201 | 1209 | ] |
paulb@201 | 1210 | |
paulb@201 | 1211 | self.current_subprograms.pop() |
paulb@201 | 1212 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@201 | 1213 | |
paulb@201 | 1214 | # Make an invocation of the subprogram. |
paulb@201 | 1215 | |
paulb@204 | 1216 | result = InvokeRef(listcomp, 1, |
paulb@201 | 1217 | produces_result=1, |
paulb@204 | 1218 | ref=subprogram |
paulb@201 | 1219 | ) |
paulb@201 | 1220 | return result |
paulb@201 | 1221 | |
paulb@201 | 1222 | def _visitListCompFor(self, node, quals): |
paulb@201 | 1223 | qual = quals[0] |
paulb@201 | 1224 | if len(quals) > 1: |
paulb@201 | 1225 | body = self._visitListCompFor(node, quals[1:]) |
paulb@201 | 1226 | if qual.ifs: |
paulb@201 | 1227 | body = self._visitListCompIf(node, qual.ifs, body) |
paulb@202 | 1228 | elif qual.ifs: |
paulb@202 | 1229 | body = self._visitListCompIf(node, qual.ifs) |
paulb@201 | 1230 | else: |
paulb@202 | 1231 | body = self._visitListCompBody(node) |
paulb@201 | 1232 | return [self._visitFor(qual, body)] |
paulb@201 | 1233 | |
paulb@201 | 1234 | def _visitListCompIf(self, node, ifs, expr=None): |
paulb@201 | 1235 | if_ = ifs[0] |
paulb@201 | 1236 | if len(ifs) > 1: |
paulb@201 | 1237 | body = self._visitListCompIf(node, ifs[1:], expr) |
paulb@201 | 1238 | elif expr is None: |
paulb@202 | 1239 | body = self._visitListCompBody(node) |
paulb@201 | 1240 | else: |
paulb@201 | 1241 | body = expr |
paulb@201 | 1242 | return [ |
paulb@201 | 1243 | Conditional(if_, 1, |
paulb@201 | 1244 | test=InvokeFunction( |
paulb@201 | 1245 | if_, |
paulb@201 | 1246 | expr=LoadAttr( |
paulb@201 | 1247 | expr=self.dispatch(if_.test), |
paulb@201 | 1248 | name="__bool__" |
paulb@201 | 1249 | ), |
paulb@201 | 1250 | ), |
paulb@201 | 1251 | body=body, |
paulb@201 | 1252 | else_=[] |
paulb@201 | 1253 | ) |
paulb@201 | 1254 | ] |
paulb@201 | 1255 | |
paulb@202 | 1256 | def _visitListCompBody(self, node): |
paulb@202 | 1257 | return [ |
paulb@202 | 1258 | InvokeFunction( |
paulb@202 | 1259 | expr=LoadAttr( |
paulb@202 | 1260 | expr=LoadTemp(index="listcomp"), |
paulb@202 | 1261 | name="append" |
paulb@202 | 1262 | ), |
paulb@202 | 1263 | args=[self.dispatch(node.expr)], |
paulb@202 | 1264 | star=None, |
paulb@202 | 1265 | dstar=None |
paulb@202 | 1266 | ) |
paulb@202 | 1267 | ] |
paulb@202 | 1268 | |
paulb@157 | 1269 | def visitMod(self, mod): |
paulb@157 | 1270 | return self._visitBinary(mod, "__mod__", "__rmod__") |
paulb@157 | 1271 | |
paulb@137 | 1272 | def visitMul(self, mul): |
paulb@137 | 1273 | return self._visitBinary(mul, "__mul__", "__rmul__") |
paulb@137 | 1274 | |
paulb@137 | 1275 | def visitName(self, name): |
paulb@137 | 1276 | result = LoadName(name, 1, name=name.name) |
paulb@137 | 1277 | return result |
paulb@137 | 1278 | |
paulb@137 | 1279 | def _visitNot(self, expr, not_=None): |
paulb@137 | 1280 | invocation = InvokeFunction( |
paulb@185 | 1281 | not_, # NOTE: May need a real original node. |
paulb@137 | 1282 | expr=LoadAttr( |
paulb@137 | 1283 | expr=expr, |
paulb@137 | 1284 | name="__bool__" |
paulb@137 | 1285 | ), |
paulb@137 | 1286 | ) |
paulb@137 | 1287 | if not_ is not None: |
paulb@137 | 1288 | result = Not(not_, 1, expr=invocation) |
paulb@137 | 1289 | else: |
paulb@137 | 1290 | result = Not(expr=invocation) |
paulb@137 | 1291 | return result |
paulb@137 | 1292 | |
paulb@137 | 1293 | def visitNot(self, not_): |
paulb@137 | 1294 | return self._visitNot(self.dispatch(not_.expr), not_) |
paulb@137 | 1295 | |
paulb@137 | 1296 | def visitOr(self, or_): |
paulb@137 | 1297 | |
paulb@137 | 1298 | """ |
paulb@137 | 1299 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@137 | 1300 | subprogram. Convert... |
paulb@137 | 1301 | |
paulb@137 | 1302 | Or (test) |
paulb@137 | 1303 | (test) |
paulb@137 | 1304 | ... |
paulb@137 | 1305 | |
paulb@137 | 1306 | ...to: |
paulb@137 | 1307 | |
paulb@137 | 1308 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1309 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1310 | (else) -> ... |
paulb@137 | 1311 | """ |
paulb@137 | 1312 | |
paulb@137 | 1313 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1314 | self.current_subprograms.append(subprogram) |
paulb@137 | 1315 | |
paulb@137 | 1316 | # In the subprogram, make instructions which store each operand, test |
paulb@137 | 1317 | # for each operand's truth status, and if appropriate return from the |
paulb@137 | 1318 | # subprogram with the value of the operand. |
paulb@137 | 1319 | |
paulb@137 | 1320 | last = or_.nodes[-1] |
paulb@137 | 1321 | results = nodes = [] |
paulb@137 | 1322 | |
paulb@137 | 1323 | for node in or_.nodes: |
paulb@137 | 1324 | expr = self.dispatch(node) |
paulb@137 | 1325 | |
paulb@137 | 1326 | # Return from the subprogram where the test is satisfied. |
paulb@137 | 1327 | |
paulb@137 | 1328 | if node is not last: |
paulb@137 | 1329 | nodes.append(StoreTemp(expr=expr)) |
paulb@185 | 1330 | invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) |
paulb@137 | 1331 | test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) |
paulb@137 | 1332 | nodes.append(test) |
paulb@137 | 1333 | |
paulb@137 | 1334 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 1335 | |
paulb@137 | 1336 | nodes = test.else_ = [ReleaseTemp()] |
paulb@137 | 1337 | |
paulb@137 | 1338 | # For the last operation, return the result. |
paulb@137 | 1339 | |
paulb@137 | 1340 | else: |
paulb@137 | 1341 | nodes.append( |
paulb@137 | 1342 | ReturnFromBlock(expr=expr) |
paulb@137 | 1343 | ) |
paulb@137 | 1344 | |
paulb@137 | 1345 | # Finish the subprogram definition. |
paulb@137 | 1346 | |
paulb@137 | 1347 | subprogram.code = results |
paulb@137 | 1348 | |
paulb@137 | 1349 | self.current_subprograms.pop() |
paulb@137 | 1350 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1351 | |
paulb@137 | 1352 | # Make an invocation of the subprogram. |
paulb@137 | 1353 | |
paulb@204 | 1354 | result = InvokeRef(or_, 1, |
paulb@201 | 1355 | produces_result=1, |
paulb@204 | 1356 | ref=subprogram |
paulb@201 | 1357 | ) |
paulb@137 | 1358 | return result |
paulb@137 | 1359 | |
paulb@137 | 1360 | def visitPass(self, pass_): |
paulb@137 | 1361 | return Pass(pass_, 1) |
paulb@137 | 1362 | |
paulb@141 | 1363 | def visitPower(self, power): |
paulb@141 | 1364 | return self._visitBinary(power, "__pow__", "__rpow__") |
paulb@141 | 1365 | |
paulb@147 | 1366 | def visitPrint(self, print_): |
paulb@147 | 1367 | |
paulb@147 | 1368 | """ |
paulb@147 | 1369 | Convert... |
paulb@147 | 1370 | |
paulb@147 | 1371 | Print (dest) -> |
paulb@147 | 1372 | (nodes) |
paulb@147 | 1373 | |
paulb@147 | 1374 | ...to: |
paulb@147 | 1375 | |
paulb@147 | 1376 | StoreTemp (index) -> "print" |
paulb@147 | 1377 | (expr) -> LoadAttr (expr) -> (dest) |
paulb@147 | 1378 | (name) -> "write" |
paulb@147 | 1379 | InvokeFunction (expr) -> LoadTemp (index) -> "print" |
paulb@147 | 1380 | (args) -> [(node)] |
paulb@147 | 1381 | ReleaseTemp (index) -> "print" |
paulb@147 | 1382 | """ |
paulb@147 | 1383 | |
paulb@147 | 1384 | if print_.dest is not None: |
paulb@147 | 1385 | dest = self.dispatch(print_.dest) |
paulb@147 | 1386 | else: |
paulb@147 | 1387 | dest = self.dispatch(compiler.ast.Name("stdout")) |
paulb@147 | 1388 | |
paulb@147 | 1389 | result = Assign(print_, 1, |
paulb@147 | 1390 | code=[ |
paulb@147 | 1391 | StoreTemp( |
paulb@147 | 1392 | index="print", |
paulb@147 | 1393 | expr=LoadAttr( |
paulb@147 | 1394 | expr=dest, |
paulb@147 | 1395 | name="write" |
paulb@147 | 1396 | ) |
paulb@147 | 1397 | ) |
paulb@147 | 1398 | ] |
paulb@147 | 1399 | ) |
paulb@147 | 1400 | |
paulb@147 | 1401 | for node in print_.nodes: |
paulb@147 | 1402 | result.code.append( |
paulb@147 | 1403 | InvokeFunction( |
paulb@185 | 1404 | print_, |
paulb@147 | 1405 | expr=LoadTemp(index="print"), |
paulb@147 | 1406 | args=[self.dispatch(node)], |
paulb@147 | 1407 | star=None, |
paulb@147 | 1408 | dstar=None |
paulb@147 | 1409 | ) |
paulb@147 | 1410 | ) |
paulb@147 | 1411 | |
paulb@147 | 1412 | result.code.append( |
paulb@147 | 1413 | ReleaseTemp(index="print") |
paulb@147 | 1414 | ) |
paulb@147 | 1415 | |
paulb@147 | 1416 | return result |
paulb@147 | 1417 | |
paulb@147 | 1418 | def visitPrintnl(self, printnl): |
paulb@147 | 1419 | result = self.visitPrint(printnl) |
paulb@147 | 1420 | result.code.insert( |
paulb@147 | 1421 | len(result.code) - 1, |
paulb@147 | 1422 | InvokeFunction( |
paulb@185 | 1423 | printnl, |
paulb@147 | 1424 | expr=LoadTemp(index="print"), |
paulb@147 | 1425 | args=[self.dispatch(compiler.ast.Const("\n"))], |
paulb@147 | 1426 | star=None, |
paulb@147 | 1427 | dstar=None |
paulb@147 | 1428 | ) |
paulb@147 | 1429 | ) |
paulb@147 | 1430 | return result |
paulb@147 | 1431 | |
paulb@137 | 1432 | def visitRaise(self, raise_): |
paulb@137 | 1433 | result = Raise(raise_, 1) |
paulb@137 | 1434 | if raise_.expr2 is None: |
paulb@137 | 1435 | result.expr = self.dispatch(raise_.expr1) |
paulb@137 | 1436 | else: |
paulb@137 | 1437 | result.expr = InvokeFunction( |
paulb@185 | 1438 | raise_, |
paulb@137 | 1439 | expr=self.dispatch(raise_.expr1), |
paulb@137 | 1440 | args=[self.dispatch(raise_.expr2)], |
paulb@137 | 1441 | star=None, |
paulb@137 | 1442 | dstar=None |
paulb@137 | 1443 | ) |
paulb@137 | 1444 | if raise_.expr3 is not None: |
paulb@137 | 1445 | result.traceback = self.dispatch(raise_.expr3) |
paulb@137 | 1446 | else: |
paulb@137 | 1447 | result.traceback = None |
paulb@137 | 1448 | return result |
paulb@137 | 1449 | |
paulb@137 | 1450 | def visitReturn(self, return_): |
paulb@137 | 1451 | result = ReturnFromFunction(return_, 1, |
paulb@137 | 1452 | expr=self.dispatch(return_.value) |
paulb@137 | 1453 | ) |
paulb@137 | 1454 | return result |
paulb@137 | 1455 | |
paulb@137 | 1456 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@137 | 1457 | if flags == "OP_ASSIGN": |
paulb@137 | 1458 | result = InvokeFunction(slice, 1, |
paulb@137 | 1459 | expr=LoadAttr( |
paulb@137 | 1460 | expr=expr, |
paulb@137 | 1461 | name="__setslice__" |
paulb@137 | 1462 | ), |
paulb@137 | 1463 | star=None, |
paulb@137 | 1464 | dstar=None, |
paulb@137 | 1465 | args=[lower, upper, value] |
paulb@137 | 1466 | ) |
paulb@137 | 1467 | elif flags == "OP_APPLY": |
paulb@137 | 1468 | args = [] |
paulb@137 | 1469 | result = InvokeFunction(slice, 1, |
paulb@137 | 1470 | expr=LoadAttr( |
paulb@137 | 1471 | expr=expr, |
paulb@137 | 1472 | name="__getslice__" |
paulb@137 | 1473 | ), |
paulb@137 | 1474 | star=None, |
paulb@137 | 1475 | dstar=None, |
paulb@137 | 1476 | args=[lower, upper] |
paulb@137 | 1477 | ) |
paulb@137 | 1478 | elif flags == "OP_DELETE": |
paulb@137 | 1479 | args = [] |
paulb@137 | 1480 | result = InvokeFunction(slice, 1, |
paulb@137 | 1481 | expr=LoadAttr( |
paulb@137 | 1482 | expr=expr, |
paulb@137 | 1483 | name="__delslice__" |
paulb@137 | 1484 | ), |
paulb@137 | 1485 | star=None, |
paulb@137 | 1486 | dstar=None, |
paulb@137 | 1487 | args=[lower, upper] |
paulb@137 | 1488 | ) |
paulb@137 | 1489 | else: |
paulb@137 | 1490 | raise NotImplementedError, flags |
paulb@137 | 1491 | |
paulb@137 | 1492 | return result |
paulb@137 | 1493 | |
paulb@137 | 1494 | def visitSlice(self, slice, in_sequence=0): |
paulb@137 | 1495 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@137 | 1496 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) |
paulb@137 | 1497 | |
paulb@201 | 1498 | def visitSliceobj(self, sliceobj): |
paulb@201 | 1499 | return InvokeFunction(sliceobj, 1, |
paulb@201 | 1500 | expr=LoadName(name="slice"), |
paulb@201 | 1501 | args=self.dispatches(sliceobj.nodes), |
paulb@201 | 1502 | star=None, |
paulb@201 | 1503 | dstar=None |
paulb@201 | 1504 | ) |
paulb@201 | 1505 | |
paulb@137 | 1506 | def visitStmt(self, stmt): |
paulb@137 | 1507 | return self.dispatches(stmt.nodes) |
paulb@137 | 1508 | |
paulb@137 | 1509 | def visitSub(self, sub): |
paulb@137 | 1510 | return self._visitBinary(sub, "__sub__", "__rsub__") |
paulb@137 | 1511 | |
paulb@137 | 1512 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@137 | 1513 | if flags == "OP_ASSIGN": |
paulb@137 | 1514 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1515 | expr=LoadAttr( |
paulb@137 | 1516 | expr=expr, |
paulb@137 | 1517 | name="__setitem__" |
paulb@137 | 1518 | ), |
paulb@137 | 1519 | star=None, |
paulb@137 | 1520 | dstar=None, |
paulb@137 | 1521 | args=[subs, value] |
paulb@137 | 1522 | ) |
paulb@137 | 1523 | elif flags == "OP_APPLY": |
paulb@137 | 1524 | args = [] |
paulb@137 | 1525 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1526 | expr=LoadAttr( |
paulb@137 | 1527 | expr=expr, |
paulb@137 | 1528 | name="__getitem__" |
paulb@137 | 1529 | ), |
paulb@137 | 1530 | star=None, |
paulb@137 | 1531 | dstar=None, |
paulb@137 | 1532 | args=[subs] |
paulb@137 | 1533 | ) |
paulb@137 | 1534 | elif flags == "OP_DELETE": |
paulb@137 | 1535 | args = [] |
paulb@137 | 1536 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1537 | expr=LoadAttr( |
paulb@137 | 1538 | expr=expr, |
paulb@137 | 1539 | name="__delitem__" |
paulb@137 | 1540 | ), |
paulb@137 | 1541 | star=None, |
paulb@137 | 1542 | dstar=None, |
paulb@137 | 1543 | args=[subs] |
paulb@137 | 1544 | ) |
paulb@137 | 1545 | else: |
paulb@137 | 1546 | raise NotImplementedError, flags |
paulb@137 | 1547 | |
paulb@137 | 1548 | return result |
paulb@137 | 1549 | |
paulb@137 | 1550 | def _visitSubscriptSubs(self, node, subs): |
paulb@137 | 1551 | if len(subs) == 1: |
paulb@137 | 1552 | return self.dispatch(subs[0]) |
paulb@137 | 1553 | else: |
paulb@137 | 1554 | return InvokeFunction(node, 1, |
paulb@137 | 1555 | expr=LoadName(name="tuple"), |
paulb@137 | 1556 | args=self.dispatches(subs), |
paulb@137 | 1557 | star=None, |
paulb@137 | 1558 | dstar=None |
paulb@137 | 1559 | ) |
paulb@137 | 1560 | |
paulb@137 | 1561 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@137 | 1562 | return self._visitSubscript( |
paulb@137 | 1563 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@137 | 1564 | self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@137 | 1565 | ) |
paulb@137 | 1566 | |
paulb@137 | 1567 | def visitTryExcept(self, tryexcept): |
paulb@137 | 1568 | |
paulb@137 | 1569 | """ |
paulb@137 | 1570 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@137 | 1571 | |
paulb@137 | 1572 | Convert... |
paulb@137 | 1573 | |
paulb@137 | 1574 | TryExcept (body) |
paulb@137 | 1575 | (else) |
paulb@137 | 1576 | (spec/assign/stmt) |
paulb@137 | 1577 | ... |
paulb@137 | 1578 | |
paulb@137 | 1579 | ...to: |
paulb@137 | 1580 | |
paulb@137 | 1581 | Try (body) |
paulb@137 | 1582 | (else) |
paulb@137 | 1583 | (handler) -> Conditional (test) -> (stmt) |
paulb@190 | 1584 | (body) -> ResetExc ... |
paulb@137 | 1585 | (else) -> Conditional (test) -> (stmt) |
paulb@190 | 1586 | (body) -> ResetExc ... |
paulb@137 | 1587 | (else) -> ... |
paulb@137 | 1588 | """ |
paulb@137 | 1589 | |
paulb@137 | 1590 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@137 | 1591 | |
paulb@137 | 1592 | if tryexcept.body is not None: |
paulb@137 | 1593 | result.body = self.dispatch(tryexcept.body) |
paulb@137 | 1594 | if tryexcept.else_ is not None: |
paulb@137 | 1595 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@137 | 1596 | |
paulb@137 | 1597 | results = nodes = [] |
paulb@137 | 1598 | catch_all = 0 |
paulb@137 | 1599 | |
paulb@137 | 1600 | for spec, assign, stmt in tryexcept.handlers: |
paulb@137 | 1601 | |
paulb@137 | 1602 | # If no specification exists, produce an unconditional block. |
paulb@137 | 1603 | |
paulb@137 | 1604 | if spec is None: |
paulb@137 | 1605 | nodes += self.dispatch(stmt) |
paulb@137 | 1606 | catch_all = 1 |
paulb@137 | 1607 | |
paulb@137 | 1608 | # Produce an exception value check. |
paulb@137 | 1609 | |
paulb@137 | 1610 | else: |
paulb@137 | 1611 | test = Conditional( |
paulb@137 | 1612 | isolate_test=1, |
paulb@193 | 1613 | test=CheckType(expr=LoadExc(), choices=self._visitTryExcept(spec)) |
paulb@137 | 1614 | ) |
paulb@137 | 1615 | test.body = [] |
paulb@137 | 1616 | |
paulb@137 | 1617 | if assign is not None: |
paulb@137 | 1618 | test.body.append( |
paulb@137 | 1619 | Assign( |
paulb@137 | 1620 | code=[ |
paulb@137 | 1621 | StoreTemp(expr=LoadExc()), |
paulb@137 | 1622 | self.dispatch(assign), |
paulb@137 | 1623 | ReleaseTemp() |
paulb@137 | 1624 | ] |
paulb@137 | 1625 | ) |
paulb@137 | 1626 | ) |
paulb@137 | 1627 | |
paulb@190 | 1628 | test.body += [ResetExc()] + self.dispatch(stmt) |
paulb@137 | 1629 | nodes.append(test) |
paulb@137 | 1630 | nodes = test.else_ = [] |
paulb@137 | 1631 | |
paulb@137 | 1632 | # Add a raise operation to deal with unhandled exceptions. |
paulb@137 | 1633 | |
paulb@137 | 1634 | if not catch_all: |
paulb@137 | 1635 | nodes.append( |
paulb@137 | 1636 | Raise( |
paulb@137 | 1637 | expr=LoadExc()) |
paulb@137 | 1638 | ) |
paulb@137 | 1639 | |
paulb@137 | 1640 | result.handler = results |
paulb@137 | 1641 | return result |
paulb@137 | 1642 | |
paulb@137 | 1643 | def _visitTryExcept(self, spec): |
paulb@137 | 1644 | |
paulb@137 | 1645 | "Return a list of nodes for the given exception type 'spec'." |
paulb@137 | 1646 | |
paulb@137 | 1647 | if isinstance(spec, compiler.ast.Tuple): |
paulb@137 | 1648 | nodes = [] |
paulb@137 | 1649 | for node in spec.nodes: |
paulb@137 | 1650 | nodes += self._visitTryExcept(node) |
paulb@137 | 1651 | else: |
paulb@137 | 1652 | nodes = [self.dispatch(spec)] |
paulb@137 | 1653 | return nodes |
paul@0 | 1654 | |
paul@0 | 1655 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1656 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1657 | if tryfinally.body is not None: |
paul@0 | 1658 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1659 | if tryfinally.final is not None: |
paul@0 | 1660 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1661 | return result |
paul@0 | 1662 | |
paulb@137 | 1663 | def visitTuple(self, tuple): |
paulb@137 | 1664 | return self._visitBuiltin(tuple, "tuple") |
paulb@137 | 1665 | |
paulb@137 | 1666 | def visitUnaryAdd(self, unaryadd): |
paulb@137 | 1667 | return self._visitUnary(unaryadd, "__pos__") |
paulb@137 | 1668 | |
paulb@137 | 1669 | def visitUnarySub(self, unarysub): |
paulb@137 | 1670 | return self._visitUnary(unarysub, "__neg__") |
paulb@137 | 1671 | |
paulb@137 | 1672 | def visitWhile(self, while_): |
paulb@137 | 1673 | |
paulb@137 | 1674 | """ |
paulb@137 | 1675 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@137 | 1676 | subprogram. Convert... |
paulb@137 | 1677 | |
paulb@137 | 1678 | While (test) -> (body) |
paulb@137 | 1679 | (else) |
paulb@137 | 1680 | |
paulb@137 | 1681 | ...to: |
paulb@137 | 1682 | |
paulb@137 | 1683 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@137 | 1684 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1685 | (else) -> ... |
paulb@137 | 1686 | """ |
paulb@137 | 1687 | |
paulb@137 | 1688 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@137 | 1689 | self.current_subprograms.append(subprogram) |
paulb@137 | 1690 | |
paulb@137 | 1691 | # Include a conditional statement in the subprogram. |
paulb@137 | 1692 | # Inside the conditional, add a recursive invocation to the subprogram |
paulb@137 | 1693 | # if the test condition was satisfied. |
paulb@137 | 1694 | # Return within the main section of the loop. |
paulb@137 | 1695 | |
paulb@147 | 1696 | test = Conditional( |
paulb@147 | 1697 | test=InvokeFunction( |
paulb@185 | 1698 | while_, |
paulb@147 | 1699 | expr=LoadAttr( |
paulb@147 | 1700 | expr=self.dispatch(while_.test), |
paulb@147 | 1701 | name="__bool__"), |
paulb@147 | 1702 | ), |
paulb@147 | 1703 | body=self.dispatch(while_.body) + [ |
paulb@204 | 1704 | InvokeRef( |
paulb@185 | 1705 | while_, |
paulb@204 | 1706 | ref=subprogram |
paulb@147 | 1707 | ), |
paulb@147 | 1708 | ReturnFromBlock() |
paulb@147 | 1709 | ], |
paulb@147 | 1710 | else_=[] |
paulb@147 | 1711 | ) |
paulb@137 | 1712 | |
paulb@137 | 1713 | # Provide the else section, if present, along with an explicit return. |
paulb@137 | 1714 | |
paulb@137 | 1715 | if while_.else_ is not None: |
paulb@137 | 1716 | test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] |
paulb@137 | 1717 | |
paulb@137 | 1718 | # Finish the subprogram definition. |
paulb@137 | 1719 | |
paulb@137 | 1720 | subprogram.code = [test] |
paulb@137 | 1721 | |
paulb@137 | 1722 | self.current_subprograms.pop() |
paulb@137 | 1723 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1724 | |
paulb@137 | 1725 | # Make an invocation of the subprogram. |
paulb@137 | 1726 | |
paulb@204 | 1727 | result = InvokeRef(while_, 1, ref=subprogram) |
paulb@148 | 1728 | |
paulb@148 | 1729 | # Make nice annotations for the viewer. |
paulb@148 | 1730 | |
paulb@148 | 1731 | while_._test_call = subprogram.code[0].test |
paulb@148 | 1732 | |
paulb@137 | 1733 | return result |
paulb@137 | 1734 | |
paulb@203 | 1735 | # NOTE: Not actually supported. |
paulb@203 | 1736 | # NOTE: Virtually the same as visitReturn... |
paulb@203 | 1737 | |
paulb@203 | 1738 | def visitYield(self, yield_): |
paulb@203 | 1739 | result = Yield(yield_, 1, |
paulb@203 | 1740 | expr=self.dispatch(yield_.value) |
paulb@203 | 1741 | ) |
paulb@203 | 1742 | return result |
paulb@203 | 1743 | |
paulb@137 | 1744 | # Convenience methods. |
paulb@137 | 1745 | |
paulb@137 | 1746 | def _visitBinary(self, binary, left_name, right_name): |
paulb@192 | 1747 | return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name) |
paulb@192 | 1748 | |
paulb@193 | 1749 | def _visitBinaryCompareOp(self, binary, left, right, left_name, right_name): |
paulb@193 | 1750 | |
paulb@193 | 1751 | """ |
paulb@193 | 1752 | Emulate the current mechanisms by producing nodes as follows: |
paulb@193 | 1753 | |
paulb@204 | 1754 | InvokeRef -> Subprogram -> StoreTemp (expr) -> x.__lt__(y) |
paulb@193 | 1755 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1756 | (body) -> ReleaseTemp |
paulb@193 | 1757 | StoreTemp (expr) -> y.__gt__(x) |
paulb@193 | 1758 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1759 | (body) -> ReturnFromBlock (expr) -> False |
paulb@193 | 1760 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1761 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1762 | """ |
paulb@193 | 1763 | |
paulb@193 | 1764 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@193 | 1765 | self.current_subprograms.append(subprogram) |
paulb@193 | 1766 | |
paulb@193 | 1767 | subprogram.code = [ |
paulb@193 | 1768 | StoreTemp( |
paulb@193 | 1769 | expr=InvokeFunction( |
paulb@193 | 1770 | binary, |
paulb@193 | 1771 | expr=LoadAttr(expr=left, name=left_name), |
paulb@193 | 1772 | args=[right], |
paulb@193 | 1773 | star=None, |
paulb@193 | 1774 | dstar=None) |
paulb@193 | 1775 | ), |
paulb@193 | 1776 | Conditional( |
paulb@193 | 1777 | isolate_test=1, |
paulb@193 | 1778 | test=CheckType( |
paulb@193 | 1779 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1780 | ), |
paulb@193 | 1781 | body=[ |
paulb@193 | 1782 | ReleaseTemp(), |
paulb@193 | 1783 | StoreTemp( |
paulb@193 | 1784 | expr=InvokeFunction( |
paulb@193 | 1785 | binary, |
paulb@193 | 1786 | expr=LoadAttr(expr=right, name=right_name), |
paulb@193 | 1787 | args=[left], |
paulb@193 | 1788 | star=None, |
paulb@193 | 1789 | dstar=None) |
paulb@193 | 1790 | ), |
paulb@193 | 1791 | Conditional( |
paulb@193 | 1792 | isolate_test=1, |
paulb@193 | 1793 | test=CheckType( |
paulb@193 | 1794 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1795 | ), |
paulb@193 | 1796 | body=[ |
paulb@193 | 1797 | ReturnFromBlock( |
paulb@193 | 1798 | expr=LoadName(name="False") |
paulb@193 | 1799 | ) |
paulb@193 | 1800 | ], |
paulb@193 | 1801 | else_=[ |
paulb@193 | 1802 | CheckType( |
paulb@193 | 1803 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1804 | ), |
paulb@193 | 1805 | ReturnFromBlock( |
paulb@193 | 1806 | expr=LoadTemp() |
paulb@193 | 1807 | ) |
paulb@193 | 1808 | ] |
paulb@193 | 1809 | ) |
paulb@193 | 1810 | ], |
paulb@193 | 1811 | else_=[ |
paulb@193 | 1812 | CheckType( |
paulb@193 | 1813 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1814 | ), |
paulb@193 | 1815 | ReturnFromBlock( |
paulb@193 | 1816 | expr=LoadTemp() |
paulb@193 | 1817 | ) |
paulb@193 | 1818 | ] |
paulb@193 | 1819 | ) |
paulb@193 | 1820 | ] |
paulb@193 | 1821 | |
paulb@193 | 1822 | self.current_subprograms.pop() |
paulb@193 | 1823 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@193 | 1824 | |
paulb@204 | 1825 | result = InvokeRef( |
paulb@193 | 1826 | binary, |
paulb@193 | 1827 | produces_result=1, |
paulb@204 | 1828 | ref=subprogram |
paulb@193 | 1829 | ) |
paulb@193 | 1830 | |
paulb@193 | 1831 | # Make nice annotations for the viewer. |
paulb@193 | 1832 | |
paulb@193 | 1833 | binary._left_call = subprogram.code[0].expr |
paulb@193 | 1834 | binary._right_call = subprogram.code[1].body[1].expr |
paulb@193 | 1835 | |
paulb@193 | 1836 | return result |
paulb@193 | 1837 | |
paulb@192 | 1838 | def _visitBinaryOp(self, binary, left, right, left_name, right_name): |
paulb@137 | 1839 | |
paulb@137 | 1840 | """ |
paulb@137 | 1841 | Emulate the current mechanisms by producing nodes as follows: |
paulb@137 | 1842 | |
paulb@204 | 1843 | InvokeRef -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) |
paulb@137 | 1844 | (else) |
paulb@193 | 1845 | (handler) -> Conditional (test) -> CheckType (expr) -> LoadExc |
paulb@137 | 1846 | (choices) -> LoadName TypeError |
paulb@137 | 1847 | (body) -> ReturnFromBlock (expr) -> y.__radd__(x) |
paulb@137 | 1848 | (else) |
paulb@137 | 1849 | """ |
paulb@137 | 1850 | |
paulb@137 | 1851 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1852 | self.current_subprograms.append(subprogram) |
paulb@137 | 1853 | |
paulb@137 | 1854 | subprogram.code = [ |
paulb@137 | 1855 | Try(binary, 1, |
paulb@137 | 1856 | body=[ |
paulb@137 | 1857 | ReturnFromBlock( |
paulb@137 | 1858 | expr=InvokeFunction( |
paulb@185 | 1859 | binary, |
paulb@192 | 1860 | expr=LoadAttr(expr=left, name=left_name), |
paulb@192 | 1861 | args=[right], |
paulb@137 | 1862 | star=None, |
paulb@137 | 1863 | dstar=None) |
paulb@137 | 1864 | ) |
paulb@137 | 1865 | ], |
paulb@137 | 1866 | else_=[], |
paulb@137 | 1867 | finally_=[], |
paulb@137 | 1868 | handler=[ |
paulb@137 | 1869 | Conditional( |
paulb@193 | 1870 | test=CheckType(expr=LoadExc(), choices=[LoadName(name="TypeError")]), |
paulb@137 | 1871 | body=[ |
paulb@137 | 1872 | ReturnFromBlock( |
paulb@137 | 1873 | expr=InvokeFunction( |
paulb@185 | 1874 | binary, |
paulb@192 | 1875 | expr=LoadAttr(expr=right, name=right_name), |
paulb@192 | 1876 | args=[left], |
paulb@137 | 1877 | star=None, |
paulb@137 | 1878 | dstar=None) |
paulb@137 | 1879 | ) |
paulb@137 | 1880 | ], |
paulb@137 | 1881 | else_=[] |
paulb@137 | 1882 | ) |
paulb@137 | 1883 | ] |
paulb@137 | 1884 | ) |
paulb@137 | 1885 | ] |
paulb@137 | 1886 | |
paulb@137 | 1887 | self.current_subprograms.pop() |
paulb@137 | 1888 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1889 | |
paulb@204 | 1890 | result = InvokeRef( |
paulb@185 | 1891 | binary, |
paulb@148 | 1892 | produces_result=1, |
paulb@204 | 1893 | ref=subprogram |
paulb@148 | 1894 | ) |
paulb@148 | 1895 | |
paulb@148 | 1896 | # Make nice annotations for the viewer. |
paulb@148 | 1897 | |
paulb@148 | 1898 | binary._left_call = subprogram.code[0].body[0].expr |
paulb@148 | 1899 | binary._right_call = subprogram.code[0].handler[0].body[0].expr |
paulb@148 | 1900 | |
paulb@137 | 1901 | return result |
paulb@137 | 1902 | |
paulb@137 | 1903 | def _visitBuiltin(self, builtin, name): |
paulb@137 | 1904 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) |
paulb@137 | 1905 | return result |
paulb@137 | 1906 | |
paulb@137 | 1907 | def _visitUnary(self, unary, name): |
paulb@148 | 1908 | result = InvokeFunction(unary, 1, |
paulb@137 | 1909 | expr=LoadAttr( |
paulb@137 | 1910 | expr=self.dispatch(unary.expr), |
paulb@137 | 1911 | name=name |
paulb@148 | 1912 | ) |
paulb@137 | 1913 | ) |
paulb@137 | 1914 | |
paulb@148 | 1915 | # Make nice annotations for the viewer. |
paulb@148 | 1916 | |
paulb@148 | 1917 | unary._unary_call = result |
paulb@148 | 1918 | |
paulb@148 | 1919 | return result |
paulb@148 | 1920 | |
paulb@57 | 1921 | # Convenience functions. |
paulb@57 | 1922 | |
paulb@158 | 1923 | def simplify(filename, builtins=0, module_name=None): |
paulb@57 | 1924 | |
paulb@57 | 1925 | """ |
paulb@88 | 1926 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1927 | |
paulb@88 | 1928 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 1929 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 1930 | """ |
paulb@57 | 1931 | |
paulb@57 | 1932 | simplifier = Simplifier(builtins) |
paulb@57 | 1933 | module = compiler.parseFile(filename) |
paulb@57 | 1934 | compiler.misc.set_filename(filename, module) |
paulb@88 | 1935 | if builtins: |
paulb@158 | 1936 | name = module_name or "__builtins__" |
paulb@88 | 1937 | else: |
paulb@88 | 1938 | path, ext = os.path.splitext(filename) |
paulb@88 | 1939 | path, name = os.path.split(path) |
paulb@158 | 1940 | name = module_name or name |
paulb@88 | 1941 | simplified = simplifier.process(module, name) |
paulb@57 | 1942 | return simplified |
paulb@57 | 1943 | |
paul@0 | 1944 | # vim: tabstop=4 expandtab shiftwidth=4 |