Exemple #1
0
 def print_buffer_getter(cls, ast_buffer, is_in_struct=False):
     """
     Returns a string representation declaring a buffer getter as required in nest.
     :param ast_buffer: a single variable symbol representing a buffer.
     :type ast_buffer: VariableSymbol
     :param is_in_struct: indicates whether this getter is used in a struct or not
     :type is_in_struct: bool
     :return: a string representation of the getter.
     :rtype: str
     """
     assert (ast_buffer is not None and isinstance(ast_buffer, VariableSymbol)), \
         '(PyNestMl.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     assert (is_in_struct is not None and isinstance(is_in_struct, bool)), \
         '(PyNestMl.CodeGeneration.Printer) No or wrong type of is-in-struct provided (%s)!' % type(is_in_struct)
     declaration = 'inline '
     if ast_buffer.has_vector_parameter():
         declaration += 'std::vector<'
         declaration += PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol())
         declaration += '> &'
     else:
         declaration += PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol()) + '&'
     declaration += ' get_' + ast_buffer.get_symbol_name() + '() {'
     if is_in_struct:
         declaration += 'return ' + ast_buffer.get_symbol_name() + ';'
     else:
         declaration += 'return B_.get_' + ast_buffer.get_symbol_name() + '();'
     declaration += '}'
     return declaration
Exemple #2
0
 def print_buffer_getter(cls, ast_buffer, is_in_struct=False):
     """
     Returns a string representation declaring a buffer getter as required in nest.
     :param ast_buffer: a single variable symbol representing a buffer.
     :type ast_buffer: VariableSymbol
     :param is_in_struct: indicates whether this getter is used in a struct or not
     :type is_in_struct: bool
     :return: a string representation of the getter.
     :rtype: str
     """
     assert (ast_buffer is not None and isinstance(ast_buffer, VariableSymbol)), \
         '(PyNestMl.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     assert (is_in_struct is not None and isinstance(is_in_struct, bool)), \
         '(PyNestMl.CodeGeneration.Printer) No or wrong type of is-in-struct provided (%s)!' % type(is_in_struct)
     declaration = 'inline '
     if ast_buffer.has_vector_parameter():
         declaration += 'std::vector<'
         declaration += PyNestml2NestTypeConverter.convert(
             ast_buffer.get_type_symbol())
         declaration += '> &'
     else:
         declaration += PyNestml2NestTypeConverter.convert(
             ast_buffer.get_type_symbol()) + '&'
     declaration += ' get_' + ast_buffer.get_symbol_name() + '() {'
     if is_in_struct:
         declaration += 'return ' + ast_buffer.get_symbol_name() + ';'
     else:
         declaration += 'return B_.get_' + ast_buffer.get_symbol_name(
         ) + '();'
     declaration += '}'
     return declaration
Exemple #3
0
 def print_function_declaration(cls, ast_function):
     """
     Returns a nest processable function declaration head, i.e. the part which appears in the .h file.
     :param ast_function: a single function.
     :type ast_function: ASTFunction
     :return: the corresponding string representation.
     :rtype: str
     """
     from pynestml.meta_model.ast_function import ASTFunction
     from pynestml.symbols.symbol import SymbolKind
     assert (ast_function is not None and isinstance(ast_function, ASTFunction)), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_function provided (%s)!' % type(ast_function)
     function_symbol = ast_function.get_scope().resolve_to_symbol(
         ast_function.get_name(), SymbolKind.FUNCTION)
     if function_symbol is None:
         raise RuntimeError('Cannot resolve the method ' +
                            ast_function.get_name())
     declaration = ast_function.print_comment('//') + '\n'
     declaration += PyNestml2NestTypeConverter.convert(
         function_symbol.get_return_type()).replace('.', '::')
     declaration += ' '
     declaration += ast_function.get_name() + '('
     for typeSym in function_symbol.get_parameter_types():
         declaration += PyNestml2NestTypeConverter.convert(typeSym)
         if function_symbol.get_parameter_types().index(typeSym) < len(
                 function_symbol.get_parameter_types()) - 1:
             declaration += ', '
     declaration += ') const\n'
     return declaration
Exemple #4
0
 def print_function_declaration(cls, ast_function):
     """
     Returns a nest processable function declaration head, i.e. the part which appears in the .h file.
     :param ast_function: a single function.
     :type ast_function: ASTFunction
     :return: the corresponding string representation.
     :rtype: str
     """
     from pynestml.meta_model.ast_function import ASTFunction
     from pynestml.symbols.symbol import SymbolKind
     assert (ast_function is not None and isinstance(ast_function, ASTFunction)), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_function provided (%s)!' % type(ast_function)
     function_symbol = ast_function.get_scope().resolve_to_symbol(ast_function.get_name(), SymbolKind.FUNCTION)
     if function_symbol is not None:
         declaration = ast_function.print_comment('//') + '\n'
         declaration += PyNestml2NestTypeConverter.convert(function_symbol.get_return_type()).replace('.', '::')
         declaration += ' '
         declaration += ast_function.get_name() + '('
         for typeSym in function_symbol.get_parameter_types():
             declaration += PyNestml2NestTypeConverter.convert(typeSym)
             if function_symbol.get_parameter_types().index(typeSym) < len(
                     function_symbol.get_parameter_types()) - 1:
                 declaration += ', '
         declaration += ')\n'
         return declaration
     else:
         raise RuntimeError('Cannot resolve the method ' + ast_function.get_name())
Exemple #5
0
 def print_buffer_declaration(cls, ast_buffer):
     """
     Returns a string representation for the declaration of a buffer.
     :param ast_buffer: a single buffer variable symbol
     :type ast_buffer: VariableSymbol
     :return: the corresponding string representation
     :rtype: str
     """
     assert isinstance(ast_buffer, VariableSymbol), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     if ast_buffer.has_vector_parameter():
         buffer_type = 'std::vector< ' + PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol()) + ' >'
     else:
         buffer_type = PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol())
     buffer_type.replace(".", "::")
     return buffer_type + " " + ast_buffer.get_symbol_name()
Exemple #6
0
 def print_buffer_declaration(cls, ast_buffer):
     """
     Returns a string representation for the declaration of a buffer.
     :param ast_buffer: a single buffer variable symbol
     :type ast_buffer: VariableSymbol
     :return: the corresponding string representation
     :rtype: str
     """
     assert isinstance(ast_buffer, VariableSymbol), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     if ast_buffer.has_vector_parameter():
         buffer_type = 'std::vector< ' + PyNestml2NestTypeConverter.convert(
             ast_buffer.get_type_symbol()) + ' >'
     else:
         buffer_type = PyNestml2NestTypeConverter.convert(
             ast_buffer.get_type_symbol())
     buffer_type.replace(".", "::")
     return buffer_type + " " + ast_buffer.get_symbol_name()
Exemple #7
0
 def print_function_definition(cls, ast_function, namespace):
     """
     Returns a nest processable function definition, i.e. the part which appears in the .c file.
     :param ast_function: a single function.
     :type ast_function: ASTFunction
     :param namespace: the namespace in which this function is defined in
     :type namespace: str
     :return: the corresponding string representation.
     :rtype: str
     """
     assert isinstance(ast_function, ASTFunction), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_function provided (%s)!' % type(ast_function)
     assert isinstance(namespace, str), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of namespace provided (%s)!' % type(namespace)
     function_symbol = ast_function.get_scope().resolve_to_symbol(
         ast_function.get_name(), SymbolKind.FUNCTION)
     if function_symbol is not None:
         # first collect all parameters
         params = list()
         for param in ast_function.get_parameters():
             params.append(param.get_name())
         declaration = ast_function.print_comment('//') + '\n'
         declaration += PyNestml2NestTypeConverter.convert(
             function_symbol.get_return_type()).replace('.', '::')
         declaration += ' '
         if namespace is not None:
             declaration += namespace + '::'
         declaration += ast_function.get_name() + '('
         for typeSym in function_symbol.get_parameter_types():
             # create the type name combination, e.g. double Tau
             declaration += PyNestml2NestTypeConverter.convert(typeSym) + ' ' + \
                            params[function_symbol.get_parameter_types().index(typeSym)]
             # if not the last component, separate by ','
             if function_symbol.get_parameter_types().index(typeSym) < \
                     len(function_symbol.get_parameter_types()) - 1:
                 declaration += ', '
         declaration += ')\n'
         return declaration
     else:
         raise RuntimeError('Cannot resolve the method ' +
                            ast_function.get_name())
Exemple #8
0
 def print_function_definition(cls, ast_function, namespace):
     """
     Returns a nest processable function definition, i.e. the part which appears in the .c file.
     :param ast_function: a single function.
     :type ast_function: ASTFunction
     :param namespace: the namespace in which this function is defined in
     :type namespace: str
     :return: the corresponding string representation.
     :rtype: str
     """
     assert isinstance(ast_function, ASTFunction), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_function provided (%s)!' % type(ast_function)
     assert isinstance(namespace, str), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of namespace provided (%s)!' % type(namespace)
     function_symbol = ast_function.get_scope().resolve_to_symbol(ast_function.get_name(), SymbolKind.FUNCTION)
     if function_symbol is not None:
         # first collect all parameters
         params = list()
         for param in ast_function.get_parameters():
             params.append(param.get_name())
         declaration = ast_function.print_comment('//') + '\n'
         declaration += PyNestml2NestTypeConverter.convert(function_symbol.get_return_type()).replace('.', '::')
         declaration += ' '
         if namespace is not None:
             declaration += namespace + '::'
         declaration += ast_function.get_name() + '('
         for typeSym in function_symbol.get_parameter_types():
             # create the type name combination, e.g. double Tau
             declaration += PyNestml2NestTypeConverter.convert(typeSym) + ' ' + \
                            params[function_symbol.get_parameter_types().index(typeSym)]
             # if not the last component, separate by ','
             if function_symbol.get_parameter_types().index(typeSym) < \
                     len(function_symbol.get_parameter_types()) - 1:
                 declaration += ', '
         declaration += ')\n'
         return declaration
     else:
         raise RuntimeError('Cannot resolve the method ' + ast_function.get_name())
Exemple #9
0
 def print_buffer_array_getter(self, ast_buffer):
     """
     Returns a string containing the nest declaration for a multi-receptor spike buffer.
     :param ast_buffer: a single buffer Variable Symbol
     :type ast_buffer: VariableSymbol
     :return: a string representation of the getter
     :rtype: str
     """
     assert (ast_buffer is not None and isinstance(ast_buffer, VariableSymbol)), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     if ast_buffer.is_spike_buffer() and ast_buffer.is_inhibitory() and ast_buffer.is_excitatory():
         return 'inline ' + PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol()) + '&' + ' get_' \
                + ast_buffer.get_symbol_name() + '() {' + \
                '  return spike_inputs_[' + ast_buffer.get_symbol_name().upper() + ' - 1]; }'
     else:
         return self.print_buffer_getter(ast_buffer, True)
Exemple #10
0
 def print_buffer_array_getter(self, ast_buffer):
     """
     Returns a string containing the nest declaration for a multi-receptor spike buffer.
     :param ast_buffer: a single buffer Variable Symbol
     :type ast_buffer: VariableSymbol
     :return: a string representation of the getter
     :rtype: str
     """
     assert (ast_buffer is not None and isinstance(ast_buffer, VariableSymbol)), \
         '(PyNestML.CodeGeneration.Printer) No or wrong type of ast_buffer symbol provided (%s)!' % type(ast_buffer)
     if ast_buffer.is_spike_buffer() and ast_buffer.is_inhibitory(
     ) and ast_buffer.is_excitatory():
         return 'inline ' + PyNestml2NestTypeConverter.convert(ast_buffer.get_type_symbol()) + '&' + ' get_' \
                + ast_buffer.get_symbol_name() + '() {' + \
                '  return spike_inputs_[' + ast_buffer.get_symbol_name().upper() + ' - 1]; }'
     else:
         return self.print_buffer_getter(ast_buffer, True)