-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSortIndex.fs
496 lines (426 loc) · 11.9 KB
/
SortIndex.fs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
: Check-Environment ( Check we have the required wordlists )
S" CORE-EXT"
S" FACILITY-EXT"
S" FILE"
S" STRING"
S" SEARCH-ORDER"
BEGIN
DEPTH
0 >
WHILE
2DUP
ENVIRONMENT? IF
DROP 2DROP
ELSE
CR ." Required wordset missing: " TYPE
ABORT
THEN
REPEAT
;
Check-Environment
: input-filename S" forth.wrd" ; ( Unsorted - definition order )
: output-filename S" forth.wds" ; ( Sorted - alphabetical order )
\ The input line has the following format:
\
\ \indexentry{10.6.2}{1306}{40}{EKEY>FKEY}{facility}{ EXT}{X:ekeys}{EKEYtoFKEY}{e-key-to-f-key}
\
\ \indexentry - The LaTeX command that generates the index entry
\ {10.6.2} - The Section the definition appears it
\ (Chapter 10, section 6, subsection 2)
\ Note: There is no subsection for Core words
\ {1306} - The Word number, a four digit number,
\ or {-} if a number has not yet been assigned
\ {40} - The Word sub-number (or {} if undefined)
\ {EKEY>FKEY} - The LaTeX friendly version of the word name
\ {facility} - The name of the word set
\ { EXT} - If the word is part of the extended word set or {}
\ if it is part of the core word set
\ {X:ekeys} - The proposal that introduced the word (or {})
\ {EKEYtoFKEY} - A friendly cross-reference label given to the word
\ (must not contain URL or PDF special characters)
\ {e-key-to-f-key} - The "English" pronunciation for the word
\ (or {} if none given)
\ ==== String handling ====
: $. ( c-addr -- )
DUP 0 = IF
." (null)" DROP
ELSE
COUNT TYPE
THEN
;
: $, ( c-addr len -- addr )
ALIGN HERE >R
DUP C, ( Store string len )
HERE OVER CHARS ALLOT ( Reserve space for len characters )
SWAP CMOVE ( Copy string into dictionary )
R> ( Return address of count )
;
( Find the first occurrence of the character /char/ in the string )
( identified by /c-addr/ /len/ starting at character position /n1/. )
( If the character is found, /flag/ will be true and /n2/ is the )
( position of the found character. Otherwise /flag/ is false and )
( the value of /n2/ is unknown. Should /n1/ be equal to or larger )
( than /len/, /flag/ will be false. )
: indexOf ( c-addr len char n1 -- n2 flag )
ROT 2DUP < IF
FALSE SWAP ROT DO
DROP
OVER I CHARS + C@
OVER = IF
2DROP I TRUE LEAVE
THEN
FALSE
LOOP
DUP 0= IF
ROT DROP
THEN
ELSE
ROT 2DROP NIP FALSE
THEN
;
\ ==== Character handling ====
0 CHAR+ CONSTANT /CHAR
( Convert the character /char/ into upper case )
: C>UPPER ( char -- char' )
DUP [CHAR] a [ CHAR z 1+ ] LITERAL WITHIN IF
[ CHAR a CHAR A - ] LITERAL -
THEN
;
( Determine if /char/ is an ASCII alphabetic character )
: ?ALPHA ( char -- flag )
C>UPPER [CHAR] A [ CHAR Z 1+ ] LITERAL WITHIN
;
( Convert the ASCII character to its hexadecimal value )
: C>HEX ( char -- n )
C>UPPER [CHAR] 0 - DUP 9 > IF 7 - THEN
DUP $F > ABORT" Invalid Hex character"
;
\ ==== Data node ====
( n' = n + len; addr' = addr + n )
: +FIELD: ( n len "<name>" -- n'; addr -- addr' )
CREATE OVER , + DOES> @ +
;
: node, ( len -- addr )
ALIGN HERE SWAP DUP ALLOT 2DUP ERASE DROP
;
0
1 CELLS +FIELD: Node.Next
1 CELLS +FIELD: Node.Line
1 CELLS +FIELD: Node.Name
1 CELLS +FIELD: Node.Number
CONSTANT IndexNode
VARIABLE FirstNode
0 VALUE CurrentNode
: .Node ( addr -- )
BASE @ >R CR
." Node: " DUP HEX U. CR
." Next: " DUP Node.Next @ U. CR
." Line: " DUP Node.Line @ $. CR
." Name: " DUP Node.Name @ $. CR
." Key: " DUP Node.Number @ $. CR
DROP
R> BASE !
;
: .n ( node -- )
DUP Node.Number @ $.
SPACE
DUP Node.Name @ DUP $.
C@ $20 SWAP - SPACES
Node.Line @ $.
;
( Add to next on stack, n4 = n1 + n3 )
: N+ ( n1 n2 n3 -- n4 n2 )
ROT + SWAP
;
\ ==== Line buffer ====
128 CHARS CONSTANT /line-buffer
CREATE line-buffer
/line-buffer 2 CHARS + ALLOT
0 VALUE /line
: nth-brace ( n1 -- n2 )
0 SWAP 0 DO
line-buffer /line ROT [CHAR] { SWAP indexOf
DROP 1+
LOOP
;
\ ==== Source index file ====
0 VALUE Index-File-ID
( Open the unsorted index file and place the file ID in )
( Index-File-ID or abort if the file can not be opened for some )
( reason )
: open-index ( -- )
input-filename R/O OPEN-FILE
ABORT" Can't find index file: Forth.wrd"
TO Index-File-ID
;
\ ==== Copy the input line into the current node ====
: line>node ( -- )
line-buffer /line $, CurrentNode Node.Line !
;
\ ==== Copy word index number into number field of current node ====
: >NUM ( c-addr n -- d )
0 S>D 2SWAP >NUMBER 2DROP
;
( Read subsection number from line, "1" for Core or "2" for EXT )
: line>subsection ( -- d )
line-buffer /line [CHAR] } 0 indexOf DROP
1- CHARS line-buffer + C@ [CHAR] 0 - S>D
;
( Read Chapter number from line )
: line>chapter ( -- d )
line-buffer 12 CHARS + 2 >NUM
;
( Read Sub-Word number from line )
: line>subword ( -- d )
3 nth-brace CHARS line-buffer + 2 >NUM
;
( Read Word number from line )
: line>number ( -- d )
2 nth-brace CHARS line-buffer + 4 >NUM
;
( Use the word number, word sub-number, chapter and sub section to )
( generate what should be a unique key for the word. Unfortunately,)
( during the development of a word set this is frequently 000000xx2 )
( I.e., not unique, we therefore have to use the words name to help )
( identify the correct location in the ordered index. )
: numb>node ( -- )
line>number
line>subword
line>chapter
line>subsection
<# # 2DROP # # 2DROP # # 2DROP # # # # #>
$, CurrentNode Node.Number !
;
\ ==== Copy word name into the current node ====
\ WARNING: TeX commands are case sensitive
WORDLIST CONSTANT TeX-Wordlist
GET-ORDER TeX-Wordlist SWAP 1+ SET-ORDER DEFINITIONS
: \& ( dest src -- dest' src' )
OVER [CHAR] & SWAP C!
;
: \char ( dest src -- dest' src' ) ( \char "xx )
BEGIN DUP C@ BL = WHILE CHAR+ REPEAT
DUP C@ [CHAR] " <> ABORT" Invalid TeX"
CHAR+ DUP C@ C>HEX
SWAP CHAR+ DUP C@ C>HEX
ROT $10 * +
ROT 2DUP C!
NIP SWAP
;
GET-ORDER NIP 1- SET-ORDER DEFINITIONS
: TeXCommand ( dest src -- dest' src' )
DUP
CHAR+ C@ ?ALPHA IF
1 BEGIN
1+
2DUP CHARS + C@ ?ALPHA 0=
UNTIL
ELSE
2
THEN
2DUP TeX-Wordlist SEARCH-WORDLIST IF
>R CHARS + R> EXECUTE
ELSE
CR ." Unknown TeX command: " TYPE ABORT
THEN
;
\ Unfortunately the name is in a TeX friendly format, which is not
\ sort-order friendly. We must evaluate any TeX commands before
\ copying the name into the node. The only TeX commands we need
\ to worry about are:
\
\ \char "xx Replace with character code xx (in hex)
\ \& Replaced with &
\ {xx} Remove braces from text given just "xx"
\
\ As these all replace the original text with something smaller we
\ can do it in-line before copying the name into the node.
\ Remember the name is surrounded by braces.
: name>node ( -- )
line-buffer DUP 4 nth-brace CHARS + ( dest src )
0 >R ( brace count )
BEGIN
DUP C@
CASE
[CHAR] \ OF TeXCommand ( Run TeX command )
/CHAR N+ ( Increment dest )
TRUE
ENDOF
[CHAR] { OF R> 1+ >R ( Increment brace count )
TRUE
ENDOF
[CHAR] } OF R@ IF
R> 1- >R ( Decrement brace count )
TRUE
ELSE
FALSE ( Finish on brace count = 0 )
THEN
ENDOF
( Default - Copy character )
ROT 2DUP C! ( Copy character )
NIP CHAR+ SWAP ( Increment dest )
TRUE 0
ENDCASE
WHILE
CHAR+ ( Increment src )
REPEAT
R> DROP ( Drop brace count )
DROP ( Drop src )
line-buffer - /CHAR / ( Calculate length of name )
line-buffer SWAP $, CurrentNode Node.Name !
;
\ ==== Read the index file into memory ====
( Read a single line of the unsorted index file into an IndexNode )
: read-index-line ( -- flag )
line-buffer /line-buffer Index-File-ID READ-LINE THROW
SWAP TO /line DUP IF
/line /line-buffer = ABORT" line-buffer too short"
IndexNode node,
DUP CurrentNode Node.Next !
TO CurrentNode
THEN
;
: build-index-node ( -- )
line>node ( Copy line from buffer into node )
numb>node ( Create number string for node )
name>node ( Copy word name into node )
;
: init-list
IndexNode node, DUP FirstNode ! TO CurrentNode
;
: read-index
init-list
open-index
BEGIN
read-index-line
WHILE
build-index-node
REPEAT
Index-File-ID CLOSE-FILE THROW
;
\ ==== Debug ====
: show-nodes
CR FirstNode @
BEGIN
Node.Next @
DUP
WHILE
DUP .n CR
REPEAT
DROP
;
\ If we can regenerate the original unsorted file without any changes
\ we know we have read it in correctly.
: write-unsorted-index
S" unsorted.out" W/O CREATE-FILE THROW
TO Index-File-ID
FirstNode @
BEGIN
Node.Next @ DUP
WHILE
DUP Node.Line @ COUNT
Index-File-ID WRITE-LINE THROW
REPEAT
DROP
Index-File-ID CLOSE-FILE THROW
;
\ ==== Compare index nodes ====
\ We are going to use an insertion sort to build an index of the
\ nodes. First we need to be able to compare nodes.
: Node= ( node1 node2 -- n )
\ n = 1 when node2 > node1
\ n = 0 when node2 = node1
\ n = -1 when node2 < node1
( During the development of a wordlist the word numbers are )
( likely to be 0000, so we must compare on the word names )
2DUP Node.Name @ COUNT
ROT Node.Name @ COUNT
COMPARE DUP 0= IF
( Where the same word appears in multiple word sets we place)
( them in order of chapter, so use the calculated key to )
( order them )
DROP
2DUP Node.Number @ COUNT
ROT Node.Number @ COUNT
COMPARE
THEN
NIP NIP
;
\ ==== Sort Index ====
: Count-Nodes
0 FirstNode @
BEGIN
Node.Next @ DUP
WHILE
1 N+
REPEAT
DROP
;
0 VALUE #Nodes
0 VALUE Index
: build-index
Count-Nodes DUP TO #Nodes
1+ CELLS node, TO Index
;
: show-index
Index
CR
BEGIN
DUP @ DUP
WHILE
.n CR
CELL+
REPEAT
2DROP
;
: insert-node ( node -- )
>R
Index
BEGIN
DUP @ DUP IF
R@ Node= 0< IF
\ Open space for new node
DUP DUP CELL+
Index #nodes CELLS +
OVER - MOVE
FALSE
ELSE
DUP
THEN
THEN
WHILE
CELL+
REPEAT
( addr - Location in index to place node )
R> SWAP !
;
: sort-index
build-index
FirstNode @
BEGIN
Node.Next @ DUP
WHILE
DUP insert-node
REPEAT
DROP
;
\ ==== Write sorted index out to file ====
: write-index
output-filename W/O CREATE-FILE THROW
TO Index-File-ID
Index
BEGIN
DUP @ DUP
WHILE
Node.Line @ COUNT
Index-File-ID WRITE-LINE THROW
CELL+
REPEAT
2DROP
Index-File-ID CLOSE-FILE THROW
;
\ ==== Run the program ====
read-index
sort-index
write-index
bye