Esempio n. 1
0
    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)]
Esempio n. 2
0
    def visitImportFrom(self, node):
        modname = jast.StringConstant(node.module)
        module = self.get_module(node.module.split('.'), 0)
        if len(node.names) == 0:
            self.addModule(node.module, '*')
            self._loadNames(self._getnames(module), module)
            return jast.InvokeStatic("org.python.core.imp", "importAll",
                                    [modname, self.frame.frame])

        topmodname = jast.StringConstant(node.module)
        modnames = [alias.name for alias in node.names]
        modnamesArray = jast.FilledArray(
            "String",
            map(lambda x: jast.StringConstant(x), modnames))

        do_import = jast.InvokeStatic("org.python.core.imp", "importFrom",
                                      [topmodname, modnamesArray,
                                       self.frame.frame])

        imp_accu = self.frame.gettemp('PyObject[]')
        stmts = [ jast.Set(imp_accu, do_import) ]
        
        for i in range(len(node.names)):
            modname = node.names[i].name
            asname = node.names[i].asname
            if asname is None:
                asname = modname
            code = jast.Subscript(imp_accu, i)
            stmts.append(self.set_name(asname, 
         	module.getattr(modname).makeReference(code)))

        stmts.append(jast.Set(imp_accu,jast.Null))
        self.frame.freetemp(imp_accu)
        return stmts       
Esempio n. 3
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)
Esempio n. 4
0
    def addPyProxyInterface(self):
        self.statements.append(
            jast.Declare('private PyInstance', jast.Identifier('__proxy')))
        code = jast.Set(jast.Identifier("__proxy"), jast.Identifier("inst"))
        code = jast.Block([code])
        self.statements.append(
            jast.Method("_setPyInstance", "public",
                        ["void", ("PyInstance", "inst")], code))
        code = jast.Block([jast.Return(jast.Identifier("__proxy"))])
        self.statements.append(
            jast.Method("_getPyInstance", "public", ["PyInstance"], code))

        self.statements.append(
            jast.Declare('private PySystemState',
                         jast.Identifier('__sysstate')))
        code = jast.Set(jast.Identifier("__sysstate"), jast.Identifier("inst"))
        code = jast.Block([code])
        self.statements.append(
            jast.Method("_setPySystemState", "public",
                        ["void", ("PySystemState", "inst")], code))
        code = jast.Block([jast.Return(jast.Identifier("__sysstate"))])
        self.statements.append(
            jast.Method("_getPySystemState", "public", ["PySystemState"],
                        code))

        frozen = self.module.getFrozen()
        this = jast.Identifier("this")
        initargs = [
            this,
            jast.StringConstant(self.modname),
            jast.StringConstant(self.name),
            jast.Identifier("args"), self.packages, self.properties, frozen,
            jast.StringArray(self.modules)
        ]

        initproxy = jast.InvokeStatic("Py", "initProxy", initargs)

        code = jast.Block([initproxy])
        self.statements.append(
            jast.Method("__initProxy__", "public",
                        ["void", ("Object[]", "args")], code))

        self.interfaces.append(org.python.core.PyProxy)
Esempio n. 5
0
    def importfrom_stmt(self, top, names):
        module = self.get_module(top, 0)
        if names == '*':
            return self.importall_stmt(module)
            #print 'import * from', module
            #names = module.dir()

        modnames = []
        asnames = []
        for modname, asname in names:
            if asname is None:
                asname = modname
            asnames.append(asname)
            modnames.append(modname)

        topmodname = jast.StringConstant(".".join(top))
        modnamesArray = jast.FilledArray(
            "String", map(lambda x: jast.StringConstant(x), modnames))

        do_import = jast.InvokeStatic(
            "org.python.core.imp", "importFrom",
            [topmodname, modnamesArray, self.frame.frame])

        if not self.imp_accu:
            imp_accu = self.imp_accu = jast.Identifier("imp_accu")
            self.makeFreeDecl("PyObject[]", imp_accu)
        else:
            imp_accu = self.imp_accu

        stmts = [jast.Set(imp_accu, do_import)]

        for i in range(len(asnames)):
            asname = asnames[i]
            modname = modnames[i]
            code = jast.Subscript(imp_accu, i)
            stmts.append(
                self.set_name(asname,
                              module.getattr(modname).makeReference(code)))

        stmts.append(jast.Set(imp_accu, jast.Null))

        return stmts
Esempio n. 6
0
    def bool_op(self, x, y, swap=0):
        tmp = self.frame.gettemp("PyObject")
        test = jast.Invoke(jast.Set(tmp,
                                    self.visit(x).asAny()), "__nonzero__", [])
        yes, no = tmp, self.visit(y).asAny()
        if swap:
            yes, no = no, yes

        op = self.factory.makePyObject(jast.TriTest(test, yes, no))
        self.frame.freetemp(tmp)
        return op
Esempio n. 7
0
    def dumpConstants(self):
        self.dumpCodes()
        stmts = []
        decls = []
        for type, label, value in self.constants:
            decls.append(jast.Declare("private static " + type, label))
            stmts.append(jast.Set(label, value))

        setconstants = jast.Method("initConstants", "private static", ["void"],
                                   jast.Block(stmts))
        decls.append(setconstants)
        return decls
Esempio n. 8
0
    def bool_op(self, x, y, op):
        tmp = self.frame.gettemp("PyObject")
        test = jast.Invoke(jast.Set(tmp, x.asAny()),
                           "__nonzero__", [])
        if op == ast.BoolOp.Or:
            yes, no = tmp, y.asAny()
        else:
            no, yes = tmp, y.asAny()

        op = self.factory.makePyObject(jast.TriTest(test, yes, no))
        self.frame.freetemp(tmp)
        return op
Esempio n. 9
0
 def seqSet(self, elts):
     n = len(elts)
     unpacked = jast.InvokeStatic("org.python.core.Py", "unpackSequence",
                        [self.temporary.asAny(), jast.IntegerConstant(n)])
     tmp = self.frame.gettemp('PyObject[]')
     stmts = [ jast.Set(tmp, unpacked) ]
     
     for i in range(n):
         code = jast.Subscript(tmp, i)
         stmts.append(self.set(elts[i], self.factory.makePyObject(code)))
     self.frame.freetemp(tmp)
     return stmts
Esempio n. 10
0
    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)]
Esempio n. 11
0
    def set_list(self, seq, value):
        if hasattr(self, 'AUG'):
            raise SyntaxError, "augmented assign to tuple not possible"
        if len(seq) > 0 and seq[-1].id == JJTCOMMA:
            del seq[-1]
        n = len(seq)

        unpacked = jast.InvokeStatic(
            "org.python.core.Py", "unpackSequence",
            [value.asAny(), jast.IntegerConstant(n)])
        tmp = self.frame.gettemp('PyObject[]')
        stmts = [jast.Set(tmp, unpacked)]

        for i in range(n):
            code = jast.Subscript(tmp, i)
            stmts.append(self.set(seq[i], self.factory.makePyObject(code)))
        self.frame.freetemp(tmp)
        return stmts
Esempio n. 12
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)
Esempio n. 13
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
Esempio n. 14
0
    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
Esempio n. 15
0
 def makeTemp(self, value):
     tmp = self.frame.gettemp('PyObject')
     setit = jast.Set(tmp, value.asAny())
     return self.factory.makePyObject(tmp), setit
Esempio n. 16
0
 def makeTemp(self, frame):
     return PyObject(jast.Set(frame.gettemp(self.type), self.asAny()),
                     self.parent)