How to use the pyccel.ast.core.FunctionDef function in pyccel

To help you get started, we’ve selected a few pyccel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyccel / pyccel / pyccel / ast / core.py View on Github external
if expr.lhs is None:
            raise TypeError('Found None lhs')

        try:
            free_symbols = expr.lhs.free_symbols
            symbols = list(free_symbols)
        except:
            # TODO must raise an Exception here
            #      this occurs only when parsing lapack.pyh
            symbols = []
#            print(type(expr.lhs), expr.lhs)
#            print(expr)
#            raise SystemExit('ERROR')
        return symbols

    elif isinstance(expr, (FunctionDef, For, While)):
        return get_assigned_symbols(expr.body)

    elif isinstance(expr, If):
        return get_assigned_symbols(expr.bodies)

    return []
# ...
github pyccel / pyccel / pyccel / stdlib / clapp / plaf / matrix.py View on Github external
def module(self):
        return 'plf_m_matrix_{0}'.format(self._instance)

    @property
    def dtype(self):
        return 'plf_t_matrix_{0}'.format(self._instance)

    def _sympystr(self, printer):
        sstr = printer.doprint
        return '{}'.format(sstr(self.name))
##########################################################

##########################################################
#                     coo matrix
##########################################################
class Matrix_coo_create(FunctionDef):
    """ Represents a Matrix create procedure. """
    def __new__(cls):
        """
        Represents a call to create for a coo matrix.

        Matrix_coo_create is implemented as a FunctionDef, where the result is
        an instance of Matrix_coo. This is done by specifying the result of the
        create using the DataTypeFactory.
        """
        # ...
        name = 'create'

        cls._name = name
        # ...

        # ...
github pyccel / pyccel / pyccel / functional / ast.py View on Github external
def __new__( cls, name, arguments, results, body, **kwargs ):
        generators = kwargs.pop('generators', {})
        m_results  = kwargs.pop('m_results',   [])

        obj = FunctionDef.__new__(cls, name, arguments, results, body, **kwargs)
        obj._generators = generators
        obj._m_results  = m_results

        return obj
github pyccel / pyccel / pyccel / ast / f2py.py View on Github external
func_alias = func.clone('mod_' + str(func.name))

    # from module import func as func_alias
    imports = [Import(target=AsName(func.name, func_alias.name), source=mod_name)]

    # function arguments
    args = sanitize_arguments(func.arguments)
    # function body
    call    = FunctionCall(func_alias, args)
    results = func.results
    results = results[0] if len(results) == 1 else results
    stmt    = call if len(func.results) == 0 else Assign(results, call)
    body    = [stmt]

    # new function declaration
    new_func = FunctionDef(func.name, list(args), func.results, body,
                       arguments_inout = func.arguments_inout,
                       functions = func.functions,
                       imports = imports,
                       )

    # make it compatible with f2py
    static_func = as_static_function(new_func, name)

    return static_func
github pyccel / pyccel / src_old / syntax_core.py View on Github external
expressions = []
            for i in func.expr:
                expressions += [sympify(i).evalf()]
            expr = Tuple(*expressions)
        else:
            # TODO to treat other cases
            x_out   = Variable('double', 'x_out')
            results = [x_out]

            expr = sympify(func.expr).evalf()

        body = [Assign(x_out, expr)]

        # TODO local_vars must be updated inside FunctionDef
        #      this is needed for _print_FunctionDef
        F = FunctionDef(str(lhs), arguments, results, body, local_vars=arguments)
        namespace[str(lhs)] = F
        return F
    else:
        raise ValueError("Expecting a builtin function. given : ", name)
    # ...
github pyccel / pyccel / pyccel / symbolic / lambdify.py View on Github external
def lambdify(expr, args):
    if isinstance(args, Lambda):
        new_expr = args.expr
        new_expr = Return(new_expr)
        new_expr.set_fst(expr)
        f_arguments = args.variables
        func = FunctionDef('lambda', f_arguments, [], [new_expr])
        return func


    code = compile(args.body[0],'','single')
    g={}
    eval(code,g)
    f_name = str(args.name)
    code = g[f_name]
    new_args = args.arguments
    new_expr = code(*new_args)
    f_arguments = list(new_expr.free_symbols)
    stmts = cse(new_expr)
    if isinstance(stmts[-1], (Assign, GC)):
        var = stmts[-1].lhs
    else:
        var  = create_variable(expr)
github pyccel / pyccel / pyccel / codegen / printing / fcode.py View on Github external
# ... TODO add other elements
        private_funcs = [f.name for f in expr.funcs if f.is_private]
        private = private_funcs
        if private:
            private = ','.join(self._print(i) for i in private)
            private = 'private :: {}'.format(private)
        else:
            private = ''
        # ...

        decs    = expr.declarations
        func_in_func = False
        for func in expr.funcs:
            for i in func.body:
                if isinstance(i, FunctionDef):
                    func_in_func = True
                    break
        if expr.classes or expr.interfaces or func_in_func:
            # TODO shall we use expr.variables? or have a more involved algo
            #      we will need to walk through the expression and see what are
            #      the variables that are needed in the definitions of classes
            variables = []
            for i in expr.interfaces:
                variables += i.functions[0].global_vars
            for i in expr.funcs:
                variables += i.global_vars
            variables =list(set(variables))
            for i in range(len(decs)):
                #remove variables that are declared in the modules
                if decs[i].variable in variables:
                    decs[i] = None
github pyccel / pyccel / pyccel / ast / core.py View on Github external
# ...

    if isinstance(expr, ValuedVariable):
        if expr.variable.name in var:
            return expr.value
    elif isinstance(expr, Variable):

        # expr.cls_base if of type ClassDef

        if expr.cls_base:
            return get_initial_value(expr.cls_base, var)
    elif isinstance(expr, Assign):

        if str(expr.lhs) in var:
            return expr.rhs
    elif isinstance(expr, FunctionDef):

        value = get_initial_value(expr.body, var)
        if not is_None(value):
            r = get_initial_value(expr.arguments, value)
            if 'self._linear' in var:
                print ('>>>> ', var, value, r)
            if not r is None:
                return r
        return value

    elif isinstance(expr, ConstructorCall):

        return get_initial_value(expr.func, var)
    elif isinstance(expr, (list, tuple, Tuple)):

        for i in expr:
github pyccel / pyccel / pyccel / ast / core.py View on Github external
args = (self.name,
                self.arguments,
                self.results,
                self.body,
                self.local_vars,
                self.global_vars,
                self.cls_name,
                self.hide,
                self.kind,
                self.is_static,
                self.imports,
                self.decorators,)
        return args


class SympyFunction(FunctionDef):
    """Represents a function definition."""

    def rename(self, newname):
        """
        Rename the SympyFunction name by creating a new SympyFunction with
        newname.

        newname: str
            new name for the SympyFunction
        """
        return SympyFunction(newname, self.arguments,
                           self.results, self.body,
                           cls_name=self.cls_name)
github pyccel / pyccel / pyccel / ast / core.py View on Github external
def set_recursive(self):
        return FunctionDef(
            self.name,
            self.arguments,
            self.results,
            self.body,
            local_vars=self.local_vars,
            global_vars=self.global_vars,
            cls_name=self.cls_name,
            hide=self.hide,
            kind=self.kind,
            is_static=self.is_static,
            header=self.header,
            imports = self.imports,
            decorators = self.decorators,
            is_recursive=True,
            functions=self.functions,
            )