Skip to content

Commit 1677c91

Browse files
authored
Merge pull request #367 from Shaikh-Ubaid/improve_chr_ord_impl
Improve chr ord impl
2 parents 36a1b57 + 9f2f1b2 commit 1677c91

File tree

4 files changed

+177
-28
lines changed

4 files changed

+177
-28
lines changed

integration_tests/test_builtin.py

Lines changed: 161 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,172 @@
11
from ltypes import i32
22

33
def test_ord():
4-
i: i32
54
s: str
6-
s = "1"
7-
i = ord(s)
8-
assert i == 49
9-
assert ord("1") == 49
5+
6+
exclamation_unicode: i32
7+
s = '!'
8+
exclamation_unicode = ord(s)
9+
assert(ord('!') == 33) # testing compile time implementation
10+
assert(exclamation_unicode == 33) # testing runtime implementation
11+
12+
dollar_unicode: i32
13+
s = '$'
14+
dollar_unicode = ord(s)
15+
assert(ord('$') == 36) # testing compile time implementation
16+
assert(dollar_unicode == 36) # testing runtime implementation
17+
18+
left_parenthesis_unicode: i32
19+
s = '('
20+
left_parenthesis_unicode = ord(s)
21+
assert(ord('(') == 40) # testing compile time implementation
22+
assert(left_parenthesis_unicode == 40) # testing runtime implementation
23+
24+
plus_unicode: i32
25+
s = '+'
26+
plus_unicode = ord(s)
27+
assert(ord('+') == 43) # testing compile time implementation
28+
assert(plus_unicode == 43) # testing runtime implementation
29+
30+
zero_unicode: i32
31+
s = '0'
32+
zero_unicode = ord(s)
33+
assert(ord('0') == 48) # testing compile time implementation
34+
assert(zero_unicode == 48) # testing runtime implementation
35+
36+
nine_unicode: i32
37+
s = '9'
38+
nine_unicode = ord(s)
39+
assert(ord('9') == 57) # testing compile time implementation
40+
assert(nine_unicode == 57) # testing runtime implementation
41+
42+
semicolon_unicode: i32
43+
s = ';'
44+
semicolon_unicode = ord(s)
45+
assert(ord(';') == 59) # testing compile time implementation
46+
assert(semicolon_unicode == 59) # testing runtime implementation
47+
48+
capital_a_unicode: i32
49+
s = 'A'
50+
capital_a_unicode = ord(s)
51+
assert(ord('A') == 65) # testing compile time implementation
52+
assert(capital_a_unicode == 65) # testing runtime implementation
53+
54+
capital_z_unicode: i32
55+
s = 'Z'
56+
capital_z_unicode = ord(s)
57+
assert(ord('Z') == 90) # testing compile time implementation
58+
assert(capital_z_unicode == 90) # testing runtime implementation
59+
60+
right_bracket_unicode: i32
61+
s = ']'
62+
right_bracket_unicode = ord(s)
63+
assert(ord(']') == 93) # testing compile time implementation
64+
assert(right_bracket_unicode == 93) # testing runtime implementation
65+
66+
small_a_unicode: i32
67+
s = 'a'
68+
small_a_unicode = ord(s)
69+
assert(ord('a') == 97) # testing compile time implementation
70+
assert(small_a_unicode == 97) # testing runtime implementation
71+
72+
small_z_unicode: i32
73+
s = 'z'
74+
small_z_unicode = ord(s)
75+
assert(ord('z') == 122) # testing compile time implementation
76+
assert(small_z_unicode == 122) # testing runtime implementation
77+
78+
right_brace_unicode: i32
79+
s = '}'
80+
right_brace_unicode = ord(s)
81+
assert(ord('}') == 125) # testing compile time implementation
82+
assert(right_brace_unicode == 125) # testing runtime implementation
1083

1184

1285
def test_chr():
13-
i: i32
14-
i = 48
15-
s: str
16-
s = chr(i)
17-
assert s == "0"
18-
assert chr(48) == "0"
86+
i: i32
87+
88+
exclamation: str
89+
i = 33
90+
exclamation = chr(i)
91+
assert(chr(33) == '!') # testing compile time implementation
92+
assert(exclamation == '!') # testing runtime implementation
93+
94+
dollar: str
95+
i = 36
96+
dollar = chr(i)
97+
assert(chr(36) == '$') # testing compile time implementation
98+
assert(dollar == '$') # testing runtime implementation
99+
100+
left_parenthesis: str
101+
i = 40
102+
left_parenthesis = chr(i)
103+
assert(chr(40) == '(') # testing compile time implementation
104+
assert(left_parenthesis == '(') # testing runtime implementation
105+
106+
plus: str
107+
i = 43
108+
plus = chr(i)
109+
assert(chr(43) == '+') # testing compile time implementation
110+
assert(plus == '+') # testing runtime implementation
111+
112+
zero: str
113+
i = 48
114+
zero = chr(i)
115+
assert(chr(48) == '0') # testing compile time implementation
116+
assert(zero == '0') # testing runtime implementation
117+
118+
nine: str
119+
i = 57
120+
nine = chr(i)
121+
assert(chr(57) == '9') # testing compile time implementation
122+
assert(nine == '9') # testing runtime implementation
123+
124+
semicolon: str
125+
i = 59
126+
semicolon = chr(i)
127+
assert(chr(59) == ';') # testing compile time implementation
128+
assert(semicolon == ';') # testing runtime implementation
129+
130+
capital_a: str
131+
i = 65
132+
capital_a = chr(i)
133+
assert(chr(65) == 'A') # testing compile time implementation
134+
assert(capital_a == 'A') # testing runtime implementation
135+
136+
capital_z: str
137+
i = 90
138+
capital_z = chr(i)
139+
assert(chr(90) == 'Z') # testing compile time implementation
140+
assert(capital_z == 'Z') # testing runtime implementation
141+
142+
right_bracket: str
143+
i = 93
144+
right_bracket = chr(i)
145+
assert(chr(93) == ']') # testing compile time implementation
146+
assert(right_bracket == ']') # testing runtime implementation
147+
148+
small_a: str
149+
i = 97
150+
small_a = chr(i)
151+
assert(chr(97) == 'a') # testing compile time implementation
152+
assert(small_a == 'a') # testing runtime implementation
153+
154+
small_z: str
155+
i = 122
156+
small_z = chr(i)
157+
assert(chr(122) == 'z') # testing compile time implementation
158+
assert(small_z == 'z') # testing runtime implementation
159+
160+
right_brace: str
161+
i = 125
162+
right_brace = chr(i)
163+
assert(chr(125) == '}') # testing compile time implementation
164+
assert(right_brace == '}') # testing runtime implementation
19165

20166

21167
test_ord()
22168
test_chr()
169+
170+
171+
172+

src/runtime/lpython_builtin.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
#from sys import exit
33

44

5-
def ord(s: str) -> i32:
5+
def ord(s: str) -> i32: # currently supports characters with unicode value between 32 to 126
66
"""
77
Returns an integer representing the Unicode code
88
point of a given unicode character. This is the inverse of `chr()`.
99
"""
10-
if s == '0':
11-
return 48
12-
elif s == '1':
13-
return 49
14-
# else:
15-
# exit(1)
10+
if len(s) != 1:
11+
return -1 # not a character
12+
i: i32
13+
for i in range(32, 127):
14+
if chr(i) == s:
15+
return i
1616

1717

18-
def chr(i: i32) -> str:
18+
def chr(i: i32) -> str: # currently supports unicode values between 32 to 126
1919
"""
2020
Returns the string representing a unicode character from
2121
the given Unicode code point. This is the inverse of `ord()`.
2222
"""
23-
if i == 48:
24-
return '0'
25-
elif i == 49:
26-
return '1'
27-
# else:
28-
# exit(1)
23+
if i < 32 or i > 126:
24+
return "Not yet supported"
25+
all_chars: str
26+
all_chars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
27+
return all_chars[i - 32]
2928

3029

3130
#: abs() as a generic procedure.

tests/reference/asr-test_builtin-aa64615.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-test_builtin-aa64615",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/test_builtin.py",
5-
"infile_hash": "3007954574ed4dc6170c6927213e68ac5b560bebbead4d91ac8f0c1f",
5+
"infile_hash": "b027cd7ebfa6c0ea9d5ebf92fa75ca18f630bb5d3698b37d004bdbb3",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_builtin-aa64615.stdout",
9-
"stdout_hash": "c3b35af2d61f1e854f13af498a40eb281043b41b018a3c3fa902f096",
9+
"stdout_hash": "98fea42b1f37b978289b9b6974dcb9712972c3807673f0afd33ef36a",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)