-
Notifications
You must be signed in to change notification settings - Fork 55
/
bitwise_operators.py
308 lines (249 loc) · 8.92 KB
/
bitwise_operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#
"""
Module handling all Python builtin operators
These operators all have a precision as detailed here:
https://docs.python.org/3/reference/expressions.html#operator-precedence
They also have specific rules to determine the datatype, rank, shape
"""
from .builtins import PythonInt
from .datatypes import PrimitiveBooleanType, PrimitiveIntegerType
from .datatypes import PythonNativeBool, PythonNativeInt
from .operators import PyccelUnaryOperator, PyccelOperator
__all__ = (
'PyccelBitComparisonOperator',
'PyccelBitOperator',
'PyccelBitAnd',
'PyccelBitOr',
'PyccelBitXor',
'PyccelInvert',
'PyccelLShift',
'PyccelRShift',
)
#==============================================================================
class PyccelInvert(PyccelUnaryOperator):
"""
Class representing a call to the Python bitwise not operator.
Class representing a call to the Python bitwise not operator.
I.e:
~a
is equivalent to:
PyccelInvert(a)
Parameters
----------
arg : TypedAstNode
The argument passed to the operator.
"""
__slots__ = ()
_precedence = 14
def _calculate_type(self, arg):
"""
Get the type of the result of the function.
Arguments must be integers or booleans. Any booleans are cast
to integers.
Parameters
----------
arg : TypedAstNode
The argument passed to the operator.
Returns
-------
DataType
The datatype of the result of the operation.
"""
dtype = PythonNativeInt()
assert isinstance(getattr(arg.dtype, 'primitive_type', None), (PrimitiveBooleanType, PrimitiveIntegerType))
self._args = (PythonInt(arg) if arg.dtype is PythonNativeBool() else arg,)
return dtype
def __repr__(self):
return f'~{repr(self.args[0])}'
#==============================================================================
class PyccelBitOperator(PyccelOperator):
"""
Abstract superclass representing a Python bitwise operator with two arguments.
Abstract superclass representing a Python bitwise operator with two arguments.
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
_shape = None
__slots__ = ('_class_type',)
def __init__(self, arg1, arg2):
super().__init__(arg1, arg2)
def _set_order(self):
pass
def _calculate_type(self, arg1, arg2):
"""
Get the type of the result of the function.
If one argument is a string then all arguments must be strings.
If the arguments are numeric then the datatype
matches the broadest type.
e.g.
1 + 2j -> PyccelAdd(LiteralInteger, LiteralComplex) -> complex
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
Returns
-------
DataType
The datatype of the result of the operation.
"""
try:
class_type = arg1.class_type + arg2.class_type
except NotImplementedError as err:
raise TypeError(f'Cannot determine the type of {arg1} {self.op} {arg2}') from err # pylint: disable=no-member
assert isinstance(getattr(class_type, 'primitive_type', None), (PrimitiveBooleanType, PrimitiveIntegerType))
self._args = [PythonInt(a) if a.dtype is PythonNativeBool() else a for a in (arg1, arg2)]
return class_type
def _set_shape(self):
"""
Set the shape of the resulting object.
Set the shape of the resulting object. For a PyccelBitOperator,
the shape is a class attribute so nothing needs to be done.
"""
def __repr__(self):
return f'{self.args[0]} {self.op} {self.args[1]}' # pylint: disable=no-member
#==============================================================================
class PyccelRShift(PyccelBitOperator):
"""
Class representing a call to the Python right shift operator.
Class representing a call to the Python right shift operator.
I.e:
a >> b
is equivalent to:
PyccelRShift(a, b)
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
_precedence = 11
op = ">>"
#==============================================================================
class PyccelLShift(PyccelBitOperator):
"""
Class representing a call to the Python right shift operator.
Class representing a call to the Python right shift operator.
I.e:
a << b
is equivalent to:
PyccelRShift(a, b)
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
_precedence = 11
op = "<<"
#==============================================================================
class PyccelBitComparisonOperator(PyccelBitOperator):
"""
Abstract superclass representing a bitwise comparison operator.
Abstract superclass representing a Python bitwise comparison
operator with two arguments
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
def _calculate_type(self, arg1, arg2):
"""
Get the type of the result of the function.
If one argument is a string then all arguments must be strings.
If the arguments are numeric then the dtype
matches the broadest type.
e.g.
1 + 2j -> PyccelAdd(LiteralInteger, LiteralComplex) -> complex
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
Returns
-------
DataType
The datatype of the result of the operation.
"""
try:
class_type = arg1.class_type & arg2.class_type
except NotImplementedError as err:
raise TypeError(f'Cannot determine the type of {arg1} {self.op} {arg2}') from err # pylint: disable=no-member
primitive_type = class_type.primitive_type
assert isinstance(primitive_type, (PrimitiveBooleanType, PrimitiveIntegerType))
if isinstance(primitive_type, PrimitiveIntegerType):
self._args = [PythonInt(a) if a.dtype is PythonNativeBool() else a for a in (arg1, arg2)]
return class_type
#==============================================================================
class PyccelBitXor(PyccelBitComparisonOperator):
"""
Class representing a call to the Python bitwise XOR operator.
Class representing a call to the Python bitwise XOR operator.
I.e:
a ^ b
is equivalent to:
PyccelBitXor(a, b)
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
_precedence = 9
op = "^"
#==============================================================================
class PyccelBitOr(PyccelBitComparisonOperator):
"""
Class representing a call to the Python bitwise OR operator.
Class representing a call to the Python bitwise OR operator.
I.e:
a | b
is equivalent to:
PyccelBitOr(a, b)
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
_precedence = 8
op = "|"
#==============================================================================
class PyccelBitAnd(PyccelBitComparisonOperator):
"""
Class representing a call to the Python bitwise AND operator.
Class representing a call to the Python bitwise AND operator.
I.e:
a & b
is equivalent to:
PyccelBitAnd(a, b)
Parameters
----------
arg1 : TypedAstNode
The first argument passed to the operator.
arg2 : TypedAstNode
The second argument passed to the operator.
"""
__slots__ = ()
_precedence = 10
op = "&"