"STRUCT - simple struct implementation"

struct
"struct" implements neon/mops/mpe-like structures.
"structs" implements fsl/mforth-like structures.

the two wordsets are designed to let the sub-words
to be used interchangably both inside STRUCT and
STRUCTURE definitions. They will also work inside
pfe's class-definitions btw.

The provided words try to be compatible
with the simple implementation guidelines as
provided in the survey at the comp.lang.forth.repository
(http://forth.sourceforge.net/word/structure)
and the documentation on MPE' forth's implementation
(/vol/c/Programme/PfwVfx/Doc/VfxMan.Htm/struct.html)
and the structs-source of the Forth Scientific Library
(lib/fsl/structs.fth)
plus some compatibility sugar for the gforth' struct
(gforth/struct.fs)

field-layout
PFA[0] has the offset (elsewhere for the method-table)
PFA[1] has the sizeof (may serve as minimalistic type-id)

struct-layout
PFA[0] unused (elswehere method-table or type-id)
PFA[1] has the sizeof (that is instantiated)

therefore SIZEOF is designed to give a nice result in
both places.
EXTENSIONS
NEON-MOPS-MPE variant
* STRUCT ( "name" -- here zero-offset )

begin definition of a new structure (mpe.000)
 : STRUCT CREATE  !CSP
   HERE
   0 DUP ,
 DOES>
   @
 ;

struct ordinary primitive

* END-STRUCT ( here some-offset -- )

terminate definition of a new structure (mpe.000)
 : END-STRUCT  SWAP !  ?CSP ;

struct ordinary primitive

* FIELD ( offset size "name" -- offset+size )

create a field - the workhorse for both STRUCT and STRUCTURE
implementations. The created fieldname is an OFFSET:-word
that memorizes the current offset in its PFA and will add
that offset on runtime. This forth-word does *not* align.
 : FIELD CREATE
   OVER ,
   +
 DOES>
    @ +
 ;

struct defining primitive

* SUBRECORD ( outer-offset "name" -- outer-offset here zero-offset )

begin definition of a subrecord (mpe.000)
 : STRUCT CREATE  
   HERE
   0 DUP ,
 DOES>
   @
 ;

struct ordinary primitive

* END-SUBRECORD ( outer-offset here some-offset -- outer-offset+some )

end definition of a subrecord (mpe.000)
 : END-SUBRECORD  TUCK SWAP !  + ;

struct ordinary primitive

* ARRAY-OF ( some-offset n len "name" -- some-offset )

a FIELD-array
 : ARRAY-OF * FIELD ;

struct ordinary primitive

* VARIANT ( outer-offset "name" -- outer-offset here zero-offset )

Variant records describe an alternative view of the
current record or subrecord from the start to the current point.
The variant need not be of the same length, but the larger is taken
 : VARIANT SUBRECORD ;

struct ordinary primitive

* END-VARIANT ( outer-offset here some-offset -- outer-offset )

terminate definition of a new variant (mpe.000)
 : END-STRUCT  TUCK SWAP !  2DUP < IF NIP ELSE DROP THEN ;

struct ordinary primitive

* INSTANCE ( len "name" -- )

Create a named instance of a named structure.
 : INSTANCE  CREATE ALLOT ;

struct ordinary primitive

* INSTANCE-ADDR ( len -- addr )

Create nameless instance of a structure and return base address.
 : INSTANCE-ADDR  HERE SWAP ALLOT ;

struct ordinary primitive

traditional wording
* STRUCTURE ( "name" -- here zero-offset ) exec

start a structure definition
 : STRUCTURE: CREATE !CSP
   HERE
   0 DUP ,
 DOES>
   CREATE @ ALLOT
 ;

struct defining primitive

* ENDSTRUCTURE ( here some-offset -- )

finalize a previously started STRUCTURE definition
 : ENDSTRUCTURE  SWAP !  ?CSP ;

struct ordinary primitive

* SIZEOF ( "name" -- size )

get the size-value from a previous structure definition
 : SIZEOF   ' >BODY @  STATE @ IF [COMPILE] LITERAL THEN ; IMMEDIATE

struct compiling primitive

gforth compatibility
EXTENSIONS CHAR%

no special info, see general notes

struct ordinary primitive

EXTENSIONS CELL%

no special info, see general notes

struct ordinary primitive

EXTENSIONS WCHAR%

no special info, see general notes

struct ordinary primitive

EXTENSIONS DOUBLE%

no special info, see general notes

struct ordinary primitive

EXTENSIONS FLOAT%

no special info, see general notes

struct ordinary primitive

EXTENSIONS SFLOAT%

no special info, see general notes

struct ordinary primitive

EXTENSIONS DFLOAT%

no special info, see general notes

struct ordinary primitive