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)])
Example #6
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)
Example #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)
Example #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'))
Example #9
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'))
Example #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   '))
Example #11
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)
Example #12
0
 def testInit(self):
     Parser('')
     Parser(open('./tests.py'))
Example #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)
Example #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)
Example #15
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)
Example #16
0
def init(stream):
    return Parser(stream)