Skip to content

Commit f79d2eb

Browse files
authored
Remove unnecessary functions from CodePrinter (50 lines) (pyccel#1864)
Remove `_get_comment` and `_get_statement` from `CodePrinter`. `_get_comment` is unused and `_get_statement` returns the string that is passed to it. Additionally some docstrings and f-strings are added.
1 parent fda3f0b commit f79d2eb

File tree

5 files changed

+38
-50
lines changed

5 files changed

+38
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ All notable changes to this project will be documented in this file.
122122
- \[INTERNALS\] Removed unused and undocumented function `get_function_from_ast`.
123123
- \[INTERNALS\] Remove function `Module.set_name`.
124124
- \[INTERNALS\] Remove unused `assign_to` argument of `CodePrinter.doprint`.
125+
- \[INTERNALS\] Remove unnecessary functions from `CodePrinter` : `_get_statement`, `_get_comment`.
125126

126127
## \[1.11.2\] - 2024-03-05
127128

pyccel/codegen/printing/ccode.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,6 @@ def add_import(self, import_obj):
333333
elif import_obj.target:
334334
self._additional_imports[import_obj.source].define_target(import_obj.target)
335335

336-
def _get_statement(self, codestring):
337-
return "%s;\n" % codestring
338-
339-
def _get_comment(self, text):
340-
return "// {0}\n".format(text)
341-
342336
def _format_code(self, lines):
343337
return self.indent_code(lines)
344338

pyccel/codegen/printing/codeprinter.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ def _print(self, expr):
8989
return obj
9090
return self._print_not_supported(expr)
9191

92-
def _get_statement(self, codestring):
93-
"""Formats a codestring with the proper line ending."""
94-
raise NotImplementedError("This function must be implemented by "
95-
"subclass of CodePrinter.")
96-
97-
def _get_comment(self, text):
98-
"""Formats a text string as a comment."""
99-
raise NotImplementedError("This function must be implemented by "
100-
"subclass of CodePrinter.")
101-
10292
def _declare_number_const(self, name, value):
10393
"""Declare a numeric constant at the top of a function"""
10494
raise NotImplementedError("This function must be implemented by "

pyccel/codegen/printing/fcode.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373

7474

7575
# TODO: add examples
76-
# TODO: use _get_statement when returning a string
7776

7877
__all__ = ["FCodePrinter", "fcode"]
7978

@@ -308,9 +307,6 @@ def get_function(self, name):
308307
errors.report(UNDEFINED_FUNCTION, symbol=name,
309308
severity='fatal')
310309

311-
def _get_statement(self, codestring):
312-
return codestring
313-
314310
def _format_code(self, lines):
315311
return self._wrap_fortran(self.indent_code(lines))
316312

@@ -684,7 +680,7 @@ def _print_Import(self, expr):
684680

685681
# in some cases, the source is given as a string (when using metavar)
686682
code = code.replace("'", '')
687-
return self._get_statement(code) + '\n'
683+
return code + '\n'
688684

689685
def _print_PythonPrint(self, expr):
690686
end = LiteralString('\n')
@@ -887,8 +883,8 @@ def _get_print_format_and_arg(self, var, var_code = None):
887883

888884
def _print_SymbolicPrint(self, expr):
889885
# for every expression we will generate a print
890-
code = '\n'.join("print *, 'sympy> {}'".format(a) for a in expr.expr)
891-
return self._get_statement(code) + '\n'
886+
code = '\n'.join(f"print *, 'sympy> {a}'" for a in expr.expr)
887+
return code + '\n'
892888

893889
def _print_Comment(self, expr):
894890
comments = self._print(expr.text)
@@ -1409,7 +1405,7 @@ def _print_PythonMin(self, expr):
14091405
else:
14101406
code = ','.join(self._print(arg) for arg in args)
14111407
code = 'min('+code+')'
1412-
return self._get_statement(code)
1408+
return code
14131409

14141410
def _print_PythonMax(self, expr):
14151411
args = expr.args
@@ -1419,7 +1415,7 @@ def _print_PythonMax(self, expr):
14191415
else:
14201416
code = ','.join(self._print(arg) for arg in args)
14211417
code = 'max('+code+')'
1422-
return self._get_statement(code)
1418+
return code
14231419

14241420
# ... MACROS
14251421
def _print_MacroShape(self, expr):
@@ -1651,7 +1647,7 @@ def _print_AliasAssign(self, expr):
16511647
op=op,
16521648
rhs=self._print(expr.rhs))
16531649

1654-
return self._get_statement(code) + '\n'
1650+
return code + '\n'
16551651

16561652
def _print_CodeBlock(self, expr):
16571653
if not expr.unravelled:
@@ -1780,7 +1776,7 @@ def _print_Assign(self, expr):
17801776
# else:
17811777
# print('code_args > {0}'.format(code_args))
17821778
# code = 'call {0}({1})'.format(rhs_code, code_args)
1783-
return self._get_statement(code) + '\n'
1779+
return code + '\n'
17841780

17851781
#------------------------------------------------------------------------------
17861782
def _print_Allocate(self, expr):
@@ -2263,17 +2259,17 @@ def _print_ACC_Parallel(self, expr):
22632259
body = ''.join(self._print(i) for i in expr.body)
22642260

22652261
# ... TODO adapt get_statement to have continuation with OpenACC
2266-
prolog = '!$acc parallel {clauses}\n'.format(clauses=clauses)
2262+
prolog = f'!$acc parallel {clauses}\n'
22672263
epilog = '!$acc end parallel\n'
22682264
# ...
22692265

22702266
# ...
2271-
code = ('{prolog}'
2272-
'{body}'
2273-
'{epilog}').format(prolog=prolog, body=body, epilog=epilog)
2267+
code = (f'{prolog}'
2268+
f'{body}'
2269+
f'{epilog}')
22742270
# ...
22752271

2276-
return self._get_statement(code)
2272+
return code
22772273

22782274
def _print_ACC_For(self, expr):
22792275
# ...
@@ -2282,17 +2278,17 @@ def _print_ACC_For(self, expr):
22822278
# ...
22832279

22842280
# ... TODO adapt get_statement to have continuation with OpenACC
2285-
prolog = '!$acc loop {clauses}\n'.format(clauses=clauses)
2281+
prolog = f'!$acc loop {clauses}\n'
22862282
epilog = '!$acc end loop\n'
22872283
# ...
22882284

22892285
# ...
2290-
code = ('{prolog}'
2291-
'{loop}'
2292-
'{epilog}').format(prolog=prolog, loop=loop, epilog=epilog)
2286+
code = (f'{prolog}'
2287+
f'{loop}'
2288+
f'{epilog}')
22932289
# ...
22942290

2295-
return self._get_statement(code)
2291+
return code
22962292

22972293
def _print_ACC_Async(self, expr):
22982294
args = ', '.join('{0}'.format(self._print(i)) for i in expr.variables)
@@ -2744,8 +2740,8 @@ def _print_NumpyUfuncBase(self, expr):
27442740
args = [self._print(NumpyFloat(a) if isinstance(a.dtype.primitive_type, PrimitiveIntegerType) else a)\
27452741
for a in expr.args]
27462742
code_args = ', '.join(args)
2747-
code = '{0}({1})'.format(func_name, code_args)
2748-
return self._get_statement(code)
2743+
code = f'{func_name}({code_args})'
2744+
return code
27492745

27502746
def _print_NumpyIsInf(self, expr):
27512747
code = PyccelAssociativeParenthesis(PyccelAnd(
@@ -2884,8 +2880,8 @@ def _print_NumpySqrt(self, expr):
28842880
if isinstance(dtype, (PrimitiveIntegerType, PrimitiveBooleanType)):
28852881
arg = NumpyFloat(arg)
28862882
code_args = self._print(arg)
2887-
code = 'sqrt({})'.format(code_args)
2888-
return self._get_statement(code)
2883+
code = f'sqrt({code_args})'
2884+
return code
28892885

28902886
def _print_LiteralImaginaryUnit(self, expr):
28912887
""" purpose: print complex numbers nicely in Fortran."""

pyccel/codegen/printing/luacode.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,20 @@
198198
errors = Errors()
199199

200200
class LuaCodePrinter(CodePrinter):
201-
"""A printer to convert python expressions to strings of Lua code"""
201+
"""
202+
A printer for printing code in Lua.
203+
204+
A printer to convert Pyccel's AST to strings of Python code.
205+
As for all printers the navigation of this file is done via _print_X
206+
functions.
207+
208+
This file is entirely untested and should probably be removed.
209+
210+
Parameters
211+
----------
212+
settings : dict
213+
The settings.
214+
"""
202215
printmethod = "_lua_code"
203216
language = "Lua"
204217

@@ -228,12 +241,6 @@ def __init__(self, settings={}):
228241
def _rate_index_position(self, p):
229242
return p*5
230243

231-
def _get_statement(self, codestring):
232-
return "%s" % codestring
233-
234-
def _get_comment(self, text):
235-
return "-- %s" % text
236-
237244
def _declare_number_const(self, name, value):
238245
return "const %s: f64 = %s" % (name, value)
239246

@@ -456,9 +463,9 @@ def _print_Assign(self, expr):
456463
local_vars = [str(x) for x in local_vars]
457464

458465
if lhs_code in local_vars:
459-
return ("local %s = %s" % (lhs_code, rhs_code))
466+
return f"local {lhs_code} = {rhs_code}"
460467
else:
461-
return self._get_statement("%s = %s" % (lhs_code, rhs_code))
468+
return f"{lhs_code} = {rhs_code}"
462469

463470
def _print_AugAssign(self, expr):
464471
lhs_code = self._print(expr.lhs)

0 commit comments

Comments
 (0)