예제 #1
0
def translate_file(vm_file, asmfile):

    current_file_name = os.path.basename(vm_file)[:-3]
    # Initiate list of functions/files names and variables and append current file
    global_variables.function_list.append(current_file_name)

    with open(vm_file, 'r') as vmfile:
        # Create assembly file to receive translated code
        assembly_file = open(asmfile, 'a')

        parser = Parser()
        assembly_code = ""
        code_writer = CodeWriter(current_file_name)

        for line in vmfile:
            if is_blank_or_is_comment(line):
                pass
            else:
                command = parser.clean_line(line)
                command_type = parser.command_type(command)

                if command_type == "C_ARITHMETIC":
                    assembly_code = code_writer.write_arithmetic(command)

                elif command_type in ["C_LABEL", "C_GOTO", "C_IF"]:
                    command_args = parser.command_args(command)
                    assembly_code = code_writer.write_control_flow(
                        command_args)

                elif command_type in ["C_CALL", "C_RETURN", "C_FUNCTION"]:
                    command_args = parser.command_args(command)
                    assembly_code = code_writer.write_subroutines(command_args)

                else:
                    command_args = parser.command_args(command)
                    assembly_code = code_writer.write_push_pop(command_args)

                assembly_file.write(assembly_code)

        assembly_file.close()

    # Reset list of functions processed in the current file
    global_variables.function_list.clear()