def for_stmt(self, index, sequence, body, else_body=None): counter = self.frame.gettemp('int') item = self.factory.makePyObject(self.frame.gettemp("PyObject")) seq = self.frame.gettemp("PyObject") init = [] init.append(jast.Set(counter, jast.IntegerConstant(0))) init.append(jast.Set(seq, self.visit(sequence).asAny())) counter_inc = jast.PostOperation(counter, '++') test = jast.Set(item.asAny(), jast.Invoke(seq, "__finditem__", [counter_inc])) test = jast.Operation('!=', test, jast.Identifier('null')) suite = [] suite.append(self.set(index, item)) suite.append(self.visit(body)) suite = jast.Block(suite) if else_body is not None: else_body = jast.Block(self.visit(else_body)) wtmp = self.frame.gettemp('boolean') ret = [init, jast.WhileElse(test, suite, else_body, wtmp)] self.frame.freetemp(wtmp) return ret else: return [init, jast.While(test, suite)]
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 visitFor(self, node): iter = self.frame.gettemp('PyObject') item = self.factory.makePyObject(self.frame.gettemp("PyObject")) seq = self.visit(node.iter).asAny() init = [] init.append(jast.Set(iter, jast.Invoke(seq, "__iter__", []))) test = jast.Set(item.asAny(), jast.Invoke(iter, "__iternext__", [])) test = jast.Operation('!=', test, jast.Identifier('null')) suite = [] suite.append(self.set(node.target, item)) suite.append(self.suite(node.body)) suite = jast.Block(suite) if node.orelse is not None: orelse = jast.Block(self.suite(node.orelse)) wtmp = self.frame.gettemp('boolean') ret = [init, jast.WhileElse(test, suite, orelse, wtmp)] self.frame.freetemp(wtmp) return ret else: return [init, jast.While(test, suite)]
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)