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@664 | 44 | return compiler.ast.CallFunc("__static__", [quoted_name(obj.full_name())]) |
paul@664 | 45 | |
paul@664 | 46 | def module_attribute(module_name, attrname): |
paul@664 | 47 | return compiler.ast.Getattr(special_name(module_name), attrname) |
paul@664 | 48 | |
paul@646 | 49 | # Special function names. |
paul@646 | 50 | # NOTE: Some of the assignment operations should not be supported unless |
paul@646 | 51 | # NOTE: attribute usage observations are being made. |
paul@646 | 52 | |
paul@646 | 53 | assattr_functions = ("__storeattrcontext__", "__storeattrcontext__", "__storeattr__", |
paul@646 | 54 | "__storeattrindex__", None) |
paul@646 | 55 | getattr_functions = ("__loadattrcontext__", "__loadattrcontextcond__", "__loadattr__", |
paul@646 | 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@638 | 94 | # __globalnames__(name, ...) |
paul@638 | 95 | |
paul@638 | 96 | globalnames = module.module_attribute_names() and [ |
paul@638 | 97 | compiler.ast.CallFunc( |
paul@638 | 98 | special_name("__globalnames__"), |
paul@638 | 99 | [special_name(name) for name in module.module_attribute_names()] |
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@638 | 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@638 | 160 | # __instattrs__(name, ...) |
paul@638 | 161 | # __clsattrs__(name, ...) |
paul@638 | 162 | |
paul@638 | 163 | instattrs = cls.instance_attribute_names() and [ |
paul@638 | 164 | compiler.ast.CallFunc( |
paul@638 | 165 | special_name("__instattrs__"), |
paul@638 | 166 | [special_name(name) for name in cls.instance_attribute_names()] |
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@638 | 172 | special_name("__clsattrs__"), |
paul@638 | 173 | [special_name(name) for name in cls.class_attribute_names()] |
paul@638 | 174 | ) |
paul@638 | 175 | ] or [] |
paul@638 | 176 | |
paul@638 | 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@638 | 194 | special_name("__inherited__"), |
paul@645 | 195 | [quoted_ref(supercls)] + [special_name(name) for name in attrnames] |
paul@638 | 196 | )) |
paul@638 | 197 | |
paul@638 | 198 | # __descendants__(name, ...) |
paul@638 | 199 | |
paul@638 | 200 | descendants = cls.all_descendants() and [ |
paul@638 | 201 | compiler.ast.CallFunc( |
paul@638 | 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@664 | 250 | # __storetemp__(_it, __builtins__.iter(<list>)) |
paul@638 | 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@638 | 268 | compiler.ast.CallFunc(special_name("__loadattr__"), [ |
paul@638 | 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@638 | 299 | compiler.ast.Assign( |
paul@638 | 300 | [special_name(alias or name)], |
paul@638 | 301 | compiler.ast.CallFunc( |
paul@638 | 302 | special_name("__loadattribute__"), |
paul@638 | 303 | [special_name(node.modname), special_name(name)] |
paul@638 | 304 | ) |
paul@638 | 305 | ) |
paul@638 | 306 | ) |
paul@638 | 307 | |
paul@638 | 308 | return compiler.ast.Stmt(statements) |
paul@638 | 309 | |
paul@638 | 310 | def visitFunction(self, node): |
paul@638 | 311 | if not used_by_unit(node): |
paul@638 | 312 | return compiler.ast.Stmt([]) |
paul@638 | 313 | |
paul@638 | 314 | self.units.append(node.unit) |
paul@638 | 315 | |
paul@638 | 316 | try: |
paul@638 | 317 | # Ignore functions when generating the main function. |
paul@638 | 318 | |
paul@638 | 319 | if self.in_main: |
paul@638 | 320 | return compiler.ast.Stmt([]) |
paul@638 | 321 | else: |
paul@638 | 322 | return self._visitFunctionDefinition(node) |
paul@638 | 323 | finally: |
paul@638 | 324 | self.units.pop() |
paul@638 | 325 | |
paul@638 | 326 | def _visitFunctionDefinition(self, node): |
paul@638 | 327 | fn = node.unit |
paul@638 | 328 | |
paul@638 | 329 | # __localnames__(name, ...) |
paul@638 | 330 | # __globalnames__(name, ...) |
paul@638 | 331 | |
paul@645 | 332 | localnames = fn.all_locals() and [ |
paul@638 | 333 | compiler.ast.CallFunc( |
paul@638 | 334 | special_name("__localnames__"), |
paul@645 | 335 | [special_name(name) for name in fn.all_locals().keys()] |
paul@638 | 336 | ) |
paul@638 | 337 | ] or [] |
paul@638 | 338 | |
paul@638 | 339 | globalnames = fn.globals and [ |
paul@638 | 340 | compiler.ast.CallFunc( |
paul@638 | 341 | special_name("__globalnames__"), |
paul@638 | 342 | [special_name(name) for name in fn.globals] |
paul@638 | 343 | ) |
paul@638 | 344 | ] or [] |
paul@638 | 345 | |
paul@638 | 346 | defaults = [self.dispatch(n) for n in node.defaults] |
paul@638 | 347 | |
paul@645 | 348 | # NOTE: Should generate guards for attribute usage operations. |
paul@645 | 349 | |
paul@638 | 350 | code = self.dispatch(node.code) |
paul@638 | 351 | |
paul@638 | 352 | return compiler.ast.Function(node.decorators, node.name, node.argnames, defaults, node.flags, node.doc, |
paul@638 | 353 | compiler.ast.Stmt(localnames + globalnames + code.nodes)) |
paul@638 | 354 | |
paul@638 | 355 | visitGlobal = NOP |
paul@638 | 356 | |
paul@638 | 357 | def visitIf(self, node): |
paul@638 | 358 | return compiler.ast.If( |
paul@638 | 359 | [(self.dispatch(compare), self.dispatch(stmt)) for (compare, stmt) in node.tests], |
paul@638 | 360 | node.else_ and self.dispatch(node.else_) |
paul@638 | 361 | ) |
paul@638 | 362 | |
paul@638 | 363 | def visitImport(self, node): |
paul@638 | 364 | |
paul@638 | 365 | # Generate __main__ function calls for each step in the imported module |
paul@638 | 366 | # hierarchy. |
paul@638 | 367 | |
paul@638 | 368 | statements = [] |
paul@638 | 369 | |
paul@638 | 370 | for name, alias in node.names: |
paul@638 | 371 | for modname in self.module.get_module_paths(name): |
paul@638 | 372 | statements.append( |
paul@638 | 373 | compiler.ast.CallFunc(compiler.ast.Getattr(modname, "__main__"), []) |
paul@638 | 374 | ) |
paul@638 | 375 | |
paul@638 | 376 | statements.append( |
paul@638 | 377 | compiler.ast.Assign( |
paul@638 | 378 | [special_name(alias or name.split(".")[0])], |
paul@638 | 379 | compiler.ast.CallFunc( |
paul@638 | 380 | special_name("__static__"), |
paul@638 | 381 | [special_name(name)] |
paul@638 | 382 | ) |
paul@638 | 383 | ) |
paul@638 | 384 | ) |
paul@638 | 385 | |
paul@638 | 386 | return compiler.ast.Stmt(statements) |
paul@638 | 387 | |
paul@645 | 388 | def visitPass(self, node): |
paul@645 | 389 | if not isinstance(self.get_unit(), Class): |
paul@645 | 390 | return compiler.ast.Pass() |
paul@645 | 391 | else: |
paul@645 | 392 | return compiler.ast.Stmt([]) |
paul@638 | 393 | |
paul@638 | 394 | def visitPrint(self, node): |
paul@638 | 395 | return compiler.ast.Print( |
paul@638 | 396 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 397 | node.dest and self.dispatch(node.dest) |
paul@638 | 398 | ) |
paul@638 | 399 | |
paul@638 | 400 | def visitPrintnl(self, node): |
paul@638 | 401 | return compiler.ast.Print( |
paul@638 | 402 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 403 | node.dest and self.dispatch(node.dest) |
paul@638 | 404 | ) |
paul@638 | 405 | |
paul@638 | 406 | def visitRaise(self, node): |
paul@638 | 407 | return compiler.ast.Raise( |
paul@638 | 408 | node.expr1 and self.dispatch(node.expr1), |
paul@638 | 409 | node.expr2 and self.dispatch(node.expr2), |
paul@638 | 410 | node.expr3 and self.dispatch(node.expr3) |
paul@638 | 411 | ) |
paul@638 | 412 | |
paul@638 | 413 | def visitReturn(self, node): |
paul@638 | 414 | return compiler.ast.Return(self.dispatch(node.value)) |
paul@638 | 415 | |
paul@638 | 416 | def visitStmt(self, node): |
paul@638 | 417 | return compiler.ast.Stmt([self.dispatch(n) for n in node.nodes]) |
paul@638 | 418 | |
paul@638 | 419 | def visitTryExcept(self, node): |
paul@638 | 420 | # NOTE: Need to dispatch to the assignment with the exception. |
paul@638 | 421 | return compiler.ast.TryExcept( |
paul@638 | 422 | self.dispatch(node.body), |
paul@638 | 423 | [(spec and self.dispatch(spec), assign and self.dispatch(assign), self.dispatch(statement)) |
paul@638 | 424 | for spec, assign, statement in node.handlers], |
paul@638 | 425 | node.else_ and self.dispatch(node.else_) |
paul@638 | 426 | ) |
paul@638 | 427 | |
paul@638 | 428 | def visitTryFinally(self, node): |
paul@638 | 429 | return compiler.ast.TryFinally( |
paul@638 | 430 | self.dispatch(node.body), |
paul@638 | 431 | self.dispatch(node.final) |
paul@638 | 432 | ) |
paul@638 | 433 | |
paul@638 | 434 | def visitWhile(self, node): |
paul@638 | 435 | return compiler.ast.While( |
paul@638 | 436 | self.dispatch(node.test), |
paul@638 | 437 | self.dispatch(node.body), |
paul@638 | 438 | node.else_ and self.dispatch(node.else_) |
paul@638 | 439 | ) |
paul@638 | 440 | |
paul@638 | 441 | def visitYield(self, node): |
paul@638 | 442 | return compiler.ast.Yield(self.dispatch(node.value)) |
paul@638 | 443 | |
paul@638 | 444 | # Expression-related helper methods. |
paul@638 | 445 | |
paul@638 | 446 | def _visitBitBinary(self, node): |
paul@638 | 447 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 448 | last = self.dispatch(node.nodes[0]) |
paul@638 | 449 | |
paul@638 | 450 | for n in node.nodes[1:]: |
paul@638 | 451 | last = compiler.ast.CallFunc( |
paul@638 | 452 | module_attribute("operator", op_name), |
paul@638 | 453 | [last, self.dispatch(n)] |
paul@638 | 454 | ) |
paul@638 | 455 | |
paul@638 | 456 | return last |
paul@638 | 457 | |
paul@638 | 458 | def _visitBinary(self, node): |
paul@638 | 459 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 460 | |
paul@638 | 461 | return compiler.ast.CallFunc( |
paul@638 | 462 | module_attribute("operator", op_name), |
paul@638 | 463 | [self.dispatch(node.left), self.dispatch(node.right)] |
paul@638 | 464 | ) |
paul@638 | 465 | |
paul@638 | 466 | def _visitUnary(self, node): |
paul@638 | 467 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 468 | |
paul@638 | 469 | return compiler.ast.CallFunc( |
paul@638 | 470 | module_attribute("operator", op_name), |
paul@638 | 471 | [self.dispatch(node.expr)] |
paul@638 | 472 | ) |
paul@638 | 473 | |
paul@645 | 474 | def _generateValue(self, value): |
paul@645 | 475 | |
paul@645 | 476 | # Literal constants. |
paul@645 | 477 | |
paul@645 | 478 | if isinstance(value, Const): |
paul@645 | 479 | return compiler.ast.Const(value.get_value()) |
paul@645 | 480 | |
paul@645 | 481 | # Other constant structures. |
paul@645 | 482 | |
paul@645 | 483 | if isinstance(value, Constant): |
paul@645 | 484 | return quoted_ref(value) |
paul@645 | 485 | |
paul@645 | 486 | return None |
paul@645 | 487 | |
paul@646 | 488 | def _visitAttr(self, node, expr=None): |
paul@646 | 489 | unit = self.get_unit() |
paul@646 | 490 | |
paul@646 | 491 | # Choose the appropriate special functions. |
paul@646 | 492 | |
paul@646 | 493 | (opattrcontext, opattrcontextcond, opattr, |
paul@646 | 494 | opattrindex, opattrindexcontextcond) = expr and assattr_functions or getattr_functions |
paul@646 | 495 | |
paul@646 | 496 | accessor = self.dispatch(node.expr) |
paul@646 | 497 | |
paul@646 | 498 | # Generate already-deduced accesses. |
paul@646 | 499 | |
paul@646 | 500 | if node._access_type == "constant": |
paul@646 | 501 | return self._generateValue(node._value_deduced) |
paul@646 | 502 | |
paul@646 | 503 | # Generate accesses via static objects and instances. |
paul@646 | 504 | |
paul@646 | 505 | if node._attr_deduced: |
paul@646 | 506 | if node._set_context == "set": |
paul@646 | 507 | op = opattrcontext |
paul@646 | 508 | elif node._set_context == "cond": |
paul@646 | 509 | op = opattrcontextcond |
paul@646 | 510 | else: |
paul@646 | 511 | op = opattr |
paul@646 | 512 | |
paul@646 | 513 | # Handle unsupported operations. |
paul@646 | 514 | |
paul@646 | 515 | if not op: |
paul@646 | 516 | raise TranslateError("Storing of class attribute %r via self not permitted." % node.attrname) |
paul@646 | 517 | |
paul@646 | 518 | # Define the arguments: accessor, attribute name and optional value. |
paul@646 | 519 | |
paul@646 | 520 | args = [ |
paul@646 | 521 | node._access_type == "static" and \ |
paul@646 | 522 | self._generateValue(node._attr_deduced.parent) or accessor, |
paul@646 | 523 | special_name(node.attrname) |
paul@646 | 524 | ] |
paul@646 | 525 | |
paul@646 | 526 | if expr: |
paul@646 | 527 | args.append(expr) |
paul@646 | 528 | |
paul@646 | 529 | return compiler.ast.CallFunc(special_name(op), args) |
paul@646 | 530 | |
paul@646 | 531 | # Positioned accesses are normal accesses via instances. |
paul@646 | 532 | |
paul@646 | 533 | if node._access_type == "positioned": |
paul@646 | 534 | args = [accessor, special_name(node.attrname)] |
paul@646 | 535 | if expr: |
paul@646 | 536 | args.append(expr) |
paul@646 | 537 | return compiler.ast.CallFunc(special_name(opattr), args) |
paul@646 | 538 | |
paul@646 | 539 | # With no usable deductions, generate a table-based access. |
paul@646 | 540 | |
paul@646 | 541 | args = [accessor, special_name(node.attrname)] |
paul@646 | 542 | if expr: |
paul@646 | 543 | args.append(expr) |
paul@646 | 544 | access = compiler.ast.CallFunc(special_name(opattrindexcontextcond), args) |
paul@646 | 545 | |
paul@646 | 546 | # class.__class__ => __builtins__.type |
paul@646 | 547 | |
paul@646 | 548 | if node.attrname == "__class__": |
paul@646 | 549 | |
paul@646 | 550 | # __storetemp__(n, <accessor>) |
paul@664 | 551 | # __isclass__(n) and __builtins__.type or <access> |
paul@646 | 552 | |
paul@646 | 553 | temp = quoted_name(unit.temp_usage) |
paul@646 | 554 | unit.temp_usage += 1 |
paul@646 | 555 | |
paul@646 | 556 | return compiler.ast.Stmt([ |
paul@646 | 557 | compiler.ast.CallFunc(special_name("__storetemp__"), [temp, access]), |
paul@646 | 558 | compiler.ast.Or([ |
paul@646 | 559 | compiler.ast.And([ |
paul@646 | 560 | compiler.ast.CallFunc( |
paul@646 | 561 | special_name("__isclass__"), |
paul@646 | 562 | [compiler.ast.CallFunc(special_name("__loadtemp__"), [temp])] |
paul@646 | 563 | ), |
paul@664 | 564 | module_attribute("__builtins__", "type") |
paul@646 | 565 | ]), |
paul@646 | 566 | access |
paul@646 | 567 | ]) |
paul@646 | 568 | ]) |
paul@646 | 569 | |
paul@646 | 570 | # General accesses. |
paul@646 | 571 | |
paul@646 | 572 | else: |
paul@646 | 573 | return access |
paul@646 | 574 | |
paul@638 | 575 | # Expressions. |
paul@638 | 576 | |
paul@638 | 577 | def visitAdd(self, node): |
paul@638 | 578 | return self._visitBinary(node) |
paul@638 | 579 | |
paul@638 | 580 | def visitAnd(self, node): |
paul@638 | 581 | return compiler.ast.And([self.dispatch(n) for n in node.nodes]) |
paul@638 | 582 | |
paul@663 | 583 | def visitAssAttr(self, node, expr=None): |
paul@663 | 584 | |
paul@663 | 585 | # Handle deletion. |
paul@663 | 586 | |
paul@663 | 587 | if compiler.ast.is_deletion(node): |
paul@663 | 588 | return compiler.ast.Stmt([]) |
paul@663 | 589 | |
paul@646 | 590 | return self._visitAttr(node, expr) |
paul@638 | 591 | |
paul@663 | 592 | def visitAssList(self, node, expr=None): |
paul@663 | 593 | |
paul@663 | 594 | # Handle deletion. |
paul@663 | 595 | |
paul@663 | 596 | if compiler.ast.is_deletion(compiler.ast.flatten_assignment(node)): |
paul@663 | 597 | return compiler.ast.Stmt([]) |
paul@663 | 598 | |
paul@638 | 599 | return compiler.ast.Stmt([ |
paul@638 | 600 | self.dispatch(n, compiler.ast.CallFunc( |
paul@638 | 601 | module_attribute("operator", "getitem"), |
paul@638 | 602 | [expr, i] |
paul@638 | 603 | )) |
paul@638 | 604 | for (i, n) in enumerate(node.nodes) |
paul@638 | 605 | ]) |
paul@638 | 606 | |
paul@663 | 607 | def visitAssName(self, node, expr=None): |
paul@663 | 608 | |
paul@663 | 609 | # Handle deletion. |
paul@663 | 610 | |
paul@663 | 611 | if compiler.ast.is_deletion(node): |
paul@663 | 612 | return compiler.ast.Stmt([]) |
paul@663 | 613 | |
paul@638 | 614 | unit = self.get_unit() |
paul@638 | 615 | |
paul@638 | 616 | # Generate appropriate name access operation. |
paul@645 | 617 | # NOTE: Should generate guards for attribute usage operations. |
paul@638 | 618 | |
paul@638 | 619 | scope = getattr(node, "_scope", None) |
paul@638 | 620 | if not scope: |
paul@638 | 621 | attr, scope, from_name = self.get_unit()._get_with_scope(node.name) |
paul@638 | 622 | |
paul@638 | 623 | if scope == "constant": |
paul@638 | 624 | return node |
paul@638 | 625 | elif scope == "local": |
paul@638 | 626 | |
paul@638 | 627 | # Function locals are stored using a function. |
paul@638 | 628 | |
paul@638 | 629 | if isinstance(unit, Function): |
paul@638 | 630 | return compiler.ast.CallFunc( |
paul@638 | 631 | special_name("__storelocal__"), |
paul@638 | 632 | [special_name(node.name), expr] |
paul@638 | 633 | ) |
paul@638 | 634 | |
paul@638 | 635 | # Class locals are class attribute references. |
paul@638 | 636 | |
paul@638 | 637 | elif isinstance(unit, Class): |
paul@638 | 638 | return compiler.ast.CallFunc( |
paul@645 | 639 | special_name("__storeattrcontext__"), |
paul@645 | 640 | [quoted_ref(unit), special_name(node.name), expr] |
paul@638 | 641 | ) |
paul@638 | 642 | |
paul@638 | 643 | # Module locals are module attribute references. |
paul@638 | 644 | |
paul@638 | 645 | elif isinstance(unit, Module): |
paul@638 | 646 | return compiler.ast.CallFunc( |
paul@645 | 647 | special_name("__storeattr__"), |
paul@645 | 648 | [quoted_ref(unit), special_name(node.name), expr] |
paul@638 | 649 | ) |
paul@638 | 650 | else: |
paul@638 | 651 | raise TranslateError("Program unit has no local %r." % name) |
paul@638 | 652 | |
paul@638 | 653 | elif scope == "global": |
paul@638 | 654 | |
paul@638 | 655 | # Globals are references to module attributes. |
paul@638 | 656 | |
paul@638 | 657 | return compiler.ast.CallFunc( |
paul@645 | 658 | special_name("__storeattr__"), |
paul@645 | 659 | [quoted_ref(self.get_module()), special_name(node.name), expr] |
paul@638 | 660 | ) |
paul@638 | 661 | |
paul@638 | 662 | elif scope == "builtin": |
paul@638 | 663 | |
paul@638 | 664 | # Builtins are accessed via the __builtins__ module. |
paul@638 | 665 | |
paul@638 | 666 | return compiler.ast.CallFunc( |
paul@645 | 667 | special_name("__storeattr__"), |
paul@638 | 668 | [special_name("__builtins__"), special_name(node.name), expr] |
paul@638 | 669 | ) |
paul@638 | 670 | |
paul@638 | 671 | else: |
paul@638 | 672 | # NOTE: This may happen because a class attribute is optimised away. |
paul@638 | 673 | return compiler.ast.CallFunc( |
paul@638 | 674 | special_name("__storeunknown__"), |
paul@638 | 675 | [special_name(node.name), expr] |
paul@638 | 676 | ) |
paul@638 | 677 | |
paul@638 | 678 | visitAssTuple = visitAssList |
paul@638 | 679 | |
paul@638 | 680 | def visitBitand(self, node): |
paul@638 | 681 | self._visitBitBinary(node) |
paul@638 | 682 | |
paul@638 | 683 | def visitBitor(self, node): |
paul@638 | 684 | self._visitBitBinary(node) |
paul@638 | 685 | |
paul@638 | 686 | def visitBitxor(self, node): |
paul@638 | 687 | self._visitBitBinary(node) |
paul@638 | 688 | |
paul@638 | 689 | def visitCallFunc(self, node): |
paul@638 | 690 | return compiler.ast.CallFunc( |
paul@638 | 691 | self.dispatch(node.node), |
paul@638 | 692 | [self.dispatch(arg) for arg in node.args], |
paul@663 | 693 | node.star_args and self.dispatch(node.star_args), |
paul@663 | 694 | node.dstar_args and self.dispatch(node.dstar_args) |
paul@638 | 695 | ) |
paul@638 | 696 | |
paul@638 | 697 | def visitCompare(self, node): |
paul@638 | 698 | nodes = [] |
paul@638 | 699 | left = node.expr |
paul@638 | 700 | for op_name, right in node.ops: |
paul@638 | 701 | nodes.append( |
paul@638 | 702 | compiler.ast.CallFunc( |
paul@638 | 703 | module_attribute("operator", operator_functions.get(op_name)), |
paul@638 | 704 | [self.dispatch(left), self.dispatch(right)] |
paul@638 | 705 | ) |
paul@638 | 706 | ) |
paul@638 | 707 | left = right |
paul@638 | 708 | return compiler.ast.And(nodes) |
paul@638 | 709 | |
paul@638 | 710 | visitConst = NOP |
paul@638 | 711 | |
paul@638 | 712 | def visitDict(self, node): |
paul@638 | 713 | return compiler.ast.Dict([(self.dispatch(key), self.dispatch(value)) for (key, value) in node.items]) |
paul@638 | 714 | |
paul@638 | 715 | def visitDiv(self, node): |
paul@638 | 716 | return self._visitBinary(node) |
paul@638 | 717 | |
paul@638 | 718 | def visitFloorDiv(self, node): |
paul@638 | 719 | return self._visitBinary(node) |
paul@638 | 720 | |
paul@638 | 721 | def visitGetattr(self, node, expr=None): |
paul@646 | 722 | return self._visitAttr(node, expr) |
paul@645 | 723 | |
paul@638 | 724 | def visitGenExpr(self, node): |
paul@638 | 725 | return compiler.ast.GenExpr(self.dispatch(node.code)) |
paul@638 | 726 | |
paul@638 | 727 | def visitGenExprFor(self, node): |
paul@663 | 728 | expr = self.dispatch(node.iter) |
paul@638 | 729 | return compiler.ast.GenExprFor( |
paul@663 | 730 | self.dispatch(node.assign, expr), # NOTE: Needs to dispatch to AssName/AssTuple/AssList with an expression. |
paul@663 | 731 | expr, |
paul@638 | 732 | [self.dispatch(n) for n in node.ifs] |
paul@638 | 733 | ) |
paul@638 | 734 | |
paul@638 | 735 | def visitGenExprIf(self, node): |
paul@638 | 736 | return compiler.ast.GenExprIf(self.dispatch(node.test)) |
paul@638 | 737 | |
paul@638 | 738 | def visitGenExprInner(self, node): |
paul@638 | 739 | return compiler.ast.GenExprInner( |
paul@638 | 740 | self.dispatch(node.expr), |
paul@638 | 741 | [self.dispatch(n) for n in node.quals] |
paul@638 | 742 | ) |
paul@638 | 743 | |
paul@638 | 744 | def visitIfExp(self, node): |
paul@638 | 745 | return compiler.ast.IfExp( |
paul@638 | 746 | self.dispatch(node.then), |
paul@638 | 747 | self.dispatch(node.test), |
paul@638 | 748 | self.dispatch(node.else_) |
paul@638 | 749 | ) |
paul@638 | 750 | |
paul@638 | 751 | def visitInvert(self, node): |
paul@638 | 752 | return self._visitUnary(node) |
paul@638 | 753 | |
paul@638 | 754 | def visitKeyword(self, node): |
paul@638 | 755 | return compiler.ast.Keyword( |
paul@638 | 756 | node.name, |
paul@638 | 757 | self.dispatch(node.expr) |
paul@638 | 758 | ) |
paul@638 | 759 | |
paul@638 | 760 | def visitLambda(self, node): |
paul@638 | 761 | self.units.append(node.unit) |
paul@638 | 762 | |
paul@638 | 763 | try: |
paul@638 | 764 | return compiler.ast.Lambda( |
paul@638 | 765 | node.argnames, |
paul@638 | 766 | [self.dispatch(n) for n in node.defaults], |
paul@638 | 767 | node.flags, |
paul@638 | 768 | self.dispatch(node.code) |
paul@638 | 769 | ) |
paul@638 | 770 | finally: |
paul@638 | 771 | self.units.pop() |
paul@638 | 772 | |
paul@638 | 773 | def visitLeftShift(self, node): |
paul@638 | 774 | return self._visitBinary(node) |
paul@638 | 775 | |
paul@638 | 776 | def visitList(self, node, expr=None): |
paul@638 | 777 | if expr: |
paul@638 | 778 | return self.visitAssList(node, expr) |
paul@638 | 779 | return compiler.ast.List([self.dispatch(n) for n in node.nodes]) |
paul@638 | 780 | |
paul@638 | 781 | def visitListComp(self, node): |
paul@663 | 782 | |
paul@663 | 783 | """ |
paul@663 | 784 | Convert from... |
paul@663 | 785 | |
paul@663 | 786 | [<expr> for <assign> in <list> [ for <assign> in <list> ]... [ if <test> ]... ] |
paul@663 | 787 | |
paul@663 | 788 | ...to... |
paul@663 | 789 | |
paul@663 | 790 | _out = [] |
paul@663 | 791 | ... |
paul@663 | 792 | """ |
paul@663 | 793 | |
paul@663 | 794 | unit = self.get_unit() |
paul@663 | 795 | temp = quoted_name(unit.temp_usage) |
paul@663 | 796 | unit.temp_usage += 1 |
paul@663 | 797 | |
paul@663 | 798 | return compiler.ast.Stmt([ |
paul@664 | 799 | # __storetemp__(_out, __builtins__.list()) |
paul@663 | 800 | compiler.ast.CallFunc(special_name("__storetemp__"), [ |
paul@663 | 801 | temp, |
paul@663 | 802 | compiler.ast.CallFunc( |
paul@664 | 803 | module_attribute("__builtins__", "list"), |
paul@663 | 804 | [] |
paul@663 | 805 | ) |
paul@663 | 806 | ]), |
paul@663 | 807 | # ... |
paul@663 | 808 | self.dispatch(node.quals[0], temp, node.expr, node.quals[1:]) |
paul@663 | 809 | ]) |
paul@663 | 810 | |
paul@663 | 811 | def visitListCompFor(self, node, out_temp, expr, quals): |
paul@663 | 812 | |
paul@663 | 813 | """ |
paul@663 | 814 | Convert from... |
paul@663 | 815 | |
paul@663 | 816 | [<expr> for <assign> in <list> ...] |
paul@663 | 817 | |
paul@663 | 818 | ...to... |
paul@663 | 819 | |
paul@663 | 820 | _it = iter(<list>) |
paul@663 | 821 | while True: |
paul@663 | 822 | try: |
paul@663 | 823 | <assign> = _it.next() |
paul@663 | 824 | except StopIteration: |
paul@663 | 825 | break |
paul@663 | 826 | else: |
paul@663 | 827 | ... |
paul@663 | 828 | _out.append(<expr>) |
paul@663 | 829 | """ |
paul@663 | 830 | |
paul@663 | 831 | unit = self.get_unit() |
paul@663 | 832 | temp = quoted_name(unit.temp_usage) |
paul@663 | 833 | unit.temp_usage += 1 |
paul@663 | 834 | |
paul@663 | 835 | # Either generate more "for" or "if" constructs. |
paul@663 | 836 | |
paul@663 | 837 | if node.ifs or quals: |
paul@663 | 838 | nodes = node.ifs + quals |
paul@663 | 839 | body = self.dispatch(nodes[0], out_temp, expr, nodes[1:]) |
paul@663 | 840 | |
paul@663 | 841 | # Or generate the append statement. |
paul@663 | 842 | |
paul@663 | 843 | else: |
paul@663 | 844 | body = self._visitListCompExpr(out_temp, expr) |
paul@663 | 845 | |
paul@663 | 846 | # Wrap the above body in the loop construct. |
paul@663 | 847 | |
paul@663 | 848 | return compiler.ast.Stmt([ |
paul@664 | 849 | # __storetemp__(_it, __builtins__.iter(<list>)) |
paul@663 | 850 | compiler.ast.CallFunc(special_name("__storetemp__"), [ |
paul@663 | 851 | temp, |
paul@663 | 852 | compiler.ast.CallFunc( |
paul@664 | 853 | module_attribute("__builtins__", "iter"), |
paul@663 | 854 | [self.dispatch(node.list)] |
paul@663 | 855 | ) |
paul@663 | 856 | ]), |
paul@663 | 857 | # while True: ... |
paul@663 | 858 | compiler.ast.While( |
paul@663 | 859 | special_name("True"), |
paul@663 | 860 | # try: ... |
paul@663 | 861 | compiler.ast.TryExcept( |
paul@663 | 862 | compiler.ast.Stmt([ |
paul@663 | 863 | # <assign> = ... |
paul@663 | 864 | self.dispatch(node.assign, |
paul@663 | 865 | # _it.next() |
paul@663 | 866 | compiler.ast.CallFunc( |
paul@663 | 867 | compiler.ast.CallFunc(special_name("__loadattr__"), [ |
paul@663 | 868 | compiler.ast.CallFunc(special_name("__loadtemp__"), [temp]), |
paul@663 | 869 | special_name("next") |
paul@663 | 870 | ]), |
paul@663 | 871 | [] |
paul@663 | 872 | ) |
paul@663 | 873 | ) |
paul@663 | 874 | ]), |
paul@663 | 875 | # except StopIteration: ... |
paul@663 | 876 | [(special_name("StopIteration"), None, compiler.ast.Stmt([compiler.ast.Break()]))], |
paul@663 | 877 | # else: ... |
paul@663 | 878 | body |
paul@663 | 879 | ), |
paul@663 | 880 | None |
paul@663 | 881 | ) |
paul@663 | 882 | ]) |
paul@663 | 883 | |
paul@663 | 884 | def visitListCompIf(self, node, out_temp, expr, quals): |
paul@663 | 885 | |
paul@663 | 886 | # Either generate more "for" or "if" constructs. |
paul@663 | 887 | |
paul@663 | 888 | if quals: |
paul@663 | 889 | body = self.dispatch(quals[0], out_temp, expr, quals[1:]) |
paul@663 | 890 | |
paul@663 | 891 | # Or generate the append statement. |
paul@663 | 892 | |
paul@663 | 893 | else: |
paul@663 | 894 | body = self._visitListCompExpr(out_temp, expr) |
paul@663 | 895 | |
paul@663 | 896 | return compiler.ast.If( |
paul@663 | 897 | [(self.dispatch(node.test), body)], |
paul@663 | 898 | None |
paul@638 | 899 | ) |
paul@638 | 900 | |
paul@663 | 901 | def _visitListCompExpr(self, out_temp, expr): |
paul@663 | 902 | |
paul@663 | 903 | "To the 'out_temp' object, append the result of 'expr'." |
paul@638 | 904 | |
paul@663 | 905 | return compiler.ast.Stmt([ |
paul@663 | 906 | # _out.append(<expr>) |
paul@663 | 907 | compiler.ast.CallFunc( |
paul@663 | 908 | compiler.ast.CallFunc(special_name("__loadattr__"), [ |
paul@663 | 909 | compiler.ast.CallFunc(special_name("__loadtemp__"), [out_temp]), |
paul@663 | 910 | special_name("append") |
paul@663 | 911 | ]), |
paul@663 | 912 | [self.dispatch(expr)] |
paul@663 | 913 | ) |
paul@663 | 914 | ]) |
paul@638 | 915 | |
paul@638 | 916 | def visitMod(self, node): |
paul@638 | 917 | return self._visitBinary(node) |
paul@638 | 918 | |
paul@638 | 919 | def visitMul(self, node): |
paul@638 | 920 | return self._visitBinary(node) |
paul@638 | 921 | |
paul@638 | 922 | def visitName(self, node, expr=None): |
paul@638 | 923 | if expr: |
paul@638 | 924 | return self.visitAssName(node, expr) |
paul@638 | 925 | |
paul@638 | 926 | unit = self.get_unit() |
paul@645 | 927 | attr = node._attr |
paul@645 | 928 | scope = node._scope |
paul@638 | 929 | |
paul@638 | 930 | # Generate appropriate name access operation. |
paul@638 | 931 | |
paul@638 | 932 | if scope == "constant": |
paul@638 | 933 | return node |
paul@638 | 934 | |
paul@645 | 935 | # Function locals are referenced normally. |
paul@638 | 936 | |
paul@645 | 937 | elif scope == "local" and isinstance(unit, Function): |
paul@645 | 938 | return node |
paul@638 | 939 | |
paul@645 | 940 | # Other attributes should already be resolved. |
paul@638 | 941 | |
paul@663 | 942 | elif attr is not None and not isinstance(attr, Instance): |
paul@664 | 943 | if attr.is_constant(): |
paul@664 | 944 | return constant_attribute(quoted_ref(attr.parent), node.name) |
paul@664 | 945 | else: |
paul@664 | 946 | return compiler.ast.CallFunc( |
paul@664 | 947 | special_name("__loadattr__"), |
paul@664 | 948 | [quoted_ref(attr.parent), special_name(node.name)] |
paul@664 | 949 | ) |
paul@638 | 950 | |
paul@663 | 951 | # Function globals are referenced via the module. |
paul@663 | 952 | |
paul@663 | 953 | elif scope == "global": |
paul@663 | 954 | return compiler.ast.CallFunc( |
paul@663 | 955 | special_name("__loadattr__"), |
paul@663 | 956 | [quoted_ref(self.get_module()), special_name(node.name)] |
paul@663 | 957 | ) |
paul@663 | 958 | |
paul@645 | 959 | # NOTE: Unknown attributes may arise because a class attribute has been |
paul@645 | 960 | # NOTE: optimised away. |
paul@638 | 961 | |
paul@638 | 962 | else: |
paul@638 | 963 | return compiler.ast.CallFunc( |
paul@638 | 964 | special_name("__loadunknown__"), |
paul@638 | 965 | [special_name(node.name)] |
paul@638 | 966 | ) |
paul@638 | 967 | |
paul@638 | 968 | def visitNot(self, node): |
paul@638 | 969 | return compiler.ast.Not(self.dispatch(node.expr)) |
paul@638 | 970 | |
paul@638 | 971 | def visitOr(self, node): |
paul@638 | 972 | return compiler.ast.Or([self.dispatch(n) for n in node.nodes]) |
paul@638 | 973 | |
paul@638 | 974 | def visitPower(self, node): |
paul@638 | 975 | return self._visitBinary(node) |
paul@638 | 976 | |
paul@638 | 977 | def visitRightShift(self, node): |
paul@638 | 978 | return self._visitBinary(node) |
paul@638 | 979 | |
paul@638 | 980 | def visitSlice(self, node, expr=None): |
paul@638 | 981 | return compiler.ast.CallFunc( |
paul@638 | 982 | module_attribute("operator", expr and "setslice" or "getslice"), |
paul@638 | 983 | [self.dispatch(node.expr), node.lower and self.dispatch(node.lower), node.upper and self.dispatch(node.upper)] |
paul@638 | 984 | + (expr and [expr] or []) |
paul@638 | 985 | ) |
paul@638 | 986 | |
paul@638 | 987 | def visitSliceobj(self, node): |
paul@638 | 988 | return compiler.ast.Sliceobj([self.dispatch(n) for n in node.nodes]) |
paul@638 | 989 | |
paul@638 | 990 | def visitSub(self, node): |
paul@638 | 991 | return self._visitBinary(node) |
paul@638 | 992 | |
paul@638 | 993 | def visitSubscript(self, node, expr=None): |
paul@638 | 994 | return compiler.ast.CallFunc( |
paul@638 | 995 | module_attribute("operator", expr and "setitem" or "getitem"), |
paul@638 | 996 | [self.dispatch(node.expr), compiler.ast.Tuple([self.dispatch(sub) for sub in node.subs])] |
paul@638 | 997 | + (expr and [expr] or []) |
paul@638 | 998 | ) |
paul@638 | 999 | |
paul@638 | 1000 | def visitTuple(self, node, expr=None): |
paul@638 | 1001 | if expr: |
paul@638 | 1002 | return self.visitAssTuple(node, expr) |
paul@638 | 1003 | return compiler.ast.Tuple([self.dispatch(n) for n in node.nodes]) |
paul@638 | 1004 | |
paul@638 | 1005 | def visitUnaryAdd(self, node): |
paul@638 | 1006 | return self._visitUnary(node) |
paul@638 | 1007 | |
paul@638 | 1008 | def visitUnarySub(self, node): |
paul@638 | 1009 | return self._visitUnary(node) |
paul@638 | 1010 | |
paul@638 | 1011 | # Convenience functions. |
paul@638 | 1012 | |
paul@638 | 1013 | def convert(module, program, filename): |
paul@638 | 1014 | stream = open(filename, "wb") |
paul@638 | 1015 | try: |
paul@638 | 1016 | source = ConvertedSource(module, program) |
paul@638 | 1017 | source.to_stream(stream) |
paul@638 | 1018 | finally: |
paul@638 | 1019 | stream.close() |
paul@638 | 1020 | |
paul@638 | 1021 | def translate(program, directory): |
paul@638 | 1022 | if not exists(directory): |
paul@638 | 1023 | os.mkdir(directory) |
paul@638 | 1024 | |
paul@638 | 1025 | # NOTE: Add constants here. |
paul@638 | 1026 | |
paul@638 | 1027 | for module in program.get_importer().get_modules(): |
paul@638 | 1028 | convert(module, program, join(directory, "%s%spy" % (module.full_name(), extsep))) |
paul@638 | 1029 | |
paul@638 | 1030 | # vim: tabstop=4 expandtab shiftwidth=4 |