示例#1
0
def generate_assembly (input_name, output_name):
    ast = transformer.parseFile(input_name)
##    print ast
    
    # Determine locals and assign them to registers
    # Disabled -- all variables are global for the moment
##    lnf = walk(ast, LocalNameFinder(), verbose=0)
##    for name in lnf.getLocals().elements():
##        reg = LOCAL_REGS[name] = random.choice(FREE_REGISTERS)
##        FREE_REGISTERS.remove(reg)
##    print LOCAL_REGS
    
    # Walk tree and generate bytecode
    vtor = visitor.ASTVisitor()
    pv = ParrotVisitor()

    for line in ["main:",
                 'setfile "%s"' % input_name,
                 'setpackage "__main__"']:
        pv.add_line(line)

    # Generate lines of assembly code
    vtor.preorder(ast, pv)
    pv.add_line('\t' 'end')
    
    # Write the generated assembly code
    lines = pv.lines + pv.functions
    output = open(output_name, 'w')
    output.writelines(lines)
    output.close()
示例#2
0
def findImportStars(system):
    assert system.state in ['preparse']
    modlist = list(system.objectsOfType(Module))
    for mod in modlist:
        system.push(mod.parent)
        isf = ImportStarFinder(system, mod.fullName())
        try:
            ast = parseFile(mod.filepath)
        except (SyntaxError, ValueError):
            system.warning("cannot parse", mod.filepath)
        walk(ast, isf)
        system.pop(mod.parent)
    system.state = 'importstarred'
示例#3
0
def findImportStars(system):
    assert system.state in ['preparse']
    modlist = list(system.objectsOfType(Module))
    for mod in modlist:
        system.push(mod.parent)
        isf = ImportStarFinder(system, mod.fullName())
        try:
            ast = parseFile(mod.filepath)
        except (SyntaxError, ValueError):
            system.warning("cannot parse", mod.filepath)
        walk(ast, isf)
        system.pop(mod.parent)
    system.state = 'importstarred'
示例#4
0
def main(systemcls, argv):
    if '-r' in argv:
        argv.remove('-r')
        assert len(argv) == 1
        system = systemcls()
        processDirectory(system, argv[0])
        pickle.dump(system, open('da.out', 'wb'), pickle.HIGHEST_PROTOCOL)
        print
        print('warning summary:')
        for k, v in system.warnings.iteritems():
            print(k, len(v))
    else:
        system = systemcls()
        for fname in argv:
            modname = os.path.splitext(os.path.basename(fname))[0]  # XXX!
            processModuleAst(parseFile(fname), modname, system)
        system.report()
示例#5
0
def extractDocstrings(system):
    assert system.state in ['preparse', 'importstarred']
    # and so much more...
    modlist = list(system.objectsOfType(Module))
    newlist = toposort([m.fullName() for m in modlist], system.importstargraph)

    for mod in newlist:
        mod = system.allobjects[mod]
        system.push(mod.parent)
        try:
            ast = parseFile(mod.filepath)
        except (SyntaxError, ValueError):
            system.warning("cannot parse", mod.filepath)
        processModuleAst(ast, mod.name, system)
        mod.processed = True
        system.pop(mod.parent)
    system.state = 'parsed'
示例#6
0
def main(systemcls, argv):
    if '-r' in argv:
        argv.remove('-r')
        assert len(argv) == 1
        system = systemcls()
        processDirectory(system, argv[0])
        pickle.dump(system, open('da.out', 'wb'), pickle.HIGHEST_PROTOCOL)
        print
        print('warning summary:')
        for k, v in system.warnings.iteritems():
            print(k, len(v))
    else:
        system = systemcls()
        for fname in argv:
            modname = os.path.splitext(os.path.basename(fname))[0]  # XXX!
            processModuleAst(parseFile(fname), modname, system)
        system.report()
示例#7
0
def extractDocstrings(system):
    assert system.state in ['preparse', 'importstarred']
    # and so much more...
    modlist = list(system.objectsOfType(Module))
    newlist = toposort([m.fullName() for m in modlist], system.importstargraph)

    for mod in newlist:
        mod = system.allobjects[mod]
        system.push(mod.parent)
        try:
            ast = parseFile(mod.filepath)
        except (SyntaxError, ValueError):
            system.warning("cannot parse", mod.filepath)
        processModuleAst(ast, mod.name, system)
        mod.processed = True
        system.pop(mod.parent)
    system.state = 'parsed'
示例#8
0
              'compare docstrings as well.\n')


if __name__ == '__main__':
    opts, args = getopt(sys.argv[1:], '', ['diffdoc'])
    if len(args) != 2:
        usage(sys.stderr.write)
        exit(1)
    for opt, value in opts:
        if opt == '--diffdoc':
            _diffdoc = True

    lf = args[0]
    rf = args[1]

    la = transformer.parseFile(lf)
    ra = transformer.parseFile(rf)

    r, diffs = pydiff(la, ra)

    if r:
        exit(0)

    print '%d difference(s)' % len(diffs)
    print 'first file: %s\nsecond file: %s\n' % (lf, rf)
    for it in diffs:
        pprint(it)
        print

    exit(1)
示例#9
0
              'compare docstrings as well.\n')


if __name__ == '__main__':
    opts, args = getopt(sys.argv[1:], '', ['diffdoc'])
    if len(args) != 2:
        usage(sys.stderr.write)
        exit(1)
    for opt, value in opts:
        if opt == '--diffdoc':
            _diffdoc = True

    lf = args[0]
    rf = args[1]

    la = transformer.parseFile(lf)
    ra = transformer.parseFile(rf)

    r, diffs = pydiff(la, ra)

    if r:
        exit(0)

    print '%d difference(s)' % len(diffs)
    print 'first file: %s\nsecond file: %s\n' % (lf, rf)
    for it in diffs:
        pprint(it)
        print

    exit(1)