Esempio n. 1
0
    def dumpCodes(self):
        self.constants.append([
            "PyFunctionTable",
            self.getFunctionTable(),
            jast.New(self.name, [])
        ])

        for label, name, code, frame in self.codes:
            code = frame.toCellPrepend(code)

            funcid = self.addFunctionCode(name, code)

            arglist = keyworddict = jast. False
            if frame.args_arglist():
                arglist = jast. True
            if frame.args_keyworddict():
                keyworddict = jast. True

            names = jast.StringArray(frame.getnames())
            cellnames = StringArrayOrNull(frame.getcellnames())
            freenames = StringArrayOrNull(frame.getfreenames())
            npurecell = frame.get_npurecell()

            cargs = [
                jast.IntegerConstant(frame.args_count()), names,
                jast.StringConstant(self.filename),
                jast.StringConstant(name), arglist, keyworddict,
                self.getFunctionTable(),
                jast.IntegerConstant(funcid), cellnames, freenames,
                jast.IntegerConstant(npurecell),
                jast.IntegerConstant((frame.opt_globals and CO_OPTIMIZED) | (
                    frame.scope.nested_scopes and CO_NESTED))
            ]
            newcode = jast.InvokeStatic("Py", "newCode", cargs)
            self.constants.append(("PyCode", jast.Identifier(label), newcode))
Esempio n. 2
0
 def toCellPrepend(self,code):
     scope = self.scope
     pre = []
     for parmcell in scope.jy_paramcells:
         syminf = scope.tbl.get(parmcell)
         args = [jast.IntegerConstant(syminf.locals_index),
                 jast.IntegerConstant(syminf.env_index)]
         pre.append(jast.Invoke(self.frame, "to_cell", args))
     if not pre: return code
     pre.append(jast.BlankLine())
     return jast.Block(jast.flatten([pre,code]))
Esempio n. 3
0
    def dumpFuncs(self):
        meths = []
        cases = []

        args = ["PyObject", ("PyFrame", "frame")]
        access = "private static"

        callargs = [jast.Identifier("frame")]

        for name, funcid, code in self.funccodes:
            funcname = self.uniquename(name)
            meths.append(jast.Method(funcname, access, args, clean(code)))

            body = jast.Return(jast.InvokeStatic(self.name, funcname,
                                                 callargs))
            cases.append(
                [jast.IntegerConstant(funcid),
                 jast.FreeBlock([body])])

        defaultCase = jast.FreeBlock([jast.Return(jast.Null)])
        switch = jast.Block(
            [jast.Switch(jast.Identifier('index'), cases, defaultCase)])

        meths.insert(
            0,
            jast.Method("call_function", "public",
                        ["PyObject", ("int", "index"),
                         ("PyFrame", "frame")], switch))
        self.superclass = "PyFunctionTable"
        return meths
Esempio n. 4
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. 5
0
 def makeClosure(self,nested_scope):
     freenames = nested_scope.freevars
     if len(freenames) == 0: return None
     clos = []
     factory = self.compiler.factory
     for free in freenames:
         i = self.scope.tbl.get(free).env_index
         code = jast.Invoke(self.frame, "getclosure", [jast.IntegerConstant(i)])
         clos.append(factory.makePyObject(code))
     return clos
Esempio 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
Esempio n. 7
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. 8
0
def nullReturn(ret):
    if ret == Void.TYPE:
        return jast.Return()

    if Class.isPrimitive(ret):
        if ret.__name__ == 'boolean':
            value = jast. False
        elif ret.__name__ == 'char':
            value = jast.CharacterConstant('x')
        else:
            value = jast.IntegerConstant(0)
    else:
        value = jast.Null
    return jast.Return(value)
Esempio n. 9
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. 10
0
 def getIntegerConstant(self, value):
     code = jast.InvokeStatic("Py", "newInteger",
                              [jast.IntegerConstant(value)])
     return self.getConstant(value, code, "i")
Esempio n. 11
0
 def setCode(self,method,ref,value):
     if type(ref) is type(""):
         args = (jast.StringConstant(ref),value.asAny())
     else:
         args = (jast.IntegerConstant(ref),value.asAny())
     return jast.Invoke(self.frame,method,args)
Esempio n. 12
0
 def delCode(self,method,ref):
     if type(ref) is type(""):
         ref = (jast.StringConstant(ref),)
     else:
         ref = (jast.IntegerConstant(ref),)
     return jast.Invoke(self.frame,method,ref)
Esempio n. 13
0
    class Object:
        def __init__(self, code, istypes, astypes):
            self.code = code
            self.istypes = istypes
            self.astypes = astypes

        def isa(self, type):
            if type in self.istypes:
                return self.code

        def asa(self, type):
            if type in self.istypes:
                return self.code

            for astype, ascode in self.astypes:
                if astype == type:
                    return ascode

    one = Object(jast.IntegerConstant(1), [java.lang.Integer.TYPE],
                 [(org.python.core.PyObject, jast.Identifier("one"))])
    foo = Object(
        jast.Identifier("foo"), [org.python.core.PyObject],
        [(java.lang.Integer.TYPE,
          jast.InvokeStatic("Py", "toint", [jast.Identifier("foo")]))])

    print f.invoke(foo, [one])
    print f.invoke(foo, [foo])

    print call(org.python.core.Py.py2int, [one])
Esempio n. 14
0
 def igetitem(self, code, index):
     return self.domethod(code, "__getitem__",
                          Object(jast.IntegerConstant(index), IntType))
Esempio n. 15
0
        ret = JavaString()
    elif type == org.python.core.PyObject:
        ret = PyObject()
    elif type == org.python.core.PyString:
        ret = PyObject()
    else:
        ret = JavaObject(type)

    types[type] = ret
    return ret


Generic = findType(org.python.core.PyObject)
IntType = findType(java.lang.Integer.TYPE)
StringType = findType(java.lang.String)

if __name__ == '__main__':
    foo = Object(jast.Identifier("foo"), Generic)
    one = Object(jast.IntegerConstant(1), IntType)
    hello = Object(jast.StringConstant("hello"), StringType)

    print foo, one, hello
    print foo.binop("add", foo)
    print foo.binop("add", one)
    print foo.binop("add", hello)
    print foo.nonzero()
    print foo.getitem(foo)
    print foo.getitem(one)
    print foo.call([one, hello])
    print foo.call([one, hello, foo])