コード例 #1
0
    def test_validate_if_name_exists_in_symbol_table(self):

        # Arrange:
        code = "Play: LD VA, 2\nJP Args\nArgs: DRW V0, V1, 1"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)

        # Act:
        semantic.analyze(ast)

        # Arrange:
        self.assertFalse(logger.has_error)
コード例 #2
0
    def test_has_error_analyze_entire_ast(self):

        # Arrange:
        code = "LD VA, Play\nJP 409\n"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)

        # Act:
        semantic.analyze(ast)

        # Arrange:
        self.assertTrue(logger.has_error)
コード例 #3
0
    def test_convert_SE_3XNN_node_to_opcode(self):

        # Arrange:
        code = "SE V0, 255"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('30FF'), opcodes[0])
コード例 #4
0
    def test_convert_JMP_BNNN_node_to_opcode(self):

        # Arrange:
        code = "JP 4095, V0"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('BFFF'), opcodes[0])
コード例 #5
0
    def test_should_padding_when_value_should_node_complete_opcode(self):

        # Arrange:
        code = "JP 2"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('1002'), opcodes[0])
コード例 #6
0
    def test_convert_DW_to_opcode(self):

        # Arrange:
        code = "DW #F000"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('F000'), opcodes[0])
コード例 #7
0
    def test_convert_LD_FX33_node_to_opcode(self):

        # Arrange:
        code = "LD B, V6"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('F633'), opcodes[0])
コード例 #8
0
    def test_convert_DRW_node_to_opcode(self):

        # Arrange:
        code = "DRW V1, V9, #F"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('D19F'), opcodes[0])
コード例 #9
0
    def test_convert_SHL_node_to_opcode(self):

        # Arrange:
        code = "SHL V3, V4"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('834E'), opcodes[0])
コード例 #10
0
    def test_convert_ADD_8XY4_node_to_opcode(self):

        # Arrange:
        code = "ADD V2, V6"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('8264'), opcodes[0])
コード例 #11
0
    def test_convert_SNE_9XY0_node_to_opcode(self):

        # Arrange:
        code = "SNE V1, V5"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)
        semantic.analyze(ast)

        # Act:
        opcodes = assembler.generate(ast)

        # Arrange:
        self.assertEqual(self.pack('9150'), opcodes[0])
コード例 #12
0
    def test_throw_exception_when_validate_if_name_does_not_exists_in_symbol_table(
            self):

        # Arrange:
        code = "Play: LD VA, 2\nJP Draw\nArgs: DRW V0, V1, 1"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)

        # Act:
        semantic.analyze(ast)

        # Arrange:
        self.assertTrue(logger.has_error)
コード例 #13
0
ファイル: __init__.py プロジェクト: fakeezz/chasm
def exec_compile(args):
    import sys

    code = args.filename.read()
    tokens = lexical.tokenize(code)
    ast = syntactic.Ast(tokens)
    semantic.analyze(ast)

    logger.show()
    if logger.has_error:
        sys.exit(-1)

    opcodes = assembler.generate(ast)

    for opcode in opcodes:
        args.output.write(opcode)
    args.output.close()
コード例 #14
0
    def test_analyze_entire_ast(self):

        # Arrange:
        code = "LD VA, #02\nLD VB, #02\n"
        tokens = lexical.tokenize(code)
        ast = syntactic.Ast(tokens)

        # Act:
        # Arrange:
        self.assertTrue(semantic.analyze(ast))