コード例 #1
0
def main(input_file):
    if len(input_file.split('.')) == 1:
        if input_file[-1] == '/':
            input_file = input_file[:-1]
        output_file = join(input_file, input_file.split('/')[-1])
        input_files = [
            join(input_file, f) for f in listdir(input_file)
            if f.split('.')[1] == 'vm' and f != 'Sys.vm'
            and isfile(join(input_file, f))
        ]
        input_files.insert(0, join(input_file, 'Sys.vm'))
        print('Input files: {}'.format(input_files))
    else:
        input_files = [input_file]
        directory = '/'.join(input_file.split('/')[:-1])
        output_file = join(directory, input_file.split('/')[-1].split('.')[0])

    code_writer = CodeWriter(output_file)
    code_writer.write_init()

    for input_file in input_files:
        parser = Parser(input_file)
        code_writer.set_filename(input_file)
        n = 0
        while parser.advance():
            com_type = parser.command_type()
            print('LINE {}'.format(n))
            print('\tcom type: {}'.format(com_type))
            print('\tcommand: {}'.format(parser.command))
            if com_type != parser.c_return:
                arg1 = parser.arg1()
                if com_type in [
                        parser.c_push, parser.c_pop, parser.c_function,
                        parser.c_call
                ]:
                    arg2 = parser.arg2()

            if com_type == parser.c_arithmetic:
                code_writer.write_arithmetic(arg1)
            elif com_type in [parser.c_pop, parser.c_push]:
                code_writer.write_push_pop(com_type, arg1, int(arg2))
            elif com_type == parser.c_label:
                code_writer.write_label(arg1)
            elif com_type == parser.c_goto:
                code_writer.write_goto(arg1)
            elif com_type == parser.c_if:
                code_writer.write_if(arg1)
            elif com_type == parser.c_function:
                code_writer.write_function(arg1, arg2)
            elif com_type == parser.c_return:
                code_writer.write_return()
            elif com_type == parser.c_call:
                code_writer.write_call(arg1, arg2)
            n += 1
    code_writer.close()
コード例 #2
0
ファイル: vm_translator.py プロジェクト: bvargo/csci410
   def translate(self):
      code_writer = CodeWriter(self.destination_file)

      # for each source filename
      for source_filename in self.source_filenames:
         parser = Parser(source_filename)
         code_writer.set_filename(source_filename)

         # parse each command
         while parser.has_more_commands():
            # advance to the next command
            parser.advance()

            # parse the command type
            command_type = parser.command_type()
            if command_type == "C_ARITHMETIC":
               code_writer.write_arithemtic(parser.arg1())
            elif command_type in ["C_POP", "C_PUSH"]:
               code_writer.write_push_pop(command_type, parser.arg1(), parser.arg2())
            elif command_type == "C_LABEL":
               code_writer.write_label(parser.arg1())
            elif command_type == "C_GOTO":
               code_writer.write_goto(parser.arg1())
            elif command_type == "C_IF":
               code_writer.write_if(parser.arg1())
            elif command_type == "C_FUNCTION":
               code_writer.write_function(parser.arg1(), parser.arg2())
            elif command_type == "C_RETURN":
               code_writer.write_return()
            elif command_type == "C_CALL":
               code_writer.write_call(parser.arg1(), parser.arg2())
            else:
               raise Exception("Not implemented: command type " + command_type)


      # close the output file
      code_writer.close()
コード例 #3
0
ファイル: main.py プロジェクト: maininformer/nand2Tetris
            while p.has_more_lines():
                p.advance()
                command = p.command_type()
                arg_1 = p.arg_1()
                arg_2 = p.arg_2()
                if command in ['C_PUSH', 'C_POP']:
                    c.write_push_pop(command, arg_1, arg_2)

                if command == 'C_ARITHMETIC':
                    c.write_arithmetic(arg_1)

                if command == 'C_GOTO':
                    c.write_goto(arg_1)

                if command == 'C_IF':
                    c.write_if(arg_1)

                if command == 'C_LABEL':
                    c.write_label(arg_1)

                if command == 'C_FUNCTION':
                    c.write_function(arg_1, arg_2)

                if command == 'C_RETURN':
                    c.write_return()

                if command == 'C_CALL':
                    c.write_call(arg_1, arg_2)

    c.close()