Ejemplo n.º 1
0
def clean(node):
    if not hasattr(node, 'exits'):
        print node
    if node.exits():
        return node
    return jast.Block(
        [node, jast.Return(jast.GetStaticAttribute('Py', 'None'))])
Ejemplo n.º 2
0
    def do_comp(self, x, compares, tmps):
        False = jast.GetStaticAttribute("Py", "Zero")

        op, other = compares[0]
        y = self.visit(other)
        if len(compares) > 1:
            ytmp = self.frame.gettemp("PyObject")
            tmps.append(ytmp)
            gety = self.factory.makePyObject(jast.Set(ytmp, y.asAny()))
        else:
            gety = y

        ops = {
            ast.Compare.Eq:    "_eq",
            ast.Compare.NotEq: "_ne",
            ast.Compare.Lt:    "_lt",
            ast.Compare.LtE:   "_le",
            ast.Compare.Gt:    "_gt",
            ast.Compare.GtE:   "_ge",
            ast.Compare.Is:    "_is",
            ast.Compare.IsNot: "_isnot",
            ast.Compare.In:    "_in",
            ast.Compare.NotIn: "_notin",
        }

        test = x.compop(ops[op], gety)

        if len(compares) == 1:
            return test.asAny()

        rest = self.do_comp(self.factory.makePyObject(ytmp),
                            compares[1:],
                            tmps)
        return jast.TriTest(test.nonzero(), rest, False)
Ejemplo n.º 3
0
def makeReturn(code, ret):
    if ret == Void.TYPE:
        return code

    if Class.isPrimitive(ret):
        r = jast.InvokeStatic("Py", "py2" + ret.__name__, [code])
    else:
        typname = typeName(ret)
        r = jast.InvokeStatic(
            "Py", "tojava",
            [code, jast.GetStaticAttribute(typname, 'class')])
        r = jast.Cast(typname, r)
    return jast.Return(r)
Ejemplo n.º 4
0
    def asa(self, code, type, message=None):
        ret = self.isa(code, type)
        if ret is not None:
            return ret

        if primitives.has_key(type):
            return jast.InvokeStatic('Py', primitives[type], [code])
        if type == java.lang.Boolean.TYPE:
            return jast.Invoke(code, '__nonzero__', [])

        tname = type.__name__
        tojava = jast.InvokeStatic(
            'Py', 'tojava',
            [code, jast.GetStaticAttribute(tname, 'class')])
        return jast.Cast(tname, tojava)
Ejemplo n.º 5
0
    def do_comp(self, x, compares, tmps):
        False = jast.GetStaticAttribute("Py", "Zero")

        op, other = compares[0]
        y = self.visit(other)
        if len(compares) > 1:
            ytmp = self.frame.gettemp("PyObject")
            tmps.append(ytmp)
            gety = self.factory.makePyObject(jast.Set(ytmp, y.asAny()))
        else:
            gety = y

        test = x.compop(op, gety)

        if len(compares) == 1:
            return test.asAny()

        rest = self.do_comp(self.factory.makePyObject(ytmp), compares[1:],
                            tmps)
        return jast.TriTest(test.nonzero(), rest, False)
Ejemplo n.º 6
0
    def dumpMain(self):
        meths = []
        if self.javamain:
            code = []
            newargs = jast.Identifier("newargs")
            code.append(
                jast.Declare("String[]", newargs,
                             jast.NewArray("String", ["args.length+1"])))
            code.append(
                jast.Set(jast.Identifier("newargs[0]"),
                         jast.StringConstant(self.name)))

            args = [
                jast.Identifier('args'),
                jast.IntegerConstant(0),
                jast.Identifier('newargs'),
                jast.IntegerConstant(1),
                jast.Identifier('args.length')
            ]
            code.append(
                jast.InvokeStatic("java.lang.System", "arraycopy", args))

            args = [
                jast.GetStaticAttribute(
                    self.getclassname(self.name + '.' + self.pyinner.name),
                    "class"),
                jast.Identifier('newargs'),
                self.getPackages(qual=1),
                self.getMainProperties(qual=1),
                self.getFrozen(),
                jast.StringArray(self.modules.keys())
            ]

            code.append([jast.InvokeStatic("Py", "runMain", args)])
            maincode = jast.Block(code)
            meths.append(
                jast.Method("main", "public static",
                            ["void", ("String[]", "args")], maincode,
                            ["java.lang.Exception"]))

        return meths
Ejemplo n.º 7
0
 def makeEllipsis(self):
     code = jast.GetStaticAttribute("Py", "Ellipsis")
     return Object(code, Generic)
Ejemplo n.º 8
0
 def makePyNone(self):
     return Object(jast.GetStaticAttribute("Py", "None"), Generic)
Ejemplo n.º 9
0
 def getPackages(self, qual=0):
     if qual:
         return jast.GetStaticAttribute(self.name, "jpy$packages")
     else:
         return jast.Identifier("jpy$packages")
Ejemplo n.º 10
0
 def getProxyProperties(self, qual=0):
     if qual:
         return jast.GetStaticAttribute(self.name, "jpy$proxyProperties")
     else:
         return jast.Identifier("jpy$proxyProperties")
Ejemplo n.º 11
0
 def visitReturn(self, node):
     if node.value is None:
         return jast.Return(jast.GetStaticAttribute("Py", "None"))
     else:
         return jast.Return(self.visit(node.value).asAny())
Ejemplo n.º 12
0
 def return_stmt(self, value=None):
     if value is None:
         return jast.Return(jast.GetStaticAttribute("Py", "None"))
     else:
         return jast.Return(self.visit(value).asAny())