The Forth 2012 standard defines a slightly less convenient form of
structures. In general (when using field+
, you have to perform
the alignment yourself, but there are a number of convenience words
(e.g., field:
) that include alignment.
A typical usage example is:
0 field: s-a faligned 2 floats +field s-b constant s-struct
An alternative way of writing this structure is:
begin-structure s-struct field: s-a faligned 2 floats +field s-b end-structure
You can define a structure that has the same fields and additional fields as follows:
s-struct cfield: t-c cfield: t-d constant t-struct
or alternatively
s-struct extend-structure t-struct cfield: t-c cfield: t-d end-structure
begin-structure
( "name" – struct-sys 0 ) facility-ext “begin-structure”
extend-structure
( n "name" – struct-sys n ) gforth-1.0 “extend-structure”
Start a new structure name as extension of an existing structure with size n.
end-structure
( struct-sys +n – ) facility-ext “end-structure”
end a structure started wioth begin-structure
+field
( noffset1 nsize "name" – noffset2 ) facility-ext “plus-field”
Defining word; defines name ( addr1 -- addr2 )
, where
addr2 is addr1+noffset1. noffset2 is
noffset1+nsize.
cfield:
( u1 "name" – u2 ) facility-ext “c-field-colon”
Define a char-sized field
field:
( u1 "name" – u2 ) facility-ext “field-colon”
Define an aligned cell-sized field
2field:
( u1 "name" – u2 ) gforth-0.7 “two-field-colon”
Define an aligned double-cell-sized field
ffield:
( u1 "name" – u2 ) floating-ext “f-field-colon”
Define a faligned float-sized field
sffield:
( u1 "name" – u2 ) floating-ext “s-f-field-colon”
Define a sfaligned sfloat-sized field
dffield:
( u1 "name" – u2 ) floating-ext “d-f-field-colon”
Define a dfaligned dfloat-sized field
wfield:
( u1 "name" – u2 ) gforth-1.0 “w-field-colon”
Define a naturally aligned field for a 16-bit value.
lfield:
( u1 "name" – u2 ) gforth-1.0 “l-field-colon”
Define a naturally aligned field for a 32-bit value.
xfield:
( u1 "name" – u2 ) gforth-1.0 “x-field-colon”
Define a naturally aligned field for a 64-bit-value.