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