forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpython_builtin.py
More file actions
316 lines (269 loc) · 5.91 KB
/
Copy pathlpython_builtin.py
File metadata and controls
316 lines (269 loc) · 5.91 KB
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
309
310
311
312
313
314
315
316
from ltypes import i32, i64, f32, f64, c32, c64, overload
#from sys import exit
def ord(s: str) -> i32:
"""
Returns an integer representing the Unicode code
point of a given unicode character. This is the inverse of `chr()`.
"""
if s == '0':
return 48
elif s == '1':
return 49
# else:
# exit(1)
def chr(i: i32) -> str:
"""
Returns the string representing a unicode character from
the given Unicode code point. This is the inverse of `ord()`.
"""
if i == 48:
return '0'
elif i == 49:
return '1'
# else:
# exit(1)
#: abs() as a generic procedure.
#: supported types for argument:
#: i32, f32, f64, bool, c32, c64
@overload
def abs(x: f64) -> f64:
"""
Return the absolute value of `x`.
"""
if x >= 0.0:
return x
else:
return -x
@overload
def abs(x: f32) -> f32:
if x >= 0.0:
return x
else:
return -x
@overload
def abs(x: i32) -> i64:
if x >= 0:
return x
else:
return -x
@overload
def abs(b: bool) -> i32:
if b:
return 1
else:
return 0
@overload
def abs(c: c32) -> f32:
a: f32
b: f32
a = c.real
b = _lfortran_caimag(c)
return (a**2 + b**2)**(1/2)
@overload
def abs(c: c64) -> f64:
a: f64
b: f64
a = c.real
b = _lfortran_zaimag(c)
return (a**2 + b**2)**(1/2)
def str(x: i32) -> str:
"""
Return the string representation of an integer `x`.
"""
if x == 0:
return '0'
result: str
result = ''
if x < 0:
result += '-'
x = -x
rev_result: str
rev_result = ''
rev_result_len: i32
rev_result_len = 0
pos_to_str: list[str]
pos_to_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
while x > 0:
rev_result += pos_to_str[x - (x//10)*10]
rev_result_len += 1
x = x//10
pos: i32
for pos in range(rev_result_len - 1, -1, -1):
result += rev_result[pos]
return result
@overload
def bool(x: i32) -> bool:
"""
Return False when the argument `x` is 0, True otherwise.
"""
return x != 0
@overload
def bool(x: i64) -> bool:
return x != 0
@overload
def bool(f: f32) -> bool:
return f != 0.0
@overload
def bool(f: f64) -> bool:
"""
Return False when the argument `x` is 0.0, True otherwise.
"""
return f != 0.0
@overload
def bool(s: str) -> bool:
"""
Return False when the argument `s` is an empty string, True otherwise.
"""
return len(s) > 0
@overload
def bool(b: bool) -> bool:
return b
@overload
def bool(c: c32) -> bool:
pass
@overload
def bool(c: c64) -> bool:
# TODO: implement once we can access `real` and `imag` attributes
pass
def len(s: str) -> i32:
"""
Return the length of the string `s`.
"""
pass
#: pow() as a generic procedure.
#: supported types for arguments:
#: (i32, i32), (f64, f64), (i32, f64), (f64, i32)
@overload
def pow(x: i32, y: i32) -> i32:
"""
Returns x**y.
"""
return x**y
@overload
def pow(x: f64, y: f64) -> f64:
"""
Returns x**y.
"""
return x**y
@overload
def pow(x: i32, y: f64) -> f64:
return x**y
@overload
def pow(x: f64, y: i32) -> f64:
return x**y
def bin(n: i32) -> str:
"""
Returns the binary representation of an integer `n`.
"""
if n == 0:
return '0b0'
prep: str
prep = '0b'
if n < 0:
n = -n
prep = '-0b'
res: str
res = ''
res += '0' if (n - (n//2)*2) == 0 else '1'
while n > 1:
n = n//2
res += '0' if (n - (n//2)*2) == 0 else '1'
return prep + res[::-1]
def hex(n: i32) -> str:
"""
Returns the hexadecimal representation of an integer `n`.
"""
hex_values: list[str]
hex_values = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
if n == 0:
return '0x0'
prep: str
prep = '0x'
if n < 0:
prep = '-0x'
n = -n
res: str
res = ""
remainder: i32
while n > 0:
remainder = n - (n//16)*16
n -= remainder
n = n//16
res += hex_values[remainder]
return prep + res[::-1]
def oct(n: i32) -> str:
"""
Returns the octal representation of an integer `n`.
"""
_values: list[str]
_values = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
if n == 0:
return '0o0'
prep: str
prep = '0o'
if n < 0:
prep = '-0o'
n = -n
res: str
res = ""
remainder: i32
while n > 0:
remainder = n - (n//8)*8
n -= remainder
n = n//8
res += _values[remainder]
return prep + res[::-1]
#: round() as a generic procedure.
#: supported types for argument:
#: i32, f64, bool
@overload
def round(value: f64) -> i32:
"""
Rounds a floating point number to the nearest integer.
"""
if abs(value - int(value)) <= 0.5:
return int(value)
else:
return int(value) + 1
@overload
def round(value: i32) -> i64:
return value
@overload
def round(b: bool) -> i32:
return abs(b)
#: complex() as a generic procedure.
#: supported types for arguments:
#: (i32, i32), (f64, f64), (i32, f64), (f64, i32)
@overload
def complex(x: f64, y: f64) -> c64:
pass
@overload
def complex(x: i32, y: i32) -> c64:
pass
@overload
def complex(x: i32, y: f64) -> c64:
pass
@overload
def complex(x: f64, y: i32) -> c64:
pass
def divmod(x: i32, y: i32) -> tuple[i32, i32]:
#: TODO: Implement once we have tuple support in the LLVM backend
pass
def lbound(x: i32[:], dim: i32) -> i32:
pass
def ubound(x: i32[:], dim: i32) -> i32:
pass
@ccall
def _lfortran_caimag(x: c32) -> f32:
pass
@ccall
def _lfortran_zaimag(x: c64) -> f64:
pass
@overload
def _lpython_imag(x: c64) -> f64:
return _lfortran_zaimag(x)
@overload
def _lpython_imag(x: c32) -> f32:
return _lfortran_caimag(x)