Use array syntax for manipulating stdlib lists and use strings as character arrays
Opened this issue · 4 comments
I'd been mulling around writing a native syntax for manipulating lists more easily but never gotten around to doing it. Writing file names three characters at a time with list_push__
is tiring, but I solved that with an external shell script to generate the calls well enough to not bothering implementing anything better in the language.
Now I see #10 from @MickeyDelp adding a native array syntax so I'm more interested than before in getting this done! For my situation malloc/free works better than having arrays in the static arena.
So something like this:
array fname = list_new(5)
fname = 't', 'e', 's', 't', '\0'
But while at it, even better:
array fname = list_new(5)
fname = "test"
Strings to make character arrays would be good for the now existing array functionality too.
I have been playing a bit with strings and arrays. I have a branch ( https://github.com/MickeyDelp/tinyscript/tree/assign_strings_to_arrays ) that includes 14 lines of extra code in the ArrayAssign function that allows assigning a string to an array.
array fname(2) = "test"
or
array fname(2)
fname = "test"
Then fname
can be passed to a C function which expects a char*. The assign function adds a zero terminator, and won't allow memory overruns.
This is not a proper string implementation by any means. The big issue is that is still an int array, so the size that it is declared as is the number of Vals. You have to know the size of a Val (same as the size of a pointer) for your environment. My thought was that maybe we can add a string type and most of the code will be shared with the array type code. IDK. But, for now, it is a quick and dirty way to pass a string to a C function.
Oh right. My lists are uint8_t. This needs some more thought, but I'll definitely check out your branch.
Maybe add list
as a separate declaration type.
Just to be clear, My branch is storing the characters as uint8_t. It is just doing it inside the same memory area that was set aside for the int array. So, on a 32 bit system, you can store 4 chars per array entry. When you pass a TinyScript array to a C function, it is passing a pointer to the data. So, if you put a string into an array, and pass that to a C function, it can be treated as a pointer to a char (uint8_t) array.
Yes I understand that.