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
91 lines (77 loc) · 1.76 KB
/
Copy pathlpython_builtin.py
File metadata and controls
91 lines (77 loc) · 1.76 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
from ltypes import i32
#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)
# This is an implementation for f64.
# TODO: implement abs() as a generic procedure, and implement for all types
def abs(x: f64) -> f64:
"""
Return the absolute value of `x`.
"""
if x >= 0.0:
return x
else:
return -x
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
def bool(x: i32) -> bool:
"""
Return False when the argument `x` is 0, True otherwise.
"""
if x == 0:
return False
else:
return True
def len(s: str) -> i32:
"""
Return the length of the string `s`.
"""
pass
def pow(x: i32, y: i32) -> i32:
"""
Returns x**y.
"""
return x**y