paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@24 | 4 | Simplify AST structures for easier type propagation and analysis. The code in |
paulb@24 | 5 | this module processes AST trees originating from the compiler module and |
paulb@24 | 6 | produces a result tree consisting of instruction-oriented program nodes. |
paul@0 | 7 | |
paul@0 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This software is free software; you can redistribute it and/or |
paul@0 | 11 | modify it under the terms of the GNU General Public License as |
paul@0 | 12 | published by the Free Software Foundation; either version 2 of |
paul@0 | 13 | the License, or (at your option) any later version. |
paul@0 | 14 | |
paul@0 | 15 | This software is distributed in the hope that it will be useful, |
paul@0 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 18 | GNU General Public License for more details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public |
paul@0 | 21 | License along with this library; see the file LICENCE.txt |
paul@0 | 22 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 24 | |
paulb@46 | 25 | -------- |
paulb@46 | 26 | |
paulb@57 | 27 | To use this module, the easiest approach is to use the simplify function: |
paulb@57 | 28 | |
paulb@57 | 29 | simplify(filename) |
paulb@57 | 30 | |
paulb@57 | 31 | The more complicated approach involves first instantiating a Simplifier object: |
paulb@46 | 32 | |
paulb@46 | 33 | simplifier = Simplifier() |
paulb@46 | 34 | |
paulb@57 | 35 | Then, applying the simplifier to an AST tree: |
paulb@46 | 36 | |
paulb@57 | 37 | module = compiler.parseFile(filename) |
paulb@46 | 38 | simplifier.process(module) |
paul@0 | 39 | """ |
paul@0 | 40 | |
paulb@19 | 41 | from simplified import * |
paul@0 | 42 | import compiler.ast |
paulb@88 | 43 | import os |
paul@0 | 44 | |
paulb@19 | 45 | class Simplifier(Visitor): |
paul@0 | 46 | |
paul@0 | 47 | """ |
paul@0 | 48 | A simplifying visitor for AST nodes. |
paul@0 | 49 | |
paulb@134 | 50 | Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, |
paulb@134 | 51 | Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, |
paulb@136 | 52 | Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, |
paulb@136 | 53 | Invert, Keyword, Lambda, List, Module, Mul, Name, Not, Or, Pass, |
paulb@136 | 54 | Raise, Return, Slice, Stmt, Sub, Subscript, TryExcept, TryFinally, |
paulb@136 | 55 | Tuple, While, UnaryAdd, UnarySub. |
paul@0 | 56 | |
paulb@136 | 57 | Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, |
paulb@136 | 58 | Exec, LeftShift, ListComp, ListCompFor, ListCompIf, |
paulb@136 | 59 | Power, Print, Printnl, RightShift, Sliceobj, Yield. |
paul@0 | 60 | """ |
paul@0 | 61 | |
paulb@34 | 62 | def __init__(self, builtins=0): |
paulb@46 | 63 | |
paulb@46 | 64 | """ |
paulb@46 | 65 | Initialise the simplifier with the optional 'builtins' parameter |
paulb@46 | 66 | indicating whether the module contains the built-in classes and |
paulb@46 | 67 | functions. |
paulb@46 | 68 | """ |
paulb@46 | 69 | |
paulb@19 | 70 | Visitor.__init__(self) |
paul@1 | 71 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 72 | self.structures = [] # Structures/classes. |
paulb@24 | 73 | self.constants = {} # Constants. |
paul@1 | 74 | self.current_subprograms = [] # Current subprograms being processed. |
paulb@120 | 75 | self.current_structures = [] # Current structures being processed. |
paulb@34 | 76 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 77 | |
paulb@65 | 78 | # Convenience attributes. |
paulb@65 | 79 | |
paulb@65 | 80 | self.subnames = {} |
paulb@65 | 81 | |
paulb@51 | 82 | # For compiler package mechanisms. |
paulb@51 | 83 | |
paulb@51 | 84 | self.visitor = self |
paulb@51 | 85 | |
paulb@88 | 86 | def process(self, node, name): |
paulb@88 | 87 | result = self.dispatch(node, name) |
paulb@57 | 88 | result.simplifier = self |
paulb@57 | 89 | return result |
paulb@20 | 90 | |
paulb@6 | 91 | def dispatch_or_none(self, node, *args): |
paulb@6 | 92 | if node is not None: |
paulb@6 | 93 | return self.dispatch(node, *args) |
paulb@6 | 94 | else: |
paulb@79 | 95 | return LoadName(node, name="None") |
paulb@6 | 96 | |
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@137 | 226 | expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) |
paulb@137 | 227 | result = Assign(asslist, 1) |
paulb@137 | 228 | store = StoreTemp(expr=InvokeFunction(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[])) |
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@137 | 239 | return InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) |
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@137 | 585 | if not self.constants.has_key(const.value): |
paulb@137 | 586 | self.constants[const.value] = Constant(name=repr(const.value), value=const.value) |
paulb@137 | 587 | result = LoadRef(const, 1, ref=self.constants[const.value]) |
paulb@137 | 588 | return result |
paulb@137 | 589 | |
paulb@137 | 590 | def visitContinue(self, continue_): |
paulb@137 | 591 | result = InvokeBlock(continue_, 1, |
paulb@137 | 592 | expr=LoadRef(ref=self.current_subprograms[-1]) |
paulb@137 | 593 | ) |
paulb@137 | 594 | return result |
paulb@137 | 595 | |
paulb@137 | 596 | def visitDict(self, dict): |
paulb@137 | 597 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) |
paulb@137 | 598 | args = [] |
paulb@137 | 599 | for key, value in dict.items: |
paulb@137 | 600 | tuple = InvokeFunction(expr=LoadName(name="tuple"), star=None, dstar=None) |
paulb@137 | 601 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paulb@137 | 602 | args.append(tuple) |
paulb@137 | 603 | result.set_args(args) |
paulb@137 | 604 | return result |
paulb@137 | 605 | |
paulb@137 | 606 | def visitDiscard(self, discard): |
paulb@137 | 607 | return self.dispatch(discard.expr) |
paulb@137 | 608 | |
paulb@137 | 609 | def visitDiv(self, div): |
paulb@137 | 610 | return self._visitBinary(div, "__div__", "__rdiv__") |
paulb@137 | 611 | |
paulb@137 | 612 | def visitFloorDiv(self, floordiv): |
paulb@137 | 613 | return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") |
paulb@137 | 614 | |
paul@0 | 615 | def visitFor(self, for_): |
paul@0 | 616 | |
paulb@35 | 617 | """ |
paulb@35 | 618 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 619 | subprogram. Convert... |
paulb@35 | 620 | |
paulb@35 | 621 | For (assign) |
paulb@35 | 622 | (body) |
paulb@35 | 623 | (else) |
paulb@35 | 624 | |
paulb@35 | 625 | ...to: |
paulb@35 | 626 | |
paulb@35 | 627 | Assign (assign #1) |
paulb@35 | 628 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 629 | (body) |
paulb@35 | 630 | Invoke subprogram |
paulb@35 | 631 | (handler) -> ... |
paulb@35 | 632 | (else) -> ... |
paulb@35 | 633 | """ |
paul@0 | 634 | |
paulb@118 | 635 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 636 | self.current_subprograms.append(subprogram) |
paul@0 | 637 | |
paulb@34 | 638 | # Always return from conditional sections/subprograms. |
paulb@34 | 639 | |
paulb@17 | 640 | if for_.else_ is not None: |
paulb@135 | 641 | else_stmt = self.dispatch(for_.else_) + [ReturnFromBlock()] |
paulb@17 | 642 | else: |
paulb@135 | 643 | else_stmt = [ReturnFromBlock()] |
paulb@17 | 644 | |
paul@0 | 645 | # Wrap the assignment in a try...except statement. |
paulb@135 | 646 | # Inside the body, add a recursive invocation to the subprogram. |
paul@0 | 647 | |
paulb@135 | 648 | subprogram.code = [ |
paulb@135 | 649 | Try( |
paulb@135 | 650 | body=[ |
paulb@135 | 651 | Assign( |
paulb@135 | 652 | code=[ |
paulb@135 | 653 | StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)), |
paulb@135 | 654 | self.dispatch(for_.assign), |
paulb@135 | 655 | ReleaseTemp() |
paulb@135 | 656 | ]) |
paulb@135 | 657 | ] + self.dispatch(for_.body) + [ |
paulb@135 | 658 | InvokeBlock( |
paulb@135 | 659 | expr=LoadRef(ref=subprogram) |
paulb@135 | 660 | ) |
paulb@135 | 661 | ], |
paulb@135 | 662 | handler=[ |
paulb@135 | 663 | Conditional( |
paulb@135 | 664 | test=InvokeFunction( |
paulb@135 | 665 | expr=LoadName(name="isinstance"), |
paulb@135 | 666 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@135 | 667 | star=None, |
paulb@135 | 668 | dstar=None), |
paulb@135 | 669 | body=else_stmt, |
paulb@135 | 670 | else_=[Raise(expr=LoadExc())] |
paulb@135 | 671 | ) |
paulb@135 | 672 | ], |
paulb@135 | 673 | else_=[], |
paulb@135 | 674 | finally_=[] |
paulb@135 | 675 | ), |
paulb@135 | 676 | ReturnFromBlock() |
paulb@135 | 677 | ] |
paul@0 | 678 | |
paulb@35 | 679 | # Finish the subprogram definition. |
paulb@35 | 680 | |
paul@0 | 681 | self.current_subprograms.pop() |
paulb@79 | 682 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 683 | |
paul@0 | 684 | # Obtain an iterator for the sequence involved. |
paul@0 | 685 | # Then, make an invocation of the subprogram. |
paul@0 | 686 | |
paulb@79 | 687 | result = Assign(for_, 1) |
paul@0 | 688 | result.code = [ |
paulb@101 | 689 | StoreTemp( |
paulb@101 | 690 | expr=InvokeFunction( |
paulb@101 | 691 | expr=LoadAttr( |
paulb@79 | 692 | name="__iter__", |
paulb@79 | 693 | expr=self.dispatch(for_.list) |
paulb@79 | 694 | ), |
paulb@79 | 695 | args=[], |
paulb@79 | 696 | star=None, |
paulb@79 | 697 | dstar=None |
paulb@79 | 698 | ) |
paulb@79 | 699 | ), |
paulb@101 | 700 | InvokeBlock(expr=LoadRef(ref=subprogram)), |
paulb@101 | 701 | ReleaseTemp() |
paul@0 | 702 | ] |
paul@0 | 703 | return result |
paul@0 | 704 | |
paulb@137 | 705 | def visitFrom(self, from_): |
paulb@137 | 706 | result = Assign(from_, 1) |
paulb@137 | 707 | code = [] |
paulb@137 | 708 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paulb@137 | 709 | for name, alias in from_.names: |
paulb@137 | 710 | code.append( |
paulb@137 | 711 | StoreName( |
paulb@137 | 712 | expr=LoadAttr( |
paulb@137 | 713 | expr=LoadTemp(), |
paulb@137 | 714 | name=name), |
paulb@137 | 715 | name=(alias or name) |
paulb@137 | 716 | ) |
paulb@137 | 717 | ) |
paulb@137 | 718 | code.append(ReleaseTemp()) |
paulb@137 | 719 | result.code = code |
paulb@137 | 720 | return result |
paulb@137 | 721 | |
paulb@137 | 722 | def _visitFunction(self, function, subprogram): |
paulb@137 | 723 | |
paulb@137 | 724 | """ |
paulb@137 | 725 | A common function generator which transforms the given 'function' node |
paulb@137 | 726 | and initialises the given 'subprogram' appropriately. |
paulb@137 | 727 | """ |
paulb@137 | 728 | |
paulb@137 | 729 | # Discover star and dstar parameters. |
paulb@137 | 730 | |
paulb@137 | 731 | if function.flags & 4 != 0: |
paulb@137 | 732 | has_star = 1 |
paulb@137 | 733 | else: |
paulb@137 | 734 | has_star = 0 |
paulb@137 | 735 | if function.flags & 8 != 0: |
paulb@137 | 736 | has_dstar = 1 |
paulb@137 | 737 | else: |
paulb@137 | 738 | has_dstar = 0 |
paulb@137 | 739 | |
paulb@137 | 740 | # Discover the number of defaults and positional parameters. |
paulb@137 | 741 | |
paulb@137 | 742 | ndefaults = len(function.defaults) |
paulb@137 | 743 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@137 | 744 | |
paulb@137 | 745 | # Produce star and dstar parameters with appropriate defaults. |
paulb@137 | 746 | |
paulb@137 | 747 | if has_star: |
paulb@137 | 748 | star = ( |
paulb@137 | 749 | function.argnames[npositional], |
paulb@137 | 750 | InvokeFunction(expr=LoadName(name="list"), args=[], star=None, dstar=None) |
paulb@137 | 751 | ) |
paulb@137 | 752 | else: |
paulb@137 | 753 | star = None |
paulb@137 | 754 | if has_dstar: |
paulb@137 | 755 | dstar = ( |
paulb@137 | 756 | function.argnames[npositional + has_star], |
paulb@137 | 757 | InvokeFunction(expr=LoadName(name="dict"), args=[], star=None, dstar=None) |
paulb@137 | 758 | ) |
paulb@137 | 759 | else: |
paulb@137 | 760 | dstar = None |
paulb@137 | 761 | |
paulb@137 | 762 | params = [] |
paulb@137 | 763 | for i in range(0, npositional - ndefaults): |
paulb@137 | 764 | params.append((function.argnames[i], None)) |
paulb@137 | 765 | |
paulb@137 | 766 | # Process defaults. |
paulb@137 | 767 | |
paulb@137 | 768 | for i in range(0, ndefaults): |
paulb@137 | 769 | default = function.defaults[i] |
paulb@137 | 770 | if default is not None: |
paulb@137 | 771 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paulb@137 | 772 | else: |
paulb@137 | 773 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paulb@137 | 774 | |
paulb@137 | 775 | subprogram.params = params |
paulb@137 | 776 | subprogram.star = star |
paulb@137 | 777 | subprogram.dstar = dstar |
paulb@137 | 778 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 779 | |
paulb@137 | 780 | def visitFunction(self, function): |
paulb@137 | 781 | |
paulb@137 | 782 | """ |
paulb@137 | 783 | Make a subprogram for the 'function' and record it outside the main |
paulb@137 | 784 | tree. Produce something like the following: |
paulb@137 | 785 | |
paulb@137 | 786 | StoreName (name) |
paulb@137 | 787 | (expr) -> Subprogram (params) |
paulb@137 | 788 | (star) |
paulb@137 | 789 | (dstar) |
paulb@137 | 790 | """ |
paulb@137 | 791 | |
paulb@137 | 792 | subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@137 | 793 | internal=0, returns_value=1, star=None, dstar=None) |
paulb@137 | 794 | |
paulb@137 | 795 | self.current_subprograms.append(subprogram) |
paulb@137 | 796 | subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] |
paulb@137 | 797 | self.current_subprograms.pop() |
paulb@137 | 798 | self._visitFunction(function, subprogram) |
paulb@137 | 799 | |
paulb@137 | 800 | # Make a definition of the function associating it with a name. |
paulb@137 | 801 | |
paulb@137 | 802 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paulb@137 | 803 | return result |
paulb@137 | 804 | |
paulb@137 | 805 | def visitGetattr(self, getattr): |
paulb@137 | 806 | result = LoadAttr(getattr, 1, |
paulb@137 | 807 | name=getattr.attrname, |
paulb@137 | 808 | expr=self.dispatch(getattr.expr) |
paulb@137 | 809 | ) |
paulb@137 | 810 | return result |
paulb@137 | 811 | |
paulb@137 | 812 | def visitGlobal(self, global_): |
paulb@137 | 813 | result = Global(global_, 1, |
paulb@137 | 814 | names=global_.names |
paulb@137 | 815 | ) |
paulb@137 | 816 | return result |
paulb@137 | 817 | |
paulb@137 | 818 | def visitIf(self, if_): |
paulb@137 | 819 | |
paulb@137 | 820 | """ |
paulb@137 | 821 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@137 | 822 | and putting each subsequent test as part of the conditional's else |
paulb@137 | 823 | section. |
paulb@137 | 824 | |
paulb@137 | 825 | Convert... |
paulb@137 | 826 | |
paulb@137 | 827 | If (test/body) |
paulb@137 | 828 | (test/body) |
paulb@137 | 829 | ... |
paulb@137 | 830 | (else/body) |
paulb@137 | 831 | |
paulb@137 | 832 | ...to: |
paulb@137 | 833 | |
paulb@137 | 834 | Conditional (test) -> (body) |
paulb@137 | 835 | (else) -> Conditional (test) -> (body) |
paulb@137 | 836 | (else) -> ... |
paulb@137 | 837 | """ |
paulb@137 | 838 | |
paulb@137 | 839 | |
paulb@137 | 840 | results = nodes = [] |
paulb@137 | 841 | |
paulb@137 | 842 | # Produce something like... |
paulb@137 | 843 | # expr.__bool__() ? body |
paulb@137 | 844 | |
paulb@137 | 845 | first = 1 |
paulb@137 | 846 | for compare, stmt in if_.tests: |
paulb@137 | 847 | |
paulb@137 | 848 | # Set the first as the defining node. |
paulb@137 | 849 | |
paulb@137 | 850 | test = Conditional(if_, first, |
paulb@137 | 851 | test=InvokeFunction( |
paulb@137 | 852 | expr=LoadAttr( |
paulb@137 | 853 | expr=self.dispatch(compare), |
paulb@137 | 854 | name="__bool__" |
paulb@137 | 855 | ), |
paulb@137 | 856 | args=[], |
paulb@137 | 857 | star=None, |
paulb@137 | 858 | dstar=None) |
paulb@137 | 859 | ) |
paulb@137 | 860 | test.body = self.dispatch(stmt) |
paulb@137 | 861 | nodes.append(test) |
paulb@137 | 862 | nodes = test.else_ = [] |
paulb@137 | 863 | first = 0 |
paulb@137 | 864 | |
paulb@137 | 865 | # Add the compound statement from any else clause to the end. |
paulb@137 | 866 | |
paulb@137 | 867 | if if_.else_ is not None: |
paulb@137 | 868 | nodes += self.dispatch(if_.else_) |
paulb@137 | 869 | |
paulb@137 | 870 | result = results[0] |
paulb@137 | 871 | return result |
paulb@137 | 872 | |
paulb@137 | 873 | def visitImport(self, import_): |
paulb@137 | 874 | result = Assign(import_, 1) |
paulb@137 | 875 | code = [] |
paulb@137 | 876 | for path, alias in import_.names: |
paulb@137 | 877 | importer = Import(name=path) |
paulb@137 | 878 | top = alias or path.split(".")[0] |
paulb@137 | 879 | code.append(StoreName(expr=importer, name=top)) |
paulb@137 | 880 | result.code = code |
paulb@137 | 881 | return result |
paulb@137 | 882 | |
paulb@137 | 883 | def visitInvert(self, invert): |
paulb@137 | 884 | return self._visitUnary(invert, "__invert__") |
paulb@137 | 885 | |
paulb@137 | 886 | def visitKeyword(self, keyword): |
paulb@137 | 887 | result = Keyword(keyword, 1, |
paulb@137 | 888 | name=keyword.name, |
paulb@137 | 889 | expr=self.dispatch(keyword.expr) |
paulb@137 | 890 | ) |
paulb@137 | 891 | return result |
paulb@137 | 892 | |
paulb@137 | 893 | def visitLambda(self, lambda_): |
paulb@137 | 894 | |
paulb@137 | 895 | # Make a subprogram for the function and record it outside the main |
paulb@137 | 896 | # tree. |
paulb@137 | 897 | |
paulb@137 | 898 | subprogram = Subprogram(name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None) |
paulb@137 | 899 | self.current_subprograms.append(subprogram) |
paulb@137 | 900 | subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] |
paulb@137 | 901 | self.current_subprograms.pop() |
paulb@137 | 902 | self._visitFunction(lambda_, subprogram) |
paulb@137 | 903 | |
paulb@137 | 904 | # Get the subprogram reference to the lambda. |
paulb@137 | 905 | |
paulb@137 | 906 | return LoadRef(lambda_, 1, ref=subprogram) |
paulb@137 | 907 | |
paulb@137 | 908 | def visitList(self, list): |
paulb@137 | 909 | return self._visitBuiltin(list, "list") |
paulb@137 | 910 | |
paulb@137 | 911 | def visitMul(self, mul): |
paulb@137 | 912 | return self._visitBinary(mul, "__mul__", "__rmul__") |
paulb@137 | 913 | |
paulb@137 | 914 | def visitName(self, name): |
paulb@137 | 915 | result = LoadName(name, 1, name=name.name) |
paulb@137 | 916 | return result |
paulb@137 | 917 | |
paulb@137 | 918 | def _visitNot(self, expr, not_=None): |
paulb@137 | 919 | invocation = InvokeFunction( |
paulb@137 | 920 | expr=LoadAttr( |
paulb@137 | 921 | expr=expr, |
paulb@137 | 922 | name="__bool__" |
paulb@137 | 923 | ), |
paulb@137 | 924 | args=[], |
paulb@137 | 925 | star=None, |
paulb@137 | 926 | dstar=None |
paulb@137 | 927 | ) |
paulb@137 | 928 | if not_ is not None: |
paulb@137 | 929 | result = Not(not_, 1, expr=invocation) |
paulb@137 | 930 | else: |
paulb@137 | 931 | result = Not(expr=invocation) |
paulb@137 | 932 | return result |
paulb@137 | 933 | |
paulb@137 | 934 | def visitNot(self, not_): |
paulb@137 | 935 | return self._visitNot(self.dispatch(not_.expr), not_) |
paulb@137 | 936 | |
paulb@137 | 937 | def visitOr(self, or_): |
paulb@137 | 938 | |
paulb@137 | 939 | """ |
paulb@137 | 940 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@137 | 941 | subprogram. Convert... |
paulb@137 | 942 | |
paulb@137 | 943 | Or (test) |
paulb@137 | 944 | (test) |
paulb@137 | 945 | ... |
paulb@137 | 946 | |
paulb@137 | 947 | ...to: |
paulb@137 | 948 | |
paulb@137 | 949 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 950 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 951 | (else) -> ... |
paulb@137 | 952 | """ |
paulb@137 | 953 | |
paulb@137 | 954 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 955 | self.current_subprograms.append(subprogram) |
paulb@137 | 956 | |
paulb@137 | 957 | # In the subprogram, make instructions which store each operand, test |
paulb@137 | 958 | # for each operand's truth status, and if appropriate return from the |
paulb@137 | 959 | # subprogram with the value of the operand. |
paulb@137 | 960 | |
paulb@137 | 961 | last = or_.nodes[-1] |
paulb@137 | 962 | results = nodes = [] |
paulb@137 | 963 | |
paulb@137 | 964 | for node in or_.nodes: |
paulb@137 | 965 | expr = self.dispatch(node) |
paulb@137 | 966 | |
paulb@137 | 967 | # Return from the subprogram where the test is satisfied. |
paulb@137 | 968 | |
paulb@137 | 969 | if node is not last: |
paulb@137 | 970 | nodes.append(StoreTemp(expr=expr)) |
paulb@137 | 971 | invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) |
paulb@137 | 972 | test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) |
paulb@137 | 973 | nodes.append(test) |
paulb@137 | 974 | |
paulb@137 | 975 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 976 | |
paulb@137 | 977 | nodes = test.else_ = [ReleaseTemp()] |
paulb@137 | 978 | |
paulb@137 | 979 | # For the last operation, return the result. |
paulb@137 | 980 | |
paulb@137 | 981 | else: |
paulb@137 | 982 | nodes.append( |
paulb@137 | 983 | ReturnFromBlock(expr=expr) |
paulb@137 | 984 | ) |
paulb@137 | 985 | |
paulb@137 | 986 | # Finish the subprogram definition. |
paulb@137 | 987 | |
paulb@137 | 988 | subprogram.code = results |
paulb@137 | 989 | |
paulb@137 | 990 | self.current_subprograms.pop() |
paulb@137 | 991 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 992 | |
paulb@137 | 993 | # Make an invocation of the subprogram. |
paulb@137 | 994 | |
paulb@137 | 995 | result = InvokeBlock(or_, 1, produces_result=1) |
paulb@137 | 996 | result.expr = LoadRef(ref=subprogram) |
paulb@137 | 997 | return result |
paulb@137 | 998 | |
paulb@137 | 999 | def visitPass(self, pass_): |
paulb@137 | 1000 | return Pass(pass_, 1) |
paulb@137 | 1001 | |
paulb@137 | 1002 | def visitRaise(self, raise_): |
paulb@137 | 1003 | result = Raise(raise_, 1) |
paulb@137 | 1004 | if raise_.expr2 is None: |
paulb@137 | 1005 | result.expr = self.dispatch(raise_.expr1) |
paulb@137 | 1006 | else: |
paulb@137 | 1007 | result.expr = InvokeFunction( |
paulb@137 | 1008 | expr=self.dispatch(raise_.expr1), |
paulb@137 | 1009 | args=[self.dispatch(raise_.expr2)], |
paulb@137 | 1010 | star=None, |
paulb@137 | 1011 | dstar=None |
paulb@137 | 1012 | ) |
paulb@137 | 1013 | if raise_.expr3 is not None: |
paulb@137 | 1014 | result.traceback = self.dispatch(raise_.expr3) |
paulb@137 | 1015 | else: |
paulb@137 | 1016 | result.traceback = None |
paulb@137 | 1017 | return result |
paulb@137 | 1018 | |
paulb@137 | 1019 | def visitReturn(self, return_): |
paulb@137 | 1020 | result = ReturnFromFunction(return_, 1, |
paulb@137 | 1021 | expr=self.dispatch(return_.value) |
paulb@137 | 1022 | ) |
paulb@137 | 1023 | return result |
paulb@137 | 1024 | |
paulb@137 | 1025 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@137 | 1026 | if flags == "OP_ASSIGN": |
paulb@137 | 1027 | result = InvokeFunction(slice, 1, |
paulb@137 | 1028 | expr=LoadAttr( |
paulb@137 | 1029 | expr=expr, |
paulb@137 | 1030 | name="__setslice__" |
paulb@137 | 1031 | ), |
paulb@137 | 1032 | star=None, |
paulb@137 | 1033 | dstar=None, |
paulb@137 | 1034 | args=[lower, upper, value] |
paulb@137 | 1035 | ) |
paulb@137 | 1036 | elif flags == "OP_APPLY": |
paulb@137 | 1037 | args = [] |
paulb@137 | 1038 | result = InvokeFunction(slice, 1, |
paulb@137 | 1039 | expr=LoadAttr( |
paulb@137 | 1040 | expr=expr, |
paulb@137 | 1041 | name="__getslice__" |
paulb@137 | 1042 | ), |
paulb@137 | 1043 | star=None, |
paulb@137 | 1044 | dstar=None, |
paulb@137 | 1045 | args=[lower, upper] |
paulb@137 | 1046 | ) |
paulb@137 | 1047 | elif flags == "OP_DELETE": |
paulb@137 | 1048 | args = [] |
paulb@137 | 1049 | result = InvokeFunction(slice, 1, |
paulb@137 | 1050 | expr=LoadAttr( |
paulb@137 | 1051 | expr=expr, |
paulb@137 | 1052 | name="__delslice__" |
paulb@137 | 1053 | ), |
paulb@137 | 1054 | star=None, |
paulb@137 | 1055 | dstar=None, |
paulb@137 | 1056 | args=[lower, upper] |
paulb@137 | 1057 | ) |
paulb@137 | 1058 | else: |
paulb@137 | 1059 | raise NotImplementedError, flags |
paulb@137 | 1060 | |
paulb@137 | 1061 | return result |
paulb@137 | 1062 | |
paulb@137 | 1063 | def visitSlice(self, slice, in_sequence=0): |
paulb@137 | 1064 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@137 | 1065 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) |
paulb@137 | 1066 | |
paulb@137 | 1067 | def visitStmt(self, stmt): |
paulb@137 | 1068 | return self.dispatches(stmt.nodes) |
paulb@137 | 1069 | |
paulb@137 | 1070 | def visitSub(self, sub): |
paulb@137 | 1071 | return self._visitBinary(sub, "__sub__", "__rsub__") |
paulb@137 | 1072 | |
paulb@137 | 1073 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@137 | 1074 | if flags == "OP_ASSIGN": |
paulb@137 | 1075 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1076 | expr=LoadAttr( |
paulb@137 | 1077 | expr=expr, |
paulb@137 | 1078 | name="__setitem__" |
paulb@137 | 1079 | ), |
paulb@137 | 1080 | star=None, |
paulb@137 | 1081 | dstar=None, |
paulb@137 | 1082 | args=[subs, value] |
paulb@137 | 1083 | ) |
paulb@137 | 1084 | elif flags == "OP_APPLY": |
paulb@137 | 1085 | args = [] |
paulb@137 | 1086 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1087 | expr=LoadAttr( |
paulb@137 | 1088 | expr=expr, |
paulb@137 | 1089 | name="__getitem__" |
paulb@137 | 1090 | ), |
paulb@137 | 1091 | star=None, |
paulb@137 | 1092 | dstar=None, |
paulb@137 | 1093 | args=[subs] |
paulb@137 | 1094 | ) |
paulb@137 | 1095 | elif flags == "OP_DELETE": |
paulb@137 | 1096 | args = [] |
paulb@137 | 1097 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1098 | expr=LoadAttr( |
paulb@137 | 1099 | expr=expr, |
paulb@137 | 1100 | name="__delitem__" |
paulb@137 | 1101 | ), |
paulb@137 | 1102 | star=None, |
paulb@137 | 1103 | dstar=None, |
paulb@137 | 1104 | args=[subs] |
paulb@137 | 1105 | ) |
paulb@137 | 1106 | else: |
paulb@137 | 1107 | raise NotImplementedError, flags |
paulb@137 | 1108 | |
paulb@137 | 1109 | return result |
paulb@137 | 1110 | |
paulb@137 | 1111 | def _visitSubscriptSubs(self, node, subs): |
paulb@137 | 1112 | if len(subs) == 1: |
paulb@137 | 1113 | return self.dispatch(subs[0]) |
paulb@137 | 1114 | else: |
paulb@137 | 1115 | return InvokeFunction(node, 1, |
paulb@137 | 1116 | expr=LoadName(name="tuple"), |
paulb@137 | 1117 | args=self.dispatches(subs), |
paulb@137 | 1118 | star=None, |
paulb@137 | 1119 | dstar=None |
paulb@137 | 1120 | ) |
paulb@137 | 1121 | |
paulb@137 | 1122 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@137 | 1123 | return self._visitSubscript( |
paulb@137 | 1124 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@137 | 1125 | self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@137 | 1126 | ) |
paulb@137 | 1127 | |
paulb@137 | 1128 | def visitTryExcept(self, tryexcept): |
paulb@137 | 1129 | |
paulb@137 | 1130 | """ |
paulb@137 | 1131 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@137 | 1132 | |
paulb@137 | 1133 | Convert... |
paulb@137 | 1134 | |
paulb@137 | 1135 | TryExcept (body) |
paulb@137 | 1136 | (else) |
paulb@137 | 1137 | (spec/assign/stmt) |
paulb@137 | 1138 | ... |
paulb@137 | 1139 | |
paulb@137 | 1140 | ...to: |
paulb@137 | 1141 | |
paulb@137 | 1142 | Try (body) |
paulb@137 | 1143 | (else) |
paulb@137 | 1144 | (handler) -> Conditional (test) -> (stmt) |
paulb@137 | 1145 | (body) -> ... |
paulb@137 | 1146 | (else) -> Conditional (test) -> (stmt) |
paulb@137 | 1147 | (body) -> ... |
paulb@137 | 1148 | (else) -> ... |
paulb@137 | 1149 | """ |
paulb@137 | 1150 | |
paulb@137 | 1151 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@137 | 1152 | |
paulb@137 | 1153 | if tryexcept.body is not None: |
paulb@137 | 1154 | result.body = self.dispatch(tryexcept.body) |
paulb@137 | 1155 | if tryexcept.else_ is not None: |
paulb@137 | 1156 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@137 | 1157 | |
paulb@137 | 1158 | results = nodes = [] |
paulb@137 | 1159 | catch_all = 0 |
paulb@137 | 1160 | |
paulb@137 | 1161 | for spec, assign, stmt in tryexcept.handlers: |
paulb@137 | 1162 | |
paulb@137 | 1163 | # If no specification exists, produce an unconditional block. |
paulb@137 | 1164 | |
paulb@137 | 1165 | if spec is None: |
paulb@137 | 1166 | nodes += self.dispatch(stmt) |
paulb@137 | 1167 | catch_all = 1 |
paulb@137 | 1168 | |
paulb@137 | 1169 | # Produce an exception value check. |
paulb@137 | 1170 | |
paulb@137 | 1171 | else: |
paulb@137 | 1172 | test = Conditional( |
paulb@137 | 1173 | isolate_test=1, |
paulb@137 | 1174 | test=CheckExc(expr=LoadExc(), choices=self._visitTryExcept(spec)) |
paulb@137 | 1175 | ) |
paulb@137 | 1176 | test.body = [] |
paulb@137 | 1177 | |
paulb@137 | 1178 | if assign is not None: |
paulb@137 | 1179 | test.body.append( |
paulb@137 | 1180 | Assign( |
paulb@137 | 1181 | code=[ |
paulb@137 | 1182 | StoreTemp(expr=LoadExc()), |
paulb@137 | 1183 | self.dispatch(assign), |
paulb@137 | 1184 | ReleaseTemp() |
paulb@137 | 1185 | ] |
paulb@137 | 1186 | ) |
paulb@137 | 1187 | ) |
paulb@137 | 1188 | |
paulb@137 | 1189 | # Always return from conditional sections. |
paulb@137 | 1190 | |
paulb@137 | 1191 | test.body += self.dispatch(stmt) + [ReturnFromBlock()] |
paulb@137 | 1192 | nodes.append(test) |
paulb@137 | 1193 | nodes = test.else_ = [] |
paulb@137 | 1194 | |
paulb@137 | 1195 | # Add a raise operation to deal with unhandled exceptions. |
paulb@137 | 1196 | |
paulb@137 | 1197 | if not catch_all: |
paulb@137 | 1198 | nodes.append( |
paulb@137 | 1199 | Raise( |
paulb@137 | 1200 | expr=LoadExc()) |
paulb@137 | 1201 | ) |
paulb@137 | 1202 | |
paulb@137 | 1203 | result.handler = results |
paulb@137 | 1204 | return result |
paulb@137 | 1205 | |
paulb@137 | 1206 | def _visitTryExcept(self, spec): |
paulb@137 | 1207 | |
paulb@137 | 1208 | "Return a list of nodes for the given exception type 'spec'." |
paulb@137 | 1209 | |
paulb@137 | 1210 | if isinstance(spec, compiler.ast.Tuple): |
paulb@137 | 1211 | nodes = [] |
paulb@137 | 1212 | for node in spec.nodes: |
paulb@137 | 1213 | nodes += self._visitTryExcept(node) |
paulb@137 | 1214 | else: |
paulb@137 | 1215 | nodes = [self.dispatch(spec)] |
paulb@137 | 1216 | return nodes |
paul@0 | 1217 | |
paul@0 | 1218 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1219 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1220 | if tryfinally.body is not None: |
paul@0 | 1221 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1222 | if tryfinally.final is not None: |
paul@0 | 1223 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1224 | return result |
paul@0 | 1225 | |
paulb@137 | 1226 | def visitTuple(self, tuple): |
paulb@137 | 1227 | return self._visitBuiltin(tuple, "tuple") |
paulb@137 | 1228 | |
paulb@137 | 1229 | def visitUnaryAdd(self, unaryadd): |
paulb@137 | 1230 | return self._visitUnary(unaryadd, "__pos__") |
paulb@137 | 1231 | |
paulb@137 | 1232 | def visitUnarySub(self, unarysub): |
paulb@137 | 1233 | return self._visitUnary(unarysub, "__neg__") |
paulb@137 | 1234 | |
paulb@137 | 1235 | def visitWhile(self, while_): |
paulb@137 | 1236 | |
paulb@137 | 1237 | """ |
paulb@137 | 1238 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@137 | 1239 | subprogram. Convert... |
paulb@137 | 1240 | |
paulb@137 | 1241 | While (test) -> (body) |
paulb@137 | 1242 | (else) |
paulb@137 | 1243 | |
paulb@137 | 1244 | ...to: |
paulb@137 | 1245 | |
paulb@137 | 1246 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@137 | 1247 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1248 | (else) -> ... |
paulb@137 | 1249 | """ |
paulb@137 | 1250 | |
paulb@137 | 1251 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@137 | 1252 | self.current_subprograms.append(subprogram) |
paulb@137 | 1253 | |
paulb@137 | 1254 | # Include a conditional statement in the subprogram. |
paulb@137 | 1255 | |
paulb@137 | 1256 | test = Conditional(else_=[]) |
paulb@137 | 1257 | test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None) |
paulb@137 | 1258 | |
paulb@137 | 1259 | # Inside the conditional, add a recursive invocation to the subprogram |
paulb@137 | 1260 | # if the test condition was satisfied. |
paulb@137 | 1261 | |
paulb@137 | 1262 | continuation = InvokeBlock() |
paulb@137 | 1263 | continuation.expr = LoadRef(ref=subprogram) |
paulb@137 | 1264 | |
paulb@137 | 1265 | # Return within the main section of the loop. |
paulb@137 | 1266 | |
paulb@137 | 1267 | test.body = self.dispatch(while_.body) + [continuation, ReturnFromBlock()] |
paulb@137 | 1268 | |
paulb@137 | 1269 | # Provide the else section, if present, along with an explicit return. |
paulb@137 | 1270 | |
paulb@137 | 1271 | if while_.else_ is not None: |
paulb@137 | 1272 | test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] |
paulb@137 | 1273 | |
paulb@137 | 1274 | # Finish the subprogram definition. |
paulb@137 | 1275 | |
paulb@137 | 1276 | subprogram.code = [test] |
paulb@137 | 1277 | |
paulb@137 | 1278 | self.current_subprograms.pop() |
paulb@137 | 1279 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1280 | |
paulb@137 | 1281 | # Make an invocation of the subprogram. |
paulb@137 | 1282 | |
paulb@137 | 1283 | result = InvokeBlock(while_, 1) |
paulb@137 | 1284 | result.expr = LoadRef(ref=subprogram) |
paulb@137 | 1285 | return result |
paulb@137 | 1286 | |
paulb@137 | 1287 | # Convenience methods. |
paulb@137 | 1288 | |
paulb@137 | 1289 | def _visitBinary(self, binary, left_name, right_name): |
paulb@137 | 1290 | |
paulb@137 | 1291 | """ |
paulb@137 | 1292 | Emulate the current mechanisms by producing nodes as follows: |
paulb@137 | 1293 | |
paulb@137 | 1294 | InvokeBlock -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) |
paulb@137 | 1295 | (else) |
paulb@137 | 1296 | (handler) -> Conditional (test) -> CheckExc (expr) -> LoadExc |
paulb@137 | 1297 | (choices) -> LoadName TypeError |
paulb@137 | 1298 | (body) -> ReturnFromBlock (expr) -> y.__radd__(x) |
paulb@137 | 1299 | (else) |
paulb@137 | 1300 | """ |
paulb@137 | 1301 | |
paulb@137 | 1302 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1303 | self.current_subprograms.append(subprogram) |
paulb@137 | 1304 | |
paulb@137 | 1305 | subprogram.code = [ |
paulb@137 | 1306 | Try(binary, 1, |
paulb@137 | 1307 | body=[ |
paulb@137 | 1308 | ReturnFromBlock( |
paulb@137 | 1309 | expr=InvokeFunction( |
paulb@137 | 1310 | expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name), |
paulb@137 | 1311 | args=[self.dispatch(binary.right)], |
paulb@137 | 1312 | star=None, |
paulb@137 | 1313 | dstar=None) |
paulb@137 | 1314 | ) |
paulb@137 | 1315 | ], |
paulb@137 | 1316 | else_=[], |
paulb@137 | 1317 | finally_=[], |
paulb@137 | 1318 | handler=[ |
paulb@137 | 1319 | Conditional( |
paulb@137 | 1320 | test=CheckExc(expr=LoadExc(), choices=[LoadName(name="TypeError")]), |
paulb@137 | 1321 | body=[ |
paulb@137 | 1322 | ReturnFromBlock( |
paulb@137 | 1323 | expr=InvokeFunction( |
paulb@137 | 1324 | expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name), |
paulb@137 | 1325 | args=[self.dispatch(binary.left)], |
paulb@137 | 1326 | star=None, |
paulb@137 | 1327 | dstar=None) |
paulb@137 | 1328 | ) |
paulb@137 | 1329 | ], |
paulb@137 | 1330 | else_=[] |
paulb@137 | 1331 | ) |
paulb@137 | 1332 | ] |
paulb@137 | 1333 | ) |
paulb@137 | 1334 | ] |
paulb@137 | 1335 | |
paulb@137 | 1336 | self.current_subprograms.pop() |
paulb@137 | 1337 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1338 | |
paulb@137 | 1339 | result = InvokeBlock(produces_result=1) |
paulb@137 | 1340 | result.expr = LoadRef(ref=subprogram) |
paulb@137 | 1341 | return result |
paulb@137 | 1342 | |
paulb@137 | 1343 | def _visitBuiltin(self, builtin, name): |
paulb@137 | 1344 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) |
paulb@137 | 1345 | return result |
paulb@137 | 1346 | |
paulb@137 | 1347 | def _visitUnary(self, unary, name): |
paulb@137 | 1348 | return InvokeFunction(unary, 1, |
paulb@137 | 1349 | expr=LoadAttr( |
paulb@137 | 1350 | expr=self.dispatch(unary.expr), |
paulb@137 | 1351 | name=name |
paulb@137 | 1352 | ), |
paulb@137 | 1353 | args=[], |
paulb@137 | 1354 | star=None, |
paulb@137 | 1355 | dstar=None |
paulb@137 | 1356 | ) |
paulb@137 | 1357 | |
paulb@57 | 1358 | # Convenience functions. |
paulb@57 | 1359 | |
paulb@57 | 1360 | def simplify(filename, builtins=0): |
paulb@57 | 1361 | |
paulb@57 | 1362 | """ |
paulb@88 | 1363 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1364 | |
paulb@88 | 1365 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 1366 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 1367 | """ |
paulb@57 | 1368 | |
paulb@57 | 1369 | simplifier = Simplifier(builtins) |
paulb@57 | 1370 | module = compiler.parseFile(filename) |
paulb@57 | 1371 | compiler.misc.set_filename(filename, module) |
paulb@88 | 1372 | if builtins: |
paulb@88 | 1373 | name = "__builtins__" |
paulb@88 | 1374 | else: |
paulb@88 | 1375 | path, ext = os.path.splitext(filename) |
paulb@88 | 1376 | path, name = os.path.split(path) |
paulb@88 | 1377 | simplified = simplifier.process(module, name) |
paulb@57 | 1378 | return simplified |
paulb@57 | 1379 | |
paul@0 | 1380 | # vim: tabstop=4 expandtab shiftwidth=4 |