def visitAssert(self, node): if self.isAlwaysFalse("__debug__"): return jast.SimpleComment("assert") args = [self.visit(node.test).asAny()] if node.msg is not None: args.append(self.visit(node.msg).asAny()) return jast.If(self.frame.getglobal("__debug__").nonzero(), jast.InvokeStatic("Py", "assert_", args))
def assert_stmt(self, test, message=None): if self.isAlwaysFalse("__debug__"): return jast.SimpleComment("assert") args = [test.asAny()] if message is not None: args.append(message.asAny()) return jast.If( self.frame.getglobal("__debug__").nonzero(), jast.InvokeStatic("Py", "assert", args))
def dumpMain(self): if not hasattr(self, 'mainCode'): return [] meths = [] self.interfaces.append("PyRunnable") getmain = jast.Block([ jast.If(jast.Operation("==", self.mainCode, jast.Null), jast.InvokeStatic(self.name, "initConstants", [])), jast.Return(self.mainCode) ]) meths.append(jast.Method("getMain", "public", ["PyCode"], getmain)) return meths
def visitIf(self, node): test = self.visit(node.test).nonzero() body = jast.Block(self.suite(node.body)) orelse = None if node.orelse is not None: orelse = jast.Block(self.suite(node.orelse)) if isinstance(node.test, ast.Name): tname = self.getName(node.test.id) if tname is not None and self.isAlwaysFalse(tname): if node.orelse is None: return jast.SimpleComment("if "+tname) else: return orelse return jast.If(test, body, orelse)
def if_stmt(self, tests, else_body=None): jtests = [] for test, body in tests: tname = self.getName(test) if tname is not None and self.isAlwaysFalse(tname): continue test = self.visit(test).nonzero() body = jast.Block(self.visit(body)) jtests.append((test, body)) if else_body is not None: else_body = jast.Block(self.visit(else_body)) if len(jtests) == 0: if else_body is None: return jast.SimpleComment("if " + tname) else: return else_body if len(jtests) == 1: return jast.If(jtests[0][0], jtests[0][1], else_body) else: return jast.MultiIf(jtests, else_body)
def callMethod(self, name, access, ret, sig, throws=[], dosuper=1): args = [typeName(ret)] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if isinstance(access, IntType) and isAbstract(access): dosuper = 0 access = access & ~ABSTRACT if not dosuper and not self.isAdapter: getattr = jast.InvokeStatic("Py", "jgetattr", [this, jast.StringConstant(name)]) else: getattr = jast.InvokeStatic("Py", "jfindattr", [this, jast.StringConstant(name)]) inst = jast.Identifier("inst") if len(throws) == 0: jcall = "_jcall" else: jcall = "_jcallexc" jcall = makeReturn(jast.Invoke(inst, jcall, [objects]), ret) jcall = wrapThrows(jcall, throws, ret) if dosuper: supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = None if not self.issuperproxy and name not in self.getCandSupermethods( incl=0): supermethod = jast.Method("super__" + name, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.supermethods["super__" + name] = "super__" + name else: if self.isAdapter: supercall = nullReturn(ret) else: supercall = None supermethod = None if not dosuper and not self.isAdapter: test = jcall else: test = jast.If(jast.Operation("!=", inst, jast.Null), jcall, supercall) code = jast.Block([jast.Declare("PyObject", inst, getattr), test]) meth = jast.Method(name, jast.Modifier.ModifierString(access), args, code, throws) if supermethod is not None: self.statements.append(supermethod) self.statements.append(meth)
def visitTryExcept(self, node): if node.orelse is not None: elseBool = self.frame.gettemp("boolean") jbody = jast.Block(self.suite(node.body)) tests = [] ifelse = None tname = jast.Identifier("x$%d" % self.nthrowables) self.nthrowables = self.nthrowables + 1 exctmp = self.frame.gettemp("PyException") setexc = jast.Set(exctmp, jast.InvokeStatic("Py", "setException", [tname, self.frame.frame])) for exchandler in node.handlers: if exchandler.type is None: ifelse = jast.Block(self.suite(exchandler.body)) continue type = self.visit(exchandler.type) t = jast.InvokeStatic("Py", "matchException", [exctmp, type.asAny()]) newbody = [] if exchandler.name is not None: exceptionValue = self.factory.makePyObject( jast.GetInstanceAttribute(exctmp, "value")) newbody.append(self.set(exchandler.name, exceptionValue)) #print self.visit(ebody) newbody.append(self.suite(exchandler.body)) #print newbody #print jast.Block(newbody) tests.append( (t, jast.Block(newbody)) ) if ifelse is None: ifelse = jast.Throw(exctmp) if len(tests) == 0: catchBody = ifelse else: catchBody = jast.MultiIf(tests, ifelse) catchBody = [setexc, catchBody] if node.orelse is not None: catchBody = [jast.Set(elseBool, jast.False), catchBody] catchBody = jast.Block([catchBody]) self.frame.freetemp(exctmp) ret = jast.TryCatch(jbody, "Throwable", tname, catchBody) if node.orelse is not None: ret = jast.Block([jast.Set(elseBool, jast.True), ret, jast.If(elseBool, jast.Block(self.suite(node.orelse)))]) self.frame.freetemp(elseBool) return ret