Annotation of gforth/struct.fs, revision 1.7

1.7     ! anton       1: \ $Id: struct.fs,v 1.6 1995/09/06 21:00:30 pazsan Exp $
1.1       anton       2: 
                      3: \ Usage example:
                      4: \
                      5: \ struct
                      6: \     1 cells: field search-method
                      7: \     1 cells: field reveal-method
                      8: \ end-struct wordlist-map
                      9: \
                     10: \ The structure can then be extended in the following way
                     11: \ wordlist-map
                     12: \     1 cells: field enum-method
                     13: \ end-struct ext-wordlist-map \ with the fields search-method,...,enum-method
                     14: 
                     15: : nalign ( addr1 n -- addr2 )
                     16: \ addr2 is the aligned version of addr1 wrt the alignment size n
                     17:  1- tuck +  swap invert and ;
                     18: 
1.3       pazsan     19: : field ( offset1 align1 size align -- offset2 align2 )
1.1       anton      20: \ note: this version uses local variables
1.7     ! anton      21:      Header reveal dofield: cfa,
1.1       anton      22:        >r rot r@ nalign  dup ,  ( align1 size offset )
                     23:        + swap r> nalign ;
                     24: 
                     25: : end-struct ( size align -- )
                     26:  2constant ;
                     27: 
                     28: 0 1 chars end-struct struct
                     29: 
1.3       pazsan     30: \ : field  ( offset1 align1 size align -- offset2 align2 )
                     31: \    create-field
                     32: \    does> ( addr1 -- addr2 )
                     33: \      @ + ;
1.1       anton      34: 
                     35: \ I don't really like the "type:" syntax. Any other ideas? - anton
                     36: \ Also, this seems to be somewhat general. It probably belongs to some
                     37: \ other place
                     38: : cells: ( n -- size align )
                     39:     cells cell ;
                     40: 
                     41: : doubles: ( n -- size align )
                     42:     2* cells cell ;
                     43: 
                     44: : chars: ( n -- size align )
                     45:     chars 1 chars ;
                     46: 
                     47: : floats: ( n -- size align )
                     48:     floats 1 floats ;
                     49: 
1.5       pazsan     50: : dfloats: ( n -- size align )
                     51:     dfloats 1 dfloats ;
1.2       anton      52: 
1.5       pazsan     53: : sfloats: ( n -- size align )
                     54:     sfloats 1 sfloats ;
                     55: 
1.2       anton      56: : struct-align ( size align -- )
                     57:     dp @ swap nalign dp !
                     58:     drop ;
                     59: 
                     60: : struct-allot ( size align -- addr )
                     61:     over swap struct-align
                     62:     here swap allot ;
                     63: 
1.5       pazsan     64: : struct-allocate ( size align -- addr ior )
1.2       anton      65:     drop allocate ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>