Structures in zeptoforth - tabemann/zeptoforth GitHub Wiki
Structures in zeptoforth define a set of fields in a block of memory along with a constant that specifies the size of that block of memory in bytes. These fields may be of any size, and halfword, word, and double word fields are automatically 2 byte, 4 byte, and 4 byte aligned respectively except when defined with +field
.
Structure definitions are bracketed with begin-structure
, which is followed by the name the constant for the structure size will be defined as, and end-structure
, which sets this constant and concludes the structure definition.
Between these the fields in a structure are specified with:
cfield:
for byte fieldshfield:
for halfword fields with halfword alignmentfield:
for word fields with word alignment2field:
for double word fields with word alignment+field
for arbitrary-sized fields
These are followed by the name of the field in question; +field
also takes the size of the field in bytes off the stack. Fields take the address of the structure containing them and replace it with the address of the field in the structure on the stack.
For an example of structures and fields take the following:
begin-structure my-size
cfield: my-byte
hfield: my-halfword
field: my-word
2field: my-doubleword
end-structure
my-size buffer: my-structure
Afterwards, execute:
1 my-structure my-byte c! ok
2 my-structure my-halfword h! ok
3 my-structure my-word ! ok
4. my-structure my-doubleword 2! ok
my-structure my-byte c@ . 1 ok
my-structure my-halfword h@ . 2 ok
my-structure my-word @ . 3 ok
my-structure my-doubleword 2@ d. 4 ok
Note that structures provide no namespace mechanism. If one desires a namespace mechanism one should use modules alongside structures.