コード例 #1
0
def main():
    # Input file Path
    in_file = Path(argv[1])

    # Check if provided argument is
    # a file or a directory
    if (in_file.is_dir()):
        # Argument path to a directory
        out_file = in_file / in_file.with_suffix(".asm")

    elif (in_file.is_file()):
        # Argument is path to a file
        # Output file Path
        out_file = in_file.with_suffix(".asm")

    # Construct a CodeWriter to
    # handle output file
    asm_writer = CodeWriter(out_file)

    if (in_file.is_file()):
        # Translate the single file
        translate_in_file(asm_writer, in_file)

    elif (in_file.is_dir()):
        # Have to loop through every file
        asm_writer.write_bootstrap_code()

        # Loop through each file and translate
        for f in in_file.iterdir():
            # File is .vm file?
            if f.suffix == ".vm":
                print(f"translating {f.name}...")
                asm_writer.set_file_name(f.stem)
                translate_in_file(asm_writer, f)
コード例 #2
0
def main(argv):
    """
    Main flow of program dealing with extracting files for reading and initializing files to translate into
    """
    if not check_args(argv):
        return

    #  extracting asm file to be processed
    vm_files_path = argv[1]

    #  creating a .asm file to contain vm files translation to hack machine language
    if os.path.isdir(vm_files_path):
        dir_name = os.path.basename(vm_files_path)
        asm_file_name = "{0}/{1}.asm".format(vm_files_path, dir_name)
        code_writer = CodeWriter(asm_file_name)
        code_writer.write_init()
        for file in os.listdir(vm_files_path):
            if file.endswith(".vm"):
                code_writer.set_file_name(file)
                vm_parser = VMParser('{0}/{1}'.format(vm_files_path, file))
                translate_vm_file(code_writer, vm_parser)
    else:
        asm_file_name = "{0}.asm".format(os.path.splitext(vm_files_path)[0])
        code_writer = CodeWriter(asm_file_name)
        code_writer.write_init()
        code_writer.set_file_name(vm_files_path)
        vm_parser = VMParser(vm_files_path)
        translate_vm_file(code_writer, vm_parser)
コード例 #3
0
def main():
    file_name = parse_args()
    file_name_noext, _ = path.splitext(file_name)

    raw_data = None
    with open(file_name, "r") as stream:
        raw_data = stream.readlines()

    parser = Parser(raw_data)

    code_writer = CodeWriter()
    code_writer.set_file_name(file_name_noext.split("/")[-1] + ".asm")

    while parser.has_more_commands():
        parser.advance()

        if parser.command_type == C_ARITHMETIC:
            code_writer.writer_arithmetic(parser.operator)
        elif parser.command_type == C_PUSH:
            code_writer.write_push_pop(parser.operator, parser.arg1(),
                                       parser.arg2())
        elif parser.command_type == C_POP:
            code_writer.write_push_pop(parser.operator, parser.arg1(),
                                       parser.arg2())
        elif parser.command_type == C_LABEL:
            pass
        elif parser.command_type == C_GOTO:
            pass
        elif parser.command_type == C_IF:
            pass
        elif parser.command_type == C_FUNCTION:
            pass
        elif parser.command_type == C_RETURN:
            pass
        elif parser.command_type == C_CALL:
            pass
        else:
            pass

    code_writer.close()