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