coding example txtarray.src: why #t[i] to get number of elements cannot be used?

More
6 hours 35 minutes ago - 6 hours 29 minutes ago #140 by PAGIR
******* SORRY but array brackets <i>  do not appear in the text .....     *****************


Hello, I took a look at example txtarray.src in CODING sub-directory.

Inside, we have a function implemented nat txtSize(nat addr)
At the end of the file we have this call: txtSize(addr)

For me, we should replace this by: $t without coding this extract function.
-It compiles but does not work: 2044 for each t (with i=0, 1, 2)

-It seems that &t works ?: 45056, 45058, 45060

-$t does not work: it always returns 2

Compilation always work.

Remark:
text t="Hello World!";
#t returns 12
$t returns 13 (+null car)
Last edit: 6 hours 29 minutes ago by PAGIR.

Please Log in or Create an account to join the conversation.

More
5 hours 6 minutes ago - 4 hours 58 minutes ago #141 by Franck
The explanation is in the comments of the example. But I'll explain it here in more details.

$ and # evaluate the size of types at compile time and stores the result in the associated symbol. (Lofi is statically typed and that what gives it its performance).

For example:
text t="Hello World!";
has a symbol t associated with the text "Hello World!" during compilation.
So at compilation we can store the size of the text in the symbol information (along with its address) and use it elsewhere in the code whenever we reference the symbol t

At run time, the symbols no longer exist, so the execution does not have to lookup the symbols like (slow) interpreters do.

Arrays of text store the address of each text element, not the text itself. Because arrays are collections of items of the same size (two bytes in this case, which is exactly the result you get). Otherwise we would not be able to index an array element in O(1).

When we do:
text t[3]={"Hello","Joe","Infinitesimal"};
There is only a symbol for t. We do not have symbol information about the individual text elements.

The only way to know the size of a text element in this context is to evaluate it at run time. I could add an API function to do so, but I figured text manipulation in games is not of the highest priority (ROM is currently 94.8% full, so I have to be very selective), hence the Lofi implementation example.

Hope this helps.
 
Last edit: 4 hours 58 minutes ago by Franck.

Please Log in or Create an account to join the conversation.

More
4 hours 26 minutes ago #143 by PAGIR
I understand your concern about ROM and you explained it in some videos.
I am reading the manual and when I will have full view I can make some SUGGESTION of may be unuseful API's according to my beginner view.
As example, I can take min() and max() LOFI API's that are easy to code - if needed.
But I will come back when I will have a full view.
Have a nice evening.

Please Log in or Create an account to join the conversation.

Time to create page: 0.168 seconds
Powered by Kunena Forum