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
185 lines (159 loc) · 3.69 KB
/
Copy pathlpython_builtin.py
File metadata and controls
185 lines (159 loc) · 3.69 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
from ltypes import i32, f64
#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) -> f64:
"""
Returns x**y.
"""
return 1.0*x**y
def int(f: f64) -> i32:
"""
Converts a floating point number to an integer.
"""
pass # handled by LLVM
def float(i: i32) -> f64:
"""
Converts an integer to a floating point number.
"""
pass # handled by LLVM
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]
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