FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lpython/src/runtime/lpython_builtin.py at main · kinshukdua/lpython · GitHub
kinshukdua
/
lpython
Public
forked from
lcompilers/lpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lpython
/
src
/
runtime
/
lpython_builtin.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
561 lines (467 loc) · 10.2 KB
Breadcrumbs
lpython
/
src
/
runtime
/
lpython_builtin.py
Copy path
File metadata and controls
561 lines (467 loc) · 10.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
from
ltypes
import
i8
,
i16
,
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:
#: i8, i16, i32, i64, 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
:
i8
)
->
i8
:
if
x
>=
0
:
return
x
else
:
return
-
x
@
overload
def
abs
(
x
:
i16
)
->
i16
:
if
x
>=
0
:
return
x
else
:
return
-
x
@
overload
def
abs
(
x
:
i32
)
->
i32
:
if
x
>=
0
:
return
x
else
:
return
-
x
@
overload
def
abs
(
x
:
i64
)
->
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
-
_lpython_floordiv
(
x
,
10
)
*
10
]
rev_result_len
+=
1
x
=
_lpython_floordiv
(
x
,
10
)
pos
:
i32
for
pos
in
range
(
rev_result_len
-
1
,
-
1
,
-
1
):
result
+=
rev_result
[
pos
]
return
result
#: bool() as a generic procedure.
#: supported types for argument:
#: i8, i16, i32, i64, f32, f64, bool
@
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
(
x
:
i8
)
->
bool
:
return
x
!=
0
@
overload
def
bool
(
x
:
i16
)
->
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
@
interface
def
len
(
s
:
str
)
->
i32
:
"""
Return the length of the string `s`.
"""
pass
#: pow() as a generic procedure.
#: supported types for arguments:
#: (i32, i32), (i64, i64), (f64, f64),
#: (f32, f32), (i32, f64), (f64, i32),
#: (i32, f32), (f32, i32), (bool, bool), (c32, i32)
@
overload
def
pow
(
x
:
i32
,
y
:
i32
)
->
i32
:
"""
Returns x**y.
"""
return
x
**
y
@
overload
def
pow
(
x
:
i64
,
y
:
i64
)
->
i64
:
return
x
**
y
@
overload
def
pow
(
x
:
f32
,
y
:
f32
)
->
f32
:
return
x
**
y
@
overload
def
pow
(
x
:
f64
,
y
:
f64
)
->
f64
:
"""
Returns x**y.
"""
return
x
**
y
@
overload
def
pow
(
x
:
i32
,
y
:
f32
)
->
f32
:
return
x
**
y
@
overload
def
pow
(
x
:
f32
,
y
:
i32
)
->
f32
:
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
@
overload
def
pow
(
x
:
bool
,
y
:
bool
)
->
i32
:
if
y
and
not
x
:
return
0
return
1
@
overload
def
pow
(
c
:
c32
,
y
:
i32
)
->
c32
:
return
c
**
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
-
_lpython_floordiv
(
n
,
2
)
*
2
)
==
0
else
'1'
while
n
>
1
:
n
=
_lpython_floordiv
(
n
,
2
)
res
+=
'0'
if
(
n
-
_lpython_floordiv
(
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
-
_lpython_floordiv
(
n
,
16
)
*
16
n
-=
remainder
n
=
_lpython_floordiv
(
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
-
_lpython_floordiv
(
n
,
8
)
*
8
n
-=
remainder
n
=
_lpython_floordiv
(
n
,
8
)
res
+=
_values
[
remainder
]
return
prep
+
res
[::
-
1
]
#: round() as a generic procedure.
#: supported types for argument:
#: i8, i16, i32, i64, f32, f64, bool
@
overload
def
round
(
value
:
f64
)
->
i32
:
"""
Rounds a floating point number to the nearest integer.
"""
i
:
i32
i
=
int
(
value
)
f
:
f64
f
=
abs
(
value
-
i
)
if
f
<
0.5
:
return
i
elif
f
>
0.5
:
return
i
+
1
else
:
if
i
-
_lpython_floordiv
(
i
,
2
)
*
2
==
0
:
return
i
else
:
return
i
+
1
@
overload
def
round
(
value
:
f32
)
->
i32
:
i
:
i32
i
=
int
(
value
)
f
:
f64
f
=
abs
(
value
-
i
)
if
f
<
0.5
:
return
i
elif
f
>
0.5
:
return
i
+
1
else
:
if
i
-
_lpython_floordiv
(
i
,
2
)
*
2
==
0
:
return
i
else
:
return
i
+
1
@
overload
def
round
(
value
:
i32
)
->
i32
:
return
value
@
overload
def
round
(
value
:
i64
)
->
i64
:
return
value
@
overload
def
round
(
value
:
i8
)
->
i8
:
return
value
@
overload
def
round
(
value
:
i16
)
->
i16
:
return
value
@
overload
def
round
(
b
:
bool
)
->
i32
:
return
abs
(
b
)
#: complex() as a generic procedure.
#: supported types for arguments:
#: (f64, f64), (f32, f64), (f64, f32), (f32, f32),
#: (i32, i32), (i64, i64), (i32, i64), (i64, i32)
@
interface
@
overload
def
complex
(
x
:
f64
,
y
:
f64
)
->
c64
:
"""
Return a complex number with the given real and imaginary parts.
"""
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
f32
,
y
:
f32
)
->
c32
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
f32
,
y
:
f64
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
f64
,
y
:
f32
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
i32
,
y
:
i32
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
i64
,
y
:
i64
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
i32
,
y
:
i64
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
i64
,
y
:
i32
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
i32
,
y
:
f64
)
->
c64
:
return
x
+
y
*
1j
@
interface
@
overload
def
complex
(
x
:
f64
,
y
:
i32
)
->
c64
:
return
x
+
y
*
1j
@
interface
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
)
@
overload
def
_lpython_floordiv
(
a
:
f64
,
b
:
f64
)
->
f64
:
r
:
f64
r
=
a
/
b
result
:
i64
result
=
int
(
r
)
if
r
>=
0.0
or
result
==
r
:
return
float
(
result
)
return
float
(
result
-
1
)
@
overload
def
_lpython_floordiv
(
a
:
f32
,
b
:
f32
)
->
f32
:
r
:
f32
r
=
a
/
b
result
:
i32
result
=
int
(
r
)
if
r
>=
0.0
or
result
==
r
:
return
float
(
result
)
return
float
(
result
-
1
)
@
overload
def
_lpython_floordiv
(
a
:
i32
,
b
:
i32
)
->
i32
:
r
:
f32
r
=
a
/
b
result
:
i32
result
=
int
(
r
)
if
r
>=
0.0
or
result
==
r
:
return
result
return
result
-
1
@
overload
def
_lpython_floordiv
(
a
:
i64
,
b
:
i64
)
->
i64
:
r
:
f64
r
=
a
/
b
result
:
i64
result
=
int
(
r
)
if
r
>=
0.0
or
result
==
r
:
return
result
return
result
-
1
@
overload
def
_mod
(
a
:
i32
,
b
:
i32
)
->
i32
:
return
a
-
_lpython_floordiv
(
a
,
b
)
*
b
@
overload
def
_mod
(
a
:
f32
,
b
:
f32
)
->
f32
:
return
a
-
_lpython_floordiv
(
a
,
b
)
*
b
@
overload
def
_mod
(
a
:
i64
,
b
:
i64
)
->
i64
:
return
a
-
_lpython_floordiv
(
a
,
b
)
*
b
@
overload
def
_mod
(
a
:
f64
,
b
:
f64
)
->
f64
:
return
a
-
_lpython_floordiv
(
a
,
b
)
*
b
@
overload
def
_bitwise_or
(
a
:
i32
,
b
:
i32
)
->
i32
:
pass
@
overload
def
_bitwise_or
(
a
:
i64
,
b
:
i64
)
->
i64
:
pass
@
overload
def
_bitwise_and
(
a
:
i32
,
b
:
i32
)
->
i32
:
pass
@
overload
def
_bitwise_and
(
a
:
i64
,
b
:
i64
)
->
i64
:
pass
@
overload
def
_bitwise_xor
(
a
:
i32
,
b
:
i32
)
->
i32
:
pass
@
overload
def
_bitwise_xor
(
a
:
i64
,
b
:
i64
)
->
i64
:
pass
@
overload
def
_bitwise_lshift
(
a
:
i32
,
b
:
i32
)
->
i32
:
return
a
*
2
**
b
@
overload
def
_bitwise_lshift
(
a
:
i64
,
b
:
i64
)
->
i64
:
return
a
*
2
**
b
@
overload
def
_bitwise_rshift
(
a
:
i32
,
b
:
i32
)
->
i32
:
i
:
i32
i
=
2
return
_lpython_floordiv
(
a
,
i
**
b
)
@
overload
def
_bitwise_rshift
(
a
:
i64
,
b
:
i64
)
->
i64
:
i
:
i64
i
=
2
return
_lpython_floordiv
(
a
,
i
**
b
)
Back
|
FazBrowse Home
|
New Git URL