Example #1
0
 def getter(cls, variable_symbol):
     """
     Converts for a handed over symbol the corresponding name of the getter to a gsl processable format.
     :param variable_symbol: a single variable symbol.
     :type variable_symbol: VariableSymbol
     :return: the corresponding representation as a string
     :rtype: str
     """
     return NestNamesConverter.getter(variable_symbol)
Example #2
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 '')