Exemplo n.º 1
0
 def try_to_recover_or_error(_lhs_type_symbol, _rhs_type_symbol, _containing_expression):
     if _rhs_type_symbol.differs_only_in_magnitude_or_is_equal_to(_lhs_type_symbol):
         TypeCaster.do_magnitude_conversion_rhs_to_lhs(_rhs_type_symbol, _lhs_type_symbol, _containing_expression)
     elif _rhs_type_symbol.is_castable_to(_lhs_type_symbol):
         LoggingHelper.drop_implicit_cast_warning(_containing_expression.get_source_position(), _lhs_type_symbol,
                                                  _rhs_type_symbol)
     else:
         LoggingHelper.drop_incompatible_types_error(_containing_expression, _lhs_type_symbol, _rhs_type_symbol)
Exemplo n.º 2
0
    def handle_complex_assignment(self, node):
        rhs_expr = node.get_expression()
        lhs_variable_symbol = node.resolve_lhs_variable_symbol()
        rhs_type_symbol = rhs_expr.type

        if isinstance(rhs_type_symbol, ErrorTypeSymbol):
            LoggingHelper.drop_missing_type_error(node)
            return

        if self.__types_do_not_match(lhs_variable_symbol.get_type_symbol(),
                                     rhs_type_symbol):
            TypeCaster.try_to_recover_or_error(
                lhs_variable_symbol.get_type_symbol(), rhs_type_symbol,
                node.get_expression())
        return
Exemplo n.º 3
0
    def handle_simple_assignment(self, node):
        from pynestml.symbols.symbol import SymbolKind
        lhs_variable_symbol = node.get_scope().resolve_to_symbol(
            node.get_variable().get_complete_name(), SymbolKind.VARIABLE)

        rhs_type_symbol = node.get_expression().type
        if isinstance(rhs_type_symbol, ErrorTypeSymbol):
            LoggingHelper.drop_missing_type_error(node)
            return

        if lhs_variable_symbol is not None and self.__types_do_not_match(
                lhs_variable_symbol.get_type_symbol(), rhs_type_symbol):
            TypeCaster.try_to_recover_or_error(
                lhs_variable_symbol.get_type_symbol(), rhs_type_symbol,
                node.get_expression())
        return
Exemplo n.º 4
0
    def handle_compound_assignment(self, node):
        rhs_expr = node.get_expression()
        lhs_variable_symbol = node.get_variable().resolve_in_own_scope()
        rhs_type_symbol = rhs_expr.type

        if lhs_variable_symbol is None:
            code, message = Messages.get_equation_var_not_in_state_block(
                node.get_variable().get_complete_name())
            Logger.log_message(code=code,
                               message=message,
                               error_position=node.get_source_position(),
                               log_level=LoggingLevel.ERROR)
            return

        if isinstance(rhs_type_symbol, ErrorTypeSymbol):
            LoggingHelper.drop_missing_type_error(node)
            return

        lhs_type_symbol = lhs_variable_symbol.get_type_symbol()

        if node.is_compound_product:
            if self.__types_do_not_match(lhs_type_symbol,
                                         lhs_type_symbol * rhs_type_symbol):
                TypeCaster.try_to_recover_or_error(
                    lhs_type_symbol, lhs_type_symbol * rhs_type_symbol,
                    node.get_expression())
                return
            return

        if node.is_compound_quotient:
            if self.__types_do_not_match(lhs_type_symbol,
                                         lhs_type_symbol / rhs_type_symbol):
                TypeCaster.try_to_recover_or_error(
                    lhs_type_symbol, lhs_type_symbol / rhs_type_symbol,
                    node.get_expression())
                return
            return

        assert node.is_compound_sum or node.is_compound_minus
        if self.__types_do_not_match(lhs_type_symbol, rhs_type_symbol):
            TypeCaster.try_to_recover_or_error(lhs_type_symbol,
                                               rhs_type_symbol,
                                               node.get_expression())
Exemplo n.º 5
0
 def visit_declaration(self, node):
     """
     Visits a single declaration and asserts that type of lhs is equal to type of rhs.
     :param node: a single declaration.
     :type node: ASTDeclaration
     """
     assert isinstance(node, ASTDeclaration)
     if node.has_expression():
         if node.get_expression().get_source_position().equals(
                 ASTSourceLocation.get_added_source_position()):
             # no type checks are executed for added nodes, since we assume correctness
             return
         lhs_type = node.get_data_type().get_type_symbol()
         rhs_type = node.get_expression().type
         if isinstance(rhs_type, ErrorTypeSymbol):
             LoggingHelper.drop_missing_type_error(node)
             return
         if self.__types_do_not_match(lhs_type, rhs_type):
             TypeCaster.try_to_recover_or_error(lhs_type, rhs_type,
                                                node.get_expression())
     return