Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_codegen(f):
pyccel = Parser(f)
ast = pyccel.parse()
settings = {}
ast = pyccel.annotate(**settings)
name = os.path.basename(f)
name = os.path.splitext(name)[0]
codegen = Codegen(ast, name)
codegen.doprint(language='python')
# reset Errors singleton
errors = Errors()
errors.reset()
######################
def test_syntax_blockers(f):
# reset Errors singleton
errors = Errors()
errors.reset()
pyccel = Parser(f)
with pytest.raises(PyccelSyntaxError):
ast = pyccel.parse()
assert(errors.is_blockers())
def test_codegen(f):
pyccel = Parser(f)
ast = pyccel.parse()
settings = {}
ast = pyccel.annotate(**settings)
name = os.path.basename(f)
name = os.path.splitext(name)[0]
codegen = Codegen(ast, name)
codegen.doprint()
# reset Errors singleton
errors = Errors()
errors.reset()
def parse(self, verbose=False):
"""converts python ast to sympy ast."""
if self.syntax_done:
print ('> syntax analysis already done')
return self.ast
# TODO - add settings to Errors
# - filename
errors = Errors()
if self.filename:
errors.set_target(self.filename, 'file')
errors.set_parser_stage('syntax')
PyccelAstNode.stage = 'syntactic'
ast = self._visit(self.fst)
self._ast = ast
self._visit_done = True
return ast
def annotate(self, **settings):
"""."""
if self.semantic_done:
print ('> semantic analysis already done')
return self.ast
# TODO - add settings to Errors
# - filename
errors = Errors()
if self.filename:
errors.set_target(self.filename, 'file')
errors.set_parser_stage('semantic')
# then we treat the current file
ast = self.ast
# we add the try/except to allow the parser to find all possible errors
PyccelAstNode.stage = 'semantic'
ast = self._visit(ast, **settings)
self._ast = ast
# in the case of a header file, we need to convert all headers to
# FunctionDef etc ...
from pyccel.ast.core import PyccelPow, PyccelAdd, PyccelMul, PyccelDiv, PyccelMod, PyccelFloorDiv
from pyccel.ast.core import PyccelEq, PyccelNe, PyccelLt, PyccelLe, PyccelGt, PyccelGe
from pyccel.ast.core import PyccelAnd, PyccelOr, PyccelNot, PyccelMinus
from pyccel.ast.builtins import Range
from pyccel.ast.core import Declare
from pyccel.ast.core import SeparatorComment
from pyccel.codegen.printing.codeprinter import CodePrinter
from pyccel.errors.errors import Errors
from pyccel.errors.messages import *
errors = Errors()
# TODO: add examples
__all__ = ["CCodePrinter", "ccode"]
# dictionary mapping sympy function to (argument_conditions, C_function).
# Used in CCodePrinter._print_Function(self)
known_functions = {
"Abs": [(lambda x: not x.is_integer, "fabs")],
"gamma": "tgamma",
"sin" : "sin",
"cos" : "cos",
"tan" : "tan",
"asin" : "asin",
"acos" : "acos",
"atan" : "atan",
from sympy.core import Mul, Pow, S
from sympy.core.compatibility import default_sort_key
from sympy.core.sympify import _sympify
from sympy.core.mul import _keep_coeff
from sympy.printing.str import StrPrinter
from pyccel.ast.core import Assign
from pyccel.errors.errors import Errors
from pyccel.errors.messages import PYCCEL_RESTRICTION_TODO
# TODO: add examples
__all__ = ["CodePrinter"]
errors = Errors()
class CodePrinter(StrPrinter):
"""
The base class for code-printing subclasses.
"""
_operators = {
'and': '&&',
'or': '||',
'not': '!',
}
def doprint(self, expr, assign_to=None):
"""
Print the expression as code.
def _infere_type(self, expr, **settings):
"""
type inference for expressions
"""
# TODO - add settings to Errors
# - line and column
# - blocking errors
errors = Errors()
verbose = settings.pop('verbose', False)
if verbose:
print ('*** type inference for : ', type(expr))
d_var = {}
# TODO improve => put settings as attribut of Parser
if expr in (PythonInt, PythonFloat, PythonComplex, PythonBool, NumpyInt,
Int32, Int64, NumpyComplex, Complex64, Complex128, NumpyFloat,
Float64, Float32):
d_var['datatype' ] = '*'
d_var['rank' ] = 0
d_var['precision' ] = 0
return d_var
folder = None,
language = None,
compiler = None,
mpi_compiler = None,
fflags = None,
includes = (),
libdirs = (),
modules = (),
libs = (),
debug = False,
extra_args = '',
accelerator = None,
output_name = None):
# Reset Errors singleton before parsing a new file
errors = Errors()
errors.reset()
# TODO [YG, 03.02.2020]: test validity of function arguments
# Copy list arguments to local lists to avoid unexpected behavior
includes = [*includes]
libdirs = [*libdirs]
modules = [*modules]
libs = [*libs]
# Store current directory and add it to sys.path
# variable to imitate Python's import behavior
base_dirpath = os.getcwd()
sys.path.insert(0, base_dirpath)
# Unified way to handle errors: print formatted error message, then move
stage=self.parser_stage)
for path in self.error_info_map.keys():
errors = self.error_info_map[path]
if print_path: text += ' filename :: {path}\n'.format(path=path)
for err in errors:
text += ' ' + str(err) + '\n'
return text
if __name__ == '__main__':
from pyccel.errors.messages import NO_RETURN_VALUE_EXPECTED
from pyccel.errors.messages import INCOMPATIBLE_RETURN_VALUE_TYPE
from pyccel.errors.messages import UNDEFINED_VARIABLE
errors = Errors()
errors.set_parser_stage('semantic')
errors.set_target('script.py', 'file')
errors.report(NO_RETURN_VALUE_EXPECTED, severity='warning', line=24)
errors.set_target('make_knots', 'function')
errors.report(INCOMPATIBLE_RETURN_VALUE_TYPE, symbol='y', severity='error',
line=34, column=17)
errors.unset_target('function')
errors.check()
errors.set_target('eval_bsplines', 'function')
errors.report(UNDEFINED_VARIABLE, symbol='x', severity='error', blocker=True)
errors.unset_target('function')