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
{{ message }}
Repository navigation
Implement a set of built-in methods to be used on strings/integers/.... #281
Since we now have the Attribute visitor in AST->ASR, we should start implementing the set of built-in methods to be used on strings, integers, lists, dictionaries, etc.
Yes. Implement them as _lpython_bit_length(i: i32), make the AST to ASR insert these in. Those that can be implemented in Python should be. Otherwise just do the declaration and the backend (ASR->LLVM) has to implement them.
Just to make the design cleaner and not just put everything into Attribute, I suggest creating a map something similar to intrinsic procedures, where we can directly look for the methods (for example, maybe we can map list.append to a string list%append and use this converted string to find the appropriate function.)
We should try to systematically manage these methods, and at the same time keep the complexity low at the C++ level. Having some kind of a map like we do for comptime_eval is fine I think.
Reacted by Smit Lunagariya and Thirumalai Shaktivel
Please, could someone possibly share if the built in methods need to be implemented as compile time functions (like here ) or runtime functions (like here) or both?
@Shaikh-Ubaid every intrinsic function has both compile time and runtime implementation. Sorry for my late reply, I was just going through my notifications and noticed your question.
Add the function _lpython_bit_length into src/runtime/lpython_builtin.py, then in AST->ASR, when you encounter num.bit_length(), simply call this function.
, and it seems only bit_length is currently implemented.
So if you are interested in implementing any of the other ones, that would be awesome. Let me know if you have any questions or need help getting started. I am happy to meet to give you an overview.
Hello, I am trying to implement the to_bytes method for integers. As mentioned here https://docs.python.org/3/library/stdtypes.html the method returns an array of bytes, which is a type, i.e. print(type((5).to_bytes(1,'big'))) --> <class 'bytes'>
My question is that do we want to implement:
A new type, i.e. implement the class 'bytes'
Just print the string corresponding to the byte array, i.e. print((1023).to_bytes(2,'big)) --> b'\x03\xff'
Instead of a byte array make a list that corresponds to the byte array, i.e. print((1023).to_bytes(2,'big)) --> [3,255]
Something else
Also I will appreciate it if you would like to add any other tips that can help me out! Thanks!
bit_length() is a built-in method in Python that returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros.
For example, the binary representation of 9 is 0b1001, which has a length of 4 (excluding the leading 0's). Therefore, num.bit_length() returns 4.
Here's an example:
num = 9
bit_length = num.bit_length()
print(bit_length) # output: 4
You can use bit_length() method on any integer in Python.
Activity
certik commentedon Mar 24, 2022
Yes. Implement them as
_lpython_bit_length(i: i32), make the AST to ASR insert these in. Those that can be implemented in Python should be. Otherwise just do the declaration and the backend (ASR->LLVM) has to implement them.namannimmo10 commentedon Mar 25, 2022
We can try this (or something similar):
Smit-create commentedon Mar 27, 2022
Just to make the design cleaner and not just put everything into
Attribute, I suggest creating a map something similar to intrinsic procedures, where we can directly look for the methods (for example, maybe we can maplist.appendto a stringlist%appendand use this converted string to find the appropriate function.)certik commentedon Mar 27, 2022
We should try to systematically manage these methods, and at the same time keep the complexity low at the C++ level. Having some kind of a map like we do for
comptime_evalis fine I think.ubaidsk commentedon Mar 29, 2022
Please, could someone possibly share if the built in methods need to be implemented as compile time functions (like here ) or runtime functions (like here) or both?
certik commentedon Apr 13, 2022
@Shaikh-Ubaid every intrinsic function has both compile time and runtime implementation. Sorry for my late reply, I was just going through my notifications and noticed your question.
ubaidsk commentedon Apr 13, 2022
Got it. Thank you for the clarification!
certik commentedon Jul 20, 2022
Add the function
_lpython_bit_lengthintosrc/runtime/lpython_builtin.py, then in AST->ASR, when you encounternum.bit_length(), simply call this function.namannimmo10 commentedon Jul 27, 2022
@Abdelrahman-Kh-Fouad -- are you working on this?
AbdelrahmanKhaledd commentedon Jul 27, 2022
yes i will, after
str()casting.navneet1v commentedon Sep 24, 2022
Do we still have any methods left to implement? I would like to implement.
certik commentedon Sep 26, 2022
@navneet1v thanks for your interest!
If I am not mistaken, all the methods on integer that we implemented so far are here:
lpython/src/lpython/semantics/python_ast_to_asr.cpp
Line 4728 in b3534ff
bit_lengthis currently implemented.So if you are interested in implementing any of the other ones, that would be awesome. Let me know if you have any questions or need help getting started. I am happy to meet to give you an overview.
zeproffesor commentedon Oct 8, 2022
Hello, I am trying to implement the to_bytes method for integers. As mentioned here https://docs.python.org/3/library/stdtypes.html the method returns an array of bytes, which is a type, i.e.
print(type((5).to_bytes(1,'big')))--><class 'bytes'>My question is that do we want to implement:
print((1023).to_bytes(2,'big))-->b'\x03\xff'print((1023).to_bytes(2,'big))-->[3,255]Also I will appreciate it if you would like to add any other tips that can help me out! Thanks!
certik commentedon Oct 9, 2022
Thanks @zeproffesor, great question. I opened up a dedicated issue (#1187) to discuss the bytes type.
Until then just return a string. Once we decided how to handle the bytes type, we can upgrade it.
zeproffesor commentedon Oct 9, 2022
Okay! Thanks for the clarification, I'll begin working on the string implementation then.
certik commentedon Oct 10, 2022
Thanks! Feel free to open a Draft PR and post what you have and we can help if you get stuck.
zeproffesor commentedon Oct 12, 2022
@certik Created a draft pull request. Please review! Thanks!
rsaba5612 commentedon Mar 29, 2023
bit_length() is a built-in method in Python that returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros.
For example, the binary representation of 9 is 0b1001, which has a length of 4 (excluding the leading 0's). Therefore, num.bit_length() returns 4.
Here's an example:
num = 9
bit_length = num.bit_length()
print(bit_length) # output: 4
You can use bit_length() method on any integer in Python.
OmarMesqq commentedon Aug 15, 2023
Hi! Is anyone still working on this? I am quite new to compilers but I plan on helping you guys :)
Project looks promising!
I am working on a PR for this issue
certik commentedon Aug 16, 2023
@OmarMesqq perfect! Go ahead and work on it. A lot of these are implemented, but I think some are still missing. If you need any help, please ask!