import ast def process(node): classRegName = ast.nextPRegister() classTypeRegName = ast.nextPRegister() #create a new python class ast.outputInBlock("%s = getclass \"PyType\"" % ( classTypeRegName ) ) ast.outputInBlock("subclass %s, %s, \"%s\"" % ( classRegName, classTypeRegName, node.name ) ) ast.outputInBlock("store_global \"%s\", %s " % ( node.name, classRegName ) ) for x in node.bases: baseRegName = ast.nextPRegister() ast.outputInBlock("getclass %s, %s" % ( baseRegName, x ) ) ast.outputInBlock("addParent %s, %s" % ( classRegName, baseRegName ) ) ast.push() ast.enterClassDefinition() ast.pushNamespace( node.name ) ast.output( ".namespace [\"%s\"]" % ( ast.printNamespace() ) ) #generate all the methods for x in node.code.nodes: ast.evaluate( x ) ast.leaveClassDefinition() ast.popNamespace() ast.output( ".namespace [\"%s\"]" % ( ast.printNamespace() ) ) ast.pop() #add all the methods as parrot properties of the python class(PyType) pyFuncRegister = ast.nextIRegister() ast.outputInBlock("find_type %s, %s" % ( pyFuncRegister, "\"PyFunc\"" ) ) for x in node.code.nodes: pyFunc = ast.nextPRegister() methodSubroutine = ast.nextPRegister() ast.outputInBlock("find_global %s, \"%s\", \"%s\"" % ( methodSubroutine, ast.printNamespace() + "::" + node.name , x.name ) ) methodAddress = ast.nextIRegister() ast.outputInBlock("new %s, %s" % ( pyFunc, pyFuncRegister ) ) ast.outputInBlock("get_addr %s, %s" % ( methodAddress, methodSubroutine ) ) ast.outputInBlock("set_addr %s, %s" % ( pyFunc, methodAddress ) ) ast.outputInBlock("setprop %s, \"%s\", %s" % ( classRegName, x.name, pyFunc ) )