Example #1
0
def main(argv):
    parser = argparse.ArgumentParser(description='A C to Pcode compiler')
    parser.add_argument('file', help='The c file to be compiled')
    parser.add_argument('-o',
                        '--output',
                        help='Directory to write compiled C file')
    parser.add_argument('-saveast',
                        '--saveast',
                        help='Write the AST to a file',
                        action='store_true')
    parser.add_argument('-showast',
                        '--showast',
                        help='Print AST',
                        action='store_true')
    parser.add_argument('-n',
                        '--nocompile',
                        help='Disable the compilation phase',
                        action='store_true')
    args = vars(parser.parse_args())

    filepath = os.path.split(args["file"])
    filename = os.path.splitext(filepath[1])[0]
    outputpath = ""
    if (args["output"] != None):
        outputpath += args["output"] + "/"

    symboltable = SymbolTable()
    astBuilder = ASTBuilder(args["file"], symboltable)
    ast = astBuilder.build()

    print(symboltable)

    if (bool(args["nocompile"]) == False):
        compiled = ast.compile()

        # Write to file
        file = open(outputpath + filename + ".p", "w")
        file.write(compiled)
        file.close()

    # Should we serialize
    if (args["showast"] == True):
        astBuilder.serialize()

    if (args["saveast"] == True):
        file = open(outputpath + filename + ".ast", "w")
        file.write(astBuilder.serialize())
        file.close()
Example #2
0
def worker(folder):
    filename = 'repoData/' + folder + '/allPythonContent.py'
    fullfile = open(filename).read()
    file_splits = fullfile.split('########NEW FILE########')
    df_graphs = {'folder': folder, 'files': []}
    for piece in file_splits:
        piece = piece.strip()
        piece_name = piece.split('\n')[0].strip()
        if len(piece_name.split()) == 3:
            file_name = piece_name.split()[2]
            try:
                print "Foldername:" + folder, "Filename:" + file_name
                df_graph = ASTBuilder(piece).build_AST()
                if df_graph is not None:
                    df_json = df_graph.serialize()
                    if int(df_json['count']) > 1:
                        prog_info = {'file': file_name, 'graph': df_json}
                        df_graphs['files'].append(prog_info)
            except:
                print "Unexpected error in worker:", sys.exc_info()[0]
                f_test = open('srcfiles/test.py', 'w')
                f_test.write(piece)
                f_test.close()

    proc_name = str(os.getpid())
    if df_graphs['files']:
        f = open('graphs/graph-' + proc_name + '.txt', 'a')
        f.write(json.dumps(df_graphs))
        f.write('\n' + '-' * 20 + '\n')
        f.close()
Example #3
0
def worker(folder, count):
    zf=ZipFile(folder,'r')
    df_graphs=[]
    for f in zf.namelist():
        if f.endswith('.py'):
            file=zf.read(f)
            print count, "Foldername:"+folder, \
                "Filename:"+f
            try:
                df_graph = ASTBuilder(file).build_AST()
                if df_graph is not None:
                    df_json=df_graph.serialize()
                    if int(df_json['count'])>1:
                        prog_info={
                            'folder':folder,
                            'file':f,
                            'graph':df_json}
                        df_graphs.append(prog_info)
            except:
                print "Error while parsing file:",f
                pass

    if df_graphs:
        with open('graphs-zip/graph'+str(count)+'.txt','w') as f:
            for graph in df_graphs:
                f.write(json.dumps(graph))
                f.write('\n'+'-'*60+'\n')