Example #1
0
def visit(env, node):
    file_name = Name('<module>')

    asm = Assembly(
        file_name,
        node.symtable.names,
        node.symtable.varnames,
        node.symtable.freevars,
        node.symtable.cellvars)

    asm.set_lineno(1)

    stringtable = StringTable()

    env = env(asm = asm, stringtable=stringtable)

    visit_block(env, node.body, True)


    for names in (
        node.symtable.names,
        node.symtable.varnames,
        node.symtable.freevars,
        node.symtable.cellvars):

        for name in names:
            name.s = stringtable.add(name.name, interned=True)

    file_name.s = stringtable.add(file_name.name, interned=True)

    stringtable.close()

    return asm
Example #2
0
File: compile.py Project: pyzh/orz
def visit(env, node):
    file_name = Name('<module>')

    asm = Assembly(file_name, node.symtable.names, node.symtable.varnames,
                   node.symtable.freevars, node.symtable.cellvars)

    asm.set_lineno(1)

    stringtable = StringTable()

    env = env(asm=asm, stringtable=stringtable)

    visit_block(env, node.body, True)

    for names in (node.symtable.names, node.symtable.varnames,
                  node.symtable.freevars, node.symtable.cellvars):

        for name in names:
            name.s = stringtable.add(name.name, interned=True)

    file_name.s = stringtable.add(file_name.name, interned=True)

    stringtable.close()

    return asm
Example #3
0
def visit_function(env, name, node):
    argcount = len(node.args)

    func_name = Name(name)

    asm = Assembly(
        func_name,
        node.symtable.names,
        node.symtable.varnames,
        node.symtable.freevars,
        node.symtable.cellvars,
        argcount = argcount,
        varargs = True,
        parent = env.asm)

    asm.set_lineno(node.lineno)

    func_env = Environment(asm=asm, stringtable=env.stringtable)

    visit_block(func_env, node.body, True)

    for names in (
        node.symtable.names,
        node.symtable.varnames,
        node.symtable.freevars,
        node.symtable.cellvars):

        for name in names:
            name.s = env.stringtable.add(name.name, interned=True)

    func_name.s = env.stringtable.add(func_name.name, interned=True)

    func = env.asm.add_const(asm)

    for i in range(argcount):
        env.asm.load_const(None)

    if not node.symtable.freevars:
        env.asm.emit(Opcode.LOAD_CONST, func)
        env.asm.emit(Opcode.MAKE_FUNCTION, argcount)
    else:
        for freevar in node.symtable.freevars:
            env.asm.emit(Opcode.LOAD_CLOSURE, freevar.parent.slot)

        env.asm.emit(Opcode.BUILD_TUPLE, len(node.symtable.freevars))
        env.asm.emit(Opcode.LOAD_CONST, func)
        env.asm.emit(Opcode.MAKE_CLOSURE, argcount)
Example #4
0
File: compile.py Project: pyzh/orz
def visit_function(env, name, node):
    argcount = len(node.args)

    func_name = Name(name)

    asm = Assembly(func_name,
                   node.symtable.names,
                   node.symtable.varnames,
                   node.symtable.freevars,
                   node.symtable.cellvars,
                   argcount=argcount,
                   varargs=True,
                   parent=env.asm)

    asm.set_lineno(node.lineno)

    func_env = Environment(asm=asm, stringtable=env.stringtable)

    visit_block(func_env, node.body, True)

    for names in (node.symtable.names, node.symtable.varnames,
                  node.symtable.freevars, node.symtable.cellvars):

        for name in names:
            name.s = env.stringtable.add(name.name, interned=True)

    func_name.s = env.stringtable.add(func_name.name, interned=True)

    func = env.asm.add_const(asm)

    for i in range(argcount):
        env.asm.load_const(None)

    if not node.symtable.freevars:
        env.asm.emit(Opcode.LOAD_CONST, func)
        env.asm.emit(Opcode.MAKE_FUNCTION, argcount)
    else:
        for freevar in node.symtable.freevars:
            env.asm.emit(Opcode.LOAD_CLOSURE, freevar.parent.slot)

        env.asm.emit(Opcode.BUILD_TUPLE, len(node.symtable.freevars))
        env.asm.emit(Opcode.LOAD_CONST, func)
        env.asm.emit(Opcode.MAKE_CLOSURE, argcount)