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 endvisit_assignment(self, node): scope = node.get_scope() var_name = node.get_variable().get_name() _expr = node.get_expression() var_symbol = scope.resolve_to_symbol(var_name, SymbolKind.VARIABLE) _equals = var_symbol.get_type_symbol().equals(_expr.type) \ or var_symbol.get_type_symbol().differs_only_in_magnitude(_expr.type) message = 'line ' + str(_expr.get_source_position()) + ' : LHS = ' + \ var_symbol.get_type_symbol().get_symbol_name() + \ ' RHS = ' + _expr.type.get_symbol_name() + \ ' Equal ? ' + str(_equals) if isinstance(_expr.type, UnitTypeSymbol): message += " Neuroscience Factor: " + \ str(UnitConverter().get_factor(_expr.type.astropy_unit)) Logger.log_message(error_position=node.get_source_position(), code=MessageCode.TYPE_MISMATCH, message=message, log_level=LoggingLevel.INFO) if _equals is False: Logger.log_message(message="Type mismatch in test!", code=MessageCode.TYPE_MISMATCH, error_position=node.get_source_position(), log_level=LoggingLevel.ERROR) return
def convert_name_reference(self, variable): """ Converts a single variable to nest processable format. :param variable: a single variable. :type variable: ASTVariable :return: a nest processable format. :rtype: str """ from pynestml.codegeneration.nest_printer import NestPrinter assert (variable is not None and isinstance(variable, ASTVariable)), \ '(PyNestML.CodeGeneration.NestReferenceConverter) No or wrong type of uses-gsl provided (%s)!' % type( variable) variable_name = NestNamesConverter.convert_to_cpp_name( variable.get_complete_name()) if variable_name == PredefinedVariables.E_CONSTANT: return 'numerics::e' else: symbol = variable.get_scope().resolve_to_symbol( variable_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_name) Logger.log_message( log_level=LoggingLevel.ERROR, code=code, message=message, error_position=variable.get_source_position()) return '' else: if symbol.is_local(): return variable_name + ( '[i]' if symbol.has_vector_parameter() else '') elif symbol.is_buffer(): return NestPrinter.print_origin(symbol) + NestNamesConverter.buffer_value(symbol) \ + ('[i]' if symbol.has_vector_parameter() else '') else: if symbol.is_function: return 'get_' + variable_name + '()' + ( '[i]' if symbol.has_vector_parameter() else '') else: if symbol.is_init_values(): temp = NestPrinter.print_origin(symbol) if self.uses_gsl: temp += GSLNamesConverter.name(symbol) else: temp += NestNamesConverter.name(symbol) temp += ('[i]' if symbol.has_vector_parameter() else '') return temp else: return NestPrinter.print_origin(symbol) + \ NestNamesConverter.name(symbol) + \ ('[i]' if symbol.has_vector_parameter() else '')
def convert_name_reference(self, ast_variable): """ 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()) symbol = ast_variable.get_scope().resolve_to_symbol( ast_variable.get_complete_name(), SymbolKind.VARIABLE) if PredefinedUnits.is_unit(ast_variable.get_complete_name()): return str( UnitConverter.get_factor( PredefinedUnits.get_unit( ast_variable.get_complete_name()).get_unit())) if symbol.is_init_values(): return GSLNamesConverter.name(symbol) elif symbol.is_buffer(): return 'node.B_.' + NestNamesConverter.buffer_value(symbol) elif variable_name == PredefinedVariables.E_CONSTANT: return 'numerics::e' elif symbol.is_local() or symbol.is_function: return variable_name elif symbol.has_vector_parameter(): return 'node.get_' + variable_name + '()[i]' else: return 'node.get_' + variable_name + '()'
def convert_name_reference(self, ast_variable): """ 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()) symbol = ast_variable.get_scope().resolve_to_symbol(ast_variable.get_complete_name(), SymbolKind.VARIABLE) if PredefinedUnits.is_unit(ast_variable.get_complete_name()): return str( UnitConverter.get_factor(PredefinedUnits.get_unit(ast_variable.get_complete_name()).get_unit())) if symbol.is_init_values(): return GSLNamesConverter.name(symbol) elif symbol.is_buffer(): return 'node.B_.' + NestNamesConverter.buffer_value(symbol) elif variable_name == PredefinedVariables.E_CONSTANT: return 'numerics::e' elif symbol.is_local() or symbol.is_function: return variable_name elif symbol.has_vector_parameter(): return 'node.get_' + variable_name + '()[i]' else: return 'node.get_' + variable_name + '()'
def print_expression(self, node, prefix=''): """Print an expression. Parameters ---------- node : ASTExpressionNode The expression node to print. prefix : str *See documentation for the function ExpressionsPrettyPrinter::print_function_call().* Returns ------- s : str The expression string. """ # todo : printing of literals etc. should be done by constant converter, not a type converter if isinstance(node, ASTSimpleExpression): if node.is_numeric_literal(): return self.types_printer.pretty_print( node.get_numeric_literal()) elif node.is_variable() and node.get_scope() is not None: node_is_variable_symbol = node.get_scope().resolve_to_symbol( node.variable.get_complete_name(), SymbolKind.VARIABLE) is not None if not node_is_variable_symbol and PredefinedUnits.is_unit( node.variable.get_complete_name()): # case for a literal unit, e.g. "ms" return str( UnitConverter.get_factor( PredefinedUnits.get_unit( node.variable.get_complete_name()).get_unit())) return super(UnitlessExpressionPrinter, self).print_expression(node, prefix=prefix)
def convert_name_reference(self, variable): """ Converts a single variable to nest processable format. :param variable: a single variable. :type variable: ASTVariable :return: a nest processable format. :rtype: str """ from pynestml.codegeneration.nest_printer import NestPrinter assert (variable is not None and isinstance(variable, ASTVariable)), \ '(PyNestML.CodeGeneration.NestReferenceConverter) No or wrong type of uses-gsl provided (%s)!' % type( variable) variable_name = NestNamesConverter.convert_to_cpp_name(variable.get_complete_name()) if PredefinedUnits.is_unit(variable.get_complete_name()): return str( UnitConverter.get_factor(PredefinedUnits.get_unit(variable.get_complete_name()).get_unit())) if variable_name == PredefinedVariables.E_CONSTANT: return 'numerics::e' else: symbol = variable.get_scope().resolve_to_symbol(variable_name, SymbolKind.VARIABLE) if symbol is None: # this should actually not happen, but an error message is better than an exception code, message = Messages.get_could_not_resolve(variable_name) Logger.log_message(log_level=LoggingLevel.ERROR, code=code, message=message, error_position=variable.get_source_position()) return '' else: if symbol.is_local(): return variable_name + ('[i]' if symbol.has_vector_parameter() else '') elif symbol.is_buffer(): return NestPrinter.print_origin(symbol) + NestNamesConverter.buffer_value(symbol) \ + ('[i]' if symbol.has_vector_parameter() else '') else: if symbol.is_function: return 'get_' + variable_name + '()' + ('[i]' if symbol.has_vector_parameter() else '') else: if symbol.is_init_values(): temp = NestPrinter.print_origin(symbol) if self.uses_gsl: temp += GSLNamesConverter.name(symbol) else: temp += NestNamesConverter.name(symbol) temp += ('[i]' if symbol.has_vector_parameter() else '') return temp else: return NestPrinter.print_origin(symbol) + \ NestNamesConverter.name(symbol) + \ ('[i]' if symbol.has_vector_parameter() else '')
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 '')
def convert_name_reference(self, variable, prefix='', with_origins = True): """ Converts a single variable to nest processable format. :param variable: a single variable. :type variable: ASTVariable :return: a nest processable format. :rtype: str """ from pynestml.codegeneration.nest_printer import NestPrinter assert (variable is not None and isinstance(variable, ASTVariable)), \ '(PyNestML.CodeGeneration.NestReferenceConverter) No or wrong type of uses-gsl provided (%s)!' % type( variable) variable_name = NestNamesConverter.convert_to_cpp_name(variable.get_complete_name()) if variable_name == PredefinedVariables.E_CONSTANT: return 'numerics::e' assert variable.get_scope() is not None, "Undeclared variable: " + variable.get_complete_name() symbol = variable.get_scope().resolve_to_symbol(variable_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_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_name + ('[i]' 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) if with_origins else '' s += NestNamesConverter.buffer_value(symbol) if symbol.has_vector_parameter(): s += '[i]' if not units_conversion_factor == 1: s += ")" return s if symbol.is_inline_expression: return 'get_' + variable_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 = NestPrinter.print_origin(symbol, prefix=prefix) if with_origins else '' if self.uses_gsl: temp += GSLNamesConverter.name(symbol) else: temp += NestNamesConverter.name(symbol) temp += ('[i]' if symbol.has_vector_parameter() else '') return temp return (NestPrinter.print_origin(symbol, prefix=prefix) if with_origins else '') + \ NestNamesConverter.name(symbol) + \ ('[i]' if symbol.has_vector_parameter() else '')