def test_parse_label(self):
        parser = Parser()
        lines_to_parse = ['(LABEL)']

        parsed_lines = parser.parse(lines_to_parse)

        self.assertEqual(parsed_lines, [Label('LABEL')])
    def test_parse_lines_c_instruction2(self):
        parser = Parser()
        lines_to_parse = ['D;JGT']

        parsed_lines = parser.parse(lines_to_parse)

        self.assertEqual(parsed_lines, [CInstruction('D', None, 'JGT')])
    def test_parse_comment(self):
        parser = Parser()
        lines_to_parse = ['//test']

        parsed_lines = parser.parse(lines_to_parse)

        self.assertEqual(parsed_lines, [])
    def test_parse_lines_a_instruction2(self):
        parser = Parser()
        lines_to_parse = ['@2']

        parsed_lines = parser.parse(lines_to_parse)

        self.assertEqual(parsed_lines, [AInstruction('2')])
    def test_parse_lines_c_instruction1(self):
        parser = Parser()
        lines_to_parse = ['D=M']

        parsed_lines = parser.parse(lines_to_parse)

        self.assertEqual(parsed_lines, [CInstruction('M', 'D', None)])
示例#6
0
文件: tests.py 项目: bcho/homework
    def testSymbol(self):
        parser = Parser('@abc')
        parser.advance()
        self.assertEqual('abc', parser.symbol)

        parser = Parser('(abc)')
        parser.advance()
        self.assertEqual('abc', parser.symbol)

        parser = Parser('foobar')
        parser.advance()
        self.assertRaises(RuntimeError, lambda: parser.symbol)
示例#7
0
    def testHasMoreCommandsAndAdvance(self):
        parser = Parser('')
        self.assertFalse(parser.hasMoreCommands)

        parser = Parser('hello')
        self.assertEqual(None, parser.currentCommand)
        self.assertTrue(parser.hasMoreCommands)
        parser.advance()
        self.assertFalse(parser.hasMoreCommands)
        self.assertRaises(EOFError, parser.advance)

        parser = Parser('// this is a comment')
        self.assertFalse(parser.hasMoreCommands)

        parser = Parser('foobar // this is a command')
        self.assertTrue(parser.hasMoreCommands)
示例#8
0
 def testCleanLine(self):
     self.assertEqual(None, Parser.cleanLine(''))
     self.assertEqual('123', Parser.cleanLine('123'))
     self.assertEqual(None, Parser.cleanLine('// 123'))
     self.assertEqual(None, Parser.cleanLine('//123'))
     self.assertEqual('123', Parser.cleanLine('123//123'))
     self.assertEqual('123', Parser.cleanLine('123 //123'))
     self.assertEqual('123', Parser.cleanLine('123 // 123'))
示例#9
0
文件: tests.py 项目: bcho/homework
 def testCleanLine(self):
     self.assertEqual(None, Parser.cleanLine(''))
     self.assertEqual('123', Parser.cleanLine('123'))
     self.assertEqual(None, Parser.cleanLine('// 123'))
     self.assertEqual(None, Parser.cleanLine('//123'))
     self.assertEqual('123', Parser.cleanLine('123//123'))
     self.assertEqual('123', Parser.cleanLine('123 //123'))
     self.assertEqual('123', Parser.cleanLine('123 // 123'))
示例#10
0
 def test_is_whitespace(self):
     self.assertTrue(Parser.is_whitespace('     '))
     self.assertTrue(Parser.is_whitespace(''))
     self.assertTrue(Parser.is_whitespace('// a comment'))
     self.assertFalse(Parser.is_whitespace('@i'))
     self.assertFalse(Parser.is_whitespace('A=M+D'))
     self.assertFalse(Parser.is_whitespace('  @asd   '))
示例#11
0
文件: tests.py 项目: bcho/homework
    def testHasMoreCommandsAndAdvance(self):
        parser = Parser('')
        self.assertFalse(parser.hasMoreCommands)

        parser = Parser('hello')
        self.assertEqual(None, parser.currentCommand)
        self.assertTrue(parser.hasMoreCommands)
        parser.advance()
        self.assertFalse(parser.hasMoreCommands)
        self.assertRaises(EOFError, parser.advance)

        parser = Parser('// this is a comment')
        self.assertFalse(parser.hasMoreCommands)

        parser = Parser('foobar // this is a command')
        self.assertTrue(parser.hasMoreCommands)
示例#12
0
 def testInit(self):
     Parser('')
     Parser(open('./tests.py'))
示例#13
0
    def testDestCompJump(self):
        parser = Parser('dest=comp;JMP')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('dest = comp ; JMP')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('dest=comp')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('null', parser.jump)

        parser = Parser('comp;JMP')
        parser.advance()
        self.assertEqual('null', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('comp')
        parser.advance()
        self.assertEqual('null', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('null', parser.jump)

        parser = Parser('@abc')
        parser.advance()
        self.assertRaises(RuntimeError, lambda: parser.dest)
        self.assertRaises(RuntimeError, lambda: parser.comp)
        self.assertRaises(RuntimeError, lambda: parser.jump)
示例#14
0
    def testSymbol(self):
        parser = Parser('@abc')
        parser.advance()
        self.assertEqual('abc', parser.symbol)

        parser = Parser('(abc)')
        parser.advance()
        self.assertEqual('abc', parser.symbol)

        parser = Parser('foobar')
        parser.advance()
        self.assertRaises(RuntimeError, lambda: parser.symbol)
示例#15
0
文件: tests.py 项目: bcho/homework
    def testDestCompJump(self):
        parser = Parser('dest=comp;JMP')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('dest = comp ; JMP')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('dest=comp')
        parser.advance()
        self.assertEqual('dest', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('null', parser.jump)

        parser = Parser('comp;JMP')
        parser.advance()
        self.assertEqual('null', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('JMP', parser.jump)

        parser = Parser('comp')
        parser.advance()
        self.assertEqual('null', parser.dest)
        self.assertEqual('comp', parser.comp)
        self.assertEqual('null', parser.jump)

        parser = Parser('@abc')
        parser.advance()
        self.assertRaises(RuntimeError, lambda: parser.dest)
        self.assertRaises(RuntimeError, lambda: parser.comp)
        self.assertRaises(RuntimeError, lambda: parser.jump)
示例#16
0
def init(stream):
    return Parser(stream)