ante
ante copied to clipboard
`impl Cast int string` in the stdlib should compile
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 acceptstring
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 orref
s will still be necessary since this code callsmalloc
manually.- [x] An
offset
function for pointers will likely require adding a compiler builtin inllvm/builtin.rs
- [x] An
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?
#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 ref
s 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.
Thanks for the rundown, I'll find out which issue I want to start chipping away at soon :)
I think i might try to take this on when I have a free weekend :)
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.
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 :)
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 :).
With #110 merged, this should be possible now
@jfecher this is solved
@locriacyber indeed, thanks for noticing