Example #1
0
 def test_command_type_arithmetic(self):
     '''Test getting command type of arithmetic commands'''
     parser = Parser(code_1)
     eq_(parser.command_type('add'), 'C_ARITHMETIC')
     eq_(parser.command_type('sub'), 'C_ARITHMETIC')
     eq_(parser.command_type('neg'), 'C_ARITHMETIC')
     eq_(parser.command_type('eq'), 'C_ARITHMETIC')
     eq_(parser.command_type('gt'), 'C_ARITHMETIC')
     eq_(parser.command_type('lt'), 'C_ARITHMETIC')
     eq_(parser.command_type('and'), 'C_ARITHMETIC')
     eq_(parser.command_type('or'), 'C_ARITHMETIC')
     eq_(parser.command_type('not'), 'C_ARITHMETIC')
Example #2
0
 def test_command_type_arithmetic(self):
     '''Test getting command type of arithmetic commands'''
     parser = Parser(code_1)
     eq_(parser.command_type('add'), 'C_ARITHMETIC')
     eq_(parser.command_type('sub'), 'C_ARITHMETIC')
     eq_(parser.command_type('neg'), 'C_ARITHMETIC')
     eq_(parser.command_type('eq'), 'C_ARITHMETIC')
     eq_(parser.command_type('gt'), 'C_ARITHMETIC')
     eq_(parser.command_type('lt'), 'C_ARITHMETIC')
     eq_(parser.command_type('and'), 'C_ARITHMETIC')
     eq_(parser.command_type('or'), 'C_ARITHMETIC')
     eq_(parser.command_type('not'), 'C_ARITHMETIC')
Example #3
0
    def translate(path):
        """Convert a *.vm file (or a folder containing *.vm files) into a single *.asm program.

        Use a single CodeWriter for handling the output.
        Use a separate Parser for handling each input file.
        Converted into one large *.asm file.
        """

        _, name = os.path.split(path)
        out_file = os.path.join(path, name)
        # import pdb;pdb.set_trace()
        with CodeWriter(out_file) as cw:  # write to path/path.asm
            for file, name in VMTranslator.get_files(path):
                with Parser(file) as p:
                    cw.set_file_name(name)
                    for cmd in p:
                        # import pdb;pdb.set_trace()
                        if p.command_type() == CmdTypes.C_ARITHMETIC:
                            cw.write_arithmetic(p.arg1())
                        elif any([
                                p.cmd_type == c
                                for c in (CmdTypes.C_PUSH, CmdTypes.C_POP)
                        ]):
                            cw.write_push_pop(p.cmd, p.arg1(), p.arg2())
                        elif p.cmd_type == CmdTypes.C_LABEL:
                            cw.write_label(p.arg1())
                        elif p.cmd_type == CmdTypes.C_IF:
                            cw.write_if(p.arg1())
                        elif p.cmd_type == CmdTypes.C_GOTO:
                            cw.write_goto(p.arg1())
                        elif p.cmd_type == CmdTypes.C_FUNCTION:
                            cw.write_function(p.arg1(), p.arg2())
                        elif p.cmd_type == CmdTypes.C_RETURN:
                            cw.write_return()
                        elif p.cmd_type == CmdTypes.C_CALL:
                            cw.write_call(p.arg1(), p.arg2())
                        else:
                            print(
                                "I haven't write code to handle '{}' yet ...".
                                format(cmd))
Example #4
0
 def test_get_2nd_arg(self):
     '''Test getting the 2nd arg of a command'''
     parser = Parser(code_1)
     eq_(parser.arg2('push local 1'), '1')
Example #5
0
 def test_get_1st_arg(self):
     '''Test getting the 1st arg of a command'''
     parser = Parser(code_1)
     eq_(parser.arg1('push local 1'), 'local')
     eq_(parser.arg1('add'), 'add')
Example #6
0
 def test_skip_comments(self):
     parser = Parser(comments)
     eq_(parser.parse().next(), 'add')
Example #7
0
 def test_skip_blank_lines(self):
     parser = Parser(blank_lines)
     self.assertRaises(StopIteration, parser.parse().next)
Example #8
0
 def test_parser(self):
     '''Test vm translator parser'''
     parser = Parser(code_1)
     parser.parse()
Example #9
0
 def test_get_2nd_arg(self):
     '''Test getting the 2nd arg of a command'''
     parser = Parser(code_1)
     eq_(parser.arg2('push local 1'), '1')
Example #10
0
 def test_unknown_command(self):
     '''Raises error when passed unknown command type'''
     parser = Parser()
     self.assertRaises(UnknownCmdError, parser.command_type, 'blah')
Example #11
0
 def test_get_1st_arg_from_return(self):
     '''Test getting the 1st arg of return command'''
     parser = Parser(code_1)
     self.assertRaises(AssertionError, parser.arg1, 'return')
Example #12
0
 def test_get_1st_arg(self):
     '''Test getting the 1st arg of a command'''
     parser = Parser(code_1)
     eq_(parser.arg1('push local 1'), 'local')
     eq_(parser.arg1('add'), 'add')
Example #13
0
 def test_skip_comments(self):
     parser = Parser(comments)
     eq_(parser.parse().next(), 'add')
Example #14
0
 def test_skip_blank_lines(self):
     parser = Parser(blank_lines)
     self.assertRaises(StopIteration, parser.parse().next)
Example #15
0
 def test_parser(self):
     '''Test vm translator parser'''
     parser = Parser(code_1)
     parser.parse()