pyccel.ast.operators module#
Module handling all Python builtin operators These operators all have a precedence as detailed here: https://docs.python.org/3/reference/expressions.html#operator-precedence They also have specific rules to determine the dtype, rank, shape, class_type
- class pyccel.ast.operators.IfTernaryOperator(cond, value_true, value_false)[source]#
Bases:
PyccelOperator
Represent a ternary conditional operator in the code.
Represent a ternary conditional operator in the code, of the form (a if cond else b).
- Parameters:
cond (TypedAstNode) – The condition which determines which result is returned.
value_true (TypedAstNode) – The value returned if the condition is true.
value_false (TypedAstNode) – The value returned if the condition is false.
Examples
>>> from pyccel.ast.internals import PyccelSymbol >>> from pyccel.ast.core import Assign >>> from pyccel.ast.operators import IfTernaryOperator >>> n = PyccelSymbol('n') >>> x = 5 if n > 1 else 2 >>> IfTernaryOperator(PyccelGt(n > 1), 5, 2) IfTernaryOperator(PyccelGt(n > 1), 5, 2)
- property cond#
The condition property for IfTernaryOperator class
- property value_false#
The value_if_cond_false property for IfTernaryOperator class
- property value_true#
The value_if_cond_true property for IfTernaryOperator class
- class pyccel.ast.operators.PyccelAdd(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python addition operator.
Class representing a call to the Python addition operator. I.e:
a + b
is equivalent to: >>> PyccelAdd(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelAnd(*args, simplify=False)[source]#
Bases:
PyccelBooleanOperator
Class representing a call to the Python AND operator.
Class representing a call to the Python AND operator. I.e:
a and b
is equivalent to: >>> PyccelAnd(a, b)
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelArithmeticOperator(arg1, arg2)[source]#
Bases:
PyccelBinaryOperator
Abstract superclass representing a Python arithmetic operator.
This class is necessary to handle specific precedence rules for arithmetic operators. I.e. to handle the error: Extension: Unary operator following arithmetic operator (use parentheses).
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelAssociativeParenthesis(arg)[source]#
Bases:
PyccelUnaryOperator
Class representing parentheses.
Class representing parentheses around an expression to group ideas or to ensure the execution order of the code.
- Parameters:
arg (TypedAstNode) – The argument in the PyccelAssociativeParenthesis.
- class pyccel.ast.operators.PyccelBinaryOperator(arg1, arg2)[source]#
Bases:
PyccelOperator
Superclass representing a Python operator with two arguments.
Abstract superclass representing a Python operator with two arguments.
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelBooleanOperator(*args)[source]#
Bases:
PyccelOperator
Superclass representing a boolean operator with two arguments.
Abstract superclass representing a Python boolean operator with two arguments.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
- class pyccel.ast.operators.PyccelComparisonOperator(arg1, arg2)[source]#
Bases:
PyccelBinaryOperator
Superclass representing a Python comparison operator.
Abstract superclass representing a Python comparison operator with two arguments.
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelDiv(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python division operator.
Class representing a call to the Python division operator. I.e:
a / b
is equivalent to: >>> PyccelDiv(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelEq(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python equality operator.
Class representing a call to the Python equality operator. I.e:
a == b
is equivalent to: >>> PyccelEq(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- op = '=='#
- class pyccel.ast.operators.PyccelFloorDiv(arg1, arg2)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python integer division operator.
Class representing a call to the Python integer division operator. I.e:
a // b
is equivalent to: >>> PyccelFloorDiv(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelGe(arg1, arg2)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python greater or equal operator.
Class representing a call to the Python greater or equal operator. I.e:
a >= b
is equivalent to: >>> PyccelGe(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- op = '>='#
- class pyccel.ast.operators.PyccelGt(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python greater than operator.
Class representing a call to the Python greater than operator. I.e:
a > b
is equivalent to: >>> PyccelGt(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- op = '>'#
- class pyccel.ast.operators.PyccelIn(element, container)[source]#
Bases:
PyccelBooleanOperator
Represents an in expression in the code.
Represents an in expression in the code.
- Parameters:
element (TypedAstNode) – The first argument passed to the operator.
container (TypedAstNode) – The first argument passed to the operator.
- property container#
Second operator argument.
Second operator argument.
- property element#
First operator argument.
First operator argument.
- class pyccel.ast.operators.PyccelIs(*args)[source]#
Bases:
PyccelBooleanOperator
Represents an is expression in the code.
Represents an is expression in the code.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
Examples
>>> from pyccel.ast.operators import PyccelIs >>> from pyccel.ast.literals import Nil >>> from pyccel.ast.internals import PyccelSymbol >>> x = PyccelSymbol('x') >>> PyccelIs(x, Nil()) PyccelIs(x, None)
- eval()[source]#
Determines the value of the expression x is None when x is known.
If a boolean value cannot be computed, return the string “unknown”.
- property lhs#
First operator argument
- property rhs#
First operator argument
- class pyccel.ast.operators.PyccelIsNot(*args)[source]#
Bases:
PyccelIs
Represents a is not expression in the code.
Represents a is not expression in the code.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
Examples
>>> from pyccel.ast.operators import PyccelIsNot >>> from pyccel.ast.literals import Nil >>> from pyccel.ast.internals import PyccelSymbol >>> x = PyccelSymbol('x') >>> PyccelIsNot(x, Nil()) PyccelIsNot(x, None)
- class pyccel.ast.operators.PyccelLe(arg1, arg2)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python less or equal operator.
Class representing a call to the Python less or equal operator. I.e:
a <= b
is equivalent to: >>> PyccelLe(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- op = '<='#
- class pyccel.ast.operators.PyccelLt(arg1, arg2)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python less than operator.
Class representing a call to the Python less than operator. I.e:
a < b
is equivalent to: >>> PyccelLt(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- op = '<'#
- class pyccel.ast.operators.PyccelMinus(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python subtraction operator.
Class representing a call to the Python subtraction operator. I.e:
a - b
is equivalent to: >>> PyccelMinus(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelMod(arg1, arg2)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python modulo operator.
Class representing a call to the Python modulo operator. I.e:
a % b
is equivalent to: >>> PyccelMod(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelMul(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python multiplication operator.
Class representing a call to the Python multiplication operator. I.e:
a * b
is equivalent to: >>> PyccelMul(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelNe(arg1=None, arg2=None, simplify=False)[source]#
Bases:
PyccelComparisonOperator
Class representing a call to the Python inequality operator.
Class representing a call to the Python inequality operator. I.e:
a != b
is equivalent to: >>> PyccelNe(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- op = '!='#
- class pyccel.ast.operators.PyccelNot(arg)[source]#
Bases:
PyccelUnaryOperator
Class representing a call to the Python not operator.
Class representing a call to the Python not operator. I.e:
not a
is equivalent to: >>> PyccelNot(a).
- Parameters:
arg (TypedAstNode) – The argument passed to the operator.
- class pyccel.ast.operators.PyccelOperator(*args)[source]#
Bases:
TypedAstNode
Abstract superclass for all builtin operators.
Abstract superclass for all builtin operators. The __init__ function is common but the functions called by __init__ are specialised.
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
- property args#
Arguments of the operator
- property precedence#
The precedence of the operator.
The precedence of the operator as defined here: https://docs.python.org/3/reference/expressions.html#operator-precedence The precedence shows the order in which operators should be handled. In this file it is represented as an integer. The higher the integer value of the precedence, the higher the priority of the operator.
- Returns:
The precedence of the operator.
- Return type:
int
- class pyccel.ast.operators.PyccelOr(*args, simplify=False)[source]#
Bases:
PyccelBooleanOperator
Class representing a call to the Python OR operator.
Class representing a call to the Python OR operator. I.e:
a or b
is equivalent to: >>> PyccelOr(a, b)
- Parameters:
*args (tuple of TypedAstNode) – The arguments passed to the operator.
simplify (bool) – True if the expression should be simplified to be as compact/readable as possible. False if the arguments should be preserved as they are.
- class pyccel.ast.operators.PyccelPow(arg1, arg2)[source]#
Bases:
PyccelArithmeticOperator
Class representing a call to the Python exponent operator.
Class representing a call to the Python exponent operator. I.e:
a ** b
is equivalent to: >>> PyccelPow(a, b)
- Parameters:
arg1 (TypedAstNode) – The first argument passed to the operator.
arg2 (TypedAstNode) – The second argument passed to the operator.
- class pyccel.ast.operators.PyccelUnary(arg)[source]#
Bases:
PyccelUnaryOperator
Class representing a call to the Python positive operator.
Class representing a call to the Python positive operator. I.e:
+a
is equivalent to: >>> PyccelUnary(a)
- Parameters:
arg (TypedAstNode) – The argument passed to the operator.
- class pyccel.ast.operators.PyccelUnaryOperator(arg)[source]#
Bases:
PyccelOperator
Superclass representing an operator with only one argument.
Abstract superclass representing a Python operator with only one argument.
- Parameters:
arg (TypedAstNode) – The argument passed to the operator.
- class pyccel.ast.operators.PyccelUnarySub(arg)[source]#
Bases:
PyccelUnary
Class representing a call to the Python negative operator.
Class representing a call to the Python negative operator. I.e:
-a
is equivalent to: >>> PyccelUnarySub(a)
- Parameters:
arg (TypedAstNode) – The argument passed to the operator.