Skip to content

Commit

Permalink
split up NESTMLPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
C.A.P. Linssen committed Sep 1, 2024
1 parent a25a199 commit 002395f
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 160 deletions.
4 changes: 2 additions & 2 deletions pynestml/codegeneration/printers/cpp_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def _print_cpp_name(cls, variable_name: str) -> str:
:param variable_name: a single name.
:return: a string representation
"""
differential_order = variable_name.count("\"")
differential_order = variable_name.count("'")
if differential_order > 0:
return variable_name.replace("\"", "").replace("$", "__DOLLAR") + "__" + "d" * differential_order
return variable_name.replace("'", "").replace("$", "__DOLLAR") + "__" + "d" * differential_order

return variable_name.replace("$", "__DOLLAR")

Expand Down
2 changes: 0 additions & 2 deletions pynestml/codegeneration/printers/nest_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

from __future__ import annotations

from pynestml.utils.ast_utils import ASTUtils

from pynestml.codegeneration.nest_code_generator_utils import NESTCodeGeneratorUtils
from pynestml.codegeneration.printers.cpp_variable_printer import CppVariablePrinter
from pynestml.codegeneration.printers.expression_printer import ExpressionPrinter
Expand Down
143 changes: 143 additions & 0 deletions pynestml/codegeneration/printers/nestml_expression_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# -*- coding: utf-8 -*-
#
# nestml_expression_printer.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from pynestml.codegeneration.printers.expression_printer import ExpressionPrinter
from pynestml.meta_model.ast_arithmetic_operator import ASTArithmeticOperator
from pynestml.meta_model.ast_comparison_operator import ASTComparisonOperator
from pynestml.meta_model.ast_expression import ASTExpression
from pynestml.meta_model.ast_logical_operator import ASTLogicalOperator
from pynestml.meta_model.ast_node import ASTNode
from pynestml.meta_model.ast_unary_operator import ASTUnaryOperator


class NESTMLExpressionPrinter(ExpressionPrinter):
r"""
Printer for ``ASTExpression`` nodes in NESTML syntax.
"""

def print(self, node: ASTNode) -> str:
if isinstance(node, ASTExpression):
if node.get_implicit_conversion_factor() and not node.get_implicit_conversion_factor() == 1:
return "(" + str(node.get_implicit_conversion_factor()) + " * (" + self.print_expression(node) + "))"

return self.print_expression(node)

if isinstance(node, ASTArithmeticOperator):
return self.print_arithmetic_operator(node)

if isinstance(node, ASTUnaryOperator):
return self.print_unary_operator(node)

if isinstance(node, ASTComparisonOperator):
return self.print_comparison_operator(node)

if isinstance(node, ASTLogicalOperator):
return self.print_logical_operator(node)

return self._simple_expression_printer.print(node)

def print_logical_operator(self, node: ASTLogicalOperator) -> str:
if node.is_logical_and:
return " and "

if node.is_logical_or:
return " or "

raise Exception("Unknown logical operator")

def print_comparison_operator(self, node: ASTComparisonOperator) -> str:
if node.is_lt:
return " < "

if node.is_le:
return " <= "

if node.is_eq:
return " == "

if node.is_ne:
return " != "

if node.is_ne2:
return " <> "

if node.is_ge:
return " >= "

if node.is_gt:
return " > "

raise RuntimeError("(PyNestML.ComparisonOperator.Print) Type of comparison operator not specified!")

def print_unary_operator(self, node: ASTUnaryOperator) -> str:
if node.is_unary_plus:
return "+"

if node.is_unary_minus:
return "-"

if node.is_unary_tilde:
return "~"

raise RuntimeError("Type of unary operator not specified!")

def print_arithmetic_operator(self, node: ASTArithmeticOperator) -> str:
if node.is_times_op:
return " * "

if node.is_div_op:
return " / "

if node.is_modulo_op:
return " % "

if node.is_plus_op:
return " + "

if node.is_minus_op:
return " - "

if node.is_pow_op:
return " ** "

raise RuntimeError("(PyNestML.ArithmeticOperator.Print) Arithmetic operator not specified.")

def print_expression(self, node: ASTExpression) -> str:
ret = ""
if node.is_expression():
if node.is_encapsulated:
ret += "("
if node.is_logical_not:
ret += "not "
if node.is_unary_operator():
ret += self.print(node.get_unary_operator())
ret += self.print(node.get_expression())
if node.is_encapsulated:
ret += ")"
elif node.is_compound_expression():
ret += self.print(node.get_lhs())
ret += self.print(node.get_binary_operator())
ret += self.print(node.get_rhs())
elif node.is_ternary_operator():
ret += self.print(node.get_condition()) + "?" + self.print(
node.get_if_true()) + ":" + self.print(node.get_if_not())

return ret
40 changes: 40 additions & 0 deletions pynestml/codegeneration/printers/nestml_function_call_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
#
# nestml_function_call_printer.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from pynestml.codegeneration.printers.function_call_printer import FunctionCallPrinter
from pynestml.meta_model.ast_function_call import ASTFunctionCall


class NESTMLFunctionCallPrinter(FunctionCallPrinter):
r"""
Printer for ASTFunctionCall in C++ syntax.
"""

def print_function_call(self, node: ASTFunctionCall) -> str:
ret = str(node.get_name()) + "("
for i in range(0, len(node.get_args())):
ret += self._expression_printer.print(node.get_args()[i])
if i < len(node.get_args()) - 1: # in the case that it is not the last arg, print also a comma
ret += ","

ret += ")"

return ret
Loading

0 comments on commit 002395f

Please sign in to comment.