Skip to content

Implement a set of built-in methods to be used on strings/integers/.... #281

Open

Description

@namannimmo10

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.

Here's a list of those operating on strings: https://www.w3schools.com/python/python_ref_string.asp
And one can navigate on the left for other types.

Let's start with something easy, like bit_length for integers:

>>> num = 9
>>> num.bit_length()
4

Activity

  1. certik commented on Mar 24, 2022

    @certik
    Contributor

    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.

  2. namannimmo10 commented on Mar 25, 2022

    @namannimmo10
    CollaboratorAuthor

    We can try this (or something similar):

    >>> def _lpython_bit_length(i: int) -> int:
    ...   x: str
    ...   x = bin(i)
    ...   if i == 0: return 0
    ...   elif i < 0: x = x[3:]
    ...   else: x = x[2:]
    ...
    ...   return len(x)
    ...
    >>> all(_lpython_bit_length(i) == i.bit_length() for i in range(-1000, 1000))
    True
    >>>
  3. Smit-create commented on Mar 27, 2022

    @Smit-create
    Collaborator

    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.)

  4. certik commented on Mar 27, 2022

    @certik
    Contributor

    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.

  5. ubaidsk commented on Mar 29, 2022

    @ubaidsk
    Collaborator

    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?

  6. certik commented on Apr 13, 2022

    @certik
    Contributor

    @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.

  7. ubaidsk commented on Apr 13, 2022

    @ubaidsk
    Collaborator

    Got it. Thank you for the clarification!

  8. certik commented on Jul 20, 2022

    @certik
    Contributor

    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.

  9. namannimmo10 commented on Jul 27, 2022

    @namannimmo10
    CollaboratorAuthor

    @Abdelrahman-Kh-Fouad -- are you working on this?

  10. AbdelrahmanKhaledd commented on Jul 27, 2022

    @AbdelrahmanKhaledd
    Collaborator

    @Abdelrahman-Kh-Fouad -- are you working on this?

    yes i will, after str() casting.

  11. navneet1v commented on Sep 24, 2022

    @navneet1v

    Do we still have any methods left to implement? I would like to implement.

  12. certik commented on Sep 26, 2022

    @certik
    Contributor

    @navneet1v thanks for your interest!

    If I am not mistaken, all the methods on integer that we implemented so far are here:

    } else if (AST::is_a<AST::ConstantInt_t>(*at->m_value)) {
    , 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.

  13. zeproffesor commented on Oct 8, 2022

    @zeproffesor

    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:

    1. A new type, i.e. implement the class 'bytes'
    2. Just print the string corresponding to the byte array, i.e. print((1023).to_bytes(2,'big)) --> b'\x03\xff'
    3. Instead of a byte array make a list that corresponds to the byte array, i.e. print((1023).to_bytes(2,'big)) --> [3,255]
    4. Something else

    Also I will appreciate it if you would like to add any other tips that can help me out! Thanks!

  14. certik commented on Oct 9, 2022

    @certik
    Contributor

    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.

  15. zeproffesor commented on Oct 9, 2022

    @zeproffesor

    Okay! Thanks for the clarification, I'll begin working on the string implementation then.

  16. certik commented on Oct 10, 2022

    @certik
    Contributor

    Thanks! Feel free to open a Draft PR and post what you have and we can help if you get stuck.

  17. zeproffesor commented on Oct 12, 2022

    @zeproffesor

    @certik Created a draft pull request. Please review! Thanks!

  18. rsaba5612 commented on Mar 29, 2023

    @rsaba5612

    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.

  19. OmarMesqq commented on Aug 15, 2023

    @OmarMesqq
    Contributor

    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

  20. certik commented on Aug 16, 2023

    @certik
    Contributor

    @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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions