Exemplo n.º 1
0
def main_process(filename = None):
    #print("================================================================")
    ast = getAst(filename)
    #print("================================================================")
    srcText = '';
    with open(filename) as infile:
        srcText = srcText + infile.read()
    #print("================================================================")
    for (i, topLevelDecl) in ast.children():
        if nodeType(topLevelDecl) == 'FuncDef':
            func = {'name' : getFunctionName(topLevelDecl),
                    'type' : getFunctionReturnType(topLevelDecl),
                    'params' : getFunctionParamlist(topLevelDecl),
                    'body' : getFunctionBody(topLevelDecl),
                    'file' : str(topLevelDecl.coord).split(':')[0],
                    'line' : int(str(topLevelDecl.coord).split(':')[1]) }
            endPos = getClosingSymbolLocation('{', '}', srcText, getFunctionLocation(srcText, func)[0])
            func['endLine'] = srcText.count('\n', 0, endPos) + 1
            ASTModifier.addFunction(func)
    #pp.pprint(ASTModifier.getFunctions())
    #print("================================================================")
    funcTexts = []
    for (name, func) in ASTModifier.getFunctions().iteritems():
        #print(func['name'])
        ASTModifier.enterFunction(name)
        funcTexts.append({'loc' : getFunctionLocation(srcText, func),
                          'text' : transformFunction(func)})
        ASTModifier.exitFunction(name)
        #print("================================================================")
    funcTexts = sorted(funcTexts, key=lambda k: k['loc'][0])
    newText = '#include <stdio.h>\n'
    last = 0
    for funcText in funcTexts:
        newText = newText + srcText[last:funcText['loc'][0]] + funcText['text']
        last = funcText['loc'][1] + 1
    newText = newText + srcText[last:]
    with open(filename + "-dbg.c", "w") as outfile:
        outfile.write(newText)