Skip to content

Commit 392abaf

Browse files
committed
Initial implementation of chr() and ord()
1 parent d33c16f commit 392abaf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/runtime/lfortran_builtin.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from ltypes import i32
2+
from sys import exit
3+
4+
5+
def ord(s: str) -> i32:
6+
"""
7+
Returns an integer representing the Unicode code
8+
point of a given unicode character. This is the inverse of `chr()`.
9+
"""
10+
if s == '0':
11+
return 48
12+
elif s == '1':
13+
return 49
14+
else:
15+
exit(1)
16+
17+
18+
def chr(i: i32) -> str:
19+
"""
20+
Returns the string representing a unicode character from
21+
the given Unicode code point. This is the inverse of `ord()`.
22+
"""
23+
if i == 48:
24+
return '0'
25+
elif i == 49:
26+
return '1'
27+
else:
28+
exit(1)

0 commit comments

Comments
 (0)