Annotation of gforth/ds2texi.fs, revision 1.12

1.1       anton       1: \ documentation source to texi format converter
                      2: 
1.9       anton       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: 
1.1       anton      21: \ documentation source can contain lines in the form `doc-word' and
                     22: \ `short-word'. These are converted to appropriate full or short
                     23: \ (without the description) glossary entries for word.
                     24: 
                     25: \ The glossary entries are generated from data present in the wordlist
                     26: \ `documentation'. Each word resides there under its own name.
                     27: 
1.2       pazsan     28: script? [IF]
                     29: warnings off
1.7       anton      30: require search-order.fs
1.8       anton      31: require glocals.fs
1.7       anton      32: require float.fs
                     33: require struct.fs
                     34: require debugging.fs
1.2       pazsan     35: [THEN]
                     36: 
1.1       anton      37: wordlist constant documentation
                     38: 
                     39: struct
                     40:     2 cells: field doc-name
                     41:     2 cells: field doc-stack-effect
                     42:     2 cells: field doc-wordset
                     43:     2 cells: field doc-pronounciation
                     44:     2 cells: field doc-description
                     45: end-struct doc-entry
                     46: 
1.4       anton      47: create description-buffer 4096 chars allot
                     48: 
                     49: : get-description ( -- addr u )
                     50:     description-buffer
                     51:     begin
                     52:        refill
                     53:     while
                     54:        source nip
                     55:     while
                     56:        source swap >r 2dup r> -rot cmove
                     57:        chars +
                     58:        #lf over c! char+
                     59:     repeat then
                     60:     description-buffer tuck - ;
                     61: 
1.8       anton      62: : replace-_ ( c-addr u -- )
                     63:     \ replaces _ with -
                     64:     chars bounds
                     65:     +DO
                     66:        i c@ [char] _ =
                     67:        if
                     68:            [char] - i c!
                     69:        endif
                     70:        1 chars
                     71:     +loop ;
                     72:     
                     73: : condition-stack-effect ( c-addr1 u1 -- c-addr2 u2 )
1.10      anton      74:     save-mem 2dup replace-_ ;
1.8       anton      75:     
                     76: : condition-wordset ( c-addr1 u1 -- c-addr2 u2 )
                     77:     dup 0=
                     78:     if
                     79:        2drop s" unknown"
                     80:     else
1.10      anton      81:        save-mem
1.8       anton      82:     endif ;
                     83: 
                     84: : condition-pronounciation ( c-addr1 u1 -- c-addr2 u2 )
1.10      anton      85:     save-mem 2dup replace-_ ;
1.8       anton      86: 
1.4       anton      87: : make-doc ( -- )
                     88:     get-current documentation set-current
                     89:     create
                     90:        last @ name>string 2,           \ name
1.10      anton      91:        [char] ) parse save-mem 2,      \ stack-effect
1.8       anton      92:        bl parse-word condition-wordset 2,      \ wordset
1.4       anton      93:        bl parse-word dup               \ pronounciation
                     94:        if
1.8       anton      95:            condition-pronounciation
1.4       anton      96:        else
                     97:            2drop last @ name>string
                     98:        endif
                     99:        2,
1.10      anton     100:        get-description save-mem 2,
1.4       anton     101:     set-current ;
                    102: 
1.1       anton     103: : emittexi ( c -- )
                    104:     >r
                    105:     s" @{}" r@ scan 0<>
                    106:     if
                    107:        [char] @ emit
                    108:     endif
                    109:     drop r> emit ;
                    110: 
                    111: : typetexi ( addr u -- )
                    112:     0
                    113:     ?do
                    114:        dup c@ emittexi
                    115:        char+
                    116:     loop
                    117:     drop ;
                    118: 
                    119: : print-short ( doc-entry -- )
1.7       anton     120:     >r
                    121:     ." @findex "
                    122:     r@ doc-name 2@ typetexi
                    123:     ."  @var{ " r@ doc-stack-effect 2@ type ."  }  "
                    124:     r@ doc-wordset 2@ type
                    125:     cr
1.12    ! anton     126:     ." @cindex "
        !           127:     ." @code{" r@ doc-name 2@ typetexi ." }"
        !           128:     cr
1.7       anton     129:     ." @format" cr
1.1       anton     130:     ." @code{" r@ doc-name 2@ typetexi ." }       "
                    131:     ." @i{" r@ doc-stack-effect 2@ type ." }       "
                    132:     r@ doc-wordset 2@ type ."        ``"
1.3       anton     133:     r@ doc-pronounciation 2@ type ." ''" cr ." @end format" cr
1.1       anton     134:     rdrop ;
                    135: 
                    136: : print-doc ( doc-entry -- )
                    137:     >r
                    138:     r@ print-short
                    139:     r@ doc-description 2@ dup 0<>
                    140:     if
1.7       anton     141:        ." @iftex" cr ." @vskip-3ex" cr ." @end iftex" cr
                    142:        type cr cr \ ." @ifinfo" cr ." @*" cr ." @end ifinfo" cr cr
1.1       anton     143:     else
                    144:        2drop cr
                    145:     endif
                    146:     rdrop ;
                    147: 
                    148: : do-doc ( addr1 u1 addr2 u2 xt -- f )
                    149:     \ xt is the word to be executed if addr1 u1 is a string starting
                    150:     \ with the prefix addr2 u2 and continuing with a word in the
                    151:     \ wordlist `documentation'. f is true if xt is executed.
                    152:     >r dup >r
                    153:     3 pick over compare 0=
                    154:     if \ addr2 u2 is a prefix of addr1 u1
                    155:        r> /string documentation search-wordlist
                    156:        if \ the rest of addr1 u1 is in documentation
                    157:            execute r> execute true
                    158:        else
                    159:            rdrop false
                    160:        endif
                    161:     else
                    162:        2drop 2rdrop false
                    163:     endif ;
                    164: 
                    165: : process-line ( addr u -- )
                    166:     2dup s" doc-" ['] print-doc do-doc 0=
                    167:     if
                    168:        2dup s" short-" ['] print-short do-doc 0=
                    169:        if
                    170:            type cr EXIT
                    171:        endif
                    172:     endif
                    173:     2drop ;
                    174: 
                    175: 1024 constant doclinelength
                    176: 
                    177: create docline doclinelength chars allot
                    178: 
                    179: : ds2texi ( file-id -- )
                    180:     >r
                    181:     begin
                    182:        docline doclinelength r@ read-line throw
                    183:     while
                    184:        dup doclinelength = abort" docline too long"
                    185:        docline swap process-line
                    186:     repeat
                    187:     drop rdrop ;
1.2       pazsan    188: 
1.8       anton     189: : compare-ci ( addr1 u1 addr2 u2 -- n )
                    190:     \ case insensitive string compare
                    191:     2 pick swap -
                    192:     ?dup-0=-if
                    193:         capscomp
                    194:     else
                    195:        nip nip nip
                    196:        0<
                    197:        if
                    198:            -1
                    199:        else
                    200:            1
                    201:        endif
                    202:     endif  ;
                    203: 
                    204: : answord ( "name wordset pronounciation" -- )
                    205:     \ check the documentaion of an ans word
                    206:     name { D: wordname }
                    207:     name { D: wordset }
                    208:     name { D: pronounciation }
                    209:     wordname documentation search-wordlist
                    210:     if
                    211:        execute { doc }
                    212:        wordset doc doc-wordset 2@ compare-ci
                    213:        if 
                    214:            ." wordset: " wordname type ." : '"  doc doc-wordset 2@ type ." ' instead of '" wordset type ." '" cr
                    215:        endif
                    216:        pronounciation doc doc-pronounciation 2@ compare-ci
                    217:        if
                    218:            ." pronounciation: " wordname type ." : '" doc doc-pronounciation 2@ type ." ' instead of '" pronounciation type ." '" cr
                    219:        endif
                    220:     else
                    221:        ." undocumented: " wordname type cr
                    222:     endif ;

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