1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
\ Glossary generator.
\ Written in ANS Forth, requires FILES wordset.
\ This file is part of Gforth.
\ Copyright (C) 1995,1997,2000,2003,2007 Free Software Foundation, Inc.
\ Copyright (c)1993 L.C. Benschop Eindhoven.
\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU General Public License for more details.
\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.
decimal
: \G postpone \ ; immediate
\G \G is an alias for \, so it is a comment till end-of-line, but
\G it has a special meaning for the Glossary Generator.
\G \G comments should appear immediately above or below the definition of
\G the word it belongs to. The definition line should contain no more
\G than the definition, a stack comment and a \ comment after which
\G the wordset and pronounciation.
\G An isolated block of \G comments is placed at the beginning of the
\G glossary file.
VARIABLE GLOSLIST
VARIABLE CURRENT-COMMENT
VARIABLE FIXLINE
\ The Glossary entries in memory have the following form.
\ 1 cell: address of next entry, 1 cell: address of comment field
\ counted string: name counted string: stack picture
\ counted string: extra field counted string: pronunciation.
\G This command starts a fresh glossary.
: NEWGLOS
\ S" FORGET GSTART CREATE GSTART" EVALUATE
0 GLOSLIST ! ;
CREATE OLDLINE 256 CHARS ALLOT
VARIABLE CHARPTR
\G Insert the header into the list at the alphabetically correct place.
: INSERT-HEADER ( addr ---)
CHARPTR !
GLOSLIST
BEGIN
DUP @
IF
DUP @ 2 CELLS + COUNT CHARPTR @ 2 CELLS + COUNT COMPARE 0<=
ELSE
0
THEN
WHILE
@
REPEAT
DUP @ CHARPTR @ ! CHARPTR @ SWAP !
;
\G Scan a word on oldline through pointer charptr
: SCAN-WORD ( ---- addr len)
BEGIN
CHARPTR @ OLDLINE - OLDLINE C@ <= CHARPTR @ C@ BL = AND
WHILE
1 CHARS CHARPTR +!
REPEAT
CHARPTR @ 0
BEGIN
CHARPTR @ OLDLINE - OLDLINE C@ <= CHARPTR @ C@ BL <> AND
WHILE
1 CHARS CHARPTR +! 1+
REPEAT
;
: SEARCH-NAME
SCAN-WORD 2DROP
SCAN-WORD 2DUP BOUNDS ?DO
I C@ [CHAR] a [CHAR] { WITHIN IF I C@ 32 - I C! THEN
LOOP \ translate to caps.
DUP HERE C! HERE CHAR+ SWAP DUP 1+ CHARS ALLOT CMOVE
;
: SEARCH-STACK
0 C,
SCAN-WORD S" (" COMPARE 0= IF
HERE 1 CHARS -
BEGIN
CHARPTR @ OLDLINE - OLDLINE C@ <= CHARPTR @ C@ [CHAR] ) <> AND
WHILE
CHARPTR @ C@ C,
DUP C@ 1+ OVER C!
1 CHARS CHARPTR +!
REPEAT
DROP
THEN
;
: SEARCH-SETS
0 C,
;
: SEARCH-PRON
0 C,
;
\G Process the header information stored in OLDLINE
: PROCESS-HEADER
HERE 0 , CURRENT-COMMENT @ ,
OLDLINE CHARPTR !
SEARCH-NAME
SEARCH-STACK
SEARCH-SETS
SEARCH-PRON
INSERT-HEADER
;
\G Determine if line at HERE is glossary comment, if so.
\G allot it, else store into oldline.
: GLOS-COMMENT? ( --- flag)
HERE C@ 1 > HERE CHAR+ 2 S" \G" COMPARE 0= AND
IF
HERE C@ 1+ CHARS ALLOT 1 \G incorporate current line.
ELSE
FIXLINE @ 0=
IF
HERE OLDLINE HERE C@ 1+ CHARS CMOVE
THEN 0
THEN
;
\G Read lines from the file fid until \G line encountered.
\G Collect all adjacent \G lines and find header line.
\G then insert entry into list flag=0 if no entry found.
: MAKE-GLOSENTRY ( fid --- fid flag)
>R
HERE CURRENT-COMMENT !
0 FIXLINE ! 0 OLDLINE C!
BEGIN
HERE CHAR+ 255 R@ READ-LINE THROW 0= IF
DROP R> 0 EXIT \ end of file.
THEN
HERE C! \ Store length at here.
GLOS-COMMENT?
UNTIL
OLDLINE COUNT -TRAILING NIP IF 1 FIXLINE ! THEN
BEGIN
HERE CHAR+ 255 R@ READ-LINE THROW
IF
HERE C!
GLOS-COMMENT?
ELSE
DROP 0
THEN
0= UNTIL
R> 1
0 C, ALIGN \ allocate end flag after included comment lines.
PROCESS-HEADER
;
\G This command reads a source file and builds glossary info
\G for it in memory.
: MAKEGLOS ( "name")
BL WORD COUNT R/O OPEN-FILE THROW
BEGIN
MAKE-GLOSENTRY
0= UNTIL
CLOSE-FILE THROW
;
\G Build header line for glossary entry.
: BUILD-HLINE ( addr ---)
79 OLDLINE C! \ Line will be 79 chars long.
OLDLINE CHAR+ 79 BL FILL
2 CELLS +
COUNT 2DUP OLDLINE CHAR+ SWAP CMOVE \ place name
DUP >R \ save name length.
CHARS +
COUNT 2DUP OLDLINE R> 3 + CHARS + SWAP CMOVE \ move stack diagram.
CHARS +
COUNT 2DUP OLDLINE 45 CHARS + SWAP CMOVE \ move wordsets field.
CHARS +
COUNT OLDLINE 63 CHARS + SWAP CMOVE \ move pronunciation field.
;
\G write the glossary entry at address addr to file fid.
: WRITE-GLOSENTRY ( addr fid --- )
>R
DUP 2 CELLS + C@
IF
DUP BUILD-HLINE
OLDLINE CHAR+ OLDLINE C@ R@ WRITE-LINE THROW \ write header line.
THEN
CELL+ @
BEGIN
DUP C@ 1 >
WHILE \ write all comment lines without prefixing \G.
DUP 4 CHARS + OVER C@ 3 - 0 MAX R@ WRITE-LINE THROW
COUNT CHARS +
REPEAT DROP
HERE 0 R> WRITE-LINE THROW \ Write final empty line.
;
\G This command writes the glossary info from memory to a file.
\G The glossary info may be collected from more source files.
: WRITEGLOS ( "name")
BL WORD COUNT W/O CREATE-FILE THROW
GLOSLIST
BEGIN
@ DUP
WHILE
2DUP SWAP WRITE-GLOSENTRY
REPEAT DROP
CLOSE-FILE THROW
;
\G A typical glossary session may look like:
\G NEWGLOS MAKEGLOS SOURCE1.FS MAKEGLOS SOURCE2.FS WRITEGLOS GLOS.GLO
CREATE GSTART
|