Skip to content

Commit

Permalink
Merge pull request lcompilers#367 from Shaikh-Ubaid/improve_chr_ord_impl
Browse files Browse the repository at this point in the history
Improve chr ord impl
  • Loading branch information
certik authored Apr 13, 2022
2 parents 36a1b57 + 9f2f1b2 commit 1677c91
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 28 deletions.
172 changes: 161 additions & 11 deletions integration_tests/test_builtin.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,172 @@
from ltypes import i32

def test_ord():
i: i32
s: str
s = "1"
i = ord(s)
assert i == 49
assert ord("1") == 49

exclamation_unicode: i32
s = '!'
exclamation_unicode = ord(s)
assert(ord('!') == 33) # testing compile time implementation
assert(exclamation_unicode == 33) # testing runtime implementation

dollar_unicode: i32
s = '$'
dollar_unicode = ord(s)
assert(ord('$') == 36) # testing compile time implementation
assert(dollar_unicode == 36) # testing runtime implementation

left_parenthesis_unicode: i32
s = '('
left_parenthesis_unicode = ord(s)
assert(ord('(') == 40) # testing compile time implementation
assert(left_parenthesis_unicode == 40) # testing runtime implementation

plus_unicode: i32
s = '+'
plus_unicode = ord(s)
assert(ord('+') == 43) # testing compile time implementation
assert(plus_unicode == 43) # testing runtime implementation

zero_unicode: i32
s = '0'
zero_unicode = ord(s)
assert(ord('0') == 48) # testing compile time implementation
assert(zero_unicode == 48) # testing runtime implementation

nine_unicode: i32
s = '9'
nine_unicode = ord(s)
assert(ord('9') == 57) # testing compile time implementation
assert(nine_unicode == 57) # testing runtime implementation

semicolon_unicode: i32
s = ';'
semicolon_unicode = ord(s)
assert(ord(';') == 59) # testing compile time implementation
assert(semicolon_unicode == 59) # testing runtime implementation

capital_a_unicode: i32
s = 'A'
capital_a_unicode = ord(s)
assert(ord('A') == 65) # testing compile time implementation
assert(capital_a_unicode == 65) # testing runtime implementation

capital_z_unicode: i32
s = 'Z'
capital_z_unicode = ord(s)
assert(ord('Z') == 90) # testing compile time implementation
assert(capital_z_unicode == 90) # testing runtime implementation

right_bracket_unicode: i32
s = ']'
right_bracket_unicode = ord(s)
assert(ord(']') == 93) # testing compile time implementation
assert(right_bracket_unicode == 93) # testing runtime implementation

small_a_unicode: i32
s = 'a'
small_a_unicode = ord(s)
assert(ord('a') == 97) # testing compile time implementation
assert(small_a_unicode == 97) # testing runtime implementation

small_z_unicode: i32
s = 'z'
small_z_unicode = ord(s)
assert(ord('z') == 122) # testing compile time implementation
assert(small_z_unicode == 122) # testing runtime implementation

right_brace_unicode: i32
s = '}'
right_brace_unicode = ord(s)
assert(ord('}') == 125) # testing compile time implementation
assert(right_brace_unicode == 125) # testing runtime implementation


def test_chr():
i: i32
i = 48
s: str
s = chr(i)
assert s == "0"
assert chr(48) == "0"
i: i32

exclamation: str
i = 33
exclamation = chr(i)
assert(chr(33) == '!') # testing compile time implementation
assert(exclamation == '!') # testing runtime implementation

dollar: str
i = 36
dollar = chr(i)
assert(chr(36) == '$') # testing compile time implementation
assert(dollar == '$') # testing runtime implementation

left_parenthesis: str
i = 40
left_parenthesis = chr(i)
assert(chr(40) == '(') # testing compile time implementation
assert(left_parenthesis == '(') # testing runtime implementation

plus: str
i = 43
plus = chr(i)
assert(chr(43) == '+') # testing compile time implementation
assert(plus == '+') # testing runtime implementation

zero: str
i = 48
zero = chr(i)
assert(chr(48) == '0') # testing compile time implementation
assert(zero == '0') # testing runtime implementation

nine: str
i = 57
nine = chr(i)
assert(chr(57) == '9') # testing compile time implementation
assert(nine == '9') # testing runtime implementation

semicolon: str
i = 59
semicolon = chr(i)
assert(chr(59) == ';') # testing compile time implementation
assert(semicolon == ';') # testing runtime implementation

capital_a: str
i = 65
capital_a = chr(i)
assert(chr(65) == 'A') # testing compile time implementation
assert(capital_a == 'A') # testing runtime implementation

capital_z: str
i = 90
capital_z = chr(i)
assert(chr(90) == 'Z') # testing compile time implementation
assert(capital_z == 'Z') # testing runtime implementation

right_bracket: str
i = 93
right_bracket = chr(i)
assert(chr(93) == ']') # testing compile time implementation
assert(right_bracket == ']') # testing runtime implementation

small_a: str
i = 97
small_a = chr(i)
assert(chr(97) == 'a') # testing compile time implementation
assert(small_a == 'a') # testing runtime implementation

small_z: str
i = 122
small_z = chr(i)
assert(chr(122) == 'z') # testing compile time implementation
assert(small_z == 'z') # testing runtime implementation

right_brace: str
i = 125
right_brace = chr(i)
assert(chr(125) == '}') # testing compile time implementation
assert(right_brace == '}') # testing runtime implementation


test_ord()
test_chr()




27 changes: 13 additions & 14 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
#from sys import exit


def ord(s: str) -> i32:
def ord(s: str) -> i32: # currently supports characters with unicode value between 32 to 126
"""
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)
if len(s) != 1:
return -1 # not a character
i: i32
for i in range(32, 127):
if chr(i) == s:
return i


def chr(i: i32) -> str:
def chr(i: i32) -> str: # currently supports unicode values between 32 to 126
"""
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)
if i < 32 or i > 126:
return "Not yet supported"
all_chars: str
all_chars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
return all_chars[i - 32]


#: abs() as a generic procedure.
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-test_builtin-aa64615.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-test_builtin-aa64615",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/test_builtin.py",
"infile_hash": "3007954574ed4dc6170c6927213e68ac5b560bebbead4d91ac8f0c1f",
"infile_hash": "b027cd7ebfa6c0ea9d5ebf92fa75ca18f630bb5d3698b37d004bdbb3",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_builtin-aa64615.stdout",
"stdout_hash": "c3b35af2d61f1e854f13af498a40eb281043b41b018a3c3fa902f096",
"stdout_hash": "98fea42b1f37b978289b9b6974dcb9712972c3807673f0afd33ef36a",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 1677c91

Please sign in to comment.