Beispiel #1
0
        else:
            dst_file = os.path.basename(path)
        dst_file += '.asm'
        bootstrap = True

    dst_file = open(os.path.join(path, dst_file), 'w+')

    parser = Parser()
    generator = CodeGenerator()

    bootstrap_code = ''
    if bootstrap:
        parsed_line = parser.parse('call Sys.init 0')
        bootstrap_code += '@256\n' + 'D=A\n' + '@SP\n' + 'M=D\n'
        bootstrap_code += generator.generate_code(parsed_line)
    dst_file.write(bootstrap_code)

    for f_name in file_names:
        generator.set_new_file_name(file_name=f_name)
        src_file = open(os.path.join(path, f_name+'.vm'), 'r')
        src_line = src_file.readline()
        while src_line:
            parsed_line = parser.parse(src_line)
            if parsed_line:
                output_code = generator.generate_code(parsed_line)
                # print(output_code)
                dst_file.write(output_code)
            src_line = src_file.readline()
        src_file.close()
    dst_file.close()