Annotation of gforth/compat/struct.fs, revision 1.2

1.1       anton       1: \ data structures (like C structs)
                      2: 
                      3: \ Copyright (C) 1995 Free Software Foundation, Inc.
                      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
                     21: 
                     22: \ Usage example:
                     23: \
                     24: \ struct
                     25: \     1 cells: field search-method
                     26: \     1 cells: field reveal-method
                     27: \ end-struct wordlist-map
                     28: \
                     29: \ The structure can then be extended in the following way
                     30: \ wordlist-map
                     31: \     1 cells: field enum-method
                     32: \ end-struct ext-wordlist-map \ with the fields search-method,...,enum-method
                     33: 
1.2     ! anton      34: \ This is an ANS Forth program with an environmental dependency on
        !            35: \ alignments that are powers of 2 (rewrite nalign for other systems)
        !            36: \ and with an environmental dependence on case insensitivity (convert
        !            37: \ everything to upper case for state sensitive systems).
        !            38: 
        !            39: \ The program uses the following words
        !            40: \ !!
        !            41: 
1.1       anton      42: : nalign ( addr1 n -- addr2 )
                     43: \ addr2 is the aligned version of addr1 wrt the alignment size n
                     44:  1- tuck +  swap invert and ;
                     45: 
1.2     ! anton      46: : dofield ( -- )
        !            47: does> ( name execution: addr1 -- addr2 )
        !            48:     @ + ;
        !            49: 
        !            50: : dozerofield ( -- )
        !            51:     immediate
        !            52: does> ( name execution: -- )
        !            53:     drop ;
        !            54: 
        !            55: : create-field ( offset1 align1 size align "name" -- offset2 align2 )
1.1       anton      56:     create
1.2     ! anton      57:     >r rot r@ nalign  dup ,  ( align1 size offset  R: align )
        !            58:     + swap r> nalign ;
        !            59: 
        !            60: : field ( offset1 align1 size align "name" -- offset2 align2 )
        !            61:     \ name execution: addr1 -- addr2
        !            62:     3 pick >r \ this uglyness is just for optimizing with dozerofield
        !            63:     create-field
        !            64:     r>
        !            65:     dup if
        !            66:        dofield
        !            67:     else
        !            68:        dozerofield
        !            69:     then ;
1.1       anton      70: 
                     71: : end-struct ( size align -- )
1.2     ! anton      72:     tuck nalign swap \ pad size to full alignment
        !            73:     2constant ;
1.1       anton      74: 
                     75: 0 1 chars end-struct struct
                     76: 
                     77: \ I don't really like the "type:" syntax. Any other ideas? - anton
                     78: \ Also, this seems to be somewhat general. It probably belongs to some
                     79: \ other place
                     80: : cells: ( n -- size align )
1.2     ! anton      81:     cells 1 aligned ;
1.1       anton      82: 
                     83: : doubles: ( n -- size align )
1.2     ! anton      84:     2* cells 1 aligned ;
1.1       anton      85: 
                     86: : chars: ( n -- size align )
                     87:     chars 1 chars ;
                     88: 
                     89: : floats: ( n -- size align )
1.2     ! anton      90:     floats 1 faligned ;
1.1       anton      91: 
                     92: : dfloats: ( n -- size align )
1.2     ! anton      93:     dfloats 1 dfaligned ;
1.1       anton      94: 
                     95: : sfloats: ( n -- size align )
1.2     ! anton      96:     sfloats 1 sfaligned ;
1.1       anton      97: 
                     98: : struct-align ( size align -- )
                     99:     here swap nalign here - allot
                    100:     drop ;
                    101: 
                    102: : struct-allot ( size align -- addr )
                    103:     over swap struct-align
                    104:     here swap allot ;
                    105: 
                    106: : struct-allocate ( size align -- addr ior )
                    107:     drop allocate ;
1.2     ! anton     108: 
        !           109: : struct-alloc ( size align -- addr )
        !           110:     struct-allocate throw ;

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