Ejemplo n.º 1
0
def add_declaration_to_internals(neuron: ASTNeuron, variable_name: str,
                                 init_expression: str) -> ASTNeuron:
    """
    Adds the variable as stored in the declaration tuple to the neuron. The declared variable is of type real.
    :param neuron: a single neuron instance
    :param variable_name: the name of the variable to add
    :param init_expression: initialization expression
    :return: the neuron extended by the variable
    """
    tmp = ModelParser.parse_expression(init_expression)
    vector_variable = ASTUtils.get_vectorized_variable(tmp, neuron.get_scope())

    declaration_string = variable_name + ' real' + (
        '[' + vector_variable.get_vector_parameter() + ']' if
        vector_variable is not None and vector_variable.has_vector_parameter()
        else '') + ' = ' + init_expression
    ast_declaration = ModelParser.parse_declaration(declaration_string)
    if vector_variable is not None:
        ast_declaration.set_size_parameter(
            vector_variable.get_vector_parameter())
    neuron.add_to_internal_block(ast_declaration)
    ast_declaration.update_scope(neuron.get_internals_blocks().get_scope())
    symtable_visitor = ASTSymbolTableVisitor()
    symtable_visitor.block_type_stack.push(BlockType.INTERNALS)
    ast_declaration.accept(symtable_visitor)
    symtable_visitor.block_type_stack.pop()
    return neuron
Ejemplo n.º 2
0
 def add_timestep_symbol(cls, neuron: ASTNeuron) -> None:
     """
     Add timestep variable to the internals block
     """
     assert neuron.get_initial_value(
         "__h"
     ) is None, "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
     assert not "__h" in [
         sym.name for sym in neuron.get_internal_symbols()
     ], "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
     neuron.add_to_internal_block(
         ModelParser.parse_declaration('__h ms = resolution()'), index=0)