import ast def process(node): # TODO: node.defaults # TODO: node.flags # TODO: node.doc functionName = node.name #emit class method or module subroutine prefix if ast.getInClassDefintion(): ast.output(".sub %s method" % functionName ) else: ast.push() ast.output(".sub %s " % functionName ) #emit parameter call signature for name in node.argnames: ast.addSymbolOnlyIfNotPresent(name) #if name != "self": ast.output("\t.param pmc " + name) ast.execute(node.code) ast.output(".end") #emit class method or module subroutine postfix if ast.getInClassDefintion(): pass else: ast.pop() #add module subroutine to module and module namespace possibleModuleName = ast.getIsModuleScope() if possibleModuleName: ast.addModuleMember( functionName ) pyFunc = ast.nextPRegister() methodSubroutine = ast.nextPRegister() methodAddress = ast.nextIRegister() moduleRegister = ast.nextPRegister() #add subroutine to module ast.outputInBlock("find_global %s, \"%s\", \"%s\"" % ( methodSubroutine, possibleModuleName, functionName ) ) ast.outputInBlock("new %s, \"PyFunc\"" % ( pyFunc ) ) ast.outputInBlock("get_addr %s, %s" % ( methodAddress, methodSubroutine ) ) ast.outputInBlock("set_addr %s, %s" % ( pyFunc, methodAddress ) ) ast.outputInBlock("find_global %s, \"%s\"" % ( moduleRegister, possibleModuleName ) ) ast.outputInBlock("setattribute %s, \"%s\", %s" % ( moduleRegister, functionName, pyFunc ) ) #add subroutine name to module namespace ast.outputInBlock("store_global \"%s\", \"%s\", %s" % ( possibleModuleName, functionName, pyFunc ) )