dom cook
Div Pro
Posts: 387
Registered: 4-3-2016
Member Is Offline
|
|
Two for one offer
Here are a couple of useful snippets.
1) Ever wanted to be able to make a copy of a struct without assigning all the values individually? Well, now you can.
2) Did you know DIV can do 2d arrays (tables)?
|
|
Vortigano
DIV Regular
Posts: 237
Registered: 12-1-2017
Location: Ciudad de Buenos Aires, Argentina
Member Is Offline
|
|
Quote: Originally posted by dom cook |
1) Ever wanted to be able to make a copy of a struct without assigning all the values individually? Well, now you can.
|
Do you mean something like C language memcpy?
|
|
dom cook
Div Pro
Posts: 387
Registered: 4-3-2016
Member Is Offline
|
|
Possibly, I don't know much C.
The motivation behind this was to explore the possibilities of passing structs to functions by reference rather than by index. In general this is not
really needed but in cases where one might want to copy or reset all the values in a large struct it can be useful especially as it does not need to
be written individually for specific structs.
|
|
Vortigano
DIV Regular
Posts: 237
Registered: 12-1-2017
Location: Ciudad de Buenos Aires, Argentina
Member Is Offline
|
|
You're going to share a method for passing structures? That'd be great!
I usually use: passing structure by reference, and use the return() statement only to verify errors or for "integer to integer" boolean operations.
|
|
dom cook
Div Pro
Posts: 387
Registered: 4-3-2016
Member Is Offline
|
|
Quote: Originally posted by Vortigano | You're going to share a method for passing structures? That'd be great!
I thought I already did. But on closer inspection I see I neglected to upload the file.
I think this is it.
Attachment: copystructure.PRG (2kB) This file has been downloaded 846 times
[Edited on 11-6-2019 by dom cook] |
|
|
dom cook
Div Pro
Posts: 387
Registered: 4-3-2016
Member Is Offline
|
|
The code's a bit confusing as it does two things, the other being using 2d arrays (which I didn't know Div supported.
The copy_structure process at the bottom is the relevant bit. It takes the address of the struct and the size of each entry and copies one entry's
data into another. Because there is no need to specify each field, this function can be reused for any struct.
Anyway I suspect this isn't quite what you were expecting but, maybe it's useful to you.
[Edited on 11-6-2019 by dom cook]
[Edited on 11-6-2019 by dom cook]
|
|
Vortigano
DIV Regular
Posts: 237
Registered: 12-1-2017
Location: Ciudad de Buenos Aires, Argentina
Member Is Offline
|
|
Wow, the map_put function g[bbc [...]], very ingenious ^u^
About the function "copy_struct", the power of "sizeof" is the law :P
I also saw that you use arrays with embedded strings, that's something I recently also discovered ... because I've always used Div's String type more
often, in order to be able to use the div string functions.
|
|
MikeDX
|
|
I'm going to have to look into this. it may be possible to abstract divs string type to use floats as I've had an idea about var length since string
is a pointer we could use it as a pointer to a number longer than 4 bytes.
might turn out to be impossible or not effective but it's worth. look
|
|
CicTec
DIV Pro
Posts: 471
Registered: 6-8-2016
Member Is Offline
|
|
Quote: Originally posted by MikeDX | I'm going to have to look into this. it may be possible to abstract divs string type to use floats as I've had an idea about var length since string
is a pointer we could use it as a pointer to a number longer than 4 bytes.
might turn out to be impossible or not effective but it's worth. look |
Mike, don't waste time on solutions that don't work, the current expression analyzer only works with INT (even strings are INT in DIV), adding
floating-point support requires the complete rewriting of the whole analyzer, the parser and other things about the compiler since it's a different
data type, you need to add type control, semantics, code generator, runtime type checking and conversions (compiled code and equivalent code to be
interpreted in the VM), it's a lot of work to more than what you imagine to make it work as all the compilers that support this feature.
[Edited on 12-6-2019 by CicTec]
|
|