paul@638 | 1 | #!/usr/bin/env python |
paul@638 | 2 | |
paul@638 | 3 | """ |
paul@638 | 4 | Produce syspython code from an inspected program. |
paul@638 | 5 | |
paul@638 | 6 | Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@638 | 7 | |
paul@638 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@638 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@638 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@638 | 11 | version. |
paul@638 | 12 | |
paul@638 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@638 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@638 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@638 | 16 | details. |
paul@638 | 17 | |
paul@638 | 18 | You should have received a copy of the GNU General Public License along with |
paul@638 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@638 | 20 | """ |
paul@638 | 21 | |
paul@638 | 22 | from micropython.common import * |
paul@638 | 23 | from micropython.data import * |
paul@638 | 24 | from micropython.errors import * |
paul@638 | 25 | from os.path import exists, extsep, join |
paul@638 | 26 | import compiler.ast |
paul@638 | 27 | import sys |
paul@638 | 28 | import os |
paul@638 | 29 | |
paul@638 | 30 | try: |
paul@638 | 31 | set |
paul@638 | 32 | except NameError: |
paul@638 | 33 | from sets import Set as set |
paul@638 | 34 | |
paul@638 | 35 | # Convenience definitions. |
paul@638 | 36 | |
paul@664 | 37 | constant_attribute = compiler.ast.Getattr |
paul@638 | 38 | special_name = compiler.ast.Name |
paul@645 | 39 | |
paul@645 | 40 | def quoted_name(s): |
paul@645 | 41 | return compiler.ast.Const(s) |
paul@638 | 42 | |
paul@664 | 43 | def quoted_ref(obj): |
paul@670 | 44 | return compiler.ast.CallFunc("static", [quoted_name(obj.full_name())]) |
paul@664 | 45 | |
paul@664 | 46 | def module_attribute(module_name, attrname): |
paul@670 | 47 | return special_name(module_name + "." + attrname) |
paul@664 | 48 | |
paul@646 | 49 | # Special function names. |
paul@666 | 50 | # Some of the assignment operations cannot be supported unless attribute usage |
paul@666 | 51 | # observations are being made. |
paul@646 | 52 | |
paul@670 | 53 | assattr_functions = ("storeattrcontext", "storeattrcontext", "storeattr", |
paul@670 | 54 | "storeattrindex", None) |
paul@670 | 55 | getattr_functions = ("loadattrcontext", "loadattrcontextcond", "loadattr", |
paul@670 | 56 | "loadattrindex", "loadattrindexcontextcond") |
paul@646 | 57 | |
paul@638 | 58 | # Source code classes. |
paul@638 | 59 | |
paul@638 | 60 | class ConvertedSource(ASTVisitor): |
paul@638 | 61 | |
paul@638 | 62 | "A conversion of module source code to syspython." |
paul@638 | 63 | |
paul@638 | 64 | def __init__(self, module, program): |
paul@638 | 65 | self.visitor = self |
paul@638 | 66 | self.module = module |
paul@638 | 67 | self.program = program |
paul@645 | 68 | self.objtable = program.get_object_table() |
paul@638 | 69 | self.in_main = False |
paul@638 | 70 | self.units = [] |
paul@638 | 71 | |
paul@638 | 72 | def get_unit(self): |
paul@638 | 73 | return self.units[-1] |
paul@638 | 74 | |
paul@638 | 75 | def get_module(self): |
paul@638 | 76 | return self.units[0] |
paul@638 | 77 | |
paul@638 | 78 | def to_stream(self, stream): |
paul@638 | 79 | |
paul@638 | 80 | "Write the converted code to the given 'stream'." |
paul@638 | 81 | |
paul@638 | 82 | module = self.dispatch(self.module.astnode) |
paul@638 | 83 | stream.write(str(module)) |
paul@638 | 84 | |
paul@638 | 85 | def NOP(self, node): |
paul@638 | 86 | return node |
paul@638 | 87 | |
paul@638 | 88 | def visitModule(self, node): |
paul@638 | 89 | module = node.unit |
paul@638 | 90 | self.units.append(module) |
paul@638 | 91 | |
paul@645 | 92 | definitions = self.process_definitions(node) |
paul@638 | 93 | |
paul@670 | 94 | # globalnames(name, ...) |
paul@638 | 95 | |
paul@638 | 96 | globalnames = module.module_attribute_names() and [ |
paul@638 | 97 | compiler.ast.CallFunc( |
paul@670 | 98 | special_name("globalnames"), |
paul@667 | 99 | [special_name(attr.name) for attr in module.attributes_as_list()] |
paul@638 | 100 | ) |
paul@638 | 101 | ] or [] |
paul@638 | 102 | |
paul@638 | 103 | # def __main__(): |
paul@638 | 104 | # ... |
paul@638 | 105 | |
paul@638 | 106 | self.in_main = True |
paul@638 | 107 | |
paul@638 | 108 | main = compiler.ast.Function( |
paul@638 | 109 | [], "__main__", [], [], 0, "Module initialisation.", |
paul@638 | 110 | compiler.ast.Stmt(globalnames + self.dispatch(node.node).nodes) |
paul@638 | 111 | ) |
paul@638 | 112 | |
paul@638 | 113 | self.in_main = False |
paul@638 | 114 | self.units.pop() |
paul@638 | 115 | |
paul@638 | 116 | return compiler.ast.Module(node.doc, compiler.ast.Stmt(definitions + [main])) |
paul@638 | 117 | |
paul@638 | 118 | # Statements. |
paul@638 | 119 | |
paul@638 | 120 | def visitAssert(self, node): |
paul@638 | 121 | return compiler.ast.Assert(self.dispatch(node.test), node.fail and self.dispatch(node.fail)) |
paul@638 | 122 | |
paul@638 | 123 | def visitAssign(self, node): |
paul@638 | 124 | expr = self.dispatch(node.expr) |
paul@638 | 125 | return compiler.ast.Stmt([self.dispatch(n, expr) for n in node.nodes]) |
paul@638 | 126 | |
paul@638 | 127 | def visitAugAssign(self, node): |
paul@638 | 128 | |
paul@638 | 129 | # lvalue = op(lvalue, expr) |
paul@670 | 130 | # -> fn(lvalue, op(lvalue, expr)) |
paul@638 | 131 | |
paul@638 | 132 | op_name = operator_functions[node.op] |
paul@638 | 133 | |
paul@638 | 134 | return self.dispatch(node.node, compiler.ast.CallFunc( |
paul@638 | 135 | module_attribute("operator", op_name), |
paul@638 | 136 | [self.dispatch(node.node), self.dispatch(node.expr)] |
paul@638 | 137 | )) |
paul@638 | 138 | |
paul@638 | 139 | visitBreak = NOP |
paul@638 | 140 | |
paul@638 | 141 | def visitClass(self, node): |
paul@638 | 142 | if not used_by_unit(node): |
paul@638 | 143 | return compiler.ast.Stmt([]) |
paul@638 | 144 | |
paul@638 | 145 | self.units.append(node.unit) |
paul@638 | 146 | try: |
paul@638 | 147 | # Incorporate class body code in the main function. |
paul@638 | 148 | |
paul@638 | 149 | if self.in_main: |
paul@638 | 150 | return self.dispatch(node.code) |
paul@638 | 151 | else: |
paul@638 | 152 | return self._visitClassDefinition(node) |
paul@638 | 153 | |
paul@638 | 154 | finally: |
paul@638 | 155 | self.units.pop() |
paul@638 | 156 | |
paul@638 | 157 | def _visitClassDefinition(self, node): |
paul@638 | 158 | cls = node.unit |
paul@638 | 159 | |
paul@670 | 160 | # instattrs(name, ...) |
paul@670 | 161 | # clsattrs(name, ...) |
paul@638 | 162 | |
paul@638 | 163 | instattrs = cls.instance_attribute_names() and [ |
paul@638 | 164 | compiler.ast.CallFunc( |
paul@670 | 165 | special_name("instattrs"), |
paul@667 | 166 | [special_name(attr.name) for attr in cls.instance_attributes_as_list()] |
paul@638 | 167 | ) |
paul@638 | 168 | ] or [] |
paul@638 | 169 | |
paul@638 | 170 | clsattrs = cls.class_attribute_names() and [ |
paul@638 | 171 | compiler.ast.CallFunc( |
paul@670 | 172 | special_name("clsattrs"), |
paul@667 | 173 | [special_name(attr.name) for attr in cls.attributes_as_list()] |
paul@638 | 174 | ) |
paul@638 | 175 | ] or [] |
paul@638 | 176 | |
paul@670 | 177 | # inherited(superclass, name, ...) |
paul@638 | 178 | # ... |
paul@638 | 179 | |
paul@638 | 180 | attrs_by_cls = {} |
paul@638 | 181 | for attrname, attr in cls.all_class_attributes().items(): |
paul@638 | 182 | supercls = attr.parent |
paul@638 | 183 | if supercls is cls: |
paul@638 | 184 | continue |
paul@638 | 185 | if not attrs_by_cls.has_key(supercls): |
paul@638 | 186 | attrs_by_cls[supercls] = [] |
paul@638 | 187 | attrs_by_cls[supercls].append(attrname) |
paul@638 | 188 | |
paul@638 | 189 | inherited = [] |
paul@638 | 190 | |
paul@638 | 191 | for supercls, attrnames in attrs_by_cls.items(): |
paul@638 | 192 | inherited.append( |
paul@638 | 193 | compiler.ast.CallFunc( |
paul@670 | 194 | special_name("inherited"), |
paul@645 | 195 | [quoted_ref(supercls)] + [special_name(name) for name in attrnames] |
paul@638 | 196 | )) |
paul@638 | 197 | |
paul@670 | 198 | # descendants(name, ...) |
paul@638 | 199 | |
paul@638 | 200 | descendants = cls.all_descendants() and [ |
paul@638 | 201 | compiler.ast.CallFunc( |
paul@670 | 202 | special_name("descendants"), |
paul@638 | 203 | [special_name(name) for name in cls.all_descendants().keys()] |
paul@638 | 204 | ) |
paul@638 | 205 | ] or [] |
paul@638 | 206 | |
paul@638 | 207 | # Process all the definitions defined inside the class. |
paul@638 | 208 | |
paul@645 | 209 | definitions = self.process_definitions(node) |
paul@638 | 210 | |
paul@638 | 211 | return compiler.ast.Class(node.name, [], node.doc, |
paul@638 | 212 | compiler.ast.Stmt(instattrs + clsattrs + inherited + descendants + definitions) |
paul@638 | 213 | ) |
paul@638 | 214 | |
paul@638 | 215 | visitContinue = NOP |
paul@638 | 216 | |
paul@638 | 217 | def visitDiscard(self, node): |
paul@638 | 218 | return compiler.ast.Discard(self.dispatch(node.expr)) |
paul@638 | 219 | |
paul@638 | 220 | def visitFor(self, node): |
paul@638 | 221 | |
paul@638 | 222 | """ |
paul@638 | 223 | Convert from... |
paul@638 | 224 | |
paul@638 | 225 | for <assign> in <list>: |
paul@638 | 226 | <body> |
paul@638 | 227 | [ else: |
paul@638 | 228 | <else_> ] |
paul@638 | 229 | |
paul@638 | 230 | ...to... |
paul@638 | 231 | |
paul@638 | 232 | _it = iter(<list>) |
paul@638 | 233 | while True: |
paul@638 | 234 | try: |
paul@638 | 235 | <assign> = _it.next() |
paul@638 | 236 | except StopIteration: |
paul@638 | 237 | [ <else_> ] |
paul@638 | 238 | break |
paul@638 | 239 | else: |
paul@638 | 240 | <body> |
paul@638 | 241 | """ |
paul@638 | 242 | |
paul@638 | 243 | unit = self.get_unit() |
paul@638 | 244 | temp = quoted_name(unit.temp_usage) |
paul@638 | 245 | unit.temp_usage += 1 |
paul@638 | 246 | |
paul@638 | 247 | else_nodes = node.else_ and self.dispatch(node.else_).nodes or [] |
paul@638 | 248 | |
paul@638 | 249 | return compiler.ast.Stmt([ |
paul@670 | 250 | # storetemp(_it, __builtins__.iter(<list>)) |
paul@670 | 251 | compiler.ast.CallFunc(special_name("storetemp"), [ |
paul@638 | 252 | temp, |
paul@638 | 253 | compiler.ast.CallFunc( |
paul@664 | 254 | module_attribute("__builtins__", "iter"), |
paul@638 | 255 | [self.dispatch(node.list)] |
paul@638 | 256 | ) |
paul@638 | 257 | ]), |
paul@638 | 258 | # while True: ... |
paul@638 | 259 | compiler.ast.While( |
paul@638 | 260 | special_name("True"), |
paul@638 | 261 | # try: ... |
paul@638 | 262 | compiler.ast.TryExcept( |
paul@638 | 263 | compiler.ast.Stmt([ |
paul@638 | 264 | # <assign> = ... |
paul@638 | 265 | self.dispatch(node.assign, |
paul@638 | 266 | # _it.next() |
paul@638 | 267 | compiler.ast.CallFunc( |
paul@670 | 268 | compiler.ast.CallFunc(special_name("loadattrindex"), [ |
paul@670 | 269 | compiler.ast.CallFunc(special_name("loadtemp"), [temp]), |
paul@638 | 270 | special_name("next") |
paul@638 | 271 | ]), |
paul@638 | 272 | [] |
paul@638 | 273 | ) |
paul@638 | 274 | ) |
paul@638 | 275 | ]), |
paul@638 | 276 | # except StopIteration: ... |
paul@638 | 277 | [(special_name("StopIteration"), None, compiler.ast.Stmt(else_nodes + [compiler.ast.Break()]))], |
paul@638 | 278 | # else: ... |
paul@638 | 279 | self.dispatch(node.body) |
paul@638 | 280 | ), |
paul@638 | 281 | None |
paul@638 | 282 | ) |
paul@638 | 283 | ]) |
paul@638 | 284 | |
paul@638 | 285 | def visitFrom(self, node): |
paul@638 | 286 | |
paul@638 | 287 | # Generate __main__ function calls for each step in the imported module |
paul@638 | 288 | # hierarchy. |
paul@638 | 289 | |
paul@638 | 290 | statements = [] |
paul@638 | 291 | |
paul@638 | 292 | for modname in self.module.get_module_paths(node.modname): |
paul@638 | 293 | statements.append( |
paul@638 | 294 | compiler.ast.CallFunc(special_name("%s.__main__" % modname ), []) |
paul@638 | 295 | ) |
paul@638 | 296 | |
paul@638 | 297 | for name, alias in node.names: |
paul@638 | 298 | statements.append( |
paul@670 | 299 | compiler.ast.CallFunc( |
paul@670 | 300 | special_name("storelocal"), |
paul@670 | 301 | [special_name(alias or name), |
paul@638 | 302 | compiler.ast.CallFunc( |
paul@670 | 303 | special_name("loadattr"), |
paul@638 | 304 | [special_name(node.modname), special_name(name)] |
paul@638 | 305 | ) |
paul@670 | 306 | ]) |
paul@638 | 307 | ) |
paul@638 | 308 | |
paul@638 | 309 | return compiler.ast.Stmt(statements) |
paul@638 | 310 | |
paul@638 | 311 | def visitFunction(self, node): |
paul@638 | 312 | if not used_by_unit(node): |
paul@638 | 313 | return compiler.ast.Stmt([]) |
paul@638 | 314 | |
paul@638 | 315 | self.units.append(node.unit) |
paul@638 | 316 | |
paul@638 | 317 | try: |
paul@665 | 318 | if self.in_main: |
paul@665 | 319 | |
paul@665 | 320 | # Generate rebindings of functions. |
paul@638 | 321 | |
paul@665 | 322 | fn = node.unit |
paul@665 | 323 | if fn.name == fn.original_name: |
paul@665 | 324 | return compiler.ast.Stmt([]) |
paul@665 | 325 | else: |
paul@665 | 326 | return compiler.ast.CallFunc( |
paul@670 | 327 | special_name("storeattr"), |
paul@665 | 328 | [quoted_ref(fn.parent), special_name(fn.original_name), |
paul@665 | 329 | quoted_ref(fn)] |
paul@665 | 330 | ) |
paul@638 | 331 | else: |
paul@638 | 332 | return self._visitFunctionDefinition(node) |
paul@638 | 333 | finally: |
paul@638 | 334 | self.units.pop() |
paul@638 | 335 | |
paul@638 | 336 | def _visitFunctionDefinition(self, node): |
paul@638 | 337 | fn = node.unit |
paul@638 | 338 | |
paul@670 | 339 | # localnames(name, ...) |
paul@670 | 340 | # globalnames(name, ...) |
paul@638 | 341 | |
paul@645 | 342 | localnames = fn.all_locals() and [ |
paul@638 | 343 | compiler.ast.CallFunc( |
paul@670 | 344 | special_name("localnames"), |
paul@645 | 345 | [special_name(name) for name in fn.all_locals().keys()] |
paul@638 | 346 | ) |
paul@638 | 347 | ] or [] |
paul@638 | 348 | |
paul@638 | 349 | globalnames = fn.globals and [ |
paul@638 | 350 | compiler.ast.CallFunc( |
paul@670 | 351 | special_name("globalnames"), |
paul@638 | 352 | [special_name(name) for name in fn.globals] |
paul@638 | 353 | ) |
paul@638 | 354 | ] or [] |
paul@638 | 355 | |
paul@638 | 356 | defaults = [self.dispatch(n) for n in node.defaults] |
paul@638 | 357 | |
paul@645 | 358 | # NOTE: Should generate guards for attribute usage operations. |
paul@645 | 359 | |
paul@638 | 360 | code = self.dispatch(node.code) |
paul@638 | 361 | |
paul@638 | 362 | return compiler.ast.Function(node.decorators, node.name, node.argnames, defaults, node.flags, node.doc, |
paul@638 | 363 | compiler.ast.Stmt(localnames + globalnames + code.nodes)) |
paul@638 | 364 | |
paul@638 | 365 | visitGlobal = NOP |
paul@638 | 366 | |
paul@638 | 367 | def visitIf(self, node): |
paul@638 | 368 | return compiler.ast.If( |
paul@638 | 369 | [(self.dispatch(compare), self.dispatch(stmt)) for (compare, stmt) in node.tests], |
paul@638 | 370 | node.else_ and self.dispatch(node.else_) |
paul@638 | 371 | ) |
paul@638 | 372 | |
paul@638 | 373 | def visitImport(self, node): |
paul@638 | 374 | |
paul@638 | 375 | # Generate __main__ function calls for each step in the imported module |
paul@638 | 376 | # hierarchy. |
paul@638 | 377 | |
paul@638 | 378 | statements = [] |
paul@638 | 379 | |
paul@638 | 380 | for name, alias in node.names: |
paul@638 | 381 | for modname in self.module.get_module_paths(name): |
paul@638 | 382 | statements.append( |
paul@638 | 383 | compiler.ast.CallFunc(compiler.ast.Getattr(modname, "__main__"), []) |
paul@638 | 384 | ) |
paul@638 | 385 | |
paul@638 | 386 | statements.append( |
paul@670 | 387 | compiler.ast.CallFunc( |
paul@670 | 388 | special_name("storelocal"), |
paul@670 | 389 | [special_name(alias or name.split(".")[0]), |
paul@638 | 390 | compiler.ast.CallFunc( |
paul@670 | 391 | special_name("static"), |
paul@638 | 392 | [special_name(name)] |
paul@638 | 393 | ) |
paul@670 | 394 | ]) |
paul@638 | 395 | ) |
paul@638 | 396 | |
paul@638 | 397 | return compiler.ast.Stmt(statements) |
paul@638 | 398 | |
paul@645 | 399 | def visitPass(self, node): |
paul@645 | 400 | if not isinstance(self.get_unit(), Class): |
paul@645 | 401 | return compiler.ast.Pass() |
paul@645 | 402 | else: |
paul@645 | 403 | return compiler.ast.Stmt([]) |
paul@638 | 404 | |
paul@638 | 405 | def visitPrint(self, node): |
paul@638 | 406 | return compiler.ast.Print( |
paul@638 | 407 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 408 | node.dest and self.dispatch(node.dest) |
paul@638 | 409 | ) |
paul@638 | 410 | |
paul@638 | 411 | def visitPrintnl(self, node): |
paul@638 | 412 | return compiler.ast.Print( |
paul@638 | 413 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 414 | node.dest and self.dispatch(node.dest) |
paul@638 | 415 | ) |
paul@638 | 416 | |
paul@638 | 417 | def visitRaise(self, node): |
paul@638 | 418 | return compiler.ast.Raise( |
paul@638 | 419 | node.expr1 and self.dispatch(node.expr1), |
paul@638 | 420 | node.expr2 and self.dispatch(node.expr2), |
paul@638 | 421 | node.expr3 and self.dispatch(node.expr3) |
paul@638 | 422 | ) |
paul@638 | 423 | |
paul@638 | 424 | def visitReturn(self, node): |
paul@638 | 425 | return compiler.ast.Return(self.dispatch(node.value)) |
paul@638 | 426 | |
paul@638 | 427 | def visitStmt(self, node): |
paul@638 | 428 | return compiler.ast.Stmt([self.dispatch(n) for n in node.nodes]) |
paul@638 | 429 | |
paul@638 | 430 | def visitTryExcept(self, node): |
paul@638 | 431 | # NOTE: Need to dispatch to the assignment with the exception. |
paul@638 | 432 | return compiler.ast.TryExcept( |
paul@638 | 433 | self.dispatch(node.body), |
paul@638 | 434 | [(spec and self.dispatch(spec), assign and self.dispatch(assign), self.dispatch(statement)) |
paul@638 | 435 | for spec, assign, statement in node.handlers], |
paul@638 | 436 | node.else_ and self.dispatch(node.else_) |
paul@638 | 437 | ) |
paul@638 | 438 | |
paul@638 | 439 | def visitTryFinally(self, node): |
paul@638 | 440 | return compiler.ast.TryFinally( |
paul@638 | 441 | self.dispatch(node.body), |
paul@638 | 442 | self.dispatch(node.final) |
paul@638 | 443 | ) |
paul@638 | 444 | |
paul@638 | 445 | def visitWhile(self, node): |
paul@638 | 446 | return compiler.ast.While( |
paul@638 | 447 | self.dispatch(node.test), |
paul@638 | 448 | self.dispatch(node.body), |
paul@638 | 449 | node.else_ and self.dispatch(node.else_) |
paul@638 | 450 | ) |
paul@638 | 451 | |
paul@638 | 452 | def visitYield(self, node): |
paul@638 | 453 | return compiler.ast.Yield(self.dispatch(node.value)) |
paul@638 | 454 | |
paul@638 | 455 | # Expression-related helper methods. |
paul@638 | 456 | |
paul@638 | 457 | def _visitBitBinary(self, node): |
paul@638 | 458 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 459 | last = self.dispatch(node.nodes[0]) |
paul@638 | 460 | |
paul@638 | 461 | for n in node.nodes[1:]: |
paul@638 | 462 | last = compiler.ast.CallFunc( |
paul@638 | 463 | module_attribute("operator", op_name), |
paul@638 | 464 | [last, self.dispatch(n)] |
paul@638 | 465 | ) |
paul@638 | 466 | |
paul@638 | 467 | return last |
paul@638 | 468 | |
paul@638 | 469 | def _visitBinary(self, node): |
paul@638 | 470 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 471 | |
paul@638 | 472 | return compiler.ast.CallFunc( |
paul@638 | 473 | module_attribute("operator", op_name), |
paul@638 | 474 | [self.dispatch(node.left), self.dispatch(node.right)] |
paul@638 | 475 | ) |
paul@638 | 476 | |
paul@638 | 477 | def _visitUnary(self, node): |
paul@638 | 478 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 479 | |
paul@638 | 480 | return compiler.ast.CallFunc( |
paul@638 | 481 | module_attribute("operator", op_name), |
paul@638 | 482 | [self.dispatch(node.expr)] |
paul@638 | 483 | ) |
paul@638 | 484 | |
paul@645 | 485 | def _generateValue(self, value): |
paul@645 | 486 | |
paul@645 | 487 | # Literal constants. |
paul@645 | 488 | |
paul@645 | 489 | if isinstance(value, Const): |
paul@645 | 490 | return compiler.ast.Const(value.get_value()) |
paul@645 | 491 | |
paul@645 | 492 | # Other constant structures. |
paul@645 | 493 | |
paul@645 | 494 | if isinstance(value, Constant): |
paul@645 | 495 | return quoted_ref(value) |
paul@645 | 496 | |
paul@645 | 497 | return None |
paul@645 | 498 | |
paul@646 | 499 | def _visitAttr(self, node, expr=None): |
paul@646 | 500 | unit = self.get_unit() |
paul@646 | 501 | |
paul@646 | 502 | # Choose the appropriate special functions. |
paul@646 | 503 | |
paul@646 | 504 | (opattrcontext, opattrcontextcond, opattr, |
paul@646 | 505 | opattrindex, opattrindexcontextcond) = expr and assattr_functions or getattr_functions |
paul@646 | 506 | |
paul@646 | 507 | accessor = self.dispatch(node.expr) |
paul@646 | 508 | |
paul@646 | 509 | # Generate already-deduced accesses. |
paul@646 | 510 | |
paul@646 | 511 | if node._access_type == "constant": |
paul@646 | 512 | return self._generateValue(node._value_deduced) |
paul@646 | 513 | |
paul@646 | 514 | # Generate accesses via static objects and instances. |
paul@646 | 515 | |
paul@646 | 516 | if node._attr_deduced: |
paul@646 | 517 | if node._set_context == "set": |
paul@646 | 518 | op = opattrcontext |
paul@646 | 519 | elif node._set_context == "cond": |
paul@646 | 520 | op = opattrcontextcond |
paul@646 | 521 | else: |
paul@646 | 522 | op = opattr |
paul@646 | 523 | |
paul@646 | 524 | # Handle unsupported operations. |
paul@646 | 525 | |
paul@646 | 526 | if not op: |
paul@646 | 527 | raise TranslateError("Storing of class attribute %r via self not permitted." % node.attrname) |
paul@646 | 528 | |
paul@646 | 529 | # Define the arguments: accessor, attribute name and optional value. |
paul@646 | 530 | |
paul@646 | 531 | args = [ |
paul@646 | 532 | node._access_type == "static" and \ |
paul@646 | 533 | self._generateValue(node._attr_deduced.parent) or accessor, |
paul@646 | 534 | special_name(node.attrname) |
paul@646 | 535 | ] |
paul@646 | 536 | |
paul@646 | 537 | if expr: |
paul@646 | 538 | args.append(expr) |
paul@646 | 539 | |
paul@670 | 540 | # Append any context to be set. |
paul@670 | 541 | |
paul@670 | 542 | if node._set_context and args[0] is not accessor: |
paul@670 | 543 | args.append(accessor) |
paul@670 | 544 | |
paul@646 | 545 | return compiler.ast.CallFunc(special_name(op), args) |
paul@646 | 546 | |
paul@646 | 547 | # Positioned accesses are normal accesses via instances. |
paul@646 | 548 | |
paul@646 | 549 | if node._access_type == "positioned": |
paul@646 | 550 | args = [accessor, special_name(node.attrname)] |
paul@646 | 551 | if expr: |
paul@646 | 552 | args.append(expr) |
paul@646 | 553 | return compiler.ast.CallFunc(special_name(opattr), args) |
paul@646 | 554 | |
paul@646 | 555 | # With no usable deductions, generate a table-based access. |
paul@646 | 556 | |
paul@646 | 557 | args = [accessor, special_name(node.attrname)] |
paul@646 | 558 | if expr: |
paul@646 | 559 | args.append(expr) |
paul@646 | 560 | access = compiler.ast.CallFunc(special_name(opattrindexcontextcond), args) |
paul@646 | 561 | |
paul@646 | 562 | # class.__class__ => __builtins__.type |
paul@646 | 563 | |
paul@646 | 564 | if node.attrname == "__class__": |
paul@646 | 565 | |
paul@670 | 566 | # storetemp(n, <accessor>) |
paul@670 | 567 | # isclass(n) and __builtins__.type or <access> |
paul@646 | 568 | |
paul@646 | 569 | temp = quoted_name(unit.temp_usage) |
paul@646 | 570 | unit.temp_usage += 1 |
paul@646 | 571 | |
paul@646 | 572 | return compiler.ast.Stmt([ |
paul@670 | 573 | compiler.ast.CallFunc(special_name("storetemp"), [temp, access]), |
paul@646 | 574 | compiler.ast.Or([ |
paul@646 | 575 | compiler.ast.And([ |
paul@646 | 576 | compiler.ast.CallFunc( |
paul@670 | 577 | special_name("isclass"), |
paul@670 | 578 | [compiler.ast.CallFunc(special_name("loadtemp"), [temp])] |
paul@646 | 579 | ), |
paul@664 | 580 | module_attribute("__builtins__", "type") |
paul@646 | 581 | ]), |
paul@646 | 582 | access |
paul@646 | 583 | ]) |
paul@646 | 584 | ]) |
paul@646 | 585 | |
paul@646 | 586 | # General accesses. |
paul@646 | 587 | |
paul@646 | 588 | else: |
paul@646 | 589 | return access |
paul@646 | 590 | |
paul@638 | 591 | # Expressions. |
paul@638 | 592 | |
paul@638 | 593 | def visitAdd(self, node): |
paul@638 | 594 | return self._visitBinary(node) |
paul@638 | 595 | |
paul@638 | 596 | def visitAnd(self, node): |
paul@638 | 597 | return compiler.ast.And([self.dispatch(n) for n in node.nodes]) |
paul@638 | 598 | |
paul@663 | 599 | def visitAssAttr(self, node, expr=None): |
paul@663 | 600 | |
paul@663 | 601 | # Handle deletion. |
paul@663 | 602 | |
paul@663 | 603 | if compiler.ast.is_deletion(node): |
paul@663 | 604 | return compiler.ast.Stmt([]) |
paul@663 | 605 | |
paul@646 | 606 | return self._visitAttr(node, expr) |
paul@638 | 607 | |
paul@663 | 608 | def visitAssList(self, node, expr=None): |
paul@663 | 609 | |
paul@663 | 610 | # Handle deletion. |
paul@663 | 611 | |
paul@663 | 612 | if compiler.ast.is_deletion(compiler.ast.flatten_assignment(node)): |
paul@663 | 613 | return compiler.ast.Stmt([]) |
paul@663 | 614 | |
paul@638 | 615 | return compiler.ast.Stmt([ |
paul@638 | 616 | self.dispatch(n, compiler.ast.CallFunc( |
paul@638 | 617 | module_attribute("operator", "getitem"), |
paul@638 | 618 | [expr, i] |
paul@638 | 619 | )) |
paul@638 | 620 | for (i, n) in enumerate(node.nodes) |
paul@638 | 621 | ]) |
paul@638 | 622 | |
paul@663 | 623 | def visitAssName(self, node, expr=None): |
paul@663 | 624 | |
paul@663 | 625 | # Handle deletion. |
paul@663 | 626 | |
paul@663 | 627 | if compiler.ast.is_deletion(node): |
paul@663 | 628 | return compiler.ast.Stmt([]) |
paul@663 | 629 | |
paul@638 | 630 | unit = self.get_unit() |
paul@638 | 631 | |
paul@638 | 632 | # Generate appropriate name access operation. |
paul@645 | 633 | # NOTE: Should generate guards for attribute usage operations. |
paul@638 | 634 | |
paul@638 | 635 | scope = getattr(node, "_scope", None) |
paul@638 | 636 | if not scope: |
paul@638 | 637 | attr, scope, from_name = self.get_unit()._get_with_scope(node.name) |
paul@638 | 638 | |
paul@638 | 639 | if scope == "constant": |
paul@638 | 640 | return node |
paul@638 | 641 | elif scope == "local": |
paul@638 | 642 | |
paul@638 | 643 | # Function locals are stored using a function. |
paul@638 | 644 | |
paul@638 | 645 | if isinstance(unit, Function): |
paul@638 | 646 | return compiler.ast.CallFunc( |
paul@670 | 647 | special_name("storelocal"), |
paul@638 | 648 | [special_name(node.name), expr] |
paul@638 | 649 | ) |
paul@638 | 650 | |
paul@638 | 651 | # Class locals are class attribute references. |
paul@638 | 652 | |
paul@638 | 653 | elif isinstance(unit, Class): |
paul@638 | 654 | return compiler.ast.CallFunc( |
paul@670 | 655 | special_name("storeattrcontext"), |
paul@645 | 656 | [quoted_ref(unit), special_name(node.name), expr] |
paul@638 | 657 | ) |
paul@638 | 658 | |
paul@638 | 659 | # Module locals are module attribute references. |
paul@638 | 660 | |
paul@638 | 661 | elif isinstance(unit, Module): |
paul@638 | 662 | return compiler.ast.CallFunc( |
paul@670 | 663 | special_name("storeattr"), |
paul@645 | 664 | [quoted_ref(unit), special_name(node.name), expr] |
paul@638 | 665 | ) |
paul@638 | 666 | else: |
paul@638 | 667 | raise TranslateError("Program unit has no local %r." % name) |
paul@638 | 668 | |
paul@638 | 669 | elif scope == "global": |
paul@638 | 670 | |
paul@638 | 671 | # Globals are references to module attributes. |
paul@638 | 672 | |
paul@638 | 673 | return compiler.ast.CallFunc( |
paul@670 | 674 | special_name("storeattr"), |
paul@645 | 675 | [quoted_ref(self.get_module()), special_name(node.name), expr] |
paul@638 | 676 | ) |
paul@638 | 677 | |
paul@638 | 678 | elif scope == "builtin": |
paul@638 | 679 | |
paul@638 | 680 | # Builtins are accessed via the __builtins__ module. |
paul@638 | 681 | |
paul@638 | 682 | return compiler.ast.CallFunc( |
paul@670 | 683 | special_name("storeattr"), |
paul@638 | 684 | [special_name("__builtins__"), special_name(node.name), expr] |
paul@638 | 685 | ) |
paul@638 | 686 | |
paul@638 | 687 | else: |
paul@638 | 688 | # NOTE: This may happen because a class attribute is optimised away. |
paul@638 | 689 | return compiler.ast.CallFunc( |
paul@670 | 690 | special_name("storeunknown"), |
paul@638 | 691 | [special_name(node.name), expr] |
paul@638 | 692 | ) |
paul@638 | 693 | |
paul@638 | 694 | visitAssTuple = visitAssList |
paul@638 | 695 | |
paul@638 | 696 | def visitBitand(self, node): |
paul@638 | 697 | self._visitBitBinary(node) |
paul@638 | 698 | |
paul@638 | 699 | def visitBitor(self, node): |
paul@638 | 700 | self._visitBitBinary(node) |
paul@638 | 701 | |
paul@638 | 702 | def visitBitxor(self, node): |
paul@638 | 703 | self._visitBitBinary(node) |
paul@638 | 704 | |
paul@638 | 705 | def visitCallFunc(self, node): |
paul@638 | 706 | return compiler.ast.CallFunc( |
paul@670 | 707 | special_name("apply"), |
paul@670 | 708 | [self.dispatch(node.node)] + [self.dispatch(arg) for arg in node.args], |
paul@663 | 709 | node.star_args and self.dispatch(node.star_args), |
paul@663 | 710 | node.dstar_args and self.dispatch(node.dstar_args) |
paul@638 | 711 | ) |
paul@638 | 712 | |
paul@638 | 713 | def visitCompare(self, node): |
paul@638 | 714 | nodes = [] |
paul@638 | 715 | left = node.expr |
paul@638 | 716 | for op_name, right in node.ops: |
paul@670 | 717 | if op_name == "is": |
paul@670 | 718 | nodes.append( |
paul@670 | 719 | compiler.ast.CallFunc( |
paul@670 | 720 | special_name("__is__"), |
paul@670 | 721 | [self.dispatch(left), self.dispatch(right)] |
paul@670 | 722 | ) |
paul@638 | 723 | ) |
paul@670 | 724 | elif op_name == "is not": |
paul@670 | 725 | nodes.append( |
paul@670 | 726 | compiler.ast.CallFunc( |
paul@670 | 727 | special_name("__is_not__"), |
paul@670 | 728 | [self.dispatch(left), self.dispatch(right)] |
paul@670 | 729 | ) |
paul@670 | 730 | ) |
paul@670 | 731 | else: |
paul@670 | 732 | nodes.append( |
paul@670 | 733 | compiler.ast.CallFunc( |
paul@670 | 734 | module_attribute("operator", operator_functions.get(op_name)), |
paul@670 | 735 | [self.dispatch(left), self.dispatch(right)] |
paul@670 | 736 | ) |
paul@670 | 737 | ) |
paul@638 | 738 | left = right |
paul@638 | 739 | return compiler.ast.And(nodes) |
paul@638 | 740 | |
paul@638 | 741 | visitConst = NOP |
paul@638 | 742 | |
paul@638 | 743 | def visitDict(self, node): |
paul@638 | 744 | return compiler.ast.Dict([(self.dispatch(key), self.dispatch(value)) for (key, value) in node.items]) |
paul@638 | 745 | |
paul@638 | 746 | def visitDiv(self, node): |
paul@638 | 747 | return self._visitBinary(node) |
paul@638 | 748 | |
paul@638 | 749 | def visitFloorDiv(self, node): |
paul@638 | 750 | return self._visitBinary(node) |
paul@638 | 751 | |
paul@638 | 752 | def visitGetattr(self, node, expr=None): |
paul@646 | 753 | return self._visitAttr(node, expr) |
paul@645 | 754 | |
paul@638 | 755 | def visitGenExpr(self, node): |
paul@638 | 756 | return compiler.ast.GenExpr(self.dispatch(node.code)) |
paul@638 | 757 | |
paul@638 | 758 | def visitGenExprFor(self, node): |
paul@663 | 759 | expr = self.dispatch(node.iter) |
paul@638 | 760 | return compiler.ast.GenExprFor( |
paul@663 | 761 | self.dispatch(node.assign, expr), # NOTE: Needs to dispatch to AssName/AssTuple/AssList with an expression. |
paul@663 | 762 | expr, |
paul@638 | 763 | [self.dispatch(n) for n in node.ifs] |
paul@638 | 764 | ) |
paul@638 | 765 | |
paul@638 | 766 | def visitGenExprIf(self, node): |
paul@638 | 767 | return compiler.ast.GenExprIf(self.dispatch(node.test)) |
paul@638 | 768 | |
paul@638 | 769 | def visitGenExprInner(self, node): |
paul@638 | 770 | return compiler.ast.GenExprInner( |
paul@638 | 771 | self.dispatch(node.expr), |
paul@638 | 772 | [self.dispatch(n) for n in node.quals] |
paul@638 | 773 | ) |
paul@638 | 774 | |
paul@638 | 775 | def visitIfExp(self, node): |
paul@638 | 776 | return compiler.ast.IfExp( |
paul@638 | 777 | self.dispatch(node.then), |
paul@638 | 778 | self.dispatch(node.test), |
paul@638 | 779 | self.dispatch(node.else_) |
paul@638 | 780 | ) |
paul@638 | 781 | |
paul@638 | 782 | def visitInvert(self, node): |
paul@638 | 783 | return self._visitUnary(node) |
paul@638 | 784 | |
paul@638 | 785 | def visitKeyword(self, node): |
paul@638 | 786 | return compiler.ast.Keyword( |
paul@638 | 787 | node.name, |
paul@638 | 788 | self.dispatch(node.expr) |
paul@638 | 789 | ) |
paul@638 | 790 | |
paul@638 | 791 | def visitLambda(self, node): |
paul@638 | 792 | self.units.append(node.unit) |
paul@638 | 793 | |
paul@638 | 794 | try: |
paul@638 | 795 | return compiler.ast.Lambda( |
paul@638 | 796 | node.argnames, |
paul@638 | 797 | [self.dispatch(n) for n in node.defaults], |
paul@638 | 798 | node.flags, |
paul@638 | 799 | self.dispatch(node.code) |
paul@638 | 800 | ) |
paul@638 | 801 | finally: |
paul@638 | 802 | self.units.pop() |
paul@638 | 803 | |
paul@638 | 804 | def visitLeftShift(self, node): |
paul@638 | 805 | return self._visitBinary(node) |
paul@638 | 806 | |
paul@638 | 807 | def visitList(self, node, expr=None): |
paul@638 | 808 | if expr: |
paul@638 | 809 | return self.visitAssList(node, expr) |
paul@638 | 810 | return compiler.ast.List([self.dispatch(n) for n in node.nodes]) |
paul@638 | 811 | |
paul@638 | 812 | def visitListComp(self, node): |
paul@663 | 813 | |
paul@663 | 814 | """ |
paul@663 | 815 | Convert from... |
paul@663 | 816 | |
paul@663 | 817 | [<expr> for <assign> in <list> [ for <assign> in <list> ]... [ if <test> ]... ] |
paul@663 | 818 | |
paul@663 | 819 | ...to... |
paul@663 | 820 | |
paul@663 | 821 | _out = [] |
paul@663 | 822 | ... |
paul@663 | 823 | """ |
paul@663 | 824 | |
paul@663 | 825 | unit = self.get_unit() |
paul@663 | 826 | temp = quoted_name(unit.temp_usage) |
paul@663 | 827 | unit.temp_usage += 1 |
paul@663 | 828 | |
paul@663 | 829 | return compiler.ast.Stmt([ |
paul@670 | 830 | # storetemp(_out, __builtins__.list()) |
paul@670 | 831 | compiler.ast.CallFunc(special_name("storetemp"), [ |
paul@663 | 832 | temp, |
paul@663 | 833 | compiler.ast.CallFunc( |
paul@664 | 834 | module_attribute("__builtins__", "list"), |
paul@663 | 835 | [] |
paul@663 | 836 | ) |
paul@663 | 837 | ]), |
paul@663 | 838 | # ... |
paul@663 | 839 | self.dispatch(node.quals[0], temp, node.expr, node.quals[1:]) |
paul@663 | 840 | ]) |
paul@663 | 841 | |
paul@663 | 842 | def visitListCompFor(self, node, out_temp, expr, quals): |
paul@663 | 843 | |
paul@663 | 844 | """ |
paul@663 | 845 | Convert from... |
paul@663 | 846 | |
paul@663 | 847 | [<expr> for <assign> in <list> ...] |
paul@663 | 848 | |
paul@663 | 849 | ...to... |
paul@663 | 850 | |
paul@663 | 851 | _it = iter(<list>) |
paul@663 | 852 | while True: |
paul@663 | 853 | try: |
paul@663 | 854 | <assign> = _it.next() |
paul@663 | 855 | except StopIteration: |
paul@663 | 856 | break |
paul@663 | 857 | else: |
paul@663 | 858 | ... |
paul@663 | 859 | _out.append(<expr>) |
paul@663 | 860 | """ |
paul@663 | 861 | |
paul@663 | 862 | unit = self.get_unit() |
paul@663 | 863 | temp = quoted_name(unit.temp_usage) |
paul@663 | 864 | unit.temp_usage += 1 |
paul@663 | 865 | |
paul@663 | 866 | # Either generate more "for" or "if" constructs. |
paul@663 | 867 | |
paul@663 | 868 | if node.ifs or quals: |
paul@663 | 869 | nodes = node.ifs + quals |
paul@663 | 870 | body = self.dispatch(nodes[0], out_temp, expr, nodes[1:]) |
paul@663 | 871 | |
paul@663 | 872 | # Or generate the append statement. |
paul@663 | 873 | |
paul@663 | 874 | else: |
paul@663 | 875 | body = self._visitListCompExpr(out_temp, expr) |
paul@663 | 876 | |
paul@663 | 877 | # Wrap the above body in the loop construct. |
paul@663 | 878 | |
paul@663 | 879 | return compiler.ast.Stmt([ |
paul@670 | 880 | # storetemp(_it, __builtins__.iter(<list>)) |
paul@670 | 881 | compiler.ast.CallFunc(special_name("storetemp"), [ |
paul@663 | 882 | temp, |
paul@663 | 883 | compiler.ast.CallFunc( |
paul@664 | 884 | module_attribute("__builtins__", "iter"), |
paul@663 | 885 | [self.dispatch(node.list)] |
paul@663 | 886 | ) |
paul@663 | 887 | ]), |
paul@663 | 888 | # while True: ... |
paul@663 | 889 | compiler.ast.While( |
paul@663 | 890 | special_name("True"), |
paul@663 | 891 | # try: ... |
paul@663 | 892 | compiler.ast.TryExcept( |
paul@663 | 893 | compiler.ast.Stmt([ |
paul@663 | 894 | # <assign> = ... |
paul@663 | 895 | self.dispatch(node.assign, |
paul@663 | 896 | # _it.next() |
paul@663 | 897 | compiler.ast.CallFunc( |
paul@670 | 898 | compiler.ast.CallFunc(special_name("loadattrindex"), [ |
paul@670 | 899 | compiler.ast.CallFunc(special_name("loadtemp"), [temp]), |
paul@663 | 900 | special_name("next") |
paul@663 | 901 | ]), |
paul@663 | 902 | [] |
paul@663 | 903 | ) |
paul@663 | 904 | ) |
paul@663 | 905 | ]), |
paul@663 | 906 | # except StopIteration: ... |
paul@663 | 907 | [(special_name("StopIteration"), None, compiler.ast.Stmt([compiler.ast.Break()]))], |
paul@663 | 908 | # else: ... |
paul@663 | 909 | body |
paul@663 | 910 | ), |
paul@663 | 911 | None |
paul@663 | 912 | ) |
paul@663 | 913 | ]) |
paul@663 | 914 | |
paul@663 | 915 | def visitListCompIf(self, node, out_temp, expr, quals): |
paul@663 | 916 | |
paul@663 | 917 | # Either generate more "for" or "if" constructs. |
paul@663 | 918 | |
paul@663 | 919 | if quals: |
paul@663 | 920 | body = self.dispatch(quals[0], out_temp, expr, quals[1:]) |
paul@663 | 921 | |
paul@663 | 922 | # Or generate the append statement. |
paul@663 | 923 | |
paul@663 | 924 | else: |
paul@663 | 925 | body = self._visitListCompExpr(out_temp, expr) |
paul@663 | 926 | |
paul@663 | 927 | return compiler.ast.If( |
paul@663 | 928 | [(self.dispatch(node.test), body)], |
paul@663 | 929 | None |
paul@638 | 930 | ) |
paul@638 | 931 | |
paul@663 | 932 | def _visitListCompExpr(self, out_temp, expr): |
paul@663 | 933 | |
paul@663 | 934 | "To the 'out_temp' object, append the result of 'expr'." |
paul@638 | 935 | |
paul@663 | 936 | return compiler.ast.Stmt([ |
paul@663 | 937 | # _out.append(<expr>) |
paul@663 | 938 | compiler.ast.CallFunc( |
paul@670 | 939 | compiler.ast.CallFunc(special_name("loadattrindex"), [ |
paul@670 | 940 | compiler.ast.CallFunc(special_name("loadtemp"), [out_temp]), |
paul@663 | 941 | special_name("append") |
paul@663 | 942 | ]), |
paul@663 | 943 | [self.dispatch(expr)] |
paul@663 | 944 | ) |
paul@663 | 945 | ]) |
paul@638 | 946 | |
paul@638 | 947 | def visitMod(self, node): |
paul@638 | 948 | return self._visitBinary(node) |
paul@638 | 949 | |
paul@638 | 950 | def visitMul(self, node): |
paul@638 | 951 | return self._visitBinary(node) |
paul@638 | 952 | |
paul@638 | 953 | def visitName(self, node, expr=None): |
paul@638 | 954 | if expr: |
paul@638 | 955 | return self.visitAssName(node, expr) |
paul@638 | 956 | |
paul@638 | 957 | unit = self.get_unit() |
paul@645 | 958 | attr = node._attr |
paul@645 | 959 | scope = node._scope |
paul@638 | 960 | |
paul@638 | 961 | # Generate appropriate name access operation. |
paul@638 | 962 | |
paul@638 | 963 | if scope == "constant": |
paul@638 | 964 | return node |
paul@638 | 965 | |
paul@645 | 966 | # Function locals are referenced normally. |
paul@638 | 967 | |
paul@645 | 968 | elif scope == "local" and isinstance(unit, Function): |
paul@645 | 969 | return node |
paul@638 | 970 | |
paul@645 | 971 | # Other attributes should already be resolved. |
paul@638 | 972 | |
paul@663 | 973 | elif attr is not None and not isinstance(attr, Instance): |
paul@664 | 974 | if attr.is_constant(): |
paul@664 | 975 | return constant_attribute(quoted_ref(attr.parent), node.name) |
paul@664 | 976 | else: |
paul@664 | 977 | return compiler.ast.CallFunc( |
paul@670 | 978 | special_name("loadattr"), |
paul@664 | 979 | [quoted_ref(attr.parent), special_name(node.name)] |
paul@664 | 980 | ) |
paul@638 | 981 | |
paul@663 | 982 | # Function globals are referenced via the module. |
paul@663 | 983 | |
paul@663 | 984 | elif scope == "global": |
paul@663 | 985 | return compiler.ast.CallFunc( |
paul@670 | 986 | special_name("loadattr"), |
paul@663 | 987 | [quoted_ref(self.get_module()), special_name(node.name)] |
paul@663 | 988 | ) |
paul@663 | 989 | |
paul@645 | 990 | # NOTE: Unknown attributes may arise because a class attribute has been |
paul@645 | 991 | # NOTE: optimised away. |
paul@638 | 992 | |
paul@638 | 993 | else: |
paul@638 | 994 | return compiler.ast.CallFunc( |
paul@670 | 995 | special_name("loadunknown"), |
paul@638 | 996 | [special_name(node.name)] |
paul@638 | 997 | ) |
paul@638 | 998 | |
paul@638 | 999 | def visitNot(self, node): |
paul@638 | 1000 | return compiler.ast.Not(self.dispatch(node.expr)) |
paul@638 | 1001 | |
paul@638 | 1002 | def visitOr(self, node): |
paul@638 | 1003 | return compiler.ast.Or([self.dispatch(n) for n in node.nodes]) |
paul@638 | 1004 | |
paul@638 | 1005 | def visitPower(self, node): |
paul@638 | 1006 | return self._visitBinary(node) |
paul@638 | 1007 | |
paul@638 | 1008 | def visitRightShift(self, node): |
paul@638 | 1009 | return self._visitBinary(node) |
paul@638 | 1010 | |
paul@638 | 1011 | def visitSlice(self, node, expr=None): |
paul@638 | 1012 | return compiler.ast.CallFunc( |
paul@638 | 1013 | module_attribute("operator", expr and "setslice" or "getslice"), |
paul@638 | 1014 | [self.dispatch(node.expr), node.lower and self.dispatch(node.lower), node.upper and self.dispatch(node.upper)] |
paul@638 | 1015 | + (expr and [expr] or []) |
paul@638 | 1016 | ) |
paul@638 | 1017 | |
paul@638 | 1018 | def visitSliceobj(self, node): |
paul@638 | 1019 | return compiler.ast.Sliceobj([self.dispatch(n) for n in node.nodes]) |
paul@638 | 1020 | |
paul@638 | 1021 | def visitSub(self, node): |
paul@638 | 1022 | return self._visitBinary(node) |
paul@638 | 1023 | |
paul@638 | 1024 | def visitSubscript(self, node, expr=None): |
paul@638 | 1025 | return compiler.ast.CallFunc( |
paul@638 | 1026 | module_attribute("operator", expr and "setitem" or "getitem"), |
paul@638 | 1027 | [self.dispatch(node.expr), compiler.ast.Tuple([self.dispatch(sub) for sub in node.subs])] |
paul@638 | 1028 | + (expr and [expr] or []) |
paul@638 | 1029 | ) |
paul@638 | 1030 | |
paul@638 | 1031 | def visitTuple(self, node, expr=None): |
paul@638 | 1032 | if expr: |
paul@638 | 1033 | return self.visitAssTuple(node, expr) |
paul@638 | 1034 | return compiler.ast.Tuple([self.dispatch(n) for n in node.nodes]) |
paul@638 | 1035 | |
paul@638 | 1036 | def visitUnaryAdd(self, node): |
paul@638 | 1037 | return self._visitUnary(node) |
paul@638 | 1038 | |
paul@638 | 1039 | def visitUnarySub(self, node): |
paul@638 | 1040 | return self._visitUnary(node) |
paul@638 | 1041 | |
paul@638 | 1042 | # Convenience functions. |
paul@638 | 1043 | |
paul@638 | 1044 | def convert(module, program, filename): |
paul@638 | 1045 | stream = open(filename, "wb") |
paul@638 | 1046 | try: |
paul@638 | 1047 | source = ConvertedSource(module, program) |
paul@638 | 1048 | source.to_stream(stream) |
paul@638 | 1049 | finally: |
paul@638 | 1050 | stream.close() |
paul@638 | 1051 | |
paul@638 | 1052 | def translate(program, directory): |
paul@638 | 1053 | if not exists(directory): |
paul@638 | 1054 | os.mkdir(directory) |
paul@638 | 1055 | |
paul@638 | 1056 | # NOTE: Add constants here. |
paul@638 | 1057 | |
paul@638 | 1058 | for module in program.get_importer().get_modules(): |
paul@638 | 1059 | convert(module, program, join(directory, "%s%spy" % (module.full_name(), extsep))) |
paul@638 | 1060 | |
paul@638 | 1061 | # vim: tabstop=4 expandtab shiftwidth=4 |