Skip to content

Commit 002395f

Browse files
author
C.A.P. Linssen
committed
split up NESTMLPrinter
1 parent a25a199 commit 002395f

12 files changed

+343
-160
lines changed

pynestml/codegeneration/printers/cpp_variable_printer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def _print_cpp_name(cls, variable_name: str) -> str:
3434
:param variable_name: a single name.
3535
:return: a string representation
3636
"""
37-
differential_order = variable_name.count("\"")
37+
differential_order = variable_name.count("'")
3838
if differential_order > 0:
39-
return variable_name.replace("\"", "").replace("$", "__DOLLAR") + "__" + "d" * differential_order
39+
return variable_name.replace("'", "").replace("$", "__DOLLAR") + "__" + "d" * differential_order
4040

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

pynestml/codegeneration/printers/nest_variable_printer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
from __future__ import annotations
2323

24-
from pynestml.utils.ast_utils import ASTUtils
25-
2624
from pynestml.codegeneration.nest_code_generator_utils import NESTCodeGeneratorUtils
2725
from pynestml.codegeneration.printers.cpp_variable_printer import CppVariablePrinter
2826
from pynestml.codegeneration.printers.expression_printer import ExpressionPrinter
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# nestml_expression_printer.py
4+
#
5+
# This file is part of NEST.
6+
#
7+
# Copyright (C) 2004 The NEST Initiative
8+
#
9+
# NEST is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 2 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# NEST is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
21+
22+
from pynestml.codegeneration.printers.expression_printer import ExpressionPrinter
23+
from pynestml.meta_model.ast_arithmetic_operator import ASTArithmeticOperator
24+
from pynestml.meta_model.ast_comparison_operator import ASTComparisonOperator
25+
from pynestml.meta_model.ast_expression import ASTExpression
26+
from pynestml.meta_model.ast_logical_operator import ASTLogicalOperator
27+
from pynestml.meta_model.ast_node import ASTNode
28+
from pynestml.meta_model.ast_unary_operator import ASTUnaryOperator
29+
30+
31+
class NESTMLExpressionPrinter(ExpressionPrinter):
32+
r"""
33+
Printer for ``ASTExpression`` nodes in NESTML syntax.
34+
"""
35+
36+
def print(self, node: ASTNode) -> str:
37+
if isinstance(node, ASTExpression):
38+
if node.get_implicit_conversion_factor() and not node.get_implicit_conversion_factor() == 1:
39+
return "(" + str(node.get_implicit_conversion_factor()) + " * (" + self.print_expression(node) + "))"
40+
41+
return self.print_expression(node)
42+
43+
if isinstance(node, ASTArithmeticOperator):
44+
return self.print_arithmetic_operator(node)
45+
46+
if isinstance(node, ASTUnaryOperator):
47+
return self.print_unary_operator(node)
48+
49+
if isinstance(node, ASTComparisonOperator):
50+
return self.print_comparison_operator(node)
51+
52+
if isinstance(node, ASTLogicalOperator):
53+
return self.print_logical_operator(node)
54+
55+
return self._simple_expression_printer.print(node)
56+
57+
def print_logical_operator(self, node: ASTLogicalOperator) -> str:
58+
if node.is_logical_and:
59+
return " and "
60+
61+
if node.is_logical_or:
62+
return " or "
63+
64+
raise Exception("Unknown logical operator")
65+
66+
def print_comparison_operator(self, node: ASTComparisonOperator) -> str:
67+
if node.is_lt:
68+
return " < "
69+
70+
if node.is_le:
71+
return " <= "
72+
73+
if node.is_eq:
74+
return " == "
75+
76+
if node.is_ne:
77+
return " != "
78+
79+
if node.is_ne2:
80+
return " <> "
81+
82+
if node.is_ge:
83+
return " >= "
84+
85+
if node.is_gt:
86+
return " > "
87+
88+
raise RuntimeError("(PyNestML.ComparisonOperator.Print) Type of comparison operator not specified!")
89+
90+
def print_unary_operator(self, node: ASTUnaryOperator) -> str:
91+
if node.is_unary_plus:
92+
return "+"
93+
94+
if node.is_unary_minus:
95+
return "-"
96+
97+
if node.is_unary_tilde:
98+
return "~"
99+
100+
raise RuntimeError("Type of unary operator not specified!")
101+
102+
def print_arithmetic_operator(self, node: ASTArithmeticOperator) -> str:
103+
if node.is_times_op:
104+
return " * "
105+
106+
if node.is_div_op:
107+
return " / "
108+
109+
if node.is_modulo_op:
110+
return " % "
111+
112+
if node.is_plus_op:
113+
return " + "
114+
115+
if node.is_minus_op:
116+
return " - "
117+
118+
if node.is_pow_op:
119+
return " ** "
120+
121+
raise RuntimeError("(PyNestML.ArithmeticOperator.Print) Arithmetic operator not specified.")
122+
123+
def print_expression(self, node: ASTExpression) -> str:
124+
ret = ""
125+
if node.is_expression():
126+
if node.is_encapsulated:
127+
ret += "("
128+
if node.is_logical_not:
129+
ret += "not "
130+
if node.is_unary_operator():
131+
ret += self.print(node.get_unary_operator())
132+
ret += self.print(node.get_expression())
133+
if node.is_encapsulated:
134+
ret += ")"
135+
elif node.is_compound_expression():
136+
ret += self.print(node.get_lhs())
137+
ret += self.print(node.get_binary_operator())
138+
ret += self.print(node.get_rhs())
139+
elif node.is_ternary_operator():
140+
ret += self.print(node.get_condition()) + "?" + self.print(
141+
node.get_if_true()) + ":" + self.print(node.get_if_not())
142+
143+
return ret
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# nestml_function_call_printer.py
4+
#
5+
# This file is part of NEST.
6+
#
7+
# Copyright (C) 2004 The NEST Initiative
8+
#
9+
# NEST is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 2 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# NEST is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
21+
22+
from pynestml.codegeneration.printers.function_call_printer import FunctionCallPrinter
23+
from pynestml.meta_model.ast_function_call import ASTFunctionCall
24+
25+
26+
class NESTMLFunctionCallPrinter(FunctionCallPrinter):
27+
r"""
28+
Printer for ASTFunctionCall in C++ syntax.
29+
"""
30+
31+
def print_function_call(self, node: ASTFunctionCall) -> str:
32+
ret = str(node.get_name()) + "("
33+
for i in range(0, len(node.get_args())):
34+
ret += self._expression_printer.print(node.get_args()[i])
35+
if i < len(node.get_args()) - 1: # in the case that it is not the last arg, print also a comma
36+
ret += ","
37+
38+
ret += ")"
39+
40+
return ret

0 commit comments

Comments
 (0)