Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

I want to code in Scheme

Name: Anonymous 2013-03-21 17:48

But I don't really know how to write idiomatic code. Is anyone here willing to rewrite some of my python code, so I can see how it's done?

Also, what is the code-tag on this board?

Name: Anonymous 2013-03-21 18:13

OP here. Sorry for the wait. It took a while to find this code and remove the unimportant parts. It's java related.

Partial translations are appreciated too.


from functools import partial as inf

# ...

nbit = {
    jint: "i",
    jlong: "l",
    jfloat: "f",
    jdouble: "d",
    jref: "a",
    jbool: "b",
    jchar: "c",
    jshort: "s"
}

def jvmop(func, name=None, sig=tuple(), **kw):
    if not hasattr(func, "sig"): func.sig = sig
    func.__name__ = name or func.__name__.replace("_", "")
    func.__dict__.update(kw)
    return func

class ConstantIndexOperand(Operand):
    __slots__ = ('index', )
    def __init__(self, index): self.index = index
    def __repr__(self): return '<ConstantIndexOperand(index=%r)>' % self.index


# ...

class JVMReader:

    def disasm(self):
        op = self.readbyte()
        widemode = op == 0xC4
        if widemode:
            op = self.readbyte()

        i = self.instructions[op]

        if widemode:
            sig = [(b'>H', o[1]) if o[0] == b'>B' else o for i in i.sig]
        else:
            sig = i.sig

        args = [t(*self.read(fmt)) for fmt, t in sig]

        return i, args

    # ...

    @inf(jvmop, sig=[(b'>H', ConstantIndexOperand)])
    def anewarray(self, type):
        count = self.stack.pop()
        self.stack.append(ty.Array(type))

    def binop_gen(type, op):
        binop_impl = lambda self: self.stack.append(ssa.BinOp(op, *self.pop(2))) # pop two elements
        return jvmop(binop_impl, name=nbit[type]+op)

    def cast_gen(source, target):
        cast_impl = lambda self: self.stack.append(ssa.Cast(target, source, self.stack.pop()))
        return jvmop(cast_opc, name="{}2{}".format(nbit[source], nbit[target]))

    # ...
   
    instructions = {
        #...
        0xBD: anewarray,
        #...
    }

    jtypes = (jint, jlong, jfloat, jdouble, jref)
    jnumtypes = jtypes[:4]

    numops = ("add", "sub", "mul", "div", "rem", "neg")

    # ...

    for i, (source, target) in enumerate(itertools.permutations(jnumtypes, 2)):
        instructions[0x85+i] = cast_gen(source, target)

    for i, (op, jtype) in enumerate(itertools.product(numops, jnumtypes)):
        instructions[0x60+i] = binop_gen(jtype, op)


to be continued…

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List