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