def test_default_context(self):
        manager = ContextManager()
        context = Context(manager)
        defaultContext = DefaultContext(manager)
        flowControlContext = FlowControlContext(manager)
        expressionContext = ExpressionContext(manager)

        manager.addContext('Default', defaultContext)
        manager.addContext('FlowControl', flowControlContext)
        manager.addContext('Expression', expressionContext)
        defaultContext.addKeyword('if')
        defaultContext.addKeyword('while')
        manager.setCurrentContexts([defaultContext])
        lexer = LexerStateMachine('if', context)
        parser = Parser(lexer, manager)
        manager.setParser(parser)
        try:
            token = parser.parseStatement(0)
            self.fail()
        except SyntaxError as e:
            pass
        lexer = LexerStateMachine('123 if', context)
        parser = Parser(lexer, manager)
        manager.setParser(parser)
        try:
            token = parser.parseStatement(0)
            self.fail()
        except SyntaxError as e:
            pass
 def test_buildScope_should_raise_given_redeclaration_of_x_3xpointer(self):
     lexer = LexerStateMachine('int x; int ***x;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     try:
         parser.parseStatement(0)
     except SyntaxError as e:
         self.assertEqual("Error[1][11]:Redeclaration of 'x'" + '\n' +
                          'nt x; int ***x;' + '\n' +
                          '             ^', e.msg)
 def test_redeclaration_of_x_given_second_x_is_pointer_in_single_statement(self):
     lexer = LexerStateMachine('int x, *x;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     try:
         parser.parseStatement(0)
         self.fail()
     except SyntaxError as e:
         self.assertEqual("Error[1][9]:Redeclaration of 'x'"+ '\n' +
                          'int x, *x;'+ '\n' +
                          '        ^',e.msg)
 def test_parseStatement_should_raise_SyntaxError_when_there_is_missing_one_close_brace(self):
     lexer = LexerStateMachine('{ 2 + 3 ; { }', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     try:
         parser.parseStatement(0)
         raise SyntaxError("Exception test failed")
     except SyntaxError as e:
         self.assertEqual("Error[1][14]:Expecting } before EOF"+'\n'+
                          '{ 2 + 3 ; { }'+'\n'+
                          '             ^', e.msg)
 def test_parseStatement_should_raise_SyntaxError_when_there_is_missing_open_brace(self):
     lexer = LexerStateMachine('2 + 3 ; }', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     token = parser.parseStatement(0)
     try:
         parser.parseStatement(0)
         raise SyntaxError("Exception test failed")
     except SyntaxError as e:
         self.assertEqual("Error[1][9]:Expected a declaration"+'\n'+
                          '2 + 3 ; }'+'\n'+
                          '        ^', e.msg)
    def test_without_expression_after_equal(self):
        lexer = LexerStateMachine('x = ;', self.context)
        parser = Parser(lexer, self.manager)
        self.manager.setParser(parser)
        try:

            parser.parseStatement(0)
            raise SyntaxError("Exception test failed")
        except SyntaxError as e:
             self.assertEqual("Error[1][5]:Expected a declaration"+'\n'+
                             'x = ;'+'\n'+
                             '    ^', e.msg)
 def test_isAllDefined_should_not_raise_given_x_is_compatible_to_y_plus_z(self):
     lexer = LexerStateMachine('int *x, *y, *z; x = y + z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
     except SyntaxError as e:
         self.fail('Should not raise')
 def test_isAllDefined_should_not_raise_given_x_is_defined(self):
     lexer = LexerStateMachine('int x;\nint y = x;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
     except SyntaxError as e:
         self.assertEqual("Error[3][9]:'y' is not declared" + '\n' +
                          'x = 2 + y;' + '\n' +
                          '        ^', e.msg)
 def test_checkIfAssignmentValid_should_not_raise_given_the_highest_pointer_level_of_rvalue_equal_pointer_level_of_lvalue(self):
     lexer = LexerStateMachine('int *x, *y, *z;\nx = y + *z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
     except SyntaxError as e:
         self.fail('Should not raise')
 def test_isAllDefined_should_raise_given_y_is_not_defined_before_its_declaration(self):
     lexer = LexerStateMachine('int x = y;\nint y;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[1][9]:'y' is not declared" + '\n' +
                          'int x = y;' + '\n' +
                          '        ^', e.msg)
 def test_checkIfAssignmentValid_should_not_raise_given_assigning_addressof_y_to_a_pointer(self):
     # addressof = &
     lexer = LexerStateMachine('int *x, y;\nx = &y;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
     except SyntaxError as e:
         self.fail('Should not raise')
Ejemplo n.º 12
0
 def test_parseStatement_should_raise_SyntaxError_for_a_plus_3_statement_without_semicolon(self):
     """
         2 + 3
     :return:
     """
     lexer = LexerStateMachine('+ 3', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     try:
         parser.parseStatement(0)
         raise SyntaxError("Exception test failed")
     except SyntaxError as e:
         self.assertEqual("Error[1][4]:Expecting ; before EOF"+'\n'+
                          '+ 3'+'\n'+
                          '   ^', e.msg)
 def test_isAllDefined_should_not_raise_given_x_y_z_is_defined(self):
     lexer = LexerStateMachine('int x; int y; int z; x = y + z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
     except SyntaxError as e:
         self.fail(e.msg)
    def test_injectRegisterRequired_will_give_min_and_max_register_to_each_of_the_token(self):
        lexer = LexerStateMachine('{ x = y + 8 * 16 / 180 - 20 ; }', self.context)
        parser = Parser(lexer, self.manager)
        self.manager.setParser(parser)

        token = parser.parseStatement(0)
        if token[0].id == '{':
            token = token[0].data[0]
        self.informationInjector.injectRegisterRequired(token)

        self.assertEqual(1, token.data[0].maxRequiredRegister)
        self.assertEqual(1, token.data[0].minRequiredRegister)
        self.assertEqual(6, token.maxRequiredRegister)
        self.assertEqual(2, token.minRequiredRegister)
        self.assertEqual(5, token.data[1].maxRequiredRegister)
        self.assertEqual(2, token.data[1].minRequiredRegister)
        self.assertEqual(1, token.data[1].data[1].maxRequiredRegister)
        self.assertEqual(1, token.data[1].data[1].minRequiredRegister)
        self.assertEqual(4, token.data[1].data[0].maxRequiredRegister)
        self.assertEqual(2, token.data[1].data[0].minRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[0].maxRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[0].minRequiredRegister)
        self.assertEqual(3, token.data[1].data[0].data[1].maxRequiredRegister)
        self.assertEqual(2, token.data[1].data[0].data[1].minRequiredRegister)
        self.assertEqual(2, token.data[1].data[0].data[1].data[0].maxRequiredRegister)
        self.assertEqual(2, token.data[1].data[0].data[1].data[0].minRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[1].maxRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[1].minRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[0].data[0].maxRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[0].data[0].minRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[0].data[1].maxRequiredRegister)
        self.assertEqual(1, token.data[1].data[0].data[1].data[0].data[1].minRequiredRegister)
 def test_isAllDefined_should_raise_given_z_has_invalid_type_in_a_statement(self):
     lexer = LexerStateMachine('int *x, *y, *z;\nx = y + **z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[2][11]:Invalid type of 'z'" + '\n' +
                          'x = y + **z;' + '\n' +
                          '          ^', e.msg)
 def test_checkIfAssignmentValid_should_raise_given_addressof_addressof_y(self):
     # addressof = &
     lexer = LexerStateMachine('int *x, y;\nx = &&y;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
     except SyntaxError as e:
         self.assertEqual("Error[2][5]:Expect (literal) or (identifier) before &&" + '\n' +
                          'x = &&y;' + '\n' +
                          '    ^', e.msg)
 def test_buildScope_should_not_raise_given_redeclaration_of_x(self):
     lexer = LexerStateMachine('int x', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     try:
         parser.parseStatement(0)
     except SyntaxError as e:
         self.assertEqual("Error[1][6]:Expecting ; before EOF" + '\n' +
                          'int x' + '\n' +
                          '     ^', e.msg)
     try:
         lexer = LexerStateMachine('int x;', self.context)
         parser.lexer = lexer
         parser.parseStatement(0)
     except SyntaxError as e:
         print(e.msg)
         self.fail('Should not raise')
 def test_checkIfAssignmentValid_should_raise_given_pointer_level_of_rvalue_is_2_pointer_level_of_lvalue_is_1(self):
     lexer = LexerStateMachine('int *x, *y, *z;\nx = y + &z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[2][3]:Incompatible assignment" + '\n' +
                          'x = y + &z;' + '\n' +
                          '  ^', e.msg)
 def test_parseStatement_call_buildScope_when_declaration(self):
     lexer = LexerStateMachine('int x;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     mockScopeBuilder = MagicMock(wraps=ScopeBuilder())
     parser.scopeBuilder = mockScopeBuilder
     token = parser.parseStatement(0)
     mockScopeBuilder.buildScope.assert_called_once_with(token[0])
 def test_isAllDefined_should_raise_given_x_is_not_compatible_to_y_plus_z(self):
     lexer = LexerStateMachine('int *x, *y, *z;\n*x = y + z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[2][4]:Incompatible assignment" + '\n' +
                          '*x = y + z;' + '\n' +
                          '   ^', e.msg)
 def test_checkIfAssignmentValid_should_raise_given_assigning_addressof_5_to_a_pointer(self):
     # addressof = &
     lexer = LexerStateMachine('int *x;\nx = &5;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[2][6]:(literal) do not have address" + '\n' +
                          'x = &5;' + '\n' +
                          '     ^', e.msg)
 def test_checkIfAssignmentValid_should_raise_given_addressof_lvalue_of_assignment(self):
     # addressof = &
     lexer = LexerStateMachine('int *x, *y, *z;\n&x = &y + &z;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         token = parser.parseStatement(0)
         semanticChecker.checkIfAllIdentifiersAreDefined(token[0])
         semanticChecker.checkIfAllTokenTypeValid(token[0])
         semanticChecker.checkIfAssignmentValid(token[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[2][4]:Invalid lvalue type of assignment" + '\n' +
                          '&x = &y + &z;' + '\n' +
                          '   ^', e.msg)
Ejemplo n.º 23
0
 def test_call_directly_to_dll_VMRun2(self):
     lexer = LexerStateMachine('int x;', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     token = parser.parseStatement(0)
     byteCodes =self.generator.generateCode(token)
     vm = VirtualMachine()
     vm.VMLoad(byteCodes)
     vm.VMRun()
 def test_isAllDefined_should_not_raise_given_x_is_globally_defined_and_y_z_are_locally_defined(self):
     lexer = LexerStateMachine('int x; {int y; int z; x = y + z;}', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     parser.semanticChecker.isEnable = True
     try:
         token = parser.parseStatement(0)
         token = parser.parseStatement(0)
     except SyntaxError as e:
         self.fail(e.msg)
Ejemplo n.º 25
0
 def test_parseStatement_will_return_None_for_an_empty_statement(self):
     """
         ;
     :return:
     """
     lexer = LexerStateMachine(';', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     token = parser.parseStatement(0)
     self.assertEqual(None, token)
Ejemplo n.º 26
0
 def test_dumpBytecodes(self):
     lexer = LexerStateMachine('while( x == 2) {x = 100;\n y = 1000;\n z=2000;} ', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     token = parser.parseStatement(0)
     self.byteCodeGenerator.variablesInThisAST['x'] = 4
     self.byteCodeGenerator.variablesInThisAST['y'] = 8
     self.byteCodeGenerator.variablesInThisAST['z'] = 12
     bytecodes = self.generator.generateCode(token)
     vm = VirtualMachine()
     vm.VMLoad(bytecodes)
 def test_y_undeclared(self):
     lexer = LexerStateMachine('int x = y, y;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     parser.semanticChecker.isEnable = True
     try:
         token = parser.parseStatement(0)
         self.fail()
     except SyntaxError as e:
         self.assertEqual("Error[1][9]:'y' is not declared"+ '\n' +
                            'int x = y, y;'+ '\n' +
                            '        ^',e.msg)
 def test_invalid_type_of_x_in_one_declaration_statement_should_raise(self):
     lexer = LexerStateMachine('int x, y = *x;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     parser.semanticChecker.isEnable = True
     try:
         token = parser.parseStatement(0)
         self.fail()
     except SyntaxError as e:
         self.assertEqual("Error[1][13]:Invalid type of 'x'"+ '\n' +
                          'int x, y = *x;'+ '\n' +
                          '            ^',e.msg)
Ejemplo n.º 29
0
 def test_parseStatement_should_return_2_plus_3_for_a_2_plus_3_statement(self):
     """
         2 + 3 ;
     :return:
     """
     lexer = LexerStateMachine('2 + 3 ;', self.context)
     parser = Parser(lexer, self.manager)
     self.manager.setParser(parser)
     token = parser.parseStatement(0)
     self.assertEqual('+', token[0].id)
     self.assertEqual(2, token[0].data[0].data[0])
     self.assertEqual(3, token[0].data[1].data[0])
 def test_isDefined_will_raise_when_x_is_not_defined(self):
     lexer = LexerStateMachine('x = 0;', self.context)
     parser = Parser(lexer, self.contextManager)
     self.contextManager.setParser(parser)
     semanticChecker = SemanticChecker(parser.scopeBuilder)
     try:
         token = parser.parseStatement(0)
         semanticChecker.checkIfIdentifierIsDefined(token[0].data[0])
         self.fail('Should raise')
     except SyntaxError as e:
         self.assertEqual("Error[1][1]:'x' is not declared" + '\n' +
                          'x = 0;' + '\n' +
                          '^', e.msg)