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