ante icon indicating copy to clipboard operation
ante copied to clipboard

`impl Cast int string` in the stdlib should compile

Open jfecher opened this issue 3 years ago • 8 comments

Currently, impl Cast int string with ... is one of the commented out sections of the prelude since it hasn't worked since the start of the rewrite of the old compiler from C++ into Rust. I think now is the time to start working on adding any small features needed to get more of these commented out sections to compile - starting with this one. Here is the current version of Cast int string:

impl Cast int string
    cast i =
        if i == 0 then return "0"
        len = mut 0
        alloc_size = 20
        buf = mut malloc (alloc_size + 1)
        buf#alloc_size := '\0'
 
        x = mut i
        isNeg = i < 0 and ((x *= -1); true)

        while x != 0 do
            len += 1
            buf#(alloc_size - len) := '0' + cast (x % 10)
            x /= 10

        if isNeg then
            len += 1
            buf#(alloc_size - len) := '-'

        offset = cast buf + (alloc_size - len)
        string (cast offset) len

For it to compile, several changes will need to be made:

  • [x] New impl syntax, should be impl Cast int string with ...
  • [x] We should implement it for all integers now that we have many: impl Cast int string given Int int with ...
  • [ ] Ante doesn't have loops anymore, so the while loop will need to be replaced with a recursive helper function. ~~Ante also doesn't have closures yet so this helper may be more verbose than otherwise necessary.~~
  • [x] The final string (cast offset) len call to manually construct a string isn't yet possible since the parser doesn't accept string as a valid value yet - this should probably be fixed so it parses correctly.
  • [x] The final and most difficult requirement on this list is to get pointers and indexing (via #) working. A minimum viable product for this can use a normal named function over #, though a method to offset pointers or refs will still be necessary since this code calls malloc manually.
    • [x] An offset function for pointers will likely require adding a compiler builtin in llvm/builtin.rs

jfecher avatar May 22 '21 18:05 jfecher

Would it simplify the implementation of this to sort out #82 first? :)

Also would you prefer this or the Ptr primitive type to be taken care of first?

thehabbos007 avatar May 22 '21 20:05 thehabbos007

#82 is mostly unrelated - using loop over a recursive function for the while loop would be a bit simpler but not enough to justify waiting on #82 imo. For this issue, we could start off having an offset function on refs instead of pointers since their runtime representation is the same. When pointers are implemented we could just add offset to the list of functions that should be changed to take pointers instead of refs so no need to do #78 beforehand either. Whichever issue you'd like to tackle first I'll leave to your preference.

jfecher avatar May 22 '21 21:05 jfecher

Thanks for the rundown, I'll find out which issue I want to start chipping away at soon :)

thehabbos007 avatar May 22 '21 21:05 thehabbos007

I think i might try to take this on when I have a free weekend :)

thehabbos007 avatar Jun 22 '21 19:06 thehabbos007

Forgot about this issue, its been a while and it may be implementable now. I'll need to revisit it sometime. Without loops or looping sugar the implementation will be uglier but hopefully at least possible.

jfecher avatar Mar 26 '22 22:03 jfecher

Same here. Sorry have been caught up in university work for a while there. I'd be interested in implementing the casting function now that the difficult bits have been tackled :)

thehabbos007 avatar Mar 27 '22 11:03 thehabbos007

Just investigated this a bit more - turns out I was mistaken and calling string as a constructor isn't implemented. I believe the extra builtins I was referencing are only on the cranelift branch for now as well. I tested uncommenting the input function and converting it lead to some small bugs so suffice to say that this isn't implementable currently at least without wrangling some other tasks.

And no need to apologize for keeping to your studies - I wouldn't want to impact that :).

jfecher avatar Mar 28 '22 00:03 jfecher

With #110 merged, this should be possible now

jfecher avatar Jun 24 '22 15:06 jfecher

@jfecher this is solved

iacore avatar Feb 21 '23 23:02 iacore

@locriacyber indeed, thanks for noticing

jfecher avatar Feb 22 '23 02:02 jfecher