示例#1
0
    def convert_name_reference(self,
                               ast_variable: ASTVariable,
                               prefix: str = ''):
        """
        Converts a single name reference to a gsl processable format.
        :param ast_variable: a single variable
        :type ast_variable: ASTVariable
        :return: a gsl processable format of the variable
        :rtype: str
        """
        variable_name = NestNamesConverter.convert_to_cpp_name(
            ast_variable.get_name())

        if variable_name == PredefinedVariables.E_CONSTANT:
            return 'numerics::e'

        symbol = ast_variable.get_scope().resolve_to_symbol(
            ast_variable.get_complete_name(), SymbolKind.VARIABLE)
        if symbol is None:
            # test if variable name can be resolved to a type
            if PredefinedUnits.is_unit(ast_variable.get_complete_name()):
                return str(
                    UnitConverter.get_factor(
                        PredefinedUnits.get_unit(
                            ast_variable.get_complete_name()).get_unit()))

            code, message = Messages.get_could_not_resolve(variable_name)
            Logger.log_message(
                log_level=LoggingLevel.ERROR,
                code=code,
                message=message,
                error_position=ast_variable.get_source_position())
            return ''

        if symbol.is_init_values():
            return GSLNamesConverter.name(symbol)

        if symbol.is_buffer():
            if isinstance(symbol.get_type_symbol(), UnitTypeSymbol):
                units_conversion_factor = UnitConverter.get_factor(
                    symbol.get_type_symbol().unit.unit)
            else:
                units_conversion_factor = 1
            s = ""
            if not units_conversion_factor == 1:
                s += "(" + str(units_conversion_factor) + " * "
            s += prefix + 'B_.' + NestNamesConverter.buffer_value(symbol)
            if symbol.has_vector_parameter():
                s += '[i]'
            if not units_conversion_factor == 1:
                s += ")"
            return s

        if symbol.is_local() or symbol.is_function:
            return variable_name

        if symbol.has_vector_parameter():
            return prefix + 'get_' + variable_name + '()[i]'

        return prefix + 'get_' + variable_name + '()'
    def visit_declaration(self, node: ASTDeclaration):

        variables = node.get_variables()
        for variable in variables:
            vector_parameter = variable.get_vector_parameter()
            if vector_parameter is not None:
                vector_parameter_var = ASTVariable(vector_parameter,
                                                   scope=node.get_scope())
                symbol = vector_parameter_var.get_scope().resolve_to_symbol(
                    vector_parameter_var.get_complete_name(),
                    SymbolKind.VARIABLE)
                vector_parameter_val = None
                if symbol is not None:
                    if isinstance(symbol.get_type_symbol(), IntegerTypeSymbol):
                        vector_parameter_val = int(
                            str(symbol.get_declaring_expression()))
                else:
                    vector_parameter_val = int(vector_parameter)

                if vector_parameter_val is not None and vector_parameter_val <= 0:
                    code, message = Messages.get_vector_parameter_wrong_size(
                        vector_parameter_var.get_complete_name(),
                        str(vector_parameter_val))
                    Logger.log_message(
                        error_position=node.get_source_position(),
                        log_level=LoggingLevel.ERROR,
                        code=code,
                        message=message)
    def visit_variable(self, node: ASTVariable):
        vector_parameter = node.get_vector_parameter()
        if vector_parameter is not None:
            vector_parameter_var = ASTVariable(vector_parameter, scope=node.get_scope())
            symbol = vector_parameter_var.get_scope().resolve_to_symbol(vector_parameter_var.get_complete_name(),
                                                                        SymbolKind.VARIABLE)

            # vector parameter is a variable
            if symbol is not None:
                if not isinstance(symbol.get_type_symbol(), IntegerTypeSymbol):
                    code, message = Messages.get_vector_parameter_wrong_type(vector_parameter_var.get_complete_name())
                    Logger.log_message(error_position=node.get_source_position(), log_level=LoggingLevel.ERROR,
                                       code=code, message=message)
示例#4
0
    def visit_declaration(self, node: ASTDeclaration):

        variables = node.get_variables()
        for var in variables:
            vector_parameter = var.get_vector_parameter()
            if vector_parameter is not None:
                vector_parameter_var = ASTVariable(vector_parameter, scope=node.get_scope())
                symbol = vector_parameter_var.get_scope().resolve_to_symbol(vector_parameter_var.get_complete_name(),
                                                                            SymbolKind.VARIABLE)
                # vector parameter is a variable
                if symbol is not None:
                    if not symbol.block_type == BlockType.PARAMETERS and not symbol.block_type == BlockType.INTERNALS:
                        code, message = Messages.get_vector_parameter_wrong_block(vector_parameter_var.get_complete_name(),
                                                                                  str(symbol.block_type))
                        Logger.log_message(error_position=node.get_source_position(), log_level=LoggingLevel.ERROR,
                                           code=code, message=message)
示例#5
0
文件: ast_utils.py 项目: nest/nestml
 def get_numeric_vector_size(cls, variable: VariableSymbol) -> int:
     """
     Returns the numerical size of the vector by resolving any variable used as a size parameter in declaration
     :param variable: vector variable
     :return: the size of the vector as a numerical value
     """
     vector_parameter = variable.get_vector_parameter()
     vector_variable = ASTVariable(vector_parameter, scope=variable.get_corresponding_scope())
     symbol = vector_variable.get_scope().resolve_to_symbol(vector_variable.get_complete_name(), SymbolKind.VARIABLE)
     if symbol is not None:
         # vector size is a variable. Get the value from RHS
         return symbol.get_declaring_expression().get_numeric_literal()
     return int(vector_parameter)
示例#6
0
    def print_vector_size_parameter(self, variable: VariableSymbol) -> str:
        """
        Prints NEST compatible vector size parameter
        :param variable: Vector variable
        :return: vector size parameter
        """
        vector_parameter = variable.get_vector_parameter()
        vector_parameter_var = ASTVariable(
            vector_parameter, scope=variable.get_corresponding_scope())
        symbol = vector_parameter_var.get_scope().resolve_to_symbol(
            vector_parameter_var.get_complete_name(), SymbolKind.VARIABLE)
        vector_param = ""
        if symbol is not None:
            # size parameter is a variable
            vector_param += self.print_origin(symbol) + vector_parameter
        else:
            # size parameter is an integer
            vector_param += vector_parameter

        return vector_param
示例#7
0
    def convert_name_reference(self, variable: ASTVariable, prefix='') -> str:
        """
        Converts a single variable to nest processable format.
        :param variable: a single variable.
        :type variable: ASTVariable
        :return: a nest processable format.
        """
        from pynestml.codegeneration.nest_printer import NestPrinter

        if isinstance(variable, ASTExternalVariable):
            _name = str(variable)
            if variable.get_alternate_name():
                # the disadvantage of this approach is that the time the value is to be obtained is not explicitly specified, so we will actually get the value at the end of the min_delay timestep
                return "((POST_NEURON_TYPE*)(__target))->get_" + variable.get_alternate_name(
                ) + "()"

            return "((POST_NEURON_TYPE*)(__target))->get_" + _name + "(_tr_t)"

        if variable.get_name() == PredefinedVariables.E_CONSTANT:
            return 'numerics::e'

        symbol = variable.get_scope().resolve_to_symbol(
            variable.get_complete_name(), SymbolKind.VARIABLE)
        if symbol is None:
            # test if variable name can be resolved to a type
            if PredefinedUnits.is_unit(variable.get_complete_name()):
                return str(
                    UnitConverter.get_factor(
                        PredefinedUnits.get_unit(
                            variable.get_complete_name()).get_unit()))

            code, message = Messages.get_could_not_resolve(variable.get_name())
            Logger.log_message(log_level=LoggingLevel.ERROR,
                               code=code,
                               message=message,
                               error_position=variable.get_source_position())
            return ''

        if symbol.is_local():
            return variable.get_name() + (
                '[' + variable.get_vector_parameter() +
                ']' if symbol.has_vector_parameter() else '')
        if symbol.is_buffer():
            if isinstance(symbol.get_type_symbol(), UnitTypeSymbol):
                units_conversion_factor = UnitConverter.get_factor(
                    symbol.get_type_symbol().unit.unit)
            else:
                units_conversion_factor = 1
            s = ""
            if not units_conversion_factor == 1:
                s += "(" + str(units_conversion_factor) + " * "
            s += NestPrinter.print_origin(
                symbol,
                prefix=prefix) + NestNamesConverter.buffer_value(symbol)
            if symbol.has_vector_parameter():
                s += '[' + variable.get_vector_parameter() + ']'
            if not units_conversion_factor == 1:
                s += ")"
            return s

        if symbol.is_inline_expression:
            return 'get_' + variable.get_name() + '()' + (
                '[i]' if symbol.has_vector_parameter() else '')

        if symbol.is_kernel():
            assert False, "NEST reference converter cannot print kernel; kernel should have been converted during code generation"

        if symbol.is_state():
            temp = ""
            temp += NestNamesConverter.getter(symbol) + "()"
            temp += ('[' + variable.get_vector_parameter() +
                     ']' if symbol.has_vector_parameter() else '')
            return temp

        variable_name = NestNamesConverter.convert_to_cpp_name(
            variable.get_complete_name())
        if symbol.is_local():
            return variable_name + ('[i]'
                                    if symbol.has_vector_parameter() else '')

        if symbol.is_inline_expression:
            return 'get_' + variable_name + '()' + (
                '[i]' if symbol.has_vector_parameter() else '')

        return NestPrinter.print_origin(symbol, prefix=prefix) + \
            NestNamesConverter.name(symbol) + \
            ('[' + variable.get_vector_parameter() + ']' if symbol.has_vector_parameter() else '')