예제 #1
0
    def replace_rhs_variable(cls, expr: ASTExpression,
                             variable_name_to_replace: str,
                             kernel_var: ASTVariable, spike_buf: ASTInputPort):
        """
        Replace variable names in definitions of kernel dynamics
        :param expr: expression in which to replace the variables
        :param variable_name_to_replace: variable name to replace in the expression
        :param kernel_var: kernel variable instance
        :param spike_buf: input port instance
        :return:
        """
        def replace_kernel_var(node):
            if type(node) is ASTSimpleExpression \
                    and node.is_variable() \
                    and node.get_variable().get_name() == variable_name_to_replace:
                var_order = node.get_variable().get_differential_order()
                new_variable_name = cls.construct_kernel_X_spike_buf_name(
                    kernel_var.get_name(),
                    spike_buf,
                    var_order - 1,
                    diff_order_symbol="'")
                new_variable = ASTVariable(new_variable_name, var_order)
                new_variable.set_source_position(
                    node.get_variable().get_source_position())
                node.set_variable(new_variable)

        expr.accept(ASTHigherOrderVisitor(visit_funcs=replace_kernel_var))
예제 #2
0
 def create_ast_expression(cls, is_encapsulated=False, unary_operator=None,
                           is_logical_not=False, expression=None, source_position=None):
     # type: (bool,ASTUnaryOperator,bool,ASTExpression|ASTSimpleExpression,ASTSourceLocation) -> ASTExpression
     """
     The factory method used to create rhs which are either encapsulated in parentheses (e.g., (10mV))
     OR have a unary (e.g., ~bitVar), OR are negated (e.g., not logVar), or are simple rhs (e.g., 10mV).
     """
     return ASTExpression(is_encapsulated=is_encapsulated, unary_operator=unary_operator,
                          is_logical_not=is_logical_not, expression=expression, source_position=source_position)
예제 #3
0
 def create_ast_ternary_expression(cls,
                                   condition,  # type: Union(ASTSimpleExpression,ASTExpression)
                                   if_true,  # type: Union(ASTSimpleExpression,ASTExpression)
                                   if_not,  # type: Union(ASTSimpleExpression,ASTExpression)
                                   source_position  # type: ASTSourceLocation
                                   ):  # type: (...) -> ASTExpression
     """
     The factory method used to create a ternary operator rhs, e.g., 10mV<V_m?10mV:V_m
     """
     return ASTExpression(condition=condition, if_true=if_true, if_not=if_not, source_position=source_position)
예제 #4
0
 def create_ast_compound_expression(cls,
                                    lhs,  # type: Union(ASTExpression,ASTSimpleExpression)
                                    binary_operator,
                                    # type: Union(ASTLogicalOperator,ASTBitOperator,ASTComparisonOperator,ASTArithmeticOperator)
                                    rhs,  # type: Union(ASTExpression,ASTSimpleExpression)
                                    source_position  # type: ASTSourceLocation
                                    ):  # type: (...) -> ASTExpression
     """
     The factory method used to create compound expressions, e.g. 10mV + V_m.
     """
     assert (binary_operator is not None and (isinstance(binary_operator, ASTBitOperator)
                                              or isinstance(binary_operator, ASTComparisonOperator)
                                              or isinstance(binary_operator, ASTLogicalOperator)
                                              or isinstance(binary_operator, ASTArithmeticOperator))), \
         '(PyNestML.AST.Expression) No or wrong type of binary operator provided (%s)!' % type(binary_operator)
     return ASTExpression(lhs=lhs, binary_operator=binary_operator, rhs=rhs, source_position=source_position)