You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once LPython can use ASR's generic function facilities, it should just be a generic function. In the meantime, let's implement:
str_int(x) ... converts an integer to a string
str_float(x) ... converts a floating point to a string
These will be implemented in pure Python, such as:
defstr_int(x):
....
# s = ...returns
For now, let's put this into one file, and print the result at the end. Some things will not be implemented in LPython yet, so let's open up issues for things that are not implemented yet, and we'll fix them.
Perfect, thanks. Yes, we have to implement if and return. For pos_to_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], we'll probably implement it as a 1D array.
# Uncomment once we implement the `sys` module#from sys import exitdeford(s: str) ->i32:
ifs=='0':
return48elifs=='1':
return49else:
exit(1)
defchr(i: i32) ->str:
ifi==48:
return'0'elifi==49:
return'1'else:
exit(1)
The ord and chr functions fail if characters other than '0', '1' for ord and unicode values other than 48, 49 for chr are passed. How are we planning to deal with this?
Shall I update the functions ord and chr as follows:
def ord(s: str) -> i32: # supports characters with unicode value between 32 to 126
if len(s) != 1:
return "Not a Character"
for i in range(32, 127):
if chr(i) == s:
return i
def chr(i: i32) -> str: # supports unicode values between 32 to 126
if i < 32 or i > 126:
return "Not Yet Supported"
all_chars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
return all_chars[i - 32]
Testing Code:
testing_chars = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
for ch in testing_chars:
print(f"{ch}: {ord(ch)}")
@Shaikh-Ubaid I think so. I think we might need to fix some bugs in LPython to make it work, but I think an implementation along your lines should work. Can you please submit it as a PR and add some tests?
P.S. My apologies for late answer, I just noticed your comments now.
Activity
Smit-create commentedon Dec 30, 2021
I tried implementing
str_int(x)using different ways:x: i32Reference code
AST
ASR
str_int(x: int)Reference code
AST
ASR
__str__)Reference code
AST
ASR
certik commentedon Dec 30, 2021
Perfect, thanks. Yes, we have to implement
ifandreturn. Forpos_to_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], we'll probably implement it as a 1D array.Smit-create commentedon Dec 30, 2021
Well,
float_to_stringrequiresfloat_to_intand we may have to set the precision too. builtin magic method__str__might be best option forstr_float.certik commentedon Jan 3, 2022
Let's use
chr(ord('0')+i)instead of['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][i].certik commentedon Jan 3, 2022
Let's implement
chrandordlike this:certik commentedon Jan 3, 2022
To implement some of the features that we need, isolate the given feature and try to implement it, such as:
ubaidsk commentedon Mar 29, 2022
The
ordandchrfunctions fail if characters other than'0','1'forordand unicode values other than48,49forchrare passed. How are we planning to deal with this?ubaidsk commentedon Mar 29, 2022
Shall I update the functions
ordandchras follows:Testing Code:
Testing Code Output:
To accomplish #281 , I think that we might need
ordandchrfunctions to implement string methods likeisalpha,isupper, etc.certik commentedon Apr 13, 2022
@Shaikh-Ubaid I think so. I think we might need to fix some bugs in LPython to make it work, but I think an implementation along your lines should work. Can you please submit it as a PR and add some tests?
P.S. My apologies for late answer, I just noticed your comments now.
ubaidsk commentedon Apr 13, 2022
Yes, I will submit the
ordandchrimplementations along with integrations tests.